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);
// }
//}
///
/// 上报属性状态
/// 调用示例:SendTargetMessage();
///
///
public void SendTargetMessage()
{
if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device != null)
{
#region 赋值基本属性与状态
bool IsAllowRun = false;
bool TemperatureReached = false;
iOTDevSXModel.KZSX = Tools.JsonConvertTools(new DevSX { data = new List { new DevSXBase { SXMC = "", SXLX = "" } } });
iOTDevSXModel.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 }
}
});
#endregion
switch (GeneralConfig.DeviceType)
{
case BPA.Message.Enum.DeviceClientType.MORKD:
break;
case BPA.Message.Enum.DeviceClientType.MORKS:
#region 赋值扩展属性与状态
IsAllowRun = GetTypeValue("AllowRun");
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)
{
iOTDevSXModel.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+" 异常,配料机未配完料,疑似碗未到配料机下方或者配料机未工作...."}
}
});
}
#endregion
break;
case BPA.Message.Enum.DeviceClientType.MORKI:
break;
case BPA.Message.Enum.DeviceClientType.MORKC:
break;
case BPA.Message.Enum.DeviceClientType.MORKIC:
break;
default:
break;
}
IOTDevServer.GetInstance().IOT_Publish(IOTDevServer.PubTopic, iOTDevSXModel.Tojson());
}
}
#endregion
#region API调用
///
/// 增加告警信息
///
///
/// 返回ID
public string HttpAddAlarm(AlarmTable alarmTable)
{
string id=string.Empty;
try
{
if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device != null)
{
string url = InternetInfo.IotApiAddress + "/api/Alarm/Create";
alarmTable.ClientId = InternetInfo.ClientId.ToString();
alarmTable.devicename = device.devicename;
string redata= HttpRequestHelper.HttpPostRequest(url,Tools.JsonConvertTools(alarmTable));
if (!string.IsNullOrEmpty(redata))
{
JsonMsg msg = Tools.JsonToObjectTools>(redata);
id = msg?.obj.IdStr;
}
}
}
catch (Exception ex)
{
MessageLog.GetInstance.Show(ex.Message);
}
return id;
}
///
/// 根据ID删除告警信息
///
///
public void HttpDeleteAlarm(string id)
{
try
{
if (string.IsNullOrEmpty(id)) { MessageLog.GetInstance.Show("API调用删除告警信息,ID不能为空!"); return; }
if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device != null)
{
string url = InternetInfo.IotApiAddress + "/api/Alarm/Delete?id="+ id;
HttpRequestHelper.HttpGetRequest(url);
}
}
catch (Exception ex)
{
MessageLog.GetInstance.Show(ex.Message);
}
}
///
/// 增加日志信息
///
///
/// 返回ID
public string HttpAddLog(LogTable logTable)
{
string id = string.Empty;
try
{
if (IOTDevServer.client != null && IOTDevServer.client.IsConnected && device != null)
{
string url = InternetInfo.IotApiAddress + "/api/Log/Create";
logTable.ClientId = InternetInfo.ClientId.ToString();
logTable.devicename = device.devicename;
string redata = HttpRequestHelper.HttpPostRequest(url,Tools.JsonConvertTools(logTable));
if (!string.IsNullOrEmpty(redata))
{
JsonMsg msg = Tools.JsonToObjectTools>(redata);
id = msg?.obj.IdStr;
}
}
}
catch (Exception ex)
{
MessageLog.GetInstance.Show(ex.Message);
}
return id;
}
#endregion
#region 外部关闭或初始化IOT调用
///
/// 初始化IOT连接
///
public bool Initialize()
{
if (!string.IsNullOrEmpty(InternetInfo.IotApiAddress)) IOTDevServer.GetInstance().SetUrl(InternetInfo.IotApiAddress);
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 IOTNode
{
public T data { get; set; }
}
///
/// 日志上报
///
public class LogIOT
{
public string SZXX { get; set; }
}
///
/// 告警上报
///
public class AlarmIOT
{
public string GJXX { get; set; }
}
}