public async Task <bool> DoProcess(AsyncSocketConnection agent, TPKGHead msg) { var packet = msg.Data as EXTSENSPacketData; if (packet == null) { return(await SendCommonResponseMessageAsync(agent, msg, 1));//给网关回复1,说明不正常 } log.Info($"(EXTSENS)收到设备状态包(16进制):{ packet.ToBytes().ToHexString()}"); log.Info($"(EXTSENS)收到设备状态包:{ packet.ToJson()}"); Console.WriteLine($"(EXTSENS)收到设备状态包:{ packet.ToJson()}"); //TODO: var gatewayid = agent.TerminalId; string strUID = packet.Equip_UID;////外接蓝牙设备UID log.Info($"外接蓝牙设备数据包UID={ strUID}"); string strUIDPrefix16 = "";//UID前16位,一共24位,去掉后面的8位是序号,前16号相同的协议也相同 if (strUID.Length > 15) { strUIDPrefix16 = strUID.Substring(0, 16); //根据此16位去库中查去获取数据位置.SortByDescending(o => o.Created) //var query = db.GetCollection<ProtocolResolveRule>().Where(o => o.UIDPrefix == strUIDPrefix16).DoQuery().FirstOrDefault(); //var query = db.GetCollection<SendStructure>().Where(o => o.UID_16 == strUIDPrefix16 && o.IsDataArea == "1").DoQuery().ToList();//查询出数据部分协议 int iLenData = packet.DataLength; //外接蓝牙设备协议总字节数量 string strProtocol = packet.Data.ToUpper(); //外接蓝牙设备协议 (蓝牙设备数据) //if (query != null && !string.IsNullOrEmpty(strProtocol)) #region 查询协议长度是否相等,若有多条长度相同,则判断命令字 var query0 = db.GetCollection <SendStructure2>().Where(o => o.UID_16 == strUIDPrefix16).DoQuery().ToList(); //查询出数据部分协议 if (query0.Count() > 0 && !string.IsNullOrEmpty(strProtocol)) //有协议 { List <String> listItem = new List <string>(); foreach (var item in query0) { string StructName = item.StructName; if (!listItem.Contains(StructName)) { listItem.Add(StructName); } } if (listItem.Count == 1)//只有一条协议 { //取协议解析数据 DecodeData(strUIDPrefix16, listItem[0], gatewayid, strUID, strProtocol, agent, msg); } else { for (int i = 0; i < listItem.Count; i++) { var query1 = db.GetCollection <SendStructure2>().Where(o => o.UID_16 == strUIDPrefix16 && o.StructName == listItem[i]).DoQuery().FirstOrDefault(); //查询出数据部分协议 if (query1 != null && Convert.ToInt32(query1.Length) == iLenData) //长度相等,则取命令字 { var query2 = db.GetCollection <SendStructure2>().Where(o => o.UID_16 == strUIDPrefix16 && o.StructName == listItem[i] && o.DataAreaDec == "2").DoQuery().ToList(); //查询出数据部分协议 if (query2 != null && query2.Count() > 0) { int count = 0; foreach (var q in query2)//一条协议中会出现多个命令字的情况 { //判断命令字 int OderiPos1 = Convert.ToInt32(q.LocationBegin + ""); int OderiPos2 = Convert.ToInt32(q.LocationEnd + ""); string Oder; if (q.LocationEnd == -1) { Oder = strProtocol.Substring((OderiPos1 - 1) * 2, 2);//得到16进制值,数据段的值 } else { Oder = strProtocol.Substring((OderiPos1 - 1) * 2, 2 * (OderiPos2 - OderiPos1) + 2);//得到16进制值,数据段的值 } string OrderCode = q.OrderCode; OrderCode = OrderCode.Replace(" ", ""); //去掉空格 if (Oder == OrderCode) //命令字相同 { count++; } } if (query2.Count() == count)//所有命令字相同 { DecodeData(strUIDPrefix16, listItem[i], gatewayid, strUID, strProtocol, agent, msg); break; } } else//无命令字,长度相同(只允许一个相等,需要做判断) { DecodeData(strUIDPrefix16, listItem[i], gatewayid, strUID, strProtocol, agent, msg); break; } } } } } //end if (!string.IsNullOrEmpty(strProtocol)) else //无协议 { //未找到解析规则,直接将数据包转发 JObject json = new JObject( new JProperty("Success", true), new JProperty("Type", "02"), new JProperty("GatewayID", gatewayid), new JProperty("UID", strUID), new JProperty("Result", strProtocol), new JProperty("TestTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")) ); //第二种构造Json方法 dynamic json2 = new System.Dynamic.ExpandoObject(); json2.Success = true; json2.Type = "02"; json2.GatewayID = gatewayid; json2.UID = strUID; json2.Result = strProtocol; json2.TestTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); string s2 = Newtonsoft.Json.JsonConvert.SerializeObject(json2);//使用第二种Json构造方式,存入数据库 //此处要加发送和保存到数据库的代码 WaitCallback ac = async(xx) => { //发送数据 DataSending send = new DataSending(); await send.SendingData(agent, msg, gatewayid, json); //var result = await client.PostAsJsonAsync("http://120.25.159.86/api/Sensor/PostBluetoothPacket", json); //log.Info($"发送到应用云平台的蓝牙设备JSON数据:{ json}"); //以下部分要重新写代码入数据库 var d = new BluetoothSensorLog() { GatewayID = agent.TerminalId, UID = strUID, Json = s2, Created = DateTime.Now, }; await db.AddAsync(d); };//end WaitCallback ac = async (xx) => System.Threading.ThreadPool.QueueUserWorkItem(ac, null); log.Info($"根据UID前16位从tProtocolResolveRule中没有查询到解析规则或者外接蓝牙设备协议为空。UID={ strUID},JSON={json}"); } #endregion } else { log.Info($"外接蓝牙设备数据包UID长度不足16位。UID={ strUID}"); //设备UID错误 JObject json = new JObject( new JProperty("Success", false), new JProperty("Result", "错误"), new JProperty("TestTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")) ); //第二种构造Json方法 dynamic json2 = new System.Dynamic.ExpandoObject(); json2.Success = false; json2.Result = "错误"; json2.TestTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); string s2 = Newtonsoft.Json.JsonConvert.SerializeObject(json2);//使用第二种Json构造方式,存入数据库 //此处要加发送 WaitCallback ac = async(xx) => { //发送数据 DataSending send = new DataSending(); await send.SendingData(agent, msg, gatewayid, json); //var result = await client.PostAsJsonAsync("http://120.25.159.86/api/Sensor/PostBluetoothPacket", json); //var result = await client.PostAsJsonAsync("http://120.25.159.86/api/Sensor/PostBluetoothPacket", json);//联桥网云应用云平台 //var result1 = await client.PostAsJsonAsync("http://202.91.226.221:8088/api/Sensor/PostSenorData", json);//杭州大数据平台 //var result2 = await client.PostAsJsonAsync("http://202.91.226.221/api/Sensor/PostSenorData", json);//杭州大数据平台 //log.Info($"发送到应用云平台的蓝牙设备JSON数据:{ json}"); //以下部分要重新写代码入数据库 var d = new BluetoothSensorLog() { GatewayID = agent.TerminalId, UID = strUID, Json = s2, Created = DateTime.Now, }; await db.AddAsync(d); };//end WaitCallback ac = async (xx) => System.Threading.ThreadPool.QueueUserWorkItem(ac, null); } return(await SendCommonResponseMessageAsync(agent, msg, 0));//给网关回复0,说明正常 }
public async Task <bool> DoProcess(AsyncSocketConnection agent, TPKGHead msg) { var packet = msg.Data as EXTSENSPacketData; if (packet == null) { return(await SendCommonResponseMessageAsync(agent, msg, 1));//给网关回复1,说明不正常 } log.Info($"(EXTSENS)收到设备状态包(16进制):{ packet.ToBytes().ToHexString()}"); log.Info($"(EXTSENS)收到设备状态包:{ packet.ToJson()}"); Console.WriteLine($"(EXTSENS)收到设备状态包:{ packet.ToJson()}"); //TODO: var gatewayid = agent.TerminalId; string strUID = packet.Equip_UID; ////外接蓝牙设备UID string strUIDPrefix16 = ""; //UID前16位,一共24位,去掉后面的8位是序号,前16号相同的协议也相同 if (strUID.Length > 15) { strUIDPrefix16 = strUID.Substring(0, 16); //根据此16位去库中查去获取数据位置 } int iLenData = packet.DataLength; //外接蓝牙设备协议总字节数量 string strProtocol = packet.Data.ToUpper(); //外接蓝牙设备协议 if (!string.IsNullOrEmpty(strProtocol)) { #region 将蓝牙设备数据传递给应用云平台 //var gatewayid = msg.Term_Code; //两种方式 // to do 需要根据gatewayid去数据库里查找其对应的应用云平台接口地址 WaitCallback ac = async(xx) => { BluetoothDeviceData obj = null; #region 诺血糖仪数据包 //三诺蓝牙设备数据包:{ "UID" : "AATJ00017000310000000002", "TestResult1" : 5.9, "TestResult1Desc" : "BloodSugar", "TestResult2" : 20.2, "TestResult2Desc" : "Temperature", "TestResult3" : -1.0, "TestResult3Desc" : "", "TestResult4" : -1.0, "TestResult4Desc" : "", "TestResult5" : -1.0, "TestResult5Desc" : "", "TestTime" : "2016-10-25 09:45:49" } // 53 4E 10 00 04 04 0E 04 16 10 37 01 01 0C 00 00 E1 00 76 // 53 4E 0E 00 04 04 0F 06 11 13 28 00 2E 00 01 41 E7 缺少了秒和校正码 if (strProtocol.Length == 34 && strProtocol.Substring(0, 4) == "534E") { // 测试结果为26.8mmol / L,时间为14年4月22日16点55分;则发送的数据为:53 4E 10 00 04 04 0E 04 16 10 37 01 01 0C 00 00 E1 00 76 //其中“0X53 4E ”数据包头;“0X10” 数据长度,从本字节后开始计算;“0X00 04” 机器代码; “0X04” 结果命令; “0x0E” 14年; //“0X04” 4月;“0X16” 22日;“0X10” 16点;“0X37” 55分;“0X01” 01秒;“0X01 0C” 表示234,此数据除以10即为23.4mmol / L; //“0x00” 样本类型;“00 E1” 表示225,此数据除以10即为22.5℃;“0x00” 校正码;“0X76” 为校验码,和校验,包含长度,高字节丢掉; if (strProtocol.Substring(10, 2) == "04") //命令字:ox04 读取当前结果 { string strV1Hex = strProtocol.Substring(22, 4); string strV2Hex = strProtocol.Substring(28, 4); obj = new BluetoothDeviceData(); obj.UID = strUID; obj.TestResult1 = SensorNetwork.Common.RadixConverter.HexStrToInt32(strV1Hex) / 10.0; obj.TestResult1Desc = "BloodSugar"; obj.TestResult2 = SensorNetwork.Common.RadixConverter.HexStrToInt32(strV2Hex) / 10.0; obj.TestResult2Desc = "Temperature"; obj.TestResult3 = obj.TestResult4 = obj.TestResult5 = -1; obj.TestResult3Desc = obj.TestResult4Desc = obj.TestResult5Desc = ""; obj.TestTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); log.Info($"三诺蓝牙设备数据包:{ obj.ToJson()}"); } } #endregion #region 开血压仪数据包 // 三开血压仪数据包:{ "UID" : "ABTJ00017000110000000001", "TestResult1" : 122.0, "TestResult1Desc" : "BloodPressureSystolic", "TestResult2" : 75.0, "TestResult2Desc" : "BloodPressureDiastolic", "TestResult3" : 95.0, "TestResult3Desc" : "Pulse", "TestResult4" : -1.0, "TestResult4Desc" : "", "TestResult5" : -1.0, "TestResult5Desc" : "", "TestTime" : "2016-10-26 10:19:55" } if (strProtocol.Length == 40 && strProtocol.Substring(0, 4) == "AA80") { //AA 80 02 08 01 05 00 00 00 00 37 00 39 //中间结果,实时数据 //最终结果AA 80 02 0f 01 06 01 12 09 13 12 01 20 00 76 00 45 00 3f 3c ,00 76 00 45 00 3f中00 76、00 45、00 03f分别对应收缩压、舒张压、脉搏 // AA 80 02 0F 01 06 01 12 09 18 17 2C 18 00 7A 00 4B 00 5F 45 if (strProtocol.Substring(6, 6) == "0F0106") { string strV1Hex = strProtocol.Substring(26, 4); string strV2Hex = strProtocol.Substring(30, 4); string strV3Hex = strProtocol.Substring(34, 4); obj = new BluetoothDeviceData(); obj.UID = strUID; obj.TestResult1 = SensorNetwork.Common.RadixConverter.HexStrToInt32(strV1Hex) / 1.0; obj.TestResult1Desc = "BloodPressureSystolic"; //收缩压 obj.TestResult2 = SensorNetwork.Common.RadixConverter.HexStrToInt32(strV2Hex) / 1.0; obj.TestResult2Desc = "BloodPressureDiastolic"; //舒张压 obj.TestResult3 = SensorNetwork.Common.RadixConverter.HexStrToInt32(strV3Hex) / 1.0; obj.TestResult3Desc = "Pulse"; //脉搏 obj.TestResult4 = obj.TestResult5 = -1; obj.TestResult4Desc = obj.TestResult5Desc = ""; obj.TestTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); log.Info($"三开血压仪数据包:{ obj.ToJson()}"); } } #endregion if (obj != null) { //1.通过http POST给应用云平台 var json = JsonConvert.SerializeObject(obj); var result = await client.PostAsJsonAsync("http://120.25.159.86/api/Sensor/PostBluetoothPacket", obj); //if (result.IsSuccessStatusCode && (await result.Content.ReadAsStringAsync()).Contains("'status':false,")) //{ // //2.通过ZMQ 发送给应用云平台 - // SensorNetwork.Common.ZeroMQ.ZMQRequest("tcp://120.25.159.86:6789", JsonConvert.SerializeObject(packet)); //} log.Info($"发送到应用云平台的蓝牙设备JSON数据:{ obj.ToJson()}"); var d = new BluetoothSensorLog() { GatewayID = agent.TerminalId, UID = obj.UID, TestResult1 = obj.TestResult1, TestResult1Desc = obj.TestResult1Desc, TestResult2 = obj.TestResult2, TestResult2Desc = obj.TestResult2Desc, TestResult3 = obj.TestResult3, TestResult3Desc = obj.TestResult3Desc, TestResult4 = obj.TestResult4, TestResult4Desc = obj.TestResult4Desc, TestResult5 = obj.TestResult5, TestResult5Desc = obj.TestResult5Desc, Created = DateTime.Now, }; await db.AddAsync(d); } }; //end WaitCallback ac = async (xx) => System.Threading.ThreadPool.QueueUserWorkItem(ac, null); #endregion } //end if (!string.IsNullOrEmpty(strProtocol)) return(await SendCommonResponseMessageAsync(agent, msg, 0));//给网关回复0,说明正常 }
public void DecodeData(string UID_16, string StructName, string GatewayID, string UID, string strProtocol, AsyncSocketConnection agent, TPKGHead msg) { //取协议解析数据 #region var query2 = db.GetCollection <SendStructure2>().Where(o => o.UID_16 == UID_16 && o.StructName == StructName && o.DataAreaDec == "1").DoQuery().ToList();//查询出数据部分协议 // to do 需要根据gatewayid去数据库里查找其对应的应用云平台接口地址 WaitCallback ac = async(xx) => { //BluetoothDeviceData obj = null; JObject json = new JObject(); dynamic json2 = new System.Dynamic.ExpandoObject();//动态构建Json数据包,该方法暂定为不可用。 try { json.Add(new JProperty("Success", true)); json.Add(new JProperty("Code", 1)); json.Add(new JProperty("Type", "01")); json.Add(new JProperty("GatewayID", GatewayID)); json.Add(new JProperty("UID", UID)); //第二种方法,动态构建Json数据包,暂时未将其发到应用云平台,只用于存储到数据库中 json2.Success = true; json2.Code = 1; json2.Type = "01"; json2.GatewayID = GatewayID; json2.UID = UID; //var s = Newtonsoft.Json.JsonConvert.SerializeObject(json2); JArray array = new JArray(); foreach (var q in query2) { //取出所有的数据域部分,进行数据处理,等到testresult,testresultdesc string strDataDesc = q.DataDesc; string strFormula = q.Formula; int iPos1 = Convert.ToInt32(q.LocationBegin + ""); int iPos2 = Convert.ToInt32(q.LocationEnd + ""); //log.Info($"获取位置:iPos1={ iPos1},iPos2={ iPos2}"); double data; string strVHex; if (q.LocationEnd == -1) { strVHex = strProtocol.Substring((iPos1 - 1) * 2, 2);//得到16进制值,数据段的值 } else { strVHex = strProtocol.Substring((iPos1 - 1) * 2, 2 * (iPos2 - iPos1) + 2); //得到16进制值,数据段的值 } if (!string.IsNullOrEmpty(q.Formula)) //存在需要进一步计算转换的公式 { int iTestResult1 = SensorNetwork.Common.RadixConverter.HexStrToInt32(strVHex); string strTemp1 = q.Formula.Replace("D", iTestResult1.ToString()); Microsoft.JScript.Vsa.VsaEngine ve = Microsoft.JScript.Vsa.VsaEngine.CreateEngine();//公式计算 var Result1 = Microsoft.JScript.Eval.JScriptEvaluate(strTemp1, ve); data = Convert.ToDouble(Result1); //log.Info($"取数:data={ data}"); } else { data = SensorNetwork.Common.RadixConverter.HexStrToInt32(strVHex) / 1.0; //log.Info($"取数:data={ data}"); } //log.Info($"数组中加入数据:TestResult={ data},TestResultDesc={q.DataDesc}"); JObject jo = new JObject(); jo.Add(new JProperty("TestResult", data)); jo.Add(new JProperty("TestResultDesc", q.DataDesc)); // array.Add(jo); } //array.Add( DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); json.Add("Result", array); json.Add(new JProperty("TestTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); json2.Result = array; json2.TestTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); string s = Newtonsoft.Json.JsonConvert.SerializeObject(json2); //发送数据 DataSending send = new DataSending(); await send.SendingData(agent, msg, GatewayID, json); } catch (Exception ex) { log.Info($"处理蓝牙设备数据包发生异常:{ ex.Message}"); //设备UID错误 JObject jsonError = new JObject( new JProperty("Success", false), new JProperty("Result", "错误"), new JProperty("TestTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")) ); //此处要加发送 //发送数据 DataSending send = new DataSending(); await send.SendingData(agent, msg, GatewayID, jsonError); } //log.Info($"蓝牙设备数据包:{ obj.ToJson()}"); // log.Info($"发送到应用云平台的蓝牙设备JSON数据:{ json}"); //log.Info($"json.tostring:{ json.ToString()}"); //以下部分要重新写代码入数据库 string js = json.ToString(); // json.ToString().Substring(0, 1002),//保存长度有问题,需要更改.Substring(0, 1002) string s2 = Newtonsoft.Json.JsonConvert.SerializeObject(json2); //使用第二种Json构造方式,存入数据库 log.Info($"js:{ js.Length}"); var d = new BluetoothSensorLog() { GatewayID = agent.TerminalId, UID = UID, Json = s2, Created = DateTime.Now, }; await db.AddAsync(d); // } };//end WaitCallback ac = async (xx) => System.Threading.ThreadPool.QueueUserWorkItem(ac, null); #endregion }