终端一体化运控平台
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

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