MsWorkstation.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using AmrControl.ADS;
  2. using AmrControl.Clients;
  3. using AmrControl.Common.HttpClients;
  4. using AmrControl.Vo;
  5. using Microsoft.AspNetCore.SignalR.Protocol;
  6. using MQTTnet.Internal;
  7. using Newtonsoft.Json;
  8. using NPOI.SS.Formula.Functions;
  9. using System.Threading;
  10. namespace AmrControl.workstation
  11. {
  12. public class MsWorkstation
  13. {
  14. DateTime StartTime { get; set; }
  15. bool IsStart = false;
  16. int period = 0;
  17. /// <summary>
  18. /// 配置信息
  19. /// </summary>
  20. private readonly IConfiguration _configuration;
  21. /// <summary>
  22. /// 服务获取
  23. /// </summary>
  24. private readonly IServiceProvider _service;
  25. WsState wsState { get; set; }
  26. /// <summary>
  27. /// 锁对象
  28. /// </summary>
  29. Mutex mutex = new Mutex();
  30. public MsWorkstation(IConfiguration configuration, IServiceProvider service)
  31. {
  32. period = 1000;
  33. try
  34. {
  35. period = Int32.Parse(configuration["Mqtt:period"]);
  36. if(period < 500)
  37. {
  38. period = 500;
  39. }
  40. }
  41. catch { }
  42. StartTime = DateTime.Now;
  43. this._configuration = configuration;
  44. this._service = service;
  45. wsState = new WsState();
  46. }
  47. TcClient client = null;
  48. //避免界面多次点击start方法
  49. int reqCount = 0;
  50. public void Start()
  51. {
  52. reqCount++;
  53. if (reqCount > 1)
  54. return;
  55. Stop();
  56. //if(client != null)
  57. //{
  58. // client.Close();
  59. //}
  60. //等待run退出,然后重启,有点风险
  61. Thread.Sleep(period + period + period);
  62. //关闭ADS
  63. //client = TcClient.GetInstance();
  64. //{
  65. // client.Close();
  66. //}
  67. StartTime = DateTime.Now;
  68. IsStart = true;
  69. Task.Run(run);
  70. reqCount = 0;
  71. }
  72. public void Stop()
  73. {
  74. IsStart = false;
  75. if (client != null)
  76. {
  77. client.Close();
  78. }
  79. }
  80. bool isRuning = false;
  81. void run()
  82. {
  83. while(IsStart)
  84. {
  85. //更新ADS的IO状态
  86. //Task.Run(readAds);
  87. //更新螺丝枪的状态
  88. Task.Run(readElectricScrewdriver);
  89. //更新电烙铁的状态
  90. Task.Run(readElectricIron);
  91. //if (client != null)
  92. //{
  93. // Task.Run(watchStationState);
  94. //}
  95. //等待时间
  96. Thread.Sleep(period);
  97. wsState.power = null;
  98. wsState.smokePurifier = null;
  99. wsState.balanceArm = null;
  100. wsState.triCokourLight = null;
  101. wsState.wristStrap = null;
  102. wsState.bodySensor = null;
  103. //发消息
  104. string msg = Newtonsoft.Json.JsonConvert.SerializeObject(wsState);
  105. sendMqMessage(msg);
  106. }
  107. }
  108. void watchStationState()
  109. {
  110. try
  111. {
  112. if (client.State == false)
  113. client.InitAds();
  114. //字节数,一个工位36个字节(倍福采用字节对齐方式最长4个字节)共18个工位的数据
  115. byte[] datas = client.ReadBytes("GVL_STATION.Station_Var", 648);
  116. List<StationStateVO> stationList = new();
  117. int stationPos = 1;
  118. for (var i = 0; i < datas.Length; i += 36)
  119. {
  120. stationList.Add(new()
  121. {
  122. stationPosNo = (stationPos++),
  123. //此处按规则新增工站编号信息(位置从仓储开始计算工位)
  124. power = Convert.ToBoolean(datas[i] | (datas[i + 1] << 8) | (datas[i + 2] << 16) | (datas[i + 3] << 32)),
  125. triColorLight = Convert.ToInt16(datas[i + 4] | (datas[i + 5] << 8) | (datas[i + 6] << 16) | (datas[i + 7] << 32)),
  126. wsristStrapMonitorCh1 = Convert.ToInt16(datas[i + 8] | (datas[i + 9] << 8) | (datas[i + 10] << 16) | (datas[i + 11] << 32)),
  127. wsristStrapMonitorCh2 = Convert.ToInt16(datas[i + 12] | (datas[i + 13] << 8) | (datas[i + 14] << 16) | (datas[i + 15] << 32)),
  128. smokePurifier = Convert.ToBoolean(datas[i + 16] | (datas[i + 17] << 8) | (datas[i + 18] << 16) | (datas[i + 19] << 32)),
  129. bigArm_encoding = Convert.ToInt16(datas[i + 20] | (datas[i + 21] << 8) | (datas[i + 22] << 16) | (datas[i + 23] << 32)),
  130. bigArm_angle = Convert.ToSingle(datas[i + 24] | (datas[i + 25] << 8) | (datas[i + 26] << 16) | (datas[i + 27] << 32)),
  131. smallArm_encoding = Convert.ToInt16(datas[i + 28] | (datas[i + 29] << 8) | (datas[i + 30] << 16) | (datas[i + 31] << 32)),
  132. smallArm_angle = Convert.ToSingle(datas[i + 32] | (datas[i + 29] << 33) | (datas[i + 34] << 16) | (datas[i + 35] << 32)),
  133. });
  134. }
  135. Console.WriteLine("stationStatus=======>"+JsonConvert.SerializeObject(stationList));
  136. Console.WriteLine("current station:" + JsonConvert.SerializeObject(stationList[8]));
  137. //将结果推送到前端,后期可修改为使用mq推送
  138. this.wsState.triCokourLight = stationList[8].triColorLight == 0 ? "off" :
  139. (stationList[8].triColorLight == 1 ? "green" : (stationList[8].triColorLight == 2 ? "yellow" : "red"));
  140. this.wsState.wristStrap = stationList[8].wsristStrapMonitorCh1 == 0
  141. && stationList[8].wsristStrapMonitorCh2 == 0 ? "off" : (stationList[8].wsristStrapMonitorCh1 == 1
  142. && stationList[8].wsristStrapMonitorCh2 == 1 ? "uwear" : "wear");
  143. this.wsState.smokePurifier = stationList[8].smokePurifier ? "on" : "off";
  144. this.wsState.power.state = stationList[8].power ? "on" : "off";
  145. }
  146. catch (Exception ex)
  147. {
  148. Console.WriteLine(ex.ToString());
  149. }
  150. }
  151. //ADS读
  152. void readAds()
  153. {
  154. //TcClient client = TcClient.GetInstance();
  155. //if (client == null)
  156. // return;
  157. //if (client.State == false)
  158. // client.InitAds();
  159. //读参数
  160. }
  161. //螺丝枪更新,从http的get请求获取
  162. void readElectricScrewdriver()
  163. {
  164. try
  165. {
  166. //todo::,从网络获取数据
  167. //http://10.10.25.100:8081/api/v1.0/processdata/latestfinalvalueobject
  168. DefaultHttpClient http = DefaultHttpClient.getClient("http://192.168.148.253:8081");
  169. Dictionary<string, string> header = new Dictionary<string, string>();
  170. header["Authorization"] = "Basic YWRtaW46MDAwMA==";
  171. HttpRespResult result = http.httpGetClient().sendRequest<string>("/api/v1.0/processdata/latestfinalvalueobject", header, null);
  172. if(result == null || result.data == null)
  173. {
  174. return;
  175. }
  176. DeprageElecScrewdriver deprage = Newtonsoft.Json.JsonConvert.DeserializeObject<DeprageElecScrewdriver>(result.data);
  177. if(deprage != null && deprage.Steps != null && deprage.Steps.Count > 0)
  178. {
  179. //在线
  180. this.wsState.electricScrewdriver.state = "on";
  181. StepState? state = deprage.Steps.Where(step => step.StepNumber==1).FirstOrDefault();
  182. if(state != null)
  183. {
  184. this.wsState.electricScrewdriver.angle = state.Angle.Value.ToString("0.00");
  185. this.wsState.electricScrewdriver.torqueForce = state.Torque.Value.ToString("0.0");
  186. this.wsState.electricScrewdriver.lowTorqueForce = state.Torque.Target.Lower.ToString("0.0");
  187. this.wsState.electricScrewdriver.highTorqueForce = state.Torque.Target.Upper.ToString("0.0");
  188. } else
  189. {
  190. this.wsState.electricScrewdriver.angle = "0.00";
  191. this.wsState.electricScrewdriver.torqueForce = "0.0";
  192. this.wsState.electricScrewdriver.lowTorqueForce = "0.0";
  193. this.wsState.electricScrewdriver.highTorqueForce = "0.0";
  194. }
  195. }
  196. else
  197. {
  198. //不在线
  199. this.wsState.electricScrewdriver.state = "off";
  200. }
  201. }
  202. catch { }
  203. }
  204. bool electricIronInit = false;
  205. int units = 0;
  206. //电烙铁更新
  207. void readElectricIron()
  208. {
  209. try
  210. {
  211. if (!electricIronInit)
  212. {
  213. mutex.WaitOne();
  214. try
  215. {
  216. if (!electricIronInit)
  217. {
  218. units = JbcDllInvoke.Init();
  219. Console.WriteLine($"read init result. {units}");
  220. electricIronInit = true;
  221. }
  222. } finally
  223. {
  224. mutex.ReleaseMutex();
  225. }
  226. }
  227. if (electricIronInit && units > 0)
  228. {
  229. try
  230. {
  231. Console.WriteLine($"初始化jbc,值为:{units}");
  232. int status = JbcDllInvoke.GetPortToolSleepStatus(units);
  233. Console.WriteLine($"read sleep status:{status}");
  234. this.wsState.electricIron.state = status > 0 ? "off" : "on";
  235. //获取设定值
  236. int setTemp = JbcDllInvoke.GetPortToolSelectedTemp(units, 1);
  237. Console.WriteLine($"set temperature:{setTemp}");
  238. this.wsState.electricIron.setTemperature = setTemp.ToString();
  239. int temp = JbcDllInvoke.GetPortToolActualTemp(units, 1);
  240. Console.WriteLine($"actual temperature:{temp}");
  241. this.wsState.electricIron.temperature = temp.ToString();
  242. int maxTemp = JbcDllInvoke.GetStationMaxTemp(units);
  243. Console.WriteLine($"max temperature:{maxTemp}");
  244. this.wsState.electricIron.highTemp = maxTemp.ToString();
  245. int minTemp = JbcDllInvoke.GetStationMinTemp(units);
  246. Console.WriteLine($"min temperature:{minTemp}");
  247. this.wsState.electricIron.lowTemp = minTemp.ToString();
  248. this.wsState.electricIron.warning = temp > maxTemp && temp < minTemp ? "true" : "false";
  249. }
  250. catch
  251. {
  252. electricIronInit = false;
  253. }
  254. }
  255. }
  256. catch (Exception ex)
  257. {
  258. Console.WriteLine (ex.Message );
  259. }
  260. }
  261. /// <summary>
  262. /// 接收到MQ消息,是命令
  263. /// </summary>
  264. /// <param name="topic"></param>
  265. /// <param name="message"></param>
  266. public void PushMqMessage(string topic, string message)
  267. {
  268. if (string.IsNullOrEmpty(message))
  269. return;
  270. try
  271. {
  272. WsCmd wscmd = (WsCmd)Newtonsoft.Json.JsonConvert.DeserializeObject<WsCmd>(message);
  273. if (wscmd != null && wscmd.Cmd=="start")
  274. {
  275. if (wscmd.Cmd == "start")
  276. {
  277. Start();
  278. }
  279. else if (wscmd.Cmd == "stop")
  280. {
  281. Stop();
  282. }
  283. }
  284. }
  285. catch(Exception ex)
  286. {
  287. }
  288. }
  289. void sendMqMessage(string msg)
  290. {
  291. try
  292. {
  293. MqttClient _mqttClient = _service.GetRequiredService<MqttClient>();
  294. if (_mqttClient != null)
  295. {
  296. _mqttClient.SendToMqtt(msg);
  297. }
  298. }
  299. catch
  300. {
  301. }
  302. }
  303. }
  304. public interface IWorkstation
  305. {
  306. void Start();
  307. void Stop();
  308. void PushMqMessage(string topic , string message);
  309. }
  310. }