XiaomiMqttClient_StationInputBegin.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using BZFAStandardLib;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.IO;
  5. using System.Threading.Tasks;
  6. namespace MainForm.ClassFile.XiaomiAPI
  7. {
  8. /// <summary>
  9. /// 小米 MqttClient类 - API拓展方法
  10. /// 节拍⽇志(OEE)
  11. /// • 节拍发⽣时上传
  12. /// 上料开始->上料结束->作业开始->作业结束->下料开始->下料结束
  13. /// </summary>
  14. public partial class XiaomiMqttClient_Extend : XiaomiMqttClient
  15. {
  16. /// <summary>
  17. /// 节拍⽇志(OEE)- 事件Id
  18. /// 上料开始->上料结束->作业开始->作业结束->下料开始->下料结束
  19. /// 上料开始:beat_log/business/OEE/station_input_begin
  20. /// 上料结束:beat_log/business/OEE/station_input_end
  21. /// 作业开始:beat_log/business/OEE/station_work_begin
  22. /// 作业结束:beat_log/business/OEE/station_work_end
  23. /// 下料开始:beat_log/business/OEE/station_output_begin
  24. /// 下料结束:beat_log/business/OEE/station_output_end
  25. /// </summary>
  26. private static string StationInputBeginId { get; set; } = "beat_log/business/OEE/{0}";
  27. /// <summary>
  28. /// 节拍⽇志(OEE)- 事件方法
  29. /// XiaomiDeviceState
  30. /// </summary>
  31. /// <param name="msg">事件数据;Json</param>
  32. /// <param name="dataId">自定义事件Id;如:guid</param>
  33. /// <returns></returns>
  34. public static (int, string) Write_StationInputBegin(StationInputBeginRequest request, string deviceCode = "")
  35. {
  36. int result = 0;
  37. string resultStr = "";
  38. string msg = JsonConvert.SerializeObject(request);
  39. byte[] bytes = ToUTF8(msg);
  40. //request.action = string.Format(StationInputBeginId, request.action);
  41. // 发送
  42. if (string.IsNullOrEmpty(deviceCode))
  43. {
  44. bool isOk = Task.Run(() => { result = Write(request.action, bytes); }).Wait(10000);
  45. if (!isOk) // 无响应
  46. return (-999, "上位机调用Iot的dll无响应[方法名Write]!");
  47. }
  48. else
  49. {
  50. bool isOk = Task.Run(() => { result = WriteWithDeviceCode(request.action, bytes, deviceCode); }).Wait(10000);
  51. if (!isOk) // 无响应
  52. return (-999, "上位机调用Iot的dll无响应[方法名WriteWithDeviceCode]!");
  53. }
  54. var try1 = Enum.TryParse(result.ToString(), out XiaomiMqttResponse_ErrCode errCode);
  55. if (try1)
  56. resultStr = errCode.ToString();
  57. else
  58. resultStr = XiaomiMqttResponse_ErrCode.ERR_UNKOWN.ToString();
  59. string logPath = GlobalContext.MqttInputBeginDir + "beatType" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
  60. File.WriteAllText(logPath, "\r\n" + DateTime.Now + "===>MQTT【节拍日志】提交:" + msg + "\r\n==>提交结果:" + resultStr + "");
  61. return (result, resultStr);
  62. }
  63. /// <summary>
  64. /// 节拍⽇志(OEE)- 参数
  65. /// </summary>
  66. public class StationInputBeginRequest
  67. {
  68. /// <summary>
  69. /// 节拍动作(XiaomiDeviceOEE)
  70. /// string[1,64]
  71. /// 上料开始:beat_log/business/OEE/station_input_begin
  72. /// 上料结束:beat_log/business/OEE/station_input_end
  73. /// 作业开始:beat_log/business/OEE/station_work_begin
  74. /// 作业结束:beat_log/business/OEE/station_work_end
  75. /// 下料开始:beat_log/business/OEE/station_output_begin
  76. /// 下料结束:beat_log/business/OEE/station_output_end
  77. /// </summary>
  78. public string action { get; set; } = string.Empty;
  79. /// <summary>
  80. /// 节拍发⽣时间
  81. /// string[1,32]
  82. /// 时间,精确到毫秒。如:2022-06-01 14:27:57.283
  83. /// </summary>
  84. public string beat_tm { get; set; } = string.Empty;
  85. /// <summary>
  86. /// 该动作操作的⽬标对象(SN)
  87. /// string[1,64]
  88. /// </summary>
  89. public string action_subject { get; set; } = string.Empty;
  90. /// <summary>
  91. /// ⼤板SN/载具SN
  92. /// 若使用虚拟SN,虚拟SN格式为:SN-编号
  93. /// string[1,64]
  94. /// </summary>
  95. public string action_subject_parent { get; set; } = string.Empty;
  96. /// <summary>
  97. /// 该动作的位置信息(⼯位、槽位),如:F06-GSTPLA11_01-SLOT-01
  98. /// string[1,32]
  99. /// ⼯位
  100. /// </summary>
  101. public string action_location { get; set; } = string.Empty;
  102. /// <summary>
  103. /// 该动作的物料信息
  104. /// string[1,256]
  105. /// 若动作的位置信息有多级,则考虑使⽤"_"分隔,如:"⼯位号_槽位号"
  106. /// </summary>
  107. public string action_material { get; set; } = string.Empty;
  108. /// <summary>
  109. /// 额外信息
  110. /// ⽤于携带额外信息
  111. /// </summary>
  112. public string extra { get; set; } = string.Empty;
  113. /// <summary>
  114. /// 数据字典中定义的分类层级1
  115. /// string[1,64]
  116. /// </summary>
  117. public string class_level_1 { get; set; } = string.Empty;
  118. /// <summary>
  119. /// 数据字典中定义的分类层级2
  120. /// string[1,64]
  121. /// </summary>
  122. public string class_level_2 { get; set; } = string.Empty;
  123. /// <summary>
  124. /// 数据字典中定义的分类层级3
  125. /// string[1,64]
  126. /// </summary>
  127. public string class_level_3 { get; set; } = string.Empty;
  128. }
  129. }
  130. }