终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

201 lines
11 KiB

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