SajetConnectShinewave.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace MesAdaptor
  7. {
  8. public class SajetConnectShinewave : ISajetConnect
  9. {
  10. private enum CMD
  11. {
  12. Signin = 1,
  13. Log = 4,
  14. ValueReport = 5,
  15. StringValueReport = 6,
  16. HeaderRegister = 8,
  17. WoCheck = 14,
  18. SnCheck = 15,
  19. Report = 16,
  20. }
  21. public bool SajetTransStart() => SajetConnectAdapter.SajetTransStart();
  22. public bool SajetTransClose() => SajetConnectAdapter.SajetTransClose();
  23. private string userId = "";
  24. public bool SajetTransSignIn(ref string data)
  25. {
  26. var attemptId = data;
  27. if (SajetTransData(CMD.Signin, ref data))
  28. {
  29. if (string.IsNullOrEmpty(data) || data.StartsWith("NG"))
  30. {
  31. userId = "";
  32. return false;
  33. }
  34. else
  35. {
  36. userId = attemptId;
  37. return true;
  38. }
  39. }
  40. userId = "";
  41. return false;
  42. }
  43. private string WorkOrder = "";
  44. public bool SajetTransWoCheck(ref string workOrder)
  45. {
  46. if (string.IsNullOrEmpty(userId))
  47. return false;
  48. if (workOrder == null)
  49. workOrder = "";
  50. var msg = userId + "," + workOrder + ",";
  51. if (SajetTransData(CMD.WoCheck, ref msg))
  52. {
  53. if (string.IsNullOrEmpty(msg) || msg.StartsWith("NG"))
  54. {
  55. WorkOrder = "";
  56. return false;
  57. }
  58. else
  59. {
  60. WorkOrder = workOrder;
  61. return true;
  62. }
  63. }
  64. WorkOrder = "";
  65. return false;
  66. }
  67. private string SN;
  68. public bool SajetTransSnCheck(ref string serialNumber)
  69. {
  70. var attemptSN = serialNumber;
  71. if (string.IsNullOrEmpty(userId))
  72. return false;
  73. if (WorkOrder == null)
  74. WorkOrder = "";
  75. var msg = userId + "," + WorkOrder + "," + serialNumber + ",";
  76. if (SajetTransData(CMD.SnCheck, ref msg))
  77. {
  78. if (string.IsNullOrEmpty(msg) || msg.StartsWith("NG"))
  79. {
  80. SN = "";
  81. return false;
  82. }
  83. else
  84. {
  85. SN = attemptSN;
  86. return true;
  87. }
  88. }
  89. SN = "";
  90. return false;
  91. }
  92. public bool SajetTranFinishSuccess()
  93. {
  94. if (string.IsNullOrEmpty(userId))
  95. return false;
  96. //if (string.IsNullOrEmpty(WorkOrder))
  97. // return false;
  98. if (string.IsNullOrEmpty(SN))
  99. return false;
  100. string msg = userId + "," + WorkOrder + "," + SN + ",";
  101. if (string.IsNullOrEmpty(msg))
  102. return false;
  103. msg += "OK,";
  104. #if DEBUG
  105. return true;
  106. #endif
  107. return SajetTransData(CMD.Report, ref msg);
  108. }
  109. public bool SajetTranFinishFail(MesErrorCode errorCode)
  110. {
  111. if (string.IsNullOrEmpty(userId))
  112. return false;
  113. //if (string.IsNullOrEmpty(WorkOrder))
  114. // return false;
  115. if (string.IsNullOrEmpty(SN))
  116. return false;
  117. string msg = userId + "," + WorkOrder + "," + SN + ",";
  118. if (string.IsNullOrEmpty(msg))
  119. return false;
  120. msg += "NG," + GetMesCodeString(errorCode) + ",";
  121. #if DEBUG
  122. return true;
  123. #endif
  124. return SajetTransData(CMD.Report, ref msg);
  125. }
  126. public string SajetTransRegisterHeader(string model, string header)
  127. {
  128. if (string.IsNullOrEmpty(userId))
  129. return "";
  130. var msg = userId + ",";
  131. msg += model + "," + header;
  132. if (SajetTransData(CMD.HeaderRegister, ref msg))
  133. {
  134. if (msg.StartsWith("OK"))
  135. {
  136. //get codename
  137. msg = msg.Substring(3);
  138. var spaceIndex = msg.IndexOf(",");
  139. if (spaceIndex > 0)
  140. {
  141. msg = msg.Substring(0, spaceIndex);
  142. }
  143. return msg;
  144. }
  145. return null;
  146. }
  147. return null;
  148. }
  149. public bool SajetTransReport(ValueReportDatas reportPairs)
  150. {
  151. if (string.IsNullOrEmpty(userId))
  152. return false;
  153. if (string.IsNullOrEmpty(SN))
  154. return false;
  155. string msgHeader = userId + "," + SN;
  156. string msg;
  157. bool result = true;
  158. foreach (var report in reportPairs)
  159. {
  160. msg = string.Format("{0},{1}:{2},", msgHeader, report.Key, report.Val.Trim());
  161. var cmdResult = SajetTransData(CMD.StringValueReport, ref msg);
  162. if (!cmdResult)
  163. result = false;
  164. }
  165. return result;
  166. }
  167. public bool SajetTransReport(Dictionary<string, int> resultPair, Dictionary<string, string> codePair)
  168. {
  169. if (string.IsNullOrEmpty(userId))
  170. return false;
  171. if (string.IsNullOrEmpty(SN))
  172. return false;
  173. string msg = userId + "," + SN + ",";
  174. foreach (var result in resultPair)
  175. {
  176. if (codePair.Keys.Contains(result.Key))
  177. {
  178. msg += string.Format("{0}:{1},", codePair[result.Key], string.Format("{0}.00", result.Value));
  179. }
  180. }
  181. return SajetTransData(CMD.ValueReport, ref msg);
  182. }
  183. public bool SajetTransLog(string data)
  184. {
  185. return true;
  186. if (string.IsNullOrEmpty(userId))
  187. return false;
  188. if (string.IsNullOrEmpty(SN))
  189. return false;
  190. string prefix = userId + "," + SN + ",";
  191. while (data.Length > 0)
  192. {
  193. var msg = prefix;
  194. int sendLength = Math.Min(250 - msg.Length, data.Length);
  195. var sendString = data.Substring(0, sendLength);
  196. msg += sendString;
  197. if (!SajetTransData(CMD.Log, ref msg))
  198. {
  199. break;
  200. }
  201. data = data.Substring(sendLength);
  202. }
  203. return data.Length == 0;
  204. }
  205. private bool SajetTransData(CMD command, ref string data)
  206. {
  207. //return true;
  208. if (!data.EndsWith(","))
  209. {
  210. data = data + ",";
  211. }
  212. return SajetConnectAdapter.SajetTransData((int)command, ref data);
  213. }
  214. private string GetSendPrefix()
  215. {
  216. string msg = "";
  217. if (string.IsNullOrEmpty(userId))
  218. {
  219. return null;
  220. }
  221. msg = userId + ",";
  222. if (string.IsNullOrEmpty(WorkOrder))
  223. {
  224. return null;
  225. }
  226. msg += WorkOrder + ",";
  227. return msg;
  228. }
  229. private string GetMesCodeString(MesErrorCode mesError)
  230. {
  231. switch (mesError)
  232. {
  233. case MesErrorCode.ChargerConnectFail:
  234. return "EC001";
  235. case MesErrorCode.ModelNameUpadateFail:
  236. return "EC002";
  237. case MesErrorCode.SerilaNumberUpadateFail:
  238. return "EC003";
  239. case MesErrorCode.UtcDatetimeUpdateFail:
  240. return "EC004";
  241. case MesErrorCode.FourthGenModelVersionMismatch:
  242. return "EC005";
  243. case MesErrorCode.FourthGenSimInstartionMismatch:
  244. return "EC006";
  245. case MesErrorCode.FourthGenSimInfoMismatch:
  246. return "EC007";
  247. case MesErrorCode.WifiModeNotClient:
  248. return "EC008";
  249. case MesErrorCode.WifiRssiLow:
  250. return "EC009";
  251. case MesErrorCode.FirmwareUploadFail:
  252. return "EC010";
  253. case MesErrorCode.FirmwareUpdateTimeout:
  254. return "EC011";
  255. case MesErrorCode.FirmwareVersionCheckFail:
  256. return "EC012";
  257. case MesErrorCode.EmergencyButtonTestFail:
  258. return "EC013";
  259. case MesErrorCode.GreenButtonTestFail:
  260. return "EC014";
  261. case MesErrorCode.BlueButtonTestFail:
  262. return "EC015";
  263. case MesErrorCode.FactoryResetFail:
  264. return "EC016";
  265. }
  266. return "EC001";
  267. }
  268. }
  269. }