SajectConnectSajet2.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. using PhihongEv.Lib;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace MesAdaptor
  9. {
  10. public class SajectConnectSajet2 : ISajetConnect
  11. {
  12. private enum CMD
  13. {
  14. Signin = 1,
  15. SnCheck = 2,
  16. Report = 3,
  17. ValueReport = 4,
  18. }
  19. private readonly string _MechineCode;
  20. public SajectConnectSajet2(string MechineCode)
  21. {
  22. if (string.IsNullOrEmpty(MechineCode))
  23. {
  24. throw new Exception("MechineCode cannot not be empty");
  25. }
  26. _MechineCode = MechineCode;
  27. }
  28. public bool SajetTransStart() => SajetConnectAdapter.SajetTransStart();
  29. public bool SajetTransClose() => SajetConnectAdapter.SajetTransClose();
  30. private string userId = "";
  31. public bool SajetTransSignIn(ref string data)
  32. {
  33. var attemptId = data;
  34. var msg = data + ";";
  35. if (SajetTransData(CMD.Signin, ref data))
  36. {
  37. if (string.IsNullOrEmpty(data) || data.StartsWith("NG"))
  38. {
  39. userId = "";
  40. return false;
  41. }
  42. else
  43. {
  44. userId = attemptId;
  45. return true;
  46. }
  47. }
  48. userId = "";
  49. return false;
  50. }
  51. [Obsolete]
  52. public bool SajetTransWoCheck(ref string workOrder)
  53. {
  54. return true;
  55. }
  56. //private SystemID systemID;
  57. string modelName;
  58. string serialNumber;
  59. public bool SajetTransSnCheck(ref string serialNumber)
  60. {
  61. if (!SystemID.TryLooseParse(serialNumber, out var id))
  62. {
  63. return false;
  64. }
  65. this.modelName = null;
  66. this.serialNumber = null;
  67. var result = SajetTransSnCheck(ref serialNumber, id.ModelName.ToString());
  68. if (result)
  69. {
  70. this.modelName = id.ModelName.ToString();
  71. this.serialNumber = serialNumber;
  72. }
  73. return result;
  74. //var msg = string.Format("{0};{1};;", id.ModelName.ToString(), id.ToString());
  75. //if (SajetTransData(CMD.SnCheck, ref msg))
  76. //{
  77. // if (string.IsNullOrEmpty(msg) || msg.StartsWith("NG"))
  78. // {
  79. // systemID = null;
  80. // return false;
  81. // }
  82. // systemID = id;
  83. // return true;
  84. //}
  85. //systemID = null;
  86. //return false;
  87. }
  88. public bool SajetTransSnCheck(ref string sn, string model)
  89. {
  90. var msg = string.Format("{0};{1};;", model, sn);
  91. this.serialNumber = null;
  92. this.modelName = null;
  93. if (SajetTransData(CMD.SnCheck, ref msg))
  94. {
  95. if (string.IsNullOrEmpty(msg) || msg.StartsWith("NG"))
  96. {
  97. return false;
  98. }
  99. this.serialNumber = sn;
  100. this.modelName = model;
  101. return true;
  102. }
  103. return false;
  104. }
  105. //public bool SajetTransSnCheck(string partNO,string sn)
  106. //{
  107. // var msg = string.Format("{0};{1};;", partNO, sn);
  108. // if (SajetTransData(CMD.SnCheck, ref msg))
  109. // {
  110. // if (string.IsNullOrEmpty(msg) || msg.StartsWith("NG"))
  111. // {
  112. // return false;
  113. // }
  114. // return true;
  115. // }
  116. // return false;
  117. //}
  118. public bool SajetTranFinishSuccess()
  119. {
  120. //File.AppendAllText("debug.txt", string.Format("userId:{0}", userId));
  121. //File.AppendAllText("debug.txt", string.Format("modelName:{0}", modelName));
  122. //File.AppendAllText("debug.txt", string.Format("serialNumber:{0}", serialNumber));
  123. if (string.IsNullOrEmpty(userId))
  124. return false;
  125. if (modelName == null)
  126. return false;
  127. if (serialNumber == null)
  128. return false;
  129. //string msg = userId + ";" + systemID.ToString() + ";";
  130. string msg = string.Format("{0};{1};{2};{3}:0;", userId, _MechineCode, modelName, serialNumber);
  131. if (string.IsNullOrEmpty(msg))
  132. return false;
  133. return SajetTransData(CMD.Report, ref msg);
  134. }
  135. //public bool SajetTranFinishSuccess(string partNO, string sn)
  136. //{
  137. // if (string.IsNullOrEmpty(userId))
  138. // return false;
  139. // //string msg = userId + ";" + systemID.ToString() + ";";
  140. // string msg = string.Format("{0};{1};{2};{3}:0;", userId, _MechineCode, partNO, sn);
  141. // if (string.IsNullOrEmpty(msg))
  142. // return false;
  143. // return SajetTransData(CMD.Report, ref msg);
  144. //}
  145. public bool SajetTranFinishFail(MesErrorCode errorCode)
  146. {
  147. if (string.IsNullOrEmpty(userId))
  148. return false;
  149. if (modelName == null)
  150. return false;
  151. if (serialNumber == null)
  152. return false;
  153. //string msg = userId + ";" + systemID.ToString() + ";";
  154. string msg = string.Format("{0};{1};{2};{3}:1:", userId, _MechineCode, modelName, serialNumber);
  155. if (string.IsNullOrEmpty(msg))
  156. return false;
  157. msg += $"{GetMesCodeString(errorCode)};"; //E114
  158. return SajetTransData(CMD.Report, ref msg);
  159. }
  160. //public bool SajetTranFinishFail(string partNO, string sn, MesErrorCode errorCode)
  161. //{
  162. // if (string.IsNullOrEmpty(userId))
  163. // return false;
  164. // //string msg = userId + ";" + systemID.ToString() + ";";
  165. // string msg = string.Format("{0};{1};{2};{3}:1:", userId, _MechineCode, partNO, sn);
  166. // if (string.IsNullOrEmpty(msg))
  167. // return false;
  168. // msg += $"{errorCode};"; //E114
  169. // return SajetTransData(CMD.Report, ref msg);
  170. //}
  171. [Obsolete]
  172. public string SajetTransRegisterHeader(string model, string header)
  173. {
  174. return "";
  175. }
  176. public bool SajetTransReport(ValueReportDatas reportDatas)
  177. {
  178. if (string.IsNullOrEmpty(userId))
  179. return false;
  180. if (modelName == null)
  181. return false;
  182. if (serialNumber == null)
  183. return false;
  184. var totalResult = true;
  185. foreach (var data in reportDatas)
  186. {
  187. var key = data.Key;
  188. var value = data.Val;
  189. var isSuccess = data.IsSuccess;
  190. value = value.Trim();
  191. var det = isSuccess ? "0" : "1";
  192. if (string.IsNullOrEmpty(value))
  193. {
  194. value = "empty";
  195. }
  196. else if (value.ToLower() == "fail")
  197. {
  198. value = "0";
  199. }
  200. else if (value.ToLower() == "success")
  201. {
  202. value = "1";
  203. }
  204. var msg = string.Format("{0};{1};{2};{3};{4}:{5}:{6};", userId, _MechineCode, modelName, serialNumber, key, value, det);
  205. var result = SajetTransData(CMD.ValueReport, ref msg);
  206. totalResult &= result;
  207. }
  208. return totalResult;
  209. }
  210. //public bool SajetTransReport(string partNO, string no, Dictionary<string, string> reportPair)
  211. //{
  212. // if (string.IsNullOrEmpty(userId))
  213. // return false;
  214. // var totalResult = true;
  215. // foreach (var pair in reportPair)
  216. // {
  217. // var msg = string.Format("{0};{1};{2};{3};{4}:{5};", userId, _MechineCode, partNO, no, pair.Key, pair.Value);
  218. // var result = SajetTransData(CMD.ValueReport, ref msg);
  219. // totalResult &= result;
  220. // }
  221. // return totalResult;
  222. //}
  223. [Obsolete]
  224. public bool SajetTransReport(Dictionary<string, int> resultPair, Dictionary<string, string> codePair)
  225. {
  226. return true;
  227. }
  228. [Obsolete]
  229. public bool SajetTransReport(Dictionary<string, string> resultPair, Dictionary<string, string> codePair)
  230. {
  231. return true;
  232. }
  233. [Obsolete]
  234. public bool SajetTransLog(string data)
  235. {
  236. return true;
  237. }
  238. private bool SajetTransData(CMD command, ref string data)
  239. {
  240. return SajetConnectAdapter.SajetTransData((int)command, ref data);
  241. }
  242. private string GetMesCodeString(MesErrorCode mesError)
  243. {
  244. switch (mesError)
  245. {
  246. case MesErrorCode.ChargerConnectFail:
  247. return "A081";
  248. case MesErrorCode.ModelNameUpadateFail:
  249. return "A082";
  250. case MesErrorCode.SerilaNumberUpadateFail:
  251. return "A083";
  252. case MesErrorCode.UtcDatetimeUpdateFail:
  253. return "A084";
  254. case MesErrorCode.FourthGenModelVersionMismatch:
  255. return "A085";
  256. case MesErrorCode.FourthGenSimInstartionMismatch:
  257. return "A086";
  258. case MesErrorCode.FourthGenSimInfoMismatch:
  259. return "A087";
  260. case MesErrorCode.WifiModeNotClient:
  261. return "A088";
  262. case MesErrorCode.WifiRssiLow:
  263. return "A089";
  264. case MesErrorCode.FirmwareUploadFail:
  265. return "A090";
  266. case MesErrorCode.FirmwareUpdateTimeout:
  267. return "A091";
  268. case MesErrorCode.FirmwareVersionCheckFail:
  269. return "A092";
  270. case MesErrorCode.EmergencyButtonTestFail:
  271. return "A093";
  272. case MesErrorCode.GreenButtonTestFail:
  273. return "A094";
  274. case MesErrorCode.BlueButtonTestFail:
  275. return "A095";
  276. case MesErrorCode.FactoryResetFail:
  277. return "A096";
  278. case MesErrorCode.SetOptionsFail:
  279. return "ST001";
  280. case MesErrorCode.ProgramFail:
  281. return "ST002";
  282. case MesErrorCode.RestFail:
  283. return "ST003";
  284. case MesErrorCode.ProgramCheckFail:
  285. return "ST004";
  286. case MesErrorCode.GetCheckSumFail:
  287. return "ST005";
  288. }
  289. return "A081";
  290. }
  291. }
  292. }