/// <summary> /// 將Request資料轉成物件傳到後端服務做DB操作(length:334) /// </summary> /// <param name="inputData"> UID + DeviceId + TxLog</param> /// <returns>Ok:0x30*6 / Fail:null or (0x30*5 + 0x31)</returns> private static byte[] DoLoadKeyTxLog(string inputData) { byte[] result = null; //if (inputData.Length != LoadKeyTxLogLength || inputData.Length != LoadKeyTxLogLength_v2) // return result; string sam_uid = inputData.Substring(0,14); //字串0~13為SAM UID(14 hex string) string deviceId = inputData.Substring(14, 32); //字串14~45為DeviceId(32 hex string) string txLog = Encoding.ASCII.GetString(StringToByteArray(inputData.Substring(46, 576))); //字串46~621為TxLog(576 hex string)=>288 string string merc_flg = inputData.Length.Equals(LoadKeyTxLogLength_v2) ? Encoding.ASCII.GetString(StringToByteArray(inputData.Substring(622, 6))) : null;//字串622~627為TxLog(6 hex string)=>3 string => "SET" string reader_type = inputData.Length.Equals(LoadKeyTxLogLength_v2) ? Encoding.ASCII.GetString(StringToByteArray(inputData.Substring(628, 4))) : null;//字串628~631為TxLog(4 hex string)=>2 string => "02" //string deviceId = BitConverter.ToString(inputData, 9, 16).Replace("-", ""); //byte[9~24] EskmsKeyTxLogPOCO_v2 response = null; EskmsKeyTxLogPOCO_v2 request = new EskmsKeyTxLogPOCO_v2() { SAM_UID = sam_uid, DeviceId = deviceId, TestTxLog = txLog, Merc_Flg = merc_flg, Reader_Type = reader_type }; //log.Debug(""); response = GetResponse(request); if (response != null && response.ReturnCode != null) { result = Encoding.ASCII.GetBytes(response.ReturnCode); //Buffer.BlockCopy(response.ReturnCode, 0, result, 0, response.ReturnCode.Length);//Copy Divers Key in Result log.Debug(m=>m("Response Bytes:{0}" , BitConverter.ToString(result).Replace("-", ""))); } return result; }
/// <summary> /// 連線後端AP並取得output data /// </summary> /// <param name="request"></param> /// <returns></returns> private static EskmsKeyTxLogPOCO_v2 GetResponse(EskmsKeyTxLogPOCO_v2 request) { string requestStr = null; byte[] requestBytes = null; string responseStr = null; byte[] responseBytes = null; string ip = null; int port = -1; int sendTimeout = -1; int receiveTimeout = -1; string serverConfig = null; string[] configs = null; EskmsKeyTxLogPOCO_v2 response = null; //********************************* //取得連線後台的WebConfig設定資料 serverConfig = ConfigGetter.GetValue(ServiceName); log.Debug(m => { m.Invoke(ServiceName + ":" + serverConfig); }); if (serverConfig != null) { configs = serverConfig.Split(':'); ip = configs[0]; port = Convert.ToInt32(configs[1]); sendTimeout = Convert.ToInt32(configs[2]); receiveTimeout = Convert.ToInt32(configs[3]); } else { log.Error("要連結的目的地設定資料不存在:" + ServiceName); return null; } //********************************* try { using (SocketClient.Domain.SocketClient connectToAP = new SocketClient.Domain.SocketClient(ip, port, sendTimeout, receiveTimeout)) { log.Debug("開始連線後端服務:" + serverConfig); if (connectToAP.ConnectToServer()) { //UTF8(JSON(POCO))=>byte array and send to AP requestStr = JsonConvert.SerializeObject(request); log.Debug(m => m("[ReaderShipmentTxLog]Request JsonString({0}): {1}", ServiceName, requestStr)); requestBytes = Encoding.UTF8.GetBytes(requestStr);//Center AP used UTF8 responseBytes = connectToAP.SendAndReceive(requestBytes); if (responseBytes != null) { responseStr = Encoding.UTF8.GetString(responseBytes); response = JsonConvert.DeserializeObject<EskmsKeyTxLogPOCO_v2>(responseStr); //Byte[] 會被JSON轉成Base64格式 log.Debug(m => { m.Invoke("[ReaderShipmentTxLog]Response JsonString:{0}", responseStr); }); } else { //Byte[] 會被JSON轉成Base64格式 log.Debug(m => { m.Invoke("[ReaderShipmentTxLog]Response JsonString: null"); }); } } } } catch (Exception ex) { log.Error("後台連線異常:" + ex.Message); } return response; }
public void Handle(AbsClientRequestHandler absClientRequestHandler) { #region variable byte[] receiveBuffer = null; int readCount = 0; string requestJsonStr = null; string outputCmd = null; EskmsKeyTxLogPOCO_v2 request = null; EskmsKeyTxLogPOCO_v2 response = null; string requestCheckErrMsg = null; string responseJsonStr = null; byte[] responseBytes = null; int sendCount = -1; AL_DBModule obDB = null; Shipment shipment = null; string txLogReturnCode = String.Empty; #endregion try { receiveBuffer = new byte[0x1000];//4k readCount = absClientRequestHandler.ClientSocket.Receive(receiveBuffer, SocketFlags.None); if (readCount == 0) { return; } //command 輸出狀態 TODO... else if (readCount == 6 && Encoding.UTF8.GetString(receiveBuffer, 0, readCount).ToLower().Contains("status")) { outputCmd = "Hello"; receiveBuffer = Encoding.UTF8.GetBytes(outputCmd); absClientRequestHandler.ClientSocket.Send(receiveBuffer); return; } else { log.Debug(m => m(">> {0}: {1}", this.GetType().Name, absClientRequestHandler.ClientNo)); //resize buffer Array.Resize(ref receiveBuffer, readCount); //init obDB = new AL_DBModule(); shipment = new Shipment(); //casting jsonstring from buffer array requestJsonStr = Encoding.UTF8.GetString(receiveBuffer); log.Debug(m => m("[{0}]Request: {1}", this.GetType().Name, requestJsonStr)); request = JsonConvert.DeserializeObject<EskmsKeyTxLogPOCO_v2>(requestJsonStr); //檢查Request資料長度(Attribute) //request.CheckLength(true, out requestCheckErrMsg); //回應資料設定 response = new EskmsKeyTxLogPOCO_v2() { SAM_UID = request.SAM_UID, DeviceId = request.DeviceId, ReturnCode = "000001",//預設為fail }; txLogReturnCode = request.TestTxLog.Substring(16, 8);//取第16~23個字串 //簡易判斷:TxLog ReturnCode if (txLogReturnCode.Equals("00000000")) { log.Debug(m => m("1.Open DB Connection")); obDB.OpenConnection(); log.Debug(m => m("2.Run Shipment DB Operate SAM_UID:{0}, DeviceId:{1}", request.SAM_UID, request.DeviceId)); //若有輸入顧客識別符號和卡機種別則寫入時就依據此資料寫入,若無則使用預設値寫入(在DB_Operate.SQL.Sql_Getter_Reader_D裡) if (!String.IsNullOrEmpty(request.Merc_Flg) && !String.IsNullOrEmpty(request.Reader_Type)) { log.Debug(m => m("2-1. 自訂卡機格式與顧客識別[Reader_Type:{0}, Merc_Flg:{1}]", request.Reader_Type, request.Merc_Flg)); //執行Reader出貨的DB操作流程 shipment.Shipment_Reader(obDB, request.SAM_UID, request.DeviceId, request.Reader_Type, request.Merc_Flg);// } else { log.Debug(m => m("2-2.預設的卡機格式與顧客識別[Reader_Type:{0}, Merc_Flg:{1}]", "03", "ICA")); //執行Reader出貨的DB操作流程 shipment.Shipment_Reader(obDB, request.SAM_UID, request.DeviceId);// } response.ReturnCode = "000000"; log.Debug(m => m("3.Shipment DB Operate End ...")); } else { log.Error(m => m("UID:{0} DeviceId:{1} TxLog ReturnCode異常:{2}",request.SAM_UID, request.DeviceId, txLogReturnCode)); } responseJsonStr = JsonConvert.SerializeObject(response); responseBytes = Encoding.UTF8.GetBytes(responseJsonStr); log.Debug(m => m("[{0}] Response:{1}", this.GetType().Name, responseJsonStr)); sendCount = absClientRequestHandler.ClientSocket.Send(responseBytes); if (sendCount != responseBytes.Length) { log.Error(m => m("異常:送出資料(length:{0}不等於原始資料(length:{1}))", sendCount, responseBytes.Length)); } log.Debug(m => m("[{0}] Response End", this.GetType().Name)); } } catch (ArgumentOutOfRangeException ex) { log.Error(m => m("資料檢核失敗:{0}", ex.ToString())); } catch (JsonException ex) { log.Error(m => m("Request(JsonString) Parse Request(Object) Failed:{0}", ex.ToString())); } catch (Exception ex) { log.Error(m => m("[{0}] Error:{1} {2}", this.GetType().Name, ex.Message, ex.StackTrace)); } finally { if (obDB != null && obDB.ALCon != null) { obDB.CloseConnection(); shipment = null; } absClientRequestHandler.ServiceState = new State_Exit(); } }