FestoControllerCfgManager.cs 13 KB

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