void HandlePostData(string strData, HttpListenerContext ctx) { string strDeviceID = ""; if ((strData.IndexOf("DevEUI") != -1 && strData.IndexOf("Data") != -1)) { CMD_Data _Cmd; try { _Cmd = JsonConvert.DeserializeObject <CMD_Data>(strData); } catch { return; } string DevEUI = _Cmd.DevEUI.Trim(); string Data = _Cmd.Data.Trim(); string strIPPort = GetIPPort(DevEUI, Data); byte[] bFrames = new byte[Data.Length / 2 + 8]; for (int i = 0; i < Data.Length / 2; i++) { bFrames[i] = DevTcpClient.GetByteFromstrHex(Data.Substring(2 * i, 2)); } for (int i = 0; i < 8; i++) { bFrames[i + Data.Length / 2] = DevTcpClient.GetByteFromstrHex(DevEUI.Substring(2 * (7 - i), 2)); } DevTcpClient duc; if (TcpClientList.ContainsKey(DevEUI)) { duc = (DevTcpClient)(TcpClientList[DevEUI]); if (DateTime.Now.Subtract(duc.LastRcvTime) > new TimeSpan(0, 2, 0)) { duc.iLastSendState = 0; } if (duc.iLastSendState == 0) { TcpClientList.Remove(DevEUI); duc = new DevTcpClient(strIPPort); duc.deviceID = strDeviceID; duc.OpenDevice(); if (!TcpClientList.ContainsKey(DevEUI)) { TcpClientList.Add(DevEUI, duc); } else { TcpClientList[DevEUI] = duc; } } duc.WriteDevice(bFrames, 0, bFrames.Length); } else { duc = new DevTcpClient(strIPPort); duc.OpenDevice(); Thread.Sleep(500); if (!TcpClientList.ContainsKey(DevEUI)) { TcpClientList.Add(DevEUI, duc); } duc.WriteDevice(bFrames, 0, bFrames.Length); } } }
void HandlePostData(string strData, HttpListenerContext ctx) { byte[] bFrame = new byte[20]; int iDataLen = 20; string strConcenAddr = ""; string strDeviceID = ""; if ((strData.IndexOf("Reportframe") != -1 && strData.IndexOf("deviceDataChanged") != -1) || (strData.ToUpper().IndexOf("RESPONSEFRAME") != -1)) { bool bRestoHttpSvrNBCtrlCmd = false; if (strData.IndexOf("Reportframe") != -1) { DevReportCmd drc = new DevReportCmd(); drc = JsonConvert.DeserializeObject <DevReportCmd>(strData); if (drc.deviceId != "") { if (drc.service.data.Reportframe != null) { string strReportFrame = drc.service.data.Reportframe.Trim(); iDataLen = strReportFrame.Length / 2; bFrame = new byte[iDataLen]; for (int i = 0; i < iDataLen; i++) { bFrame[i] = DevTcpClient.GetByteFromstrHex(strReportFrame.Substring(2 * i, 2)); } strConcenAddr = DevTcpClient.GetStrFromHex(bFrame[8]) + DevTcpClient.GetStrFromHex(bFrame[7]) + DevTcpClient.GetStrFromHex(bFrame[10]) + DevTcpClient.GetStrFromHex(bFrame[9]); if ((bFrame[0] == 0x68) && (bFrame[1] == 0x10) && ((bFrame[9] == 0x81) || (bFrame[9] == 0x06))) { strConcenAddr = DevTcpClient.GetStrFromHex(bFrame[8]) + DevTcpClient.GetStrFromHex(bFrame[7]) + DevTcpClient.GetStrFromHex(bFrame[6]) + DevTcpClient.GetStrFromHex(bFrame[5]) + DevTcpClient.GetStrFromHex(bFrame[4]) + DevTcpClient.GetStrFromHex(bFrame[3]) + DevTcpClient.GetStrFromHex(bFrame[2]); } strDeviceID = drc.deviceId.Trim(); } } } else if (strData.ToUpper().IndexOf("RESPONSEFRAME") != -1) { DevResponse drp = new DevResponse(); drp = JsonConvert.DeserializeObject <DevResponse>(strData); if (drp.result.resultDetail.Responseframe != null) { string strResponseFrame = drp.result.resultDetail.Responseframe.Trim(); iDataLen = strResponseFrame.Length / 2; bFrame = new byte[iDataLen]; for (int i = 0; i < iDataLen; i++) { bFrame[i] = DevTcpClient.GetByteFromstrHex(strResponseFrame.Substring(2 * i, 2)); } strConcenAddr = DevTcpClient.GetStrFromHex(bFrame[8]) + DevTcpClient.GetStrFromHex(bFrame[7]) + DevTcpClient.GetStrFromHex(bFrame[10]) + DevTcpClient.GetStrFromHex(bFrame[9]); if ((bFrame[0] == 0x68) && (bFrame[1] == 0x10) && ((bFrame[9] == 0x81) || (bFrame[9] == 0x06))) { strConcenAddr = DevTcpClient.GetStrFromHex(bFrame[8]) + DevTcpClient.GetStrFromHex(bFrame[7]) + DevTcpClient.GetStrFromHex(bFrame[6]) + DevTcpClient.GetStrFromHex(bFrame[5]) + DevTcpClient.GetStrFromHex(bFrame[4]) + DevTcpClient.GetStrFromHex(bFrame[3]) + DevTcpClient.GetStrFromHex(bFrame[2]); } strDeviceID = drp.deviceId.Trim(); //////// if (GlbData.NBCtrlCmdList.ContainsKey(strConcenAddr)) { NBCtrlCmd NCC = (NBCtrlCmd)GlbData.NBCtrlCmdList[strConcenAddr]; NCC.strReturnFrame = strResponseFrame; GlbData.NBCtrlCmdList[strConcenAddr] = NCC; bRestoHttpSvrNBCtrlCmd = true; } } } if (strConcenAddr.Length == 8) { DevTcpClient duc; string strIPPort = GlbData.ServerIPPort; if (TcpClientList.ContainsKey(strConcenAddr)) { duc = (DevTcpClient)(TcpClientList[strConcenAddr]); if (DateTime.Now.Subtract(duc.LastRcvTime) > new TimeSpan(0, 5, 0)) { duc.iLastSendState = 0; } if (duc.iLastSendState == 0) { TcpClientList.Remove(strConcenAddr); duc = new DevTcpClient(strIPPort); duc.deviceID = strDeviceID; duc.OpenDevice(); if (!(bFrame.Length == 20 && bFrame[12] == 0x02)) { byte[] bFrame1 = new byte[20]; bFrame1[0] = 0x68; bFrame1[1] = 0x32; bFrame1[2] = 0x00; bFrame1[3] = 0x32; bFrame1[4] = 0x00; bFrame1[5] = 0x68; bFrame1[6] = 0xC9; bFrame1[7] = DevTcpClient.GetByteFromstrHex(strConcenAddr.Substring(2, 2)); bFrame1[8] = DevTcpClient.GetByteFromstrHex(strConcenAddr.Substring(0, 2)); bFrame1[9] = DevTcpClient.GetByteFromstrHex(strConcenAddr.Substring(6, 2)); bFrame1[10] = DevTcpClient.GetByteFromstrHex(strConcenAddr.Substring(4, 2)); bFrame1[11] = 0x00; bFrame1[12] = 0x02; bFrame1[13] = 0x70; bFrame1[14] = 0x00; bFrame1[15] = 0x00; bFrame1[16] = 0x01; bFrame1[17] = 0x00; bFrame1[18] = DevTcpClient.CheckSum(bFrame1, 6, 12); bFrame1[19] = 0x16; duc.WriteDevice(bFrame1, 0, 20); } if (!TcpClientList.ContainsKey(strConcenAddr)) { TcpClientList.Add(strConcenAddr, duc); } } else { if (duc.deviceID.Trim() != strDeviceID.Trim()) { duc.deviceID = strDeviceID.Trim(); TcpClientList[strConcenAddr] = duc; } } } else { duc = new DevTcpClient(strIPPort); duc.deviceID = strDeviceID; duc.OpenDevice(); Thread.Sleep(500); if (!(bFrame.Length == 20 && bFrame[12] == 0x02)) { byte[] bFrame1 = new byte[20]; bFrame1[0] = 0x68; bFrame1[1] = 0x32; bFrame1[2] = 0x00; bFrame1[3] = 0x32; bFrame1[4] = 0x00; bFrame1[5] = 0x68; bFrame1[6] = 0xC9; bFrame1[7] = DevTcpClient.GetByteFromstrHex(strConcenAddr.Substring(2, 2)); bFrame1[8] = DevTcpClient.GetByteFromstrHex(strConcenAddr.Substring(0, 2)); bFrame1[9] = DevTcpClient.GetByteFromstrHex(strConcenAddr.Substring(6, 2)); bFrame1[10] = DevTcpClient.GetByteFromstrHex(strConcenAddr.Substring(4, 2)); bFrame1[11] = 0x00; bFrame1[12] = 0x02; bFrame1[13] = 0x70; bFrame1[14] = 0x00; bFrame1[15] = 0x00; bFrame1[16] = 0x01; bFrame1[17] = 0x00; bFrame1[18] = DevTcpClient.CheckSum(bFrame1, 6, 12); bFrame1[19] = 0x16; duc.WriteDevice(bFrame1, 0, 20); } if (!TcpClientList.ContainsKey(strConcenAddr)) { TcpClientList.Add(strConcenAddr, duc); } } if (!bRestoHttpSvrNBCtrlCmd) { duc.WriteDevice(bFrame, 0, iDataLen); } } else if (strConcenAddr.Length == 14) { DevTcpClient duc; string strIPPort = GlbData.ServerIPPort; if (TcpClientList.ContainsKey(strConcenAddr)) { duc = (DevTcpClient)(TcpClientList[strConcenAddr]); if (duc.iLastSendState == 0) { TcpClientList.Remove(strConcenAddr); duc = new DevTcpClient(strIPPort); duc.deviceID = strDeviceID; duc.OpenDevice(); if (!TcpClientList.ContainsKey(strConcenAddr)) { TcpClientList.Add(strConcenAddr, duc); } } else { if (duc.deviceID.Trim() != strDeviceID.Trim()) { duc.deviceID = strDeviceID.Trim(); TcpClientList[strConcenAddr] = duc; } } } else { duc = new DevTcpClient(strIPPort); duc.deviceID = strDeviceID; duc.OpenDevice(); Thread.Sleep(500); if (!TcpClientList.ContainsKey(strConcenAddr)) { TcpClientList.Add(strConcenAddr, duc); } } if (!bRestoHttpSvrNBCtrlCmd) { duc.WriteDevice(bFrame, 0, iDataLen); } } else { } } ///////////////130协议水表数据上送 else if (strData.IndexOf("DeliverySchedule") != -1 && strData.IndexOf("deviceDatasChanged") != -1) { DevReportCmd_Water drc = new DevReportCmd_Water(); drc = JsonConvert.DeserializeObject <DevReportCmd_Water>(strData); if (drc.deviceId != "") { int iIndex = -1; if (drc.services.Length > 0) { for (int i = 0; i < drc.services.Length; i++) { if (drc.services[i].serviceType == "DeliverySchedule") { iIndex = i; break; } } } if (iIndex < 0) { return; } if (drc.services[iIndex].data != null) { string strReportFrame = drc.services[iIndex].data.transpond.Trim(); iDataLen = strReportFrame.Length / 2; bFrame = new byte[iDataLen]; for (int i = 0; i < iDataLen; i++) { bFrame[i] = DevTcpClient.GetByteFromstrHex(strReportFrame.Substring(2 * i, 2)); } string strAddr = (bFrame[9] + bFrame[10] * 256).ToString().PadLeft(5, '0'); strConcenAddr = DevTcpClient.GetStrFromHex(bFrame[8]) + DevTcpClient.GetStrFromHex(bFrame[7]) + strAddr; strDeviceID = drc.deviceId.Trim(); } } DevTcpClient duc; string strIPPort = GlbData.ServerIPPort; if (TcpClientList.ContainsKey(strConcenAddr)) { duc = (DevTcpClient)(TcpClientList[strConcenAddr]); if (DateTime.Now.Subtract(duc.LastSendTime) > new TimeSpan(0, 5, 0)) { TcpClientList.Remove(strConcenAddr); duc = new DevTcpClient(strIPPort); duc.deviceID = strDeviceID; duc.OpenDevice(); Thread.Sleep(500); if (!TcpClientList.ContainsKey(strConcenAddr)) { TcpClientList.Add(strConcenAddr, duc); } } else { if (duc.deviceID.Trim() != strDeviceID.Trim()) { duc.deviceID = strDeviceID.Trim(); TcpClientList[strConcenAddr] = duc; } } //if (duc.iLastSendState == 0) //{ // TcpClientList.Remove(strConcenAddr); // duc = new DevTcpClient(strIPPort); // duc.deviceID = strDeviceID; // duc.OpenDevice(); // TcpClientList.Add(strConcenAddr, duc); //} //else //{ // if (duc.deviceID.Trim() != strDeviceID.Trim()) // { // duc.deviceID = strDeviceID.Trim(); // TcpClientList[strConcenAddr] = duc; // } //} duc.WriteDevice(bFrame, 0, iDataLen); } else { duc = new DevTcpClient(strIPPort); duc.deviceID = strDeviceID; duc.OpenDevice(); Thread.Sleep(500); if (!TcpClientList.ContainsKey(strConcenAddr)) { TcpClientList.Add(strConcenAddr, duc); } duc.WriteDevice(bFrame, 0, iDataLen); } } else if (strData.IndexOf("NBConcenAddr") != -1) { string[] strs = strData.Split(','); string[] strs1 = strs[0].Split(':'); string[] strs2 = strs[1].Split(':'); string strTermAddr = strs1[1]; string strCmdFrame = strs2[1]; //主站主动下发 List <CommandPara> lsCmdPars = new List <CommandPara>(); CommandPara currCmdPara = new CommandPara(); currCmdPara.isNum = false; currCmdPara.paraName = "CmdFrame"; currCmdPara.paraValue = strCmdFrame; lsCmdPars.Add(currCmdPara); currCmdPara = new CommandPara(); currCmdPara.isNum = true; currCmdPara.paraName = "TimeOut"; currCmdPara.paraValue = "30"; lsCmdPars.Add(currCmdPara); string strCTIOTID = ""; string strDeviceID1 = GlbData.GetDeviceID(strTermAddr, ref strCTIOTID); if (strDeviceID1 == "") { return; } if (strCTIOTID == "") { return; } if (GlbData.CTIOTAppParaList.Contains(strCTIOTID)) { CTIOT_APP_PARA ctiot_app_para = (CTIOT_APP_PARA)(GlbData.CTIOTAppParaList[strCTIOTID]); if (ctiot_app_para != null) { NASDK currsdk = new NASDK(ctiot_app_para.SVR_IP, ctiot_app_para.SVR_PORT, ctiot_app_para.APP_ID, ctiot_app_para.APP_PWD, ctiot_app_para.CERT_FILE, ctiot_app_para.CERT_PWD); string strToken = ctiot_app_para.TOKEN; DateTime dtLastgetTokenTime; try { dtLastgetTokenTime = DateTime.Parse(ctiot_app_para.LAST_GETTOKEN_TIME); } catch { dtLastgetTokenTime = new DateTime(2000, 1, 1); } DateTime dtNow = DateTime.Now; if (strToken == "" || (dtLastgetTokenTime < dtNow.AddMinutes(-30))) { TokenResult tr = currsdk.getToken(); if (tr == null) { return; } strToken = tr.accessToken.Trim(); string strLastgetTokenTime = dtNow.ToString("yyyy-MM-dd HH:mm:ss"); //存储 string strSQL = "update CT_IOT_APP_PARA set TOKEN='{0}',LAST_GETTOKEN_TIME='{1}' where ID=" + strCTIOTID; strSQL = string.Format(strSQL, strToken, strLastgetTokenTime); if (GlbData.DBConn.ExeSQL(strSQL)) { ctiot_app_para.TOKEN = strToken; ctiot_app_para.LAST_GETTOKEN_TIME = strLastgetTokenTime; GlbData.CTIOTAppParaList[strCTIOTID] = ctiot_app_para; } } string result = currsdk.sendCommand(strToken, strDeviceID1, ctiot_app_para.CALLBACKURL, "CmdService", "Cmd_Down", lsCmdPars); NBCtrlCmdSendResult NCCSR = new NBCtrlCmdSendResult(); NCCSR.NBConcenAddr = strTermAddr; if (result == null) { NCCSR.SendTime = ""; } else { NBCtrlCmd NCC = new NBCtrlCmd(); NCC.NBConcenAddr = strTermAddr; NCC.SendTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); NCC.strReturnFrame = ""; if (!GlbData.NBCtrlCmdList.ContainsKey(strTermAddr)) { GlbData.NBCtrlCmdList.Add(strTermAddr, NCC); } else { GlbData.NBCtrlCmdList[strTermAddr] = NCC; } TimeSpan outTimeSpan = new TimeSpan(0, 1, 0); //超时时间60秒 NCC = (NBCtrlCmd)GlbData.NBCtrlCmdList[strTermAddr]; NCCSR.NBConcenAddr = strTermAddr; NCCSR.SendTime = NCC.SendTime; while (NCC.strReturnFrame == "") { if ((DateTime.Now - DateTime.Parse(NCC.SendTime)) > outTimeSpan) { //返回超时 NCCSR.strReturnFrame = ""; break; } } if (NCC.strReturnFrame != "") { NCCSR.strReturnFrame = NCC.strReturnFrame; } } string strResult = JsonConvert.SerializeObject(NCCSR); //将消息转成jason格式返回 GlbData.NBCtrlCmdList.Remove(strTermAddr); byte[] buffer = System.Text.Encoding.UTF8.GetBytes(strResult); //对客户端输出相应信息. try { ctx.Response.ContentLength64 = buffer.Length; System.IO.Stream output = ctx.Response.OutputStream; output.Write(buffer, 0, buffer.Length); //关闭输出流,释放相应资源 output.Close(); } catch { } } } else { return; } } }