FestoControllerCfgManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. using Aitex.Common.Util;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.RT.SCCore;
  4. using Aitex.Core.Util;
  5. using DocumentFormat.OpenXml.Bibliography;
  6. using MECF.Framework.Common.Device.PowerSupplier;
  7. using MECF.Framework.Common.IOCore;
  8. using System;
  9. using System.Collections.Concurrent;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Media.Animation;
  16. namespace MECF.Framework.Common.Device.Festo
  17. {
  18. public class FestoControllerCfgManager : Singleton<FestoControllerCfgManager>
  19. {
  20. #region 内部变量
  21. /// <summary>
  22. /// do--Modbus设备字典(key-DO名称,value-Modbus设备)
  23. /// </summary>
  24. private Dictionary<string, FestoModbusDevice> _doModbusDictionary = new Dictionary<string, FestoModbusDevice>();
  25. /// <summary>
  26. /// 名称数据数组字典(key-festo名称,value-每个festo的byte数组)
  27. /// </summary>
  28. private ConcurrentDictionary<string, byte[]> _nameDatasDictionary = new ConcurrentDictionary<string, byte[]>();
  29. /// <summary>
  30. /// 名称锁字典(key-fest名称,value-locker)
  31. /// </summary>
  32. private Dictionary<string, object> _nameLockerDictionary = new Dictionary<string, object>();
  33. /// <summary>
  34. /// 名称数据数组字典(key-festo名称,value-每个festo的byte数组长度)
  35. /// </summary>
  36. private ConcurrentDictionary<string, int> _nameDataLengthDictionary = new ConcurrentDictionary<string, int>();
  37. /// <summary>
  38. /// do--Festo名称字典(key-DO名称,value-festo名称)
  39. /// </summary>
  40. private Dictionary<string,string> _doFestoNameDictionary = new Dictionary<string, string>();
  41. /// <summary>
  42. /// do对象字典(key-DO名称,value-FestDO对象)
  43. /// </summary>
  44. private Dictionary<string,FestoDO> _doDictionary=new Dictionary<string, FestoDO>();
  45. /// <summary>
  46. /// do 索引对象字典(key-地址索引-bit,value--do名称)
  47. /// </summary>
  48. private Dictionary<string, string> _doIndexDictionary = new Dictionary<string, string>();
  49. #endregion
  50. /// <summary>
  51. /// 初始化
  52. /// </summary>
  53. public void Initialize(List<string> lst)
  54. {
  55. bool isSimulate = SC.GetValue<bool>("System.IsSimulatorMode");
  56. string xmlPath = "";
  57. try
  58. {
  59. if (isSimulate)
  60. {
  61. xmlPath = PathManager.GetCfgDir() + "Devices\\FestoControllerCfg-Simulator.xml";
  62. }
  63. else
  64. {
  65. xmlPath = PathManager.GetCfgDir() + "Devices\\FestoControllerCfg.xml";
  66. }
  67. FestoControllerCfg cfg = CustomXmlSerializer.Deserialize<FestoControllerCfg>(new FileInfo(xmlPath));
  68. if (cfg != null)
  69. {
  70. foreach (FestoDeviceConfig config in cfg.FestoDeviceConfigs)
  71. {
  72. FestoModbusDevice modbusDevice = new FestoModbusDevice(config.Name, config.IpAddress, config.Port,(ushort)config.DIStartAddress,
  73. (byte)config.Channel);
  74. modbusDevice.ReceiveTimeout = config.RecvTimeout;
  75. modbusDevice.SendTimeout = config.SendTimeout;
  76. _nameLockerDictionary[config.Name] = new object();
  77. InitialDeviceConfig(config, modbusDevice,lst);
  78. ushort addressCiount = (ushort)_nameDataLengthDictionary[config.Name];
  79. modbusDevice.InitializeAddressCount(addressCiount);
  80. }
  81. }
  82. }
  83. catch
  84. {
  85. LOG.WriteLog(eEvent.ERR_FESTO, "Festo", "Load festo xml failed");
  86. }
  87. }
  88. /// <summary>
  89. /// 初始化DO Modbus设备对象
  90. /// </summary>
  91. /// <param name="deviceConfig"></param>
  92. /// <param name="modbusDevice"></param>
  93. private void InitialDeviceConfig(FestoDeviceConfig deviceConfig,FestoModbusDevice modbusDevice,List<string> lst)
  94. {
  95. List<int> addressLst = new List<int>();
  96. int index = -1;
  97. foreach(var item in deviceConfig.FestoDoes)
  98. {
  99. _doModbusDictionary[item.Name] = modbusDevice;
  100. if (!addressLst.Contains(item.Address))
  101. {
  102. addressLst.Add(item.Address);
  103. index++;
  104. }
  105. item.DataIndex = index;
  106. _doFestoNameDictionary[item.Name] = deviceConfig.Name;
  107. _doDictionary[item.Name]= item;
  108. _doIndexDictionary[$"{deviceConfig.Name}-{item.DataIndex}-{item.Bit}"] = item.Name;
  109. if (!lst.Contains(item.Name))
  110. {
  111. lst.Add(item.Name);
  112. }
  113. }
  114. _nameDataLengthDictionary[deviceConfig.Name]=addressLst.Count;
  115. }
  116. /// <summary>
  117. /// 设置DO数值
  118. /// </summary>
  119. /// <param name="doName"></param>
  120. /// <param name="value"></param>
  121. /// <returns></returns>
  122. public bool SetDoValue(string doName,bool value)
  123. {
  124. if (!_doDictionary.ContainsKey(doName))
  125. {
  126. LOG.WriteLog(eEvent.ERR_FESTO, "Festo", $"{doName} object is null");
  127. return false;
  128. }
  129. FestoDO festoDO=_doDictionary[doName];
  130. if (!_doFestoNameDictionary.ContainsKey(doName))
  131. {
  132. LOG.WriteLog(eEvent.ERR_FESTO, "Festo", $"{doName} festo name is empty");
  133. return false;
  134. }
  135. string festName = _doFestoNameDictionary[doName];
  136. if (!_nameDatasDictionary.ContainsKey(festName))
  137. {
  138. LOG.WriteLog(eEvent.ERR_FESTO, "Festo", $"{festName} is not connected");
  139. return false;
  140. }
  141. if (!_nameLockerDictionary.ContainsKey(festName))
  142. {
  143. LOG.WriteLog(eEvent.ERR_FESTO, "Festo", $"{festName} has no locker");
  144. return false;
  145. }
  146. lock (_nameLockerDictionary[festName])
  147. {
  148. byte[] data = _nameDatasDictionary[festName];
  149. if (festoDO.DataIndex >= data.Length)
  150. {
  151. LOG.WriteLog(eEvent.ERR_FESTO, "Festo", $"{festName} data index is overflow");
  152. return false;
  153. }
  154. if (!_doModbusDictionary.ContainsKey(doName))
  155. {
  156. LOG.WriteLog(eEvent.ERR_FESTO, "Festo", $"{doName} festo device is null");
  157. return false;
  158. }
  159. FestoModbusDevice device = _doModbusDictionary[doName];
  160. try
  161. {
  162. bool lastValue = festoDO.Invert ? !value : value;
  163. byte festoValue = GenerateFestoData(festoDO, data[festoDO.DataIndex], lastValue);
  164. bool result = device.SetFestoValue((ushort)festoDO.Address, festoValue);
  165. if (result)
  166. {
  167. _nameDatasDictionary[festName][festoDO.DataIndex] = festoValue;
  168. LOG.WriteLog(eEvent.INFO_FESTO, "Festo", $"{doName} write {value} success");
  169. }
  170. else
  171. {
  172. LOG.WriteLog(eEvent.ERR_FESTO, "Festo", $"{doName} write {value} failed");
  173. }
  174. return result;
  175. }
  176. catch (Exception ex)
  177. {
  178. LOG.WriteLog(eEvent.ERR_FESTO, "Festo", $"write {doName} value {value} {ex.Message}");
  179. return false;
  180. }
  181. }
  182. }
  183. /// <summary>
  184. /// 构建Festo字节数值
  185. /// </summary>
  186. /// <param name="festoDO"></param>
  187. /// <param name="source"></param>
  188. /// <param name="value"></param>
  189. /// <returns></returns>
  190. private byte GenerateFestoData(FestoDO festoDO,byte source,bool value)
  191. {
  192. int bit = festoDO.Bit;
  193. bool sourceBit = (source>>bit & 0x01) == 1;
  194. if (sourceBit == value)
  195. {
  196. return source;
  197. }
  198. else
  199. {
  200. if (value)
  201. {
  202. return (byte)(source + Math.Pow(2,bit));
  203. }
  204. else
  205. {
  206. return (byte)(source - Math.Pow(2,bit));
  207. }
  208. }
  209. }
  210. /// <summary>
  211. /// 更新Festo数组
  212. /// </summary>
  213. /// <param name="festoName"></param>
  214. /// <param name="datas"></param>
  215. public void UpdateFestoData(string festoName, byte[] datas)
  216. {
  217. if (!_nameDataLengthDictionary.ContainsKey(festoName))
  218. {
  219. LOG.WriteLog(eEvent.ERR_FESTO, "Festo", $"festo device {festoName} is invalid");
  220. return;
  221. }
  222. int dataLength = _nameDataLengthDictionary[festoName];
  223. byte[] bytes = null;
  224. if (!_nameDatasDictionary.ContainsKey(festoName))
  225. {
  226. bytes = new byte[dataLength];
  227. FirstUpdateDatas(festoName,datas);
  228. Array.Copy(datas,0,bytes,0,dataLength);
  229. _nameDatasDictionary[festoName] = bytes;
  230. return;
  231. }
  232. else
  233. {
  234. bytes=_nameDatasDictionary[festoName];
  235. }
  236. if (bytes.Length != datas.Length)
  237. {
  238. LOG.WriteLog(eEvent.ERR_FESTO, "Festo", $"festo device {festoName} array source data length {bytes.Length} is not equal to {datas.Length}");
  239. return;
  240. }
  241. CheckSourceDataUpdated(festoName,bytes, datas);
  242. }
  243. /// <summary>
  244. /// 首次更新数据
  245. /// </summary>
  246. /// <param name="datas"></param>
  247. private void FirstUpdateDatas(string festoName,byte[] datas)
  248. {
  249. for (int i = 0; i < datas.Length; i++)
  250. {
  251. bool[] sourceBits = GetByteBitValue(datas[i]);
  252. for(int j = 0; j < sourceBits.Length; j++)
  253. {
  254. UpdateIOBitValue(festoName,i, j, sourceBits[j]);
  255. }
  256. }
  257. }
  258. /// <summary>
  259. /// 通知字节数值变化
  260. /// </summary>
  261. /// <param name="source"></param>
  262. /// <param name="datas"></param>
  263. private void CheckSourceDataUpdated(string festoName,byte[] sourceDatas, byte[] datas)
  264. {
  265. for (int i = 0; i < datas.Length; i++)
  266. {
  267. byte data = datas[i];
  268. byte source=sourceDatas[i];
  269. if (data != source)
  270. {
  271. CheckSourceDOUpdated(festoName,source, data,i);
  272. sourceDatas[i] = data;
  273. }
  274. }
  275. }
  276. /// <summary>
  277. /// 通知DO数值变化
  278. /// </summary>
  279. /// <param name="source"></param>
  280. /// <param name="data"></param>
  281. private void CheckSourceDOUpdated(string festoName,byte source,byte data,int index)
  282. {
  283. bool[] sourceBits=GetByteBitValue(source);
  284. bool[] dataBits=GetByteBitValue(data);
  285. for(int i = 0; i < sourceBits.Length; i++)
  286. {
  287. if (sourceBits[i] == dataBits[i])
  288. {
  289. continue;
  290. }
  291. UpdateIOBitValue(festoName,index, i, dataBits[i]);
  292. }
  293. }
  294. /// <summary>
  295. /// 更新IO bit数值
  296. /// </summary>
  297. /// <param name="index"></param>
  298. /// <param name="bit"></param>
  299. /// <param name="value"></param>
  300. private void UpdateIOBitValue(string festoName,int index,int bit,bool value)
  301. {
  302. string strIndex = $"{festoName}-{index}-{bit}";
  303. if (!_doIndexDictionary.ContainsKey(strIndex))
  304. {
  305. return;
  306. }
  307. string doName = _doIndexDictionary[strIndex];
  308. if (!_doDictionary.ContainsKey(doName))
  309. {
  310. return;
  311. }
  312. FestoDO festoDO = _doDictionary[doName];
  313. if (festoDO.Invert)
  314. {
  315. IOModuleManager.Instance.UpdateIoValue(_doIndexDictionary[strIndex], !value);
  316. }
  317. else
  318. {
  319. IOModuleManager.Instance.UpdateIoValue(_doIndexDictionary[strIndex], value);
  320. }
  321. }
  322. /// <summary>
  323. /// 获取位数值
  324. /// </summary>
  325. /// <param name="data"></param>
  326. /// <returns></returns>
  327. private bool[] GetByteBitValue(byte data)
  328. {
  329. bool[] values = new bool[8];
  330. for(int i = 0; i < values.Length; i++)
  331. {
  332. if (i == 0)
  333. {
  334. values[i] = (data & 0x01) == 1;
  335. }
  336. else
  337. {
  338. values[i] = ((data >> i) & 0x01) == 1;
  339. }
  340. }
  341. return values;
  342. }
  343. }
  344. }