123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MesAdaptor
- {
- public class SajetConnectShinewave : ISajetConnect
- {
- private enum CMD
- {
- Signin = 1,
- Log = 4,
- ValueReport = 5,
- StringValueReport = 6,
- HeaderRegister = 8,
- WoCheck = 14,
- SnCheck = 15,
- Report = 16,
- }
- public bool SajetTransStart() => SajetConnectAdapter.SajetTransStart();
- public bool SajetTransClose() => SajetConnectAdapter.SajetTransClose();
- private string userId = "";
- public 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 string WorkOrder = "";
- public 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 string SN;
- public 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 bool SajetTransSnCheck(ref string sn, string model)
- {
- throw new NotImplementedException();
- }
- public bool SajetTranFinishSuccess()
- {
- 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;
- msg += "OK,";
- #if DEBUG
- return true;
- #endif
- return SajetTransData(CMD.Report, ref msg);
- }
- public bool SajetTranFinishFail(MesErrorCode errorCode)
- {
- 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;
- msg += "NG," + GetMesCodeString(errorCode) + ",";
- #if DEBUG
- return true;
- #endif
- return SajetTransData(CMD.Report, ref msg);
- }
- public 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 bool SajetTransReport(ValueReportDatas reportPairs)
- {
- if (string.IsNullOrEmpty(userId))
- return false;
- if (string.IsNullOrEmpty(SN))
- return false;
- string msgHeader = userId + "," + SN;
- string msg;
- bool result = true;
- foreach (var report in reportPairs)
- {
- msg = string.Format("{0},{1}:{2},", msgHeader, report.Key, report.Val);
- var cmdResult = SajetTransData(CMD.StringValueReport, ref msg);
- if (!cmdResult)
- result = false;
- }
- return result;
- }
- public 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 bool SajetTransLog(string data)
- {
- return true;
- 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 bool SajetTransData(CMD command, ref string data)
- {
- //return true;
- if (!data.EndsWith(","))
- {
- data = data + ",";
- }
- return SajetConnectAdapter.SajetTransData((int)command, ref data);
- }
- private string GetSendPrefix()
- {
- string msg = "";
- if (string.IsNullOrEmpty(userId))
- {
- return null;
- }
- msg = userId + ",";
- if (string.IsNullOrEmpty(WorkOrder))
- {
- return null;
- }
- msg += WorkOrder + ",";
- return msg;
- }
- private string GetMesCodeString(MesErrorCode mesError)
- {
- switch (mesError)
- {
- case MesErrorCode.ChargerConnectFail:
- return "EC001";
- case MesErrorCode.ModelNameUpadateFail:
- return "EC002";
- case MesErrorCode.SerilaNumberUpadateFail:
- return "EC003";
- case MesErrorCode.UtcDatetimeUpdateFail:
- return "EC004";
- case MesErrorCode.FourthGenModelVersionMismatch:
- return "EC005";
- case MesErrorCode.FourthGenSimInstartionMismatch:
- return "EC006";
- case MesErrorCode.FourthGenSimInfoMismatch:
- return "EC007";
- case MesErrorCode.WifiModeNotClient:
- return "EC008";
- case MesErrorCode.WifiRssiLow:
- return "EC009";
- case MesErrorCode.FirmwareUploadFail:
- return "EC010";
- case MesErrorCode.FirmwareUpdateTimeout:
- return "EC011";
- case MesErrorCode.FirmwareVersionCheckFail:
- return "EC012";
- case MesErrorCode.EmergencyButtonTestFail:
- return "EC013";
- case MesErrorCode.GreenButtonTestFail:
- return "EC014";
- case MesErrorCode.BlueButtonTestFail:
- return "EC015";
- case MesErrorCode.FactoryResetFail:
- return "EC016";
- case MesErrorCode.SetOptionsFail:
- return "ST001";
- case MesErrorCode.ProgramFail:
- return "ST002";
- case MesErrorCode.RestFail:
- return "ST003";
- case MesErrorCode.ProgramCheckFail:
- return "ST004";
- case MesErrorCode.GetCheckSumFail:
- return "ST005";
- }
- return "EC001";
- }
- }
- }
|