using BPA.Message.IOT;
using HBLConsole.Communication;
using HBLConsole.GVL;
using HBLConsole.Interface;
using HBLConsole.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using HBLConsole.Factory;
using BPA.Message.API请求;
namespace HBLConsole.Business
{
public class IotReport
{
#region 单例模式
private volatile static IotReport _Instance;
public static IotReport GetInstance => _Instance ?? (_Instance = new IotReport());
#endregion
#region 变量
///
/// 查询出当前设备信息
///
public DeviceTable device;
///
/// 大屏上报Model
///
public IOTDevSXModel iOTDevSXModel = new IOTDevSXModel() { };
#endregion
#region IOT上报公共调用
///
/// 上报告警消息
/// 调用示例: SendAlarmMessage(new AlarmTable { AlarmTime = DateTime.Now,AlarmType = "1",AlarmMessage = "煮面机异常",AlarmVla = "煮面机" });
///
///
public void SendAlarmMessage(AlarmTable alarmTable)
{
if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device!=null)
{
alarmTable.ClientId = InternetInfo.ClientId.ToString();
alarmTable.devicename = device.devicename;
string json = Tools.JsonConvertTools>(new IotModel { @params = new AlarmIOT { GJXX = Tools.JsonConvertTools(alarmTable) } });
IOTDevServer.GetInstance().IOT_Publish(IOTDevServer.PubTopic,json);
}
}
///
/// 上报日志消息
/// 调用示例:SendLogMessage(new LogTable { LogTime = DateTime.Now,LogType = "1",LogMessage = "程序异常",LogVla = "程序" });
///
///
public void SendLogMessage(LogTable logTable)
{
if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device != null)
{
logTable.devicename = device.devicename;
logTable.ClientId = InternetInfo.ClientId.ToString();
string json = Tools.JsonConvertTools>(new IotModel { @params = new LogIOT { SZXX = Tools.JsonConvertTools(logTable) } });
IOTDevServer.GetInstance().IOT_Publish(IOTDevServer.PubTopic,json);
}
}
///
/// 上报节点状态消息
/// 调用示例:SendNodeStatusMessage("json字符串");
///
///
public void SendNodeStatusMessage(string json)
{
if (IOTDevServer.client!=null && IOTDevServer.client.IsConnected && device != null)
{
string jsonstr = Tools.JsonConvertTools>(new IotModel { @params = new NodeStatusIOT { NodeStatus = Tools.JsonConvertTools(json) } });
IOTDevServer.GetInstance().IOT_Publish(IOTDevServer.PubTopic,jsonstr);
}
}
///
/// 上报属性状态
/// 调用示例:SendTargetMessage();
///
///
public void SendTargetMessage()
{
if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device != null)
{
string kzsx = string.Empty;
string jbsx = Tools.JsonConvertTools(new DevSX
{
data = new List
{
new DevSXBase { SXMC="设备硬件",SXLX="机器人",SXStatus=ModbusTcpHelper.GetInstance.Connected
,SXYCMS=ModbusTcpHelper.GetInstance.Connected?"":DateTime.Now+" 异常,机器人未连接,疑似未连接网络...."},
new DevSXBase { SXMC="设备软件",SXLX="上位机",SXStatus=true }
}
});
bool IsAllowRun = GetTypeValue("AllowRun");
bool TemperatureReached = GetTypeValue("TemperatureReached");
bool MissingBowl = GetTypeValue("MissingBowl");
bool MissingBowlSignal2 = GetTypeValue("MissingBowlSignal2");
bool IsNoodles = GetTypeValue("IsNoodles");//转台
bool AllowFallNoodle = GetTypeValue("AllowFallNoodle");//是否允许到面
bool[] CookNoodlesComplete = GetTypeValue("CookNoodlesComplete");//煮面完成上升信号
bool isCookNoodles = CookNoodlesComplete.ToList().Find(o => o);
//设置扩展属性与状态,连接上机器人后才会处罚
if (IsAllowRun)
{
kzsx = Tools.JsonConvertTools(new DevSX
{
data = new List
{
new DevSXBase { SXMC="设备硬件",SXLX="煮面炉",SXStatus= !TemperatureReached ,
SXYCMS=!TemperatureReached?"":DateTime.Now+" 异常,煮面炉温度不够,疑似正在加热或者未工作...."},
new DevSXBase { SXMC="设备硬件",SXLX="装小碗结构",SXStatus=MissingBowl ,
SXYCMS=MissingBowl?"":DateTime.Now+" 异常,缺小碗...."},
new DevSXBase { SXMC="设备硬件",SXLX="装大碗结构",SXStatus=MissingBowlSignal2 ,
SXYCMS=MissingBowlSignal2?"":DateTime.Now+" 异常,缺大碗...."},
new DevSXBase { SXMC="设备硬件",SXLX="取面转台",SXStatus=IsNoodles ,
SXYCMS=IsNoodles?"":DateTime.Now+" 异常,转台位置缺少物料...."},
new DevSXBase { SXMC="设备硬件",SXLX="配料机",SXStatus=!(!AllowFallNoodle && isCookNoodles) ,
SXYCMS=!((!AllowFallNoodle && isCookNoodles))?"":DateTime.Now+" 异常,配料机未配完料,疑似碗未到配料机下方或者配料机未工作...."}
}
});
}
string jsonstr = Tools.JsonConvertTools>(new IotModel { @params = new TargetIOT { JBSX = jbsx,KZSX = kzsx } });
IOTDevServer.GetInstance().IOT_Publish(IOTDevServer.PubTopic,jsonstr);
}
}
#endregion
#region 外部关闭或初始化IOT调用
///
/// 初始化IOT连接
///
public bool Initialize()
{
if (!IOTDevServer.GetInstance().CreateLinks(InternetInfo.ClientId,out device))
{
MessageLog.GetInstance.Show($"设备{InternetInfo.ClientId}阿里云上没有该设备。");
return false;
}
IOTDevServer.GetInstance().IOT_Subscribe(IOTDevServer.BroadcastTopic);//订阅广播主题
IOTDevServer.DevIOTAction += DevIOTActionHandler;
IOTDevServer.UNConnectMqtt += new Action((o) => { MessageLog.GetInstance.Show(o); });//断网自动重连接与打印
if (IOTDevServer.client.IsConnected) MessageLog.GetInstance.Show($"设备{device.devicename} {device.remark}阿里云连接成功.");
else MessageLog.GetInstance.Show($"设备{device.devicename} {device.remark}阿里云连接失败.不能上报业务信息");
return IOTDevServer.client.IsConnected;
}
///
/// 关闭IOT连接
///
public static void Close()
{
if (IOTDevServer.client != null)
IOTDevServer.GetInstance().Disconnect();
}
#endregion
#region 云端订阅主题消息接收
///
/// 接收云端消息
///
///
///
private void DevIOTActionHandler(string topic, string message)
{
if (IOTDevServer.BroadcastTopic == topic && !string.IsNullOrEmpty(message))//广播主题消息,将广播消息发送到相应客户端
{
IOTCommandModel iOTCommand = Tools.JsonToObjectTools(message);
if (iOTCommand.deviceName == device.devicename)
ActionManage.GetInstance.Send("IotBroadcast", iOTCommand);
}
}
#endregion
#region 私有函数
///
/// 获取属性状态
///
private T GetTypeValue(string str)
{
return (T)SimpleFactory.GetInstance.GVL?.GetType().GetProperty(str).GetValue(SimpleFactory.GetInstance.GVL,null);
}
#endregion
}
///
/// 日志上报
///
public class LogIOT
{
public string SZXX { get; set; }
}
///
/// 告警上报
///
public class AlarmIOT
{
public string GJXX { get; set; }
}
///
/// 节点状态上报
///
public class NodeStatusIOT
{
public string NodeStatus { get; set; }
}
///
/// 基本属性与扩展属性
///
public class TargetIOT
{
///
/// 基本属性
///
public string JBSX { get; set; }
///
/// 扩展属性
///
public string KZSX { get; set; }
}
}