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

378 рядки
21 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. //switch (temp.Key)
  37. //{
  38. // case EDataType.Bool:
  39. // temp.Value?.ForEach(value =>
  40. // {
  41. // //var res = modbusTcpMaster.ReadBool(value.StartAddress.ToString(), value.Length);
  42. // var res = modbusTcpMaster.Read<bool[]>(value.StartAddress.ToString(), value.Length);
  43. // SetValue(res.Content, item.DeviceName, value, 1);
  44. // });
  45. // break;
  46. // case EDataType.Byte:
  47. // break;
  48. // case EDataType.Int:
  49. // break;
  50. // case EDataType.Word:
  51. // temp.Value?.ForEach(value =>
  52. // {
  53. // //var res = modbusTcpMaster.ReadUshort(value.StartAddress.ToString(), value.Length);
  54. // var res = modbusTcpMaster.Read<ushort[]>(value.StartAddress.ToString(), value.Length);
  55. // SetValue(res.Content, item.DeviceName, value, 1);
  56. // });
  57. // break;
  58. // case EDataType.Dint:
  59. // break;
  60. // case EDataType.Dword:
  61. // temp.Value?.ForEach(value =>
  62. // {
  63. // //var res = modbusTcpMaster.ReadUint(value.StartAddress.ToString(), value.Length);
  64. // var res = modbusTcpMaster.Read<uint[]>(value.StartAddress.ToString(), value.Length);
  65. // SetValue(res.Content, item.DeviceName, value, 2);
  66. // });
  67. // break;
  68. // case EDataType.Float:
  69. // temp.Value?.ForEach(value =>
  70. // {
  71. // //var res = modbusTcpMaster.ReadFloat(value.StartAddress.ToString(), value.Length);
  72. // var res = modbusTcpMaster.Read<float[]>(value.StartAddress.ToString(), value.Length);
  73. // SetValue(res.Content, item.DeviceName, value, 2);
  74. // });
  75. // break;
  76. // default:
  77. // break;
  78. //}
  79. Array ResultArray = null;
  80. temp.Value?.ForEach(value =>
  81. {
  82. switch (temp.Key)
  83. {
  84. case EDataType.Bool:
  85. ResultArray = modbusTcpMaster.Read<bool[]>(value.StartAddress.ToString(), value.Length)?.Content;
  86. break;
  87. case EDataType.Byte:
  88. break;
  89. case EDataType.Int:
  90. ResultArray = modbusTcpMaster.Read<short[]>(value.StartAddress.ToString(), value.Length)?.Content;
  91. break;
  92. case EDataType.Word:
  93. ResultArray = modbusTcpMaster.Read<ushort[]>(value.StartAddress.ToString(), value.Length)?.Content;
  94. break;
  95. case EDataType.Dint:
  96. ResultArray = modbusTcpMaster.Read<int[]>(value.StartAddress.ToString(), value.Length)?.Content;
  97. break;
  98. case EDataType.Dword:
  99. ResultArray = modbusTcpMaster.Read<uint[]>(value.StartAddress.ToString(), value.Length)?.Content;
  100. break;
  101. case EDataType.Float:
  102. ResultArray = modbusTcpMaster.Read<float[]>(value.StartAddress.ToString(), value.Length)?.Content;
  103. break;
  104. default:
  105. break;
  106. }
  107. SetValue(ResultArray, item.DeviceName, value, temp.Key);
  108. });
  109. });
  110. Thread.Sleep(100);
  111. }), $"{item.DeviceName} 设备数据采集");
  112. var DeviceModel = item;
  113. });
  114. modbusTcpMaster.IsReconnect = true;
  115. modbusTcpMaster.ModbusTcpConnect(_modbusTcp.IP, _modbusTcp.PortNum);
  116. break;
  117. case BPASmart.Model.Siemens _siemens:
  118. break;
  119. default:
  120. break;
  121. }
  122. }), $"{item.DeviceName} 初始化连接");
  123. });
  124. }
  125. ConcurrentQueue<string> msg = new ConcurrentQueue<string>();
  126. private void MqttInit()
  127. {
  128. MqttHelper mqttHelper = new MqttHelper();
  129. mqttHelper.Connect("admin", "fengyoufu067101!@#", "124.222.238.75", 61613, $"分布式上位机:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
  130. mqttHelper.ConnectOk = new Action(() =>
  131. {
  132. mqttHelper.Subscrib("DistributedHostComputer/Control");
  133. ThreadManage.GetInstance().StartLong(new Action(() =>
  134. {
  135. while (msg.Count > 0)
  136. {
  137. DeviceControl();
  138. }
  139. Thread.Sleep(100);
  140. }), "MQTT 消息监听");
  141. });
  142. mqttHelper.MessageRecive = new Action<string>((s) =>
  143. {
  144. msg.Enqueue(s);
  145. });
  146. }
  147. private void DeviceControl()
  148. {
  149. if (msg.TryDequeue(out string s))
  150. {
  151. var res = JsonConvert.DeserializeObject<PublishInfo>(s);
  152. if (res != null)
  153. {
  154. res.PublishModels.ForEach(item =>
  155. {
  156. if (CommunicationDevices.ContainsKey(item.DeviceName))
  157. {
  158. string address = string.Empty;
  159. if (item.RealAddress != null && item.RealAddress.Length > 0) address = item.DeviceName;
  160. else
  161. {
  162. var res = Json<CommunicationPar>.Data.CommunicationDevices.FirstOrDefault(p => p.DeviceName == item.DeviceName);
  163. var res2 = res?.VarTableModels.FirstOrDefault(p => p.VarName?.Length > 0 && p.VarName == item.VarName);
  164. address = res2?.RealAddress;
  165. }
  166. if (item.Value != null && item.Value.Length > 0)
  167. {
  168. switch (item.DataType)
  169. {
  170. case EDataType.Bool:
  171. //CommunicationDevices[item.DeviceName].Write(address, Convert.ToBoolean(item.Value));
  172. CommunicationDevices[item.DeviceName].Write(address, Convert.ToBoolean(item.Value));
  173. break;
  174. case EDataType.Byte:
  175. //CommunicationDevices[item.DeviceName].Write(address, Convert.ToByte(item.Value));
  176. CommunicationDevices[item.DeviceName].Write(address, Convert.ToByte(item.Value));
  177. break;
  178. case EDataType.Int:
  179. //CommunicationDevices[item.DeviceName].Write(address, Convert.ToInt16(item.Value));
  180. CommunicationDevices[item.DeviceName].Write(address, Convert.ToInt16(item.Value));
  181. break;
  182. case EDataType.Word:
  183. //CommunicationDevices[item.DeviceName].Write(address, Convert.ToUInt16(item.Value));
  184. CommunicationDevices[item.DeviceName].Write(address, Convert.ToUInt16(item.Value));
  185. break;
  186. case EDataType.Dint:
  187. //CommunicationDevices[item.DeviceName].Write(address, Convert.ToInt32(item.Value));
  188. CommunicationDevices[item.DeviceName].Write(address, Convert.ToInt32(item.Value));
  189. break;
  190. case EDataType.Dword:
  191. //CommunicationDevices[item.DeviceName].Write(address, Convert.ToUInt32(item.Value));
  192. CommunicationDevices[item.DeviceName].Write(address, Convert.ToUInt32(item.Value));
  193. break;
  194. case EDataType.Float:
  195. //CommunicationDevices[item.DeviceName].Write(address, Convert.ToSingle(item.Value));
  196. CommunicationDevices[item.DeviceName].Write(address, Convert.ToSingle(item.Value));
  197. break;
  198. default:
  199. break;
  200. }
  201. }
  202. }
  203. });
  204. }
  205. }
  206. }
  207. //private void SetValue<TArray>(TArray[] arrays, string DeviceName, ReadDataModel readDataModel, ushort by)
  208. //{
  209. // if (arrays != null)
  210. // {
  211. // int index = Array.FindIndex(Json<CommunicationPar>.Data.CommunicationDevices.ToArray(), p => p.DeviceName == DeviceName);//获取设备所在集合位置
  212. // if (index >= 0 && index < Json<CommunicationPar>.Data.CommunicationDevices.Count)
  213. // {
  214. // var tempArray = Json<CommunicationPar>.Data.CommunicationDevices.ElementAt(index).VarTableModels.ToArray();
  215. // for (int i = 0; i < arrays.Length; i++)
  216. // {
  217. // int varIndex = Array.FindIndex(tempArray, p => p.RealAddress == (readDataModel.StartAddress + (i * by)).ToString());
  218. // if (varIndex >= 0 && varIndex < tempArray.Length)
  219. // {
  220. // Json<CommunicationPar>.Data.CommunicationDevices.ElementAt(index).VarTableModels.ElementAt(varIndex).CurrentValue = arrays[i].ToString();
  221. // }
  222. // }
  223. // var Devicename = Json<CommunicationPar>.Data.CommunicationDevices[index].DeviceName;
  224. // List<ReeisDataModel> reeisDataModels = new List<ReeisDataModel>();
  225. // Json<CommunicationPar>.Data.CommunicationDevices[index].VarTableModels.ToList().ForEach(tempVar =>
  226. // {
  227. // if (tempVar.VarName.Length > 0)
  228. // {
  229. // reeisDataModels.Add(new ReeisDataModel()
  230. // {
  231. // VarName = tempVar.VarName,
  232. // VarVaule = tempVar.CurrentValue,
  233. // DataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.DataType)
  234. // });
  235. // }
  236. // });
  237. // RedisHelper.GetInstance.SetValue($"{Devicename}", reeisDataModels);
  238. // }
  239. // }
  240. //}
  241. //private ushort GetBySize(EDataType eDataType)
  242. //{
  243. // switch (eDataType)
  244. // {
  245. // case EDataType.Bool:
  246. // case EDataType.Byte:
  247. // case EDataType.Int:
  248. // case EDataType.Word:
  249. // return 1;
  250. // case EDataType.Dint:
  251. // case EDataType.Dword:
  252. // case EDataType.Float:
  253. // return 2;
  254. // case EDataType.Double:
  255. // break;
  256. // case EDataType.String:
  257. // break;
  258. // default:
  259. // break;
  260. // }
  261. // return 1;
  262. //}
  263. private void SetValue(Array arrays, string DeviceName, ReadDataModel readDataModel, EDataType eDataType)
  264. {
  265. if (arrays != null)
  266. {
  267. ushort by = eDataType.GetEDataSize();
  268. int index = Array.FindIndex(Json<CommunicationPar>.Data.CommunicationDevices.ToArray(), p => p.DeviceName == DeviceName);//获取设备所在集合位置
  269. if (index >= 0 && index < Json<CommunicationPar>.Data.CommunicationDevices.Count)
  270. {
  271. var tempArray = Json<CommunicationPar>.Data.CommunicationDevices.ElementAt(index).VarTableModels.ToArray();
  272. for (int i = 0; i < arrays.Length; i++)
  273. {
  274. int varIndex = Array.FindIndex(tempArray, p => p.RealAddress == (readDataModel.StartAddress + (i * by)).ToString());
  275. if (varIndex >= 0 && varIndex < tempArray.Length)
  276. {
  277. Json<CommunicationPar>.Data.CommunicationDevices.ElementAt(index).VarTableModels.ElementAt(varIndex).CurrentValue = arrays.GetValue(i)?.ToString();
  278. }
  279. }
  280. var Devicename = Json<CommunicationPar>.Data.CommunicationDevices[index].DeviceName;
  281. List<ReeisDataModel> reeisDataModels = new List<ReeisDataModel>();
  282. Json<CommunicationPar>.Data.CommunicationDevices[index].VarTableModels.ToList().ForEach(tempVar =>
  283. {
  284. if (tempVar.VarName.Length > 0)
  285. {
  286. reeisDataModels.Add(new ReeisDataModel()
  287. {
  288. VarName = tempVar.VarName,
  289. VarVaule = tempVar.CurrentValue,
  290. DataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.DataType)
  291. });
  292. }
  293. });
  294. RedisHelper.GetInstance.SetValue($"{Devicename}", reeisDataModels);
  295. }
  296. }
  297. }
  298. //private Dictionary<EDataType, List<ReadDataModel>> GetReadDataModels(CommunicationModel communicationModel)
  299. //{
  300. // Dictionary<EDataType, List<ReadDataModel>> readDataModels = new Dictionary<EDataType, List<ReadDataModel>>();
  301. // communicationModel.VarTableModels.GroupBy(p => p.DataType)?.ToList()?.ForEach(tempVar =>
  302. // {
  303. // if (tempVar.Key != null && tempVar.Key.Length > 0)
  304. // {
  305. // //int address = tempVar.Min(p => p.RealAddress);
  306. // EDataType dataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.Key);
  307. // switch (dataType)
  308. // {
  309. // case EDataType.Bool:
  310. // case EDataType.Byte:
  311. // case EDataType.Int:
  312. // case EDataType.Word:
  313. // if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, GetDataGroup(tempVar));
  314. // break;
  315. // case EDataType.Dint:
  316. // case EDataType.Dword:
  317. // case EDataType.Float:
  318. // if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, GetDataGroup(tempVar, 2));
  319. // break;
  320. // default:
  321. // break;
  322. // }
  323. // }
  324. // });
  325. // return readDataModels;
  326. //}
  327. //private List<ReadDataModel> GetDataGroup(IGrouping<string, VariableInfo> variableInfos, int by = 1)
  328. //{
  329. // List<ReadDataModel> ReturnValue = new List<ReadDataModel>();
  330. // var res = variableInfos?.OrderBy(p => p.RealAddress).ToList();
  331. // List<int> RealAddresss = new List<int>();
  332. // variableInfos.ToList()?.ForEach(item => { if (int.TryParse(item.RealAddress, out int add)) RealAddresss.Add(add); });
  333. // int count = 0;
  334. // if (res != null)
  335. // {
  336. // //int address = variableInfos.Min(p => p.RealAddress);
  337. // int address = RealAddresss.Min();
  338. // int startAddress = address;
  339. // for (int i = 0; i < res.Count; i++)
  340. // {
  341. // if (int.TryParse(res.ElementAt(i).RealAddress, out int TempAddress))
  342. // {
  343. // if (TempAddress == address)
  344. // {
  345. // count++;
  346. // address += by;
  347. // }
  348. // else
  349. // {
  350. // ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count });
  351. // count = 1;
  352. // address = TempAddress + by;
  353. // startAddress = TempAddress;
  354. // }
  355. // }
  356. // }
  357. // ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count });
  358. // }
  359. // return ReturnValue;
  360. //}
  361. }
  362. }