@@ -62,13 +62,13 @@ namespace DataVAPI.ProcessServices.阿里云处理流程One | |||||
devModel = new ScreenMonitorModel(); | devModel = new ScreenMonitorModel(); | ||||
devModel.operatingDeviceStatus = new OperatingDeviceStatus(); devModel.operatingDeviceStatus.data = new List<DevStatus>();//现场设备于状态 | devModel.operatingDeviceStatus = new OperatingDeviceStatus(); devModel.operatingDeviceStatus.data = new List<DevStatus>();//现场设备于状态 | ||||
devModel.infoMessage = new InfoMessage(); devModel.infoMessage.data = new List<DeviceBase>();//通知消息 | devModel.infoMessage = new InfoMessage(); devModel.infoMessage.data = new List<DeviceBase>();//通知消息 | ||||
devModel.TotalSales = "0"; | |||||
//devModel.TotalSales = "0"; | |||||
//加载店铺信息 | //加载店铺信息 | ||||
LoadingShopInformation(); | LoadingShopInformation(); | ||||
//MQTT 数据接收处理 | //MQTT 数据接收处理 | ||||
ConsoleHelper.WriteInfoLine("尝试连接阿里云."); | ConsoleHelper.WriteInfoLine("尝试连接阿里云."); | ||||
IOTDevServer.GetInstance().SetUrl(APIurl); | IOTDevServer.GetInstance().SetUrl(APIurl); | ||||
if (IOTDevServer.GetInstance().CreateLinks(0, out device)) | |||||
if (IOTDevServer.GetInstance().CreateLinks(10, out device)) | |||||
{ | { | ||||
ConsoleHelper.WriteSuccessLine("连接阿里云成功!"); | ConsoleHelper.WriteSuccessLine("连接阿里云成功!"); | ||||
} | } | ||||
@@ -183,7 +183,7 @@ namespace DataVAPI.ProcessServices.阿里云处理流程One | |||||
})); | })); | ||||
} | } | ||||
} | } | ||||
devModel.TotalSales = devModel.operatingDeviceStatus.data.Count.ToString(); | |||||
//devModel.TotalSales = devModel.operatingDeviceStatus.data.Count.ToString(); | |||||
ScreenMonitorModel screen1Monitor = Tools.JsonToObjectTools<ScreenMonitorModel>(devModel.ToJSON()); | ScreenMonitorModel screen1Monitor = Tools.JsonToObjectTools<ScreenMonitorModel>(devModel.ToJSON()); | ||||
#region 1.设置data默认值 | #region 1.设置data默认值 | ||||
@@ -193,6 +193,36 @@ namespace DataVAPI.Tool.IOT | |||||
} | } | ||||
} | } | ||||
public bool CreateLinks(string key, string name, string secret) | |||||
{ | |||||
try | |||||
{ | |||||
Set(key, name, secret); | |||||
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName()); | |||||
string clientId = host.AddressList.FirstOrDefault( | |||||
ip => ip.AddressFamily == AddressFamily.InterNetwork).ToString(); | |||||
string t = Convert.ToString(DateTimeOffset.Now.ToUnixTimeMilliseconds()); | |||||
string signmethod = "hmacmd5"; | |||||
Dictionary<string, string> dict = new Dictionary<string, string>(); | |||||
dict.Add("productKey", ProductKey); | |||||
dict.Add("deviceName", DeviceName); | |||||
dict.Add("clientId", clientId); | |||||
dict.Add("timestamp", t); | |||||
mqttUserName = DeviceName + "&" + ProductKey; | |||||
mqttPassword = IotSignUtils.sign(dict, DeviceSecret, signmethod); | |||||
mqttClientId = clientId + "|securemode=3,signmethod=" + signmethod + ",timestamp=" + t + "|"; | |||||
targetServer = ProductKey + ".iot-as-mqtt." + RegionId + ".aliyuncs.com"; | |||||
ConnectMqtt(targetServer, mqttClientId, mqttUserName, mqttPassword); | |||||
return true; | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
return false; | |||||
} | |||||
} | |||||
/// <summary> | /// <summary> | ||||
/// 获取连接状态 | /// 获取连接状态 | ||||
/// </summary> | /// </summary> | ||||
@@ -12,10 +12,6 @@ namespace DataVAPI.Tool.IOT | |||||
/// </summary> | /// </summary> | ||||
public class ScreenMonitorModel | public class ScreenMonitorModel | ||||
{ | { | ||||
/// <summary> | |||||
/// 服务商家数 | |||||
/// </summary> | |||||
public string TotalSales { get; set; } | |||||
/// <summary> | /// <summary> | ||||
/// 现场运营设备状态 | /// 现场运营设备状态 | ||||
/// </summary> | /// </summary> | ||||
@@ -263,6 +259,10 @@ namespace DataVAPI.Tool.IOT | |||||
/// 客户端ID | /// 客户端ID | ||||
/// </summary> | /// </summary> | ||||
public string clientId { get; set; } | public string clientId { get; set; } | ||||
/// <summary> | |||||
/// 设备ID | |||||
/// </summary> | |||||
public string deviceId { get; set; } | |||||
public DevBase() | public DevBase() | ||||
{ | { | ||||
@@ -1,6 +1,7 @@ | |||||
using DataVAPI.Model; | using DataVAPI.Model; | ||||
using DataVAPI.Tool.IOT; | using DataVAPI.Tool.IOT; | ||||
using DataVAPI.Tool.控制台显示; | using DataVAPI.Tool.控制台显示; | ||||
using DataVAPI.UpAndDown; | |||||
using Microsoft.AspNetCore.Mvc; | using Microsoft.AspNetCore.Mvc; | ||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
using System.Net.Http; | using System.Net.Http; | ||||
@@ -51,9 +52,7 @@ namespace DataVAPI.Controllers | |||||
string st = System.Reflection.MethodBase.GetCurrentMethod().Name; | string st = System.Reflection.MethodBase.GetCurrentMethod().Name; | ||||
try | try | ||||
{ | { | ||||
HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(Tools.JsonConvertTools<CommandModel>(usersModel), Encoding.GetEncoding("UTF-8"), "application/json") }; | HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(Tools.JsonConvertTools<CommandModel>(usersModel), Encoding.GetEncoding("UTF-8"), "application/json") }; | ||||
if (usersModel == null || string.IsNullOrEmpty(usersModel.deviceName)) | if (usersModel == null || string.IsNullOrEmpty(usersModel.deviceName)) | ||||
{ | { | ||||
return JsonMsg<CommandModel>.Error(null, st, "设备名称不能为空"); ; | return JsonMsg<CommandModel>.Error(null, st, "设备名称不能为空"); ; | ||||
@@ -61,9 +60,7 @@ namespace DataVAPI.Controllers | |||||
else | else | ||||
{ | { | ||||
string PubTopic = "/broadcast/" + "grgpECHSL7q" + "/" + usersModel.deviceName + "_SetDevice"; | string PubTopic = "/broadcast/" + "grgpECHSL7q" + "/" + usersModel.deviceName + "_SetDevice"; | ||||
ConsoleHelper.WriteWarningLine("发送数据 " + PubTopic + Tools.JsonConvertTools<CommandModel>(usersModel)); | |||||
IOTDevServer.GetInstance().IOT_Publish(PubTopic, Tools.JsonConvertTools<CommandModel>(usersModel)); | |||||
ProcessServer.Instance.Publish(PubTopic, usersModel); | |||||
return JsonMsg<CommandModel>.OK(usersModel, st); | return JsonMsg<CommandModel>.OK(usersModel, st); | ||||
} | } | ||||
@@ -99,8 +96,7 @@ namespace DataVAPI.Controllers | |||||
Dictionary<string, string> keyValuePair = new Dictionary<string, string>(); | Dictionary<string, string> keyValuePair = new Dictionary<string, string>(); | ||||
keyValuePair.Add(CommandKey, CommandValue); | keyValuePair.Add(CommandKey, CommandValue); | ||||
CommandModel command = new CommandModel() { CommandName = CommandName, deviceName = deviceName, CommandValue = keyValuePair }; | CommandModel command = new CommandModel() { CommandName = CommandName, deviceName = deviceName, CommandValue = keyValuePair }; | ||||
ConsoleHelper.WriteWarningLine("发送数据 " + PubTopic + Tools.JsonConvertTools<CommandModel>(command)); | |||||
IOTDevServer.GetInstance().IOT_Publish(PubTopic, Tools.JsonConvertTools<CommandModel>(command)); | |||||
ProcessServer.Instance.Publish(PubTopic, command); | |||||
return JsonMsg<CommandModel>.OK(command, st); | return JsonMsg<CommandModel>.OK(command, st); | ||||
} | } | ||||
} | } | ||||
@@ -1,8 +1,11 @@ | |||||
using DataVApi.Order; | using DataVApi.Order; | ||||
using DataVAPI.AppConfig; | using DataVAPI.AppConfig; | ||||
using DataVAPI.ConfigureSwagger; | using DataVAPI.ConfigureSwagger; | ||||
using DataVAPI.Model; | |||||
using DataVAPI.ModelDataBus; | using DataVAPI.ModelDataBus; | ||||
using DataVAPI.Tool.IOT; | |||||
using DataVAPI.Tool.控制台显示; | using DataVAPI.Tool.控制台显示; | ||||
using DataVAPI.UpAndDown; | |||||
using Microsoft.AspNetCore.Builder; | using Microsoft.AspNetCore.Builder; | ||||
using Microsoft.AspNetCore.Hosting; | using Microsoft.AspNetCore.Hosting; | ||||
using Microsoft.Extensions.Configuration; | using Microsoft.Extensions.Configuration; | ||||
@@ -28,6 +31,7 @@ namespace DataVAPI | |||||
SugarDb.Conn = Configuration["DbConnectionstring"].ToString(); | SugarDb.Conn = Configuration["DbConnectionstring"].ToString(); | ||||
DataBus.connStr = Configuration["mongoDB.connStr"].ToString(); | DataBus.connStr = Configuration["mongoDB.connStr"].ToString(); | ||||
DataBus.dbName = Configuration["mongoDB.dbName"].ToString(); | DataBus.dbName = Configuration["mongoDB.dbName"].ToString(); | ||||
ProcessServer.Instance.Initialize(); | |||||
//注册swagger服务 | //注册swagger服务 | ||||
services.AddSwaggerUp(); | services.AddSwaggerUp(); | ||||
services.AddControllers(); | services.AddControllers(); | ||||
@@ -0,0 +1,237 @@ | |||||
using DataVAPI.Controllers; | |||||
using DataVAPI.Model; | |||||
using DataVAPI.Tool; | |||||
using DataVAPI.Tool.IOT; | |||||
using DataVAPI.Tool.控制台显示; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading; | |||||
namespace DataVAPI.UpAndDown | |||||
{ | |||||
/// <summary> | |||||
/// 上下线通知 | |||||
/// </summary> | |||||
public class ProcessServer | |||||
{ | |||||
/// <summary> | |||||
/// 上报大屏总Model | |||||
/// </summary> | |||||
public ScreenMonitorModel devModel { get; set; } | |||||
public DeviceController deviceController { get; set; } | |||||
public LogController logController { get; set; } | |||||
public ScreenController screenController { get; set; } | |||||
#region 不需要监听 | |||||
private static ProcessServer _instance; | |||||
public static ProcessServer Instance | |||||
{ | |||||
get | |||||
{ | |||||
if (_instance == null) | |||||
_instance = new ProcessServer(); | |||||
return _instance; | |||||
} | |||||
} | |||||
public ProcessServer() | |||||
{ | |||||
deviceController = new DeviceController(); | |||||
logController = new LogController(); | |||||
screenController = new ScreenController(); | |||||
} | |||||
#endregion | |||||
/// <summary> | |||||
/// 初始化 | |||||
/// </summary> | |||||
public void Initialize() | |||||
{ | |||||
devModel = new ScreenMonitorModel(); | |||||
devModel.operatingDeviceStatus = new OperatingDeviceStatus(); devModel.operatingDeviceStatus.data = new List<DevStatus>();//现场设备于状态 | |||||
devModel.infoMessage = new InfoMessage(); devModel.infoMessage.data = new List<DeviceBase>();//通知消息 | |||||
//加载店铺信息 //加载店铺信息 | |||||
LoadingShopInformation(); | |||||
//MQTT 数据接收处理 | |||||
ConsoleHelper.WriteInfoLine("尝试连接阿里云."); | |||||
if (IOTDevServer.GetInstance().CreateLinks("grgpECHSL7q", "Transit", "562dcc779b918a54c2d6589ec30ee230")) | |||||
ConsoleHelper.WriteSuccessLine($"阿里云【Transit】连接成功"); | |||||
else | |||||
{ | |||||
ConsoleHelper.WriteSuccessLine($"阿里云【Transit】连接失败"); | |||||
return; | |||||
} | |||||
IOTDevServer.UNConnectMqtt += new Action<string>((o) => { ConsoleHelper.WriteSuccessLine(o); }); | |||||
Subscribe(IOTDevServer.HeartbeatSubTopic); | |||||
IOTDevServer.DevIOTAction += DevIOTActionHandler; | |||||
ConsoleHelper.WriteSuccessLine("开始接收数据,执行队列任务!"); | |||||
} | |||||
/// <summary> | |||||
/// 加载店铺信息 | |||||
/// </summary> | |||||
/// <param name="clientId"></param> | |||||
public void LoadingShopInformation(string clientId = "") | |||||
{ | |||||
try | |||||
{ | |||||
ConsoleHelper.WriteSuccessLine("加载店铺集合中."); | |||||
JsonMsg<List<DeviceTable>> jsonMsg = deviceController.Query("", "",DateTime.MinValue,DateTime.MinValue); | |||||
jsonMsg?.obj?.data?.ForEach(par => | |||||
{ | |||||
int chid = 0; | |||||
try | |||||
{ | |||||
chid = int.Parse(par.ClientId); | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
chid = 0; | |||||
} | |||||
if (chid>0) | |||||
{ | |||||
DevStatus devStatus = new DevStatus() | |||||
{ | |||||
deviceName = par.devicename, | |||||
gmtCreate = par.devicesecret, | |||||
productKey = par.productkey, | |||||
DeviceMC = par.devtype, | |||||
DeviceMS = par.remark, | |||||
DeviceSJ = par.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"), | |||||
DeviceZT = "离线", | |||||
clientId = par.ClientId, | |||||
deviceId = par.DeviceId | |||||
}; | |||||
if (devModel.operatingDeviceStatus.data.Find(o => o.gmtCreate == par.devicesecret) == null) | |||||
{ | |||||
ConsoleHelper.WriteSuccessLine($"加载设备.{par.devtype} {par.remark}"); | |||||
devModel.operatingDeviceStatus.data.Add(devStatus); | |||||
} | |||||
} | |||||
}); | |||||
ConsoleHelper.WriteSuccessLine($"加载设备数[ {jsonMsg?.obj?.data?.Count} ]台..."); | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
ConsoleHelper.WriteErrorLine($"错误{ex.Message}"); | |||||
} | |||||
} | |||||
/// <summary> | |||||
/// 订阅主题 | |||||
/// </summary> | |||||
/// <param name="subscribe"></param> | |||||
public void Subscribe(string subscribe) | |||||
{ | |||||
IOTDevServer.GetInstance().IOT_Subscribe(subscribe); | |||||
ConsoleHelper.WriteSuccessLine("订阅主题: " + subscribe); | |||||
} | |||||
public void Publish(string PubTopic, CommandModel command) | |||||
{ | |||||
ConsoleHelper.WriteWarningLine("发送数据 " + PubTopic + Tools.JsonConvertTools<CommandModel>(command)); | |||||
IOTDevServer.GetInstance().IOT_Publish(PubTopic, Tools.JsonConvertTools<CommandModel>(command)); | |||||
} | |||||
/// <summary> | |||||
/// 更新内存集合 | |||||
/// </summary> | |||||
/// <param name="receive"></param> | |||||
public void SentData(ReceiveModel receiveModel) | |||||
{ | |||||
try | |||||
{ | |||||
if (receiveModel?.deviceContext != null && receiveModel?.status != null)//状态变更消息 | |||||
{ | |||||
devModel?.operatingDeviceStatus.data?.Find(par => par.deviceName == receiveModel.deviceContext.deviceName)?.SetStatus(receiveModel.status.value); | |||||
devModel?.infoMessage?.data?.Add(new DeviceBase { DeviceMC = receiveModel.deviceContext.deviceName, DeviceMS = $"设备{receiveModel.status.value}了!", DeviceSJ = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }); | |||||
DevStatus dev = devModel?.operatingDeviceStatus.data?.Find(par => par.deviceName == receiveModel.deviceContext.deviceName); | |||||
if (dev != null) | |||||
{ | |||||
logController.Create(new LogTable | |||||
{ | |||||
devicename = receiveModel.deviceContext.deviceName, | |||||
ClientId = dev.clientId, | |||||
LogTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), | |||||
LogType = "1", | |||||
LogMessage = $"设备{receiveModel.status.value}了!", | |||||
LogVla = "上下线通知" | |||||
}); | |||||
} | |||||
else | |||||
{ | |||||
LoadingShopInformation(); | |||||
} | |||||
} | |||||
ScreenMonitorModel screen1Monitor = Tools.JsonToObjectTools<ScreenMonitorModel>(devModel.ToJSON()); | |||||
#region 1.设置data默认值 | |||||
if (screen1Monitor.operatingDeviceStatus.data.Count == 0) | |||||
{ | |||||
screen1Monitor.operatingDeviceStatus.data.Add(new DevStatus { DeviceMC = "", DeviceMS = "", DeviceSJ = "", deviceName = "", DeviceZT = "", gmtCreate = "" }); | |||||
} | |||||
if (screen1Monitor.infoMessage.data.Count == 0) | |||||
{ | |||||
screen1Monitor.infoMessage.data.Add(new DeviceBase { DeviceMC = "", DeviceMS = "", DeviceSJ = "" }); | |||||
} | |||||
#endregion | |||||
string JSON = screen1Monitor.ToJSON(); | |||||
if (!string.IsNullOrEmpty(JSON)) | |||||
{ | |||||
screenController.CreateOrUpdate(new LargeScreenTable() { json = JSON, devicename = "Transit",ClientId = "-10",DeviceId="-10" }); | |||||
IOTDevServer.GetInstance().IOT_Publish(IOTDevServer.ScreenShowPubTopic, JSON); | |||||
} | |||||
if (devModel.infoMessage.data != null && devModel.infoMessage.data.Count > 0) | |||||
{ | |||||
List<DeviceBase> bases = devModel.infoMessage.data.ToList(); | |||||
bases?.ForEach(par => | |||||
{ | |||||
if (string.IsNullOrEmpty(par.DeviceMC)) devModel.infoMessage.data.Remove(par); | |||||
if (!string.IsNullOrEmpty(par.DeviceSJ) && DateTime.Now.AddSeconds(-5) > DateTime.Parse(par.DeviceSJ)) | |||||
{ | |||||
devModel.infoMessage.data.Remove(par); | |||||
} | |||||
}); | |||||
} | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
ConsoleHelper.WriteErrorLine($"错误{ex.Message}"); | |||||
} | |||||
} | |||||
/// <summary> | |||||
/// MQTT 消息 | |||||
/// </summary> | |||||
/// <param name="topic"></param> | |||||
/// <param name="message"></param> | |||||
private void DevIOTActionHandler(string topic, string message) | |||||
{ | |||||
if (string.IsNullOrEmpty(topic)) return; | |||||
if (topic == IOTDevServer.HeartbeatSubTopic)//上下线订阅主题 | |||||
{ | |||||
ReceiveModel receiveModel = Tools.JsonToObjectTools<ReceiveModel>(message); | |||||
if (receiveModel != null && receiveModel.status != null)//上下线通知 | |||||
{ | |||||
if (receiveModel.deviceContext.deviceName != "Transit") | |||||
{ | |||||
ConsoleHelper.WriteWarningLine("接收数据 " + topic + " 数据 " + message); | |||||
SentData(receiveModel); | |||||
} | |||||
} | |||||
} | |||||
} | |||||
} | |||||
} |