SajetConnect.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace AwInitilizer.DLL
  8. {
  9. public class SajetConnect
  10. {
  11. public enum CMD
  12. {
  13. Signin = 1,
  14. SnCheck = 15,
  15. WoCheck = 14,
  16. Report = 16,
  17. Log = 4,
  18. HeaderRegister = 8,
  19. ValueReport = 5
  20. }
  21. [DllImport("SajetConnect.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  22. public static extern bool SajetTransStart();
  23. [DllImport("SajetConnect.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  24. public static extern bool SajetTransClose();
  25. //[DllImport("SajetConnect.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  26. //public static extern bool SajetTransData(int command, IntPtr data, IntPtr length);
  27. [DllImport("SajetConnect.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  28. public static extern bool SajetTransData(int command, IntPtr data, IntPtr length);
  29. [DllImport("SajetConnect.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
  30. public static extern bool SajetTransData_C(int command, out string data);
  31. private static string userId = "";
  32. public static bool SajetTransSignIn(ref string data)
  33. {
  34. var attemptId = 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. private static string WorkOrder = "";
  52. public static bool SajetTransWoCheck(ref string workOrder)
  53. {
  54. if (string.IsNullOrEmpty(userId))
  55. return false;
  56. if (workOrder == null)
  57. workOrder = "";
  58. var msg = userId + "," + workOrder + ",";
  59. if (SajetTransData(CMD.WoCheck, ref msg))
  60. {
  61. if (string.IsNullOrEmpty(msg) || msg.StartsWith("NG"))
  62. {
  63. WorkOrder = "";
  64. return false;
  65. }
  66. else
  67. {
  68. WorkOrder = workOrder;
  69. return true;
  70. }
  71. }
  72. WorkOrder = "";
  73. return false;
  74. }
  75. private static string SN;
  76. public static bool SajetTransSnCheck(ref string serialNumber)
  77. {
  78. var attemptSN = serialNumber;
  79. if (string.IsNullOrEmpty(userId))
  80. return false;
  81. if (WorkOrder == null)
  82. WorkOrder = "";
  83. var msg = userId + "," + WorkOrder + "," + serialNumber + ",";
  84. if (SajetTransData(CMD.SnCheck, ref msg))
  85. {
  86. if (string.IsNullOrEmpty(msg) || msg.StartsWith("NG"))
  87. {
  88. SN = "";
  89. return false;
  90. }
  91. else
  92. {
  93. SN = attemptSN;
  94. return true;
  95. }
  96. }
  97. SN = "";
  98. return false;
  99. }
  100. public static bool SajetTranFinish(bool Result)
  101. {
  102. if (string.IsNullOrEmpty(userId))
  103. return false;
  104. if (string.IsNullOrEmpty(WorkOrder))
  105. return false;
  106. if (string.IsNullOrEmpty(SN))
  107. return false;
  108. string msg = userId +"," + WorkOrder + "," + SN + ",";
  109. if (string.IsNullOrEmpty(msg))
  110. return false;
  111. Result = false;
  112. if (Result)
  113. {
  114. msg += "OK,";
  115. }
  116. else
  117. {
  118. msg += "NG,";
  119. }
  120. return SajetTransData(CMD.Report, ref msg);
  121. }
  122. public static string SajetTransRegisterHeader(string model, string header)
  123. {
  124. if (string.IsNullOrEmpty(userId))
  125. return "";
  126. var msg = userId + ",";
  127. msg += model + "," + header;
  128. if (SajetTransData(CMD.HeaderRegister,ref msg))
  129. {
  130. if(msg.StartsWith("OK"))
  131. {
  132. //get codename
  133. msg = msg.Substring(3);
  134. var spaceIndex = msg.IndexOf(",");
  135. if (spaceIndex > 0)
  136. {
  137. msg = msg.Substring(0, spaceIndex);
  138. }
  139. return msg;
  140. }
  141. return null;
  142. }
  143. return null;
  144. }
  145. public static bool SajetTransReport(Dictionary<string,int> resultPair,Dictionary<string,string> codePair)
  146. {
  147. if (string.IsNullOrEmpty(userId))
  148. return false;
  149. if (string.IsNullOrEmpty(SN))
  150. return false;
  151. string msg = userId + "," + SN + ",";
  152. foreach(var result in resultPair)
  153. {
  154. if(codePair.Keys.Contains(result.Key))
  155. {
  156. msg += string.Format("{0}:{1},", codePair[result.Key],string.Format("{0}.00", result.Value) );
  157. }
  158. }
  159. return SajetTransData(CMD.ValueReport,ref msg);
  160. }
  161. public static bool SajetTransLog (string data,bool isError)
  162. {
  163. if (string.IsNullOrEmpty(userId))
  164. return false;
  165. if (string.IsNullOrEmpty(SN))
  166. return false;
  167. string prefix = userId + "," + SN + ",";
  168. while (data.Length > 0)
  169. {
  170. var msg = prefix;
  171. int sendLength = Math.Min(250 - msg.Length, data.Length);
  172. var sendString = data.Substring(0, sendLength);
  173. msg += sendString;
  174. if (!SajetTransData(CMD.Log, ref msg))
  175. {
  176. break;
  177. }
  178. data = data.Substring(sendLength);
  179. }
  180. return data.Length == 0;
  181. }
  182. private static bool SajetTransData(CMD command, ref string data)
  183. {
  184. return SajetTransData((int)command,ref data);
  185. }
  186. private static string GetSendPrefix()
  187. {
  188. string msg = "";
  189. if (string.IsNullOrEmpty(userId))
  190. {
  191. return null;
  192. }
  193. msg = userId + ",";
  194. if (string.IsNullOrEmpty(WorkOrder))
  195. {
  196. return null;
  197. }
  198. msg += WorkOrder + ",";
  199. return msg;
  200. }
  201. private static bool SajetTransData(int command,ref string data)
  202. {
  203. return true;
  204. if(!data.EndsWith(","))
  205. {
  206. data = data + ",";
  207. }
  208. var idByte = Encoding.ASCII.GetBytes(data);
  209. var length = idByte.Length;
  210. var dataArray = new byte[100];
  211. Array.Copy(idByte, 0, dataArray, 0, Math.Min(dataArray.Length, idByte.Length));
  212. IntPtr dataIntPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(100);
  213. System.Runtime.InteropServices.Marshal.Copy(idByte, 0, dataIntPtr, idByte.Length);
  214. IntPtr lengthIntPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(10);
  215. System.Runtime.InteropServices.Marshal.WriteInt32(lengthIntPtr, length);
  216. var sendResult = SajetConnect.SajetTransData(command, dataIntPtr, lengthIntPtr);
  217. var resultString1 = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(dataIntPtr);
  218. System.Runtime.InteropServices.Marshal.Copy(dataIntPtr, dataArray, 0, 100);
  219. System.Runtime.InteropServices.Marshal.Release(dataIntPtr);
  220. var readLength = System.Runtime.InteropServices.Marshal.ReadInt32(lengthIntPtr);
  221. System.Runtime.InteropServices.Marshal.Release(lengthIntPtr);
  222. var resultString = Encoding.ASCII.GetString(dataArray);
  223. data = resultString1.Substring(0, Math.Min(readLength, resultString1.Length));
  224. return sendResult;
  225. }
  226. }
  227. }