123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- namespace AwInitilizer.DLL
- {
- public class SajetConnect
- {
- public enum CMD
- {
- Signin = 1,
- SnCheck = 15,
- WoCheck = 14,
- Report = 16,
- Log = 4,
- HeaderRegister = 8,
- ValueReport = 5
- }
- [DllImport("SajetConnect.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
- public static extern bool SajetTransStart();
- [DllImport("SajetConnect.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
- public static extern bool SajetTransClose();
- //[DllImport("SajetConnect.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
- //public static extern bool SajetTransData(int command, IntPtr data, IntPtr length);
- [DllImport("SajetConnect.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
- public static extern bool SajetTransData(int command, IntPtr data, IntPtr length);
- [DllImport("SajetConnect.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
- public static extern bool SajetTransData_C(int command, out string data);
- private static string userId = "";
- public static bool SajetTransSignIn(ref string data)
- {
- var attemptId = data;
- if(SajetTransData(CMD.Signin, ref data))
- {
- if(string.IsNullOrEmpty(data) || data.StartsWith("NG"))
- {
- userId = "";
- return false;
- }
- else
- {
- userId = attemptId;
- return true;
- }
- }
- userId = "";
- return false;
- }
- private static string WorkOrder = "";
- public static bool SajetTransWoCheck(ref string workOrder)
- {
- if (string.IsNullOrEmpty(userId))
- return false;
- if (workOrder == null)
- workOrder = "";
- var msg = userId + "," + workOrder + ",";
- if (SajetTransData(CMD.WoCheck, ref msg))
- {
- if (string.IsNullOrEmpty(msg) || msg.StartsWith("NG"))
- {
- WorkOrder = "";
- return false;
- }
- else
- {
- WorkOrder = workOrder;
- return true;
- }
- }
- WorkOrder = "";
- return false;
- }
-
- private static string SN;
- public static bool SajetTransSnCheck(ref string serialNumber)
- {
- var attemptSN = serialNumber;
- if (string.IsNullOrEmpty(userId))
- return false;
- if (WorkOrder == null)
- WorkOrder = "";
- var msg = userId + "," + WorkOrder + "," + serialNumber + ",";
- if (SajetTransData(CMD.SnCheck, ref msg))
- {
- if (string.IsNullOrEmpty(msg) || msg.StartsWith("NG"))
- {
- SN = "";
- return false;
- }
- else
- {
- SN = attemptSN;
- return true;
- }
- }
- SN = "";
- return false;
- }
- public static bool SajetTranFinish(bool Result)
- {
- if (string.IsNullOrEmpty(userId))
- return false;
- if (string.IsNullOrEmpty(WorkOrder))
- return false;
- if (string.IsNullOrEmpty(SN))
- return false;
- string msg = userId +"," + WorkOrder + "," + SN + ",";
- if (string.IsNullOrEmpty(msg))
- return false;
- Result = false;
- if (Result)
- {
- msg += "OK,";
- }
- else
- {
- msg += "NG,";
- }
- return SajetTransData(CMD.Report, ref msg);
- }
- public static string SajetTransRegisterHeader(string model, string header)
- {
- if (string.IsNullOrEmpty(userId))
- return "";
- var msg = userId + ",";
- msg += model + "," + header;
- if (SajetTransData(CMD.HeaderRegister,ref msg))
- {
- if(msg.StartsWith("OK"))
- {
- //get codename
- msg = msg.Substring(3);
- var spaceIndex = msg.IndexOf(",");
- if (spaceIndex > 0)
- {
- msg = msg.Substring(0, spaceIndex);
- }
- return msg;
- }
- return null;
- }
- return null;
- }
- public static bool SajetTransReport(Dictionary<string,int> resultPair,Dictionary<string,string> codePair)
- {
- if (string.IsNullOrEmpty(userId))
- return false;
- if (string.IsNullOrEmpty(SN))
- return false;
- string msg = userId + "," + SN + ",";
- foreach(var result in resultPair)
- {
- if(codePair.Keys.Contains(result.Key))
- {
- msg += string.Format("{0}:{1},", codePair[result.Key],string.Format("{0}.00", result.Value) );
- }
- }
- return SajetTransData(CMD.ValueReport,ref msg);
- }
- public static bool SajetTransLog (string data,bool isError)
- {
- if (string.IsNullOrEmpty(userId))
- return false;
- if (string.IsNullOrEmpty(SN))
- return false;
- string prefix = userId + "," + SN + ",";
- while (data.Length > 0)
- {
- var msg = prefix;
- int sendLength = Math.Min(250 - msg.Length, data.Length);
- var sendString = data.Substring(0, sendLength);
- msg += sendString;
- if (!SajetTransData(CMD.Log, ref msg))
- {
- break;
- }
- data = data.Substring(sendLength);
- }
- return data.Length == 0;
- }
- private static bool SajetTransData(CMD command, ref string data)
- {
- return SajetTransData((int)command,ref data);
- }
- private static string GetSendPrefix()
- {
- string msg = "";
- if (string.IsNullOrEmpty(userId))
- {
- return null;
- }
- msg = userId + ",";
- if (string.IsNullOrEmpty(WorkOrder))
- {
- return null;
- }
- msg += WorkOrder + ",";
- return msg;
- }
- private static bool SajetTransData(int command,ref string data)
- {
- return true;
- if(!data.EndsWith(","))
- {
- data = data + ",";
- }
- var idByte = Encoding.ASCII.GetBytes(data);
- var length = idByte.Length;
- var dataArray = new byte[100];
- Array.Copy(idByte, 0, dataArray, 0, Math.Min(dataArray.Length, idByte.Length));
- IntPtr dataIntPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(100);
- System.Runtime.InteropServices.Marshal.Copy(idByte, 0, dataIntPtr, idByte.Length);
- IntPtr lengthIntPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(10);
- System.Runtime.InteropServices.Marshal.WriteInt32(lengthIntPtr, length);
- var sendResult = SajetConnect.SajetTransData(command, dataIntPtr, lengthIntPtr);
- var resultString1 = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(dataIntPtr);
- System.Runtime.InteropServices.Marshal.Copy(dataIntPtr, dataArray, 0, 100);
- System.Runtime.InteropServices.Marshal.Release(dataIntPtr);
- var readLength = System.Runtime.InteropServices.Marshal.ReadInt32(lengthIntPtr);
- System.Runtime.InteropServices.Marshal.Release(lengthIntPtr);
- var resultString = Encoding.ASCII.GetString(dataArray);
- data = resultString1.Substring(0, Math.Min(readLength, resultString1.Length));
- return sendResult;
- }
- }
- }
|