private static void InitScoketClient() { foreach (DataRow dr in dtRDSList.Rows) { RDSConfigModel rm = new RDSConfigModel(); string sfs = dr["SysFlag"].ToString(); rm.SysFlag = sfs.Split(','); initCarOnlineStatusMem(rm.SysFlag); string mincid = dr["MinCID"].ToString(); string maxcid = dr["MaxCID"].ToString(); long MinCID = 0, MaxCID = 0; long.TryParse(mincid, out MinCID); long.TryParse(maxcid, out MaxCID); rm.MinCID = MinCID; rm.MaxCID = MaxCID; rm.ID = dr["ID"].ToString(); rm.IP = dr["IP"].ToString(); rm.Port = dr["Port"].ToString(); rm.RDSMark = dr["RDSMark"].ToString(); rm.WCFUrl = dr["WCFUrl"].ToString(); TcpCli tc = new TcpCli(rm.IP, int.Parse(rm.Port)); tc.ConnectedServer += rm.EventConnectedServer; tc.DisConnectedServer += rm.EventDisConnectedServer; tc.ReceivedDatagram += new NetEvent(tc_ReceivedDatagram); try { tc.Connect(); Thread.Sleep(4000); if (tc.IsConnected) { rm.RunFlag = true; } else { rm.RunFlag = false; } } catch { rm.RunFlag = false; } rm.TcpClient = tc; RDSClient.Add(rm.ID, rm); if (rm.RunFlag) { LogHelper.WriteInfo("初始化与RDS服务建立连接完成(" + rm.RDSMark + ")"); } else { LogHelper.WriteInfo("初始化与RDS服务建立连接失败(" + rm.RDSMark + "),等待维护线程重连"); } } }
public ResponseAppResult AppGetOBDDriverResult(Dictionary <string, string> inparams) { ResponseAppResult Result = null; if (!inparams.Keys.Contains("cid") || inparams["cid"] == "") { Result = new ResponseAppResult(ResState.ParamsImperfect, "缺少cid或cid为空!", null); return(Result); } try { string sysflag = inparams["sysflag"]; long cid = GetCarCIDBySimCode(sysflag, inparams["cid"]); AlertorResolve ar = new AlertorResolve(); RDSConfigModel rc = RDSConfig.GetRDS(WebProc.GetAppSysflagKey(sysflag)); RealtimeDataServer.WCFServiceClient df = new RealtimeDataServer.WCFServiceClient(); df.Endpoint.Address = new System.ServiceModel.EndpointAddress(rc.WCFUrl); RealtimeDataServer.CarTransmissionProtocolInfo ctf = df.GetCarTransData(WebProc.GetAppSysflagKey(sysflag), cid); byte[] spai = null; if (ctf != null && ctf.TransData != null && ctf.TransData.ContainsKey(0x0FC8)) { spai = ctf.TransData[0x0FC8];// date[0].TransmissionProtocolInfo; List <OBDDriver> res = ar.analyzeOBDDriver(spai); Result = new ResponseAppResult(ResState.Success, "操作成功", res); } else { Result = new ResponseAppResult(ResState.RetryAfter, "未收到ODB行车数据,请稍候重试!", ""); } } catch (Exception ex) { LogHelper.WriteError("AppGetOBDDriverResult调用异常", ex); Result = new ResponseAppResult(ResState.OperationFailed, ex.Message, null); } return(Result); }
public ResponseAppResult AppGetOBDDiagnResult(Dictionary <string, string> inparams) { ResponseAppResult Result = null; if (!inparams.Keys.Contains("cid") || inparams["cid"] == "") { Result = new ResponseAppResult(ResState.ParamsImperfect, "缺少cid或cid为空!", null); return(Result); } try { string sysflag = inparams["sysflag"]; long cid = GetCarCIDBySimCode(sysflag, inparams["cid"]); AlertorResolve ar = new AlertorResolve(); RDSConfigModel rc = RDSConfig.GetRDS(WebProc.GetAppSysflagKey(sysflag)); RealtimeDataServer.WCFServiceClient df = new RealtimeDataServer.WCFServiceClient(); df.Endpoint.Address = new System.ServiceModel.EndpointAddress(rc.WCFUrl); RealtimeDataServer.CarTransmissionProtocolInfo ctf = df.GetCarTransData(WebProc.GetAppSysflagKey(sysflag), cid); byte[] spai = null; //OBDDiagn res = new OBDDiagn(); //res.VIN = "aa12345678935"; //res.NormalUnitCount = 2; //res.FaultyUnitList = new List<OBDDiagn.diagn>(); //OBDDiagn.diagn rrr = new OBDDiagn.diagn(); //rrr.FaultyCodes = "12412"; //rrr.UnitCode = 0x5; //rrr.UnitName = "允许进入/启动"; //rrr.AsmName = "车身系统"; //res.FaultyUnitList.Add(rrr); //OBDDiagn.diagn rrr2 = new OBDDiagn.diagn(); //rrr2.FaultyCodes = "27452"; //rrr2.UnitCode = 0x16; //rrr2.UnitName = "方向盘"; //rrr2.AsmName = "车身系统"; //res.FaultyUnitList.Add(rrr2); //res.NormalUnitList = new List<OBDDiagn.diagn>(); //OBDDiagn.diagn rrr3 = new OBDDiagn.diagn(); //rrr3.FaultyCodes = "00000"; //rrr3.UnitCode = 0x1; //rrr3.UnitName = "发动机"; //rrr3.AsmName = "动力总成系统"; //res.NormalUnitList.Add(rrr3); //OBDDiagn.diagn rrr4 = new OBDDiagn.diagn(); //rrr4.FaultyCodes = "00000"; //rrr4.UnitCode = 0x3; //rrr3.AsmName = "底盘系统"; // rrr4.UnitName = "ABS制动"; //res.NormalUnitList.Add(rrr4); //Result = new ResponseAppResult(ResState.Success, "操作成功", res); if (ctf != null && ctf.TransData != null && ctf.TransData.ContainsKey(0x0FC7)) { spai = ctf.TransData[0x0FC7];// date[0].TransmissionProtocolInfo; OBDDiagn res = ar.analyzeOBDDiagn(spai); if (res != null) { Result = new ResponseAppResult(ResState.Success, "操作成功", res); } else { Result = new ResponseAppResult(ResState.RetryAfter, "未收到OBD诊断数据,请稍候重试!", ""); } } else { Result = new ResponseAppResult(ResState.RetryAfter, "未收到OBD诊断数据,请稍候重试!", ""); } } catch (Exception ex) { LogHelper.WriteError("AppSendOBDDiagn调用异常", ex); Result = new ResponseAppResult(ResState.OperationFailed, ex.Message, null); } return(Result); }
public static bool SendMsg(string sysflag, string token, long cid, QMDPartnerPackage package) { RDSConfigModel rcm = GetRDS(sysflag, cid); if (rcm == null) { return(false); } TcpCli tc = rcm.TcpClient; try { if (tc.IsConnected) { byte[] sfdata = Encoding.ASCII.GetBytes(sysflag); byte[] ciddata = System.BitConverter.GetBytes(cid); MemoryStream ms = new MemoryStream(); ProtoBuf.Serializer.Serialize(ms, package); byte[] bytes = new byte[ms.Length]; ms.Seek(0, SeekOrigin.Begin); ms.Read(bytes, 0, bytes.Length); ms.Close(); //将系统标示加入数据头部长度20字节,CID数据 8个字节 byte[] data = new byte[bytes.Length + 20 + 8]; Array.Copy(sfdata, data, sfdata.Length); Array.Copy(ciddata, 0, data, 20, 8); Array.Copy(bytes, 0, data, 28, bytes.Length); tc.SendData(data); if (package.dwPackageType == 30 && (package.dwOperation1 == 10 || package.dwOperation1 == 33 || package.dwOperation1 == 41 || package.dwOperation1 == 99)) //需要回传数据的记录指令,以便于应答 时确认发送者 { if (package.dwOperation1 == 41) //行驶记录仪参数查询特殊处理同时加入命令字做为指令细分 { MemoryStream msc = new MemoryStream(package.byteContext); CTS_DriveRecordDataCollectionRequest reqdata = ProtoBuf.Serializer.Deserialize <CTS_DriveRecordDataCollectionRequest>(msc); AddOrder(sysflag, token, cid, package.dwPackageType, package.dwOperation1, reqdata.nCMD.ToString()); } else if (package.dwOperation1 == 99)//透传指令 下发加入透传id作为细分指令 { MemoryStream msc = new MemoryStream(package.byteContext); CTS_TransmissionProtocol reqdata = ProtoBuf.Serializer.Deserialize <CTS_TransmissionProtocol>(msc); string extflag = reqdata.nTransID.ToString("X2"); if (reqdata.nTransID == 0x8F02)//解放消贷控制指令特殊处理,因为上行和下行nTransID不一致。此处填充上行id方便回指令结果 { int respnTransID = reqdata.nTransID == 0x8F02 ? 0x0f01 : reqdata.nTransID; extflag = respnTransID.ToString("X2") + reqdata.sData[0].ToString("X2") + reqdata.sData[1].ToString("X2"); AddOrder(sysflag, token, cid, package.dwPackageType, package.dwOperation1, extflag); } } else { AddOrder(sysflag, token, cid, package.dwPackageType, package.dwOperation1); } } return(true); } else { LogHelper.WriteInfo(sysflag + "RDS连接中断发送数据失败(" + string.Join(",", rcm.SysFlag) + ")"); if (ReConnect(tc)) { LogHelper.WriteInfo(sysflag + "RDS服务(" + rcm.RDSMark + ")重连成功发送数据!"); return(SendMsg(sysflag, token, cid, package)); } else { return(false); } } } catch (Exception ex) { LogHelper.WriteError(sysflag + "程序异常发送数据失败! ", ex); return(false); } }