SajetConnectShinewave.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 SajetTransSnCheck(ref string sn, string model)
  93. {
  94. throw new NotImplementedException();
  95. }
  96. public bool SajetTranFinishSuccess()
  97. {
  98. if (string.IsNullOrEmpty(userId))
  99. return false;
  100. //if (string.IsNullOrEmpty(WorkOrder))
  101. // return false;
  102. if (string.IsNullOrEmpty(SN))
  103. return false;
  104. string msg = userId + "," + WorkOrder + "," + SN + ",";
  105. if (string.IsNullOrEmpty(msg))
  106. return false;
  107. msg += "OK,";
  108. #if DEBUG
  109. return true;
  110. #endif
  111. return SajetTransData(CMD.Report, ref msg);
  112. }
  113. public bool SajetTranFinishFail(MesErrorCode errorCode)
  114. {
  115. if (string.IsNullOrEmpty(userId))
  116. return false;
  117. //if (string.IsNullOrEmpty(WorkOrder))
  118. // return false;
  119. if (string.IsNullOrEmpty(SN))
  120. return false;
  121. string msg = userId + "," + WorkOrder + "," + SN + ",";
  122. if (string.IsNullOrEmpty(msg))
  123. return false;
  124. msg += "NG," + GetMesCodeString(errorCode) + ",";
  125. #if DEBUG
  126. return true;
  127. #endif
  128. return SajetTransData(CMD.Report, ref msg);
  129. }
  130. public string SajetTransRegisterHeader(string model, string header)
  131. {
  132. if (string.IsNullOrEmpty(userId))
  133. return "";
  134. var msg = userId + ",";
  135. msg += model + "," + header;
  136. if (SajetTransData(CMD.HeaderRegister, ref msg))
  137. {
  138. if (msg.StartsWith("OK"))
  139. {
  140. //get codename
  141. msg = msg.Substring(3);
  142. var spaceIndex = msg.IndexOf(",");
  143. if (spaceIndex > 0)
  144. {
  145. msg = msg.Substring(0, spaceIndex);
  146. }
  147. return msg;
  148. }
  149. return null;
  150. }
  151. return null;
  152. }
  153. public bool SajetTransReport(ValueReportDatas reportPairs)
  154. {
  155. if (string.IsNullOrEmpty(userId))
  156. return false;
  157. if (string.IsNullOrEmpty(SN))
  158. return false;
  159. string msgHeader = userId + "," + SN;
  160. string msg;
  161. bool result = true;
  162. foreach (var report in reportPairs)
  163. {
  164. msg = string.Format("{0},{1}:{2},", msgHeader, report.Key, report.Val);
  165. var cmdResult = SajetTransData(CMD.StringValueReport, ref msg);
  166. if (!cmdResult)
  167. result = false;
  168. }
  169. return result;
  170. }
  171. public bool SajetTransReport(Dictionary<string, int> resultPair, Dictionary<string, string> codePair)
  172. {
  173. if (string.IsNullOrEmpty(userId))
  174. return false;
  175. if (string.IsNullOrEmpty(SN))
  176. return false;
  177. string msg = userId + "," + SN + ",";
  178. foreach (var result in resultPair)
  179. {
  180. if (codePair.Keys.Contains(result.Key))
  181. {
  182. msg += string.Format("{0}:{1},", codePair[result.Key], string.Format("{0}.00", result.Value));
  183. }
  184. }
  185. return SajetTransData(CMD.ValueReport, ref msg);
  186. }
  187. public bool SajetTransLog(string data)
  188. {
  189. return true;
  190. if (string.IsNullOrEmpty(userId))
  191. return false;
  192. if (string.IsNullOrEmpty(SN))
  193. return false;
  194. string prefix = userId + "," + SN + ",";
  195. while (data.Length > 0)
  196. {
  197. var msg = prefix;
  198. int sendLength = Math.Min(250 - msg.Length, data.Length);
  199. var sendString = data.Substring(0, sendLength);
  200. msg += sendString;
  201. if (!SajetTransData(CMD.Log, ref msg))
  202. {
  203. break;
  204. }
  205. data = data.Substring(sendLength);
  206. }
  207. return data.Length == 0;
  208. }
  209. private bool SajetTransData(CMD command, ref string data)
  210. {
  211. //return true;
  212. if (!data.EndsWith(","))
  213. {
  214. data = data + ",";
  215. }
  216. return SajetConnectAdapter.SajetTransData((int)command, ref data);
  217. }
  218. private string GetSendPrefix()
  219. {
  220. string msg = "";
  221. if (string.IsNullOrEmpty(userId))
  222. {
  223. return null;
  224. }
  225. msg = userId + ",";
  226. if (string.IsNullOrEmpty(WorkOrder))
  227. {
  228. return null;
  229. }
  230. msg += WorkOrder + ",";
  231. return msg;
  232. }
  233. private string GetMesCodeString(MesErrorCode mesError)
  234. {
  235. switch (mesError)
  236. {
  237. case MesErrorCode.ChargerConnectFail:
  238. return "EC001";
  239. case MesErrorCode.ModelNameUpadateFail:
  240. return "EC002";
  241. case MesErrorCode.SerilaNumberUpadateFail:
  242. return "EC003";
  243. case MesErrorCode.UtcDatetimeUpdateFail:
  244. return "EC004";
  245. case MesErrorCode.FourthGenModelVersionMismatch:
  246. return "EC005";
  247. case MesErrorCode.FourthGenSimInstartionMismatch:
  248. return "EC006";
  249. case MesErrorCode.FourthGenSimInfoMismatch:
  250. return "EC007";
  251. case MesErrorCode.WifiModeNotClient:
  252. return "EC008";
  253. case MesErrorCode.WifiRssiLow:
  254. return "EC009";
  255. case MesErrorCode.FirmwareUploadFail:
  256. return "EC010";
  257. case MesErrorCode.FirmwareUpdateTimeout:
  258. return "EC011";
  259. case MesErrorCode.FirmwareVersionCheckFail:
  260. return "EC012";
  261. case MesErrorCode.EmergencyButtonTestFail:
  262. return "EC013";
  263. case MesErrorCode.GreenButtonTestFail:
  264. return "EC014";
  265. case MesErrorCode.BlueButtonTestFail:
  266. return "EC015";
  267. case MesErrorCode.FactoryResetFail:
  268. return "EC016";
  269. case MesErrorCode.SetOptionsFail:
  270. return "ST001";
  271. case MesErrorCode.ProgramFail:
  272. return "ST002";
  273. case MesErrorCode.RestFail:
  274. return "ST003";
  275. case MesErrorCode.ProgramCheckFail:
  276. return "ST004";
  277. case MesErrorCode.GetCheckSumFail:
  278. return "ST005";
  279. }
  280. return "EC001";
  281. }
  282. }
  283. }