终端一体化运控平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

218 строки
11 KiB

  1. using BPASmart.Model;
  2. using BPA.Helper;
  3. using BPA.Communication;
  4. using Microsoft.EntityFrameworkCore.Metadata.Conventions;
  5. using System.Collections.Concurrent;
  6. using Newtonsoft.Json;
  7. namespace BPASmart.Server
  8. {
  9. internal class CommunicationServer : IServer
  10. {
  11. ConcurrentDictionary<string, ICommunication> CommunicationDevices = new ConcurrentDictionary<string, ICommunication>();
  12. public void Init()
  13. {
  14. BPASmartClient.Message.MessageLog.GetInstance.ShowDebugLog("通讯模块初始化");
  15. MessageLog.GetInstance.NotifyShow = new Action<string>((s) =>
  16. {
  17. BPASmartClient.Message.MessageLog.GetInstance.ShowDebugLog(s);
  18. });
  19. RedisHelper.GetInstance.Connect(new ConfigurationOptions()
  20. {
  21. ServerAddress = $"124.222.238.75:16000",
  22. Password = "123456",
  23. });
  24. MqttInit();
  25. Json<CommunicationPar>.Data.CommunicationDevices.ToList()?.ForEach(item =>
  26. {
  27. ThreadManage.GetInstance().Start(new Action(() =>
  28. {
  29. switch (item.CommDevice)
  30. {
  31. case BPASmart.Model.ModbusRtu _modbusRtu:
  32. break;
  33. case BPASmart.Model.ModbusTcp _modbusTcp:
  34. BPA.Communication.ModbusTcp modbusTcpMaster = new BPA.Communication.ModbusTcp();
  35. modbusTcpMaster.ConnectOk = new Action(() =>
  36. {
  37. if (!CommunicationDevices.ContainsKey(item.DeviceName))
  38. CommunicationDevices.TryAdd(item.DeviceName, modbusTcpMaster);
  39. ThreadManage.GetInstance().StartLong(new Action(() =>
  40. {
  41. item.VarTableModels.GetReadDataModels().ToList()?.ForEach(temp =>
  42. {
  43. Array ResultArray = null;
  44. temp.Value?.ForEach(value =>
  45. {
  46. switch (temp.Key)
  47. {
  48. case EDataType.Bool:
  49. ResultArray = modbusTcpMaster.Read<bool[]>(value.StartAddress.ToString(), value.Length)?.Content;
  50. break;
  51. case EDataType.Byte:
  52. break;
  53. case EDataType.Int:
  54. ResultArray = modbusTcpMaster.Read<short[]>(value.StartAddress.ToString(), value.Length)?.Content;
  55. break;
  56. case EDataType.Word:
  57. ResultArray = modbusTcpMaster.Read<ushort[]>(value.StartAddress.ToString(), value.Length)?.Content;
  58. break;
  59. case EDataType.Dint:
  60. ResultArray = modbusTcpMaster.Read<int[]>(value.StartAddress.ToString(), value.Length)?.Content;
  61. break;
  62. case EDataType.Dword:
  63. ResultArray = modbusTcpMaster.Read<uint[]>(value.StartAddress.ToString(), value.Length)?.Content;
  64. break;
  65. case EDataType.Float:
  66. ResultArray = modbusTcpMaster.Read<float[]>(value.StartAddress.ToString(), value.Length)?.Content;
  67. break;
  68. default:
  69. break;
  70. }
  71. SetValue(ResultArray, item.DeviceName, value, temp.Key);
  72. });
  73. });
  74. Thread.Sleep(100);
  75. }), $"{item.DeviceName} 设备数据采集");
  76. var DeviceModel = item;
  77. });
  78. modbusTcpMaster.IsReconnect = true;
  79. modbusTcpMaster.Connect(new ConfigurationOptions()
  80. {
  81. IpAddress = _modbusTcp.IP,
  82. Port = _modbusTcp.PortNum
  83. });
  84. break;
  85. case BPASmart.Model.Siemens _siemens:
  86. break;
  87. default:
  88. break;
  89. }
  90. }), $"{item.DeviceName} 初始化连接");
  91. });
  92. }
  93. ConcurrentQueue<string> msg = new ConcurrentQueue<string>();
  94. private void MqttInit()
  95. {
  96. MqttHelper mqttHelper = new MqttHelper();
  97. mqttHelper.Connect("admin", "fengyoufu067101!@#", "124.222.238.75", 61613, $"分布式上位机:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
  98. mqttHelper.ConnectOk = new Action(() =>
  99. {
  100. mqttHelper.Subscrib(Topics.DeviceControl);
  101. ThreadManage.GetInstance().StartLong(new Action(() =>
  102. {
  103. while (msg.Count > 0)
  104. {
  105. if (msg.TryDequeue(out string s))
  106. {
  107. var res = JsonConvert.DeserializeObject(s) as IMessage;
  108. if (res != null)
  109. {
  110. if (res is PublishInfo _mPublishInfo)
  111. {
  112. DeviceControl(_mPublishInfo);
  113. }
  114. }
  115. }
  116. }
  117. Thread.Sleep(100);
  118. }), "MQTT 消息监听");
  119. });
  120. mqttHelper.MessageRecive = new Action<string>((s) =>
  121. {
  122. msg.Enqueue(s);
  123. });
  124. }
  125. private void DeviceControl(PublishInfo publishInfo)
  126. {
  127. publishInfo.PublishModels.ForEach(item =>
  128. {
  129. if (CommunicationDevices.ContainsKey(item.DeviceName))
  130. {
  131. string address = string.Empty;
  132. if (item.RealAddress != null && item.RealAddress.Length > 0) address = item.DeviceName;
  133. else
  134. {
  135. var res = Json<CommunicationPar>.Data.CommunicationDevices.FirstOrDefault(p => p.DeviceName == item.DeviceName);
  136. var res2 = res?.VarTableModels.FirstOrDefault(p => p.VarName?.Length > 0 && p.VarName == item.VarName);
  137. address = res2?.RealAddress;
  138. }
  139. if (item.Value != null && item.Value.Length > 0)
  140. {
  141. switch (item.DataType)
  142. {
  143. case EDataType.Bool:
  144. CommunicationDevices[item.DeviceName].Write(address, Convert.ToBoolean(item.Value));
  145. break;
  146. case EDataType.Byte:
  147. CommunicationDevices[item.DeviceName].Write(address, Convert.ToByte(item.Value));
  148. break;
  149. case EDataType.Int:
  150. CommunicationDevices[item.DeviceName].Write(address, Convert.ToInt16(item.Value));
  151. break;
  152. case EDataType.Word:
  153. CommunicationDevices[item.DeviceName].Write(address, Convert.ToUInt16(item.Value));
  154. break;
  155. case EDataType.Dint:
  156. CommunicationDevices[item.DeviceName].Write(address, Convert.ToInt32(item.Value));
  157. break;
  158. case EDataType.Dword:
  159. CommunicationDevices[item.DeviceName].Write(address, Convert.ToUInt32(item.Value));
  160. break;
  161. case EDataType.Float:
  162. CommunicationDevices[item.DeviceName].Write(address, Convert.ToSingle(item.Value));
  163. break;
  164. default:
  165. break;
  166. }
  167. }
  168. }
  169. });
  170. }
  171. private void SetValue(Array arrays, string DeviceName, ReadDataModel readDataModel, EDataType eDataType)
  172. {
  173. if (arrays != null)
  174. {
  175. ushort by = eDataType.GetEDataSize();
  176. int index = Array.FindIndex(Json<CommunicationPar>.Data.CommunicationDevices.ToArray(), p => p.DeviceName == DeviceName);//获取设备所在集合位置
  177. if (index >= 0 && index < Json<CommunicationPar>.Data.CommunicationDevices.Count)
  178. {
  179. var tempArray = Json<CommunicationPar>.Data.CommunicationDevices.ElementAt(index).VarTableModels.ToArray();
  180. for (int i = 0; i < arrays.Length; i++)
  181. {
  182. int varIndex = Array.FindIndex(tempArray, p => p.RealAddress == (readDataModel.StartAddress + (i * by)).ToString());
  183. if (varIndex >= 0 && varIndex < tempArray.Length)
  184. {
  185. Json<CommunicationPar>.Data.CommunicationDevices.ElementAt(index).VarTableModels.ElementAt(varIndex).CurrentValue = arrays.GetValue(i)?.ToString();
  186. }
  187. }
  188. var Devicename = Json<CommunicationPar>.Data.CommunicationDevices[index].DeviceName;
  189. List<ReeisDataModel> reeisDataModels = new List<ReeisDataModel>();
  190. Json<CommunicationPar>.Data.CommunicationDevices[index].VarTableModels.ToList().ForEach(tempVar =>
  191. {
  192. if (tempVar.VarName.Length > 0)
  193. {
  194. reeisDataModels.Add(new ReeisDataModel()
  195. {
  196. VarName = tempVar.VarName,
  197. VarVaule = tempVar.CurrentValue,
  198. DataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.DataType)
  199. });
  200. }
  201. });
  202. RedisHelper.GetInstance.Write($"{Devicename}", reeisDataModels);
  203. }
  204. }
  205. }
  206. }
  207. }