void ProcessHeartbeat(MessageInfo message, SHaiSession session) { if (message.MessageAttribute == "notify") { Console.WriteLine("{0} -> 接收到网关:{1}上传的心跳信息", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), message.GatewayID); byte[] data = XMLHelper.GetXmlBytes(message, "heart_beat", "time", DateTime.Now.ToString("yyyyMMddHHmmss")); session.Send(ToolUtil.AppendPacketHeader(2, data), 0, data.Length + 7); } }
void ProcessEnergyData(MessageInfo message, SHaiSession session, string xmlSource) { if (message.MessageAttribute == "report") { Console.WriteLine("{0} -> 接收到:" + message.GatewayID + "网关发来的建筑" + message.BuildID + "的定时上报的能耗数据,数据时间为:" + message.MessageContent, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); //保存数据到文件中 string source = XMLHelper.GetMeterList(xmlSource); bool receiveSuccess = SQLiteHelper.InsertGatewayDataToDB(message, source); if (!receiveSuccess) { return; } //回送接收到数据 byte[] data = XMLHelper.GetXmlBytes(message, "energy_data", message.MessageAttribute, message.MessageContent, "OK"); session.Send(ToolUtil.AppendPacketHeader(3, data), 0, data.Length + 7); } else if (message.MessageAttribute == "continuous") { Console.WriteLine("{0} -> 接收到:" + message.GatewayID + "网关发来的建筑" + message.BuildID + "的断点续传的能耗数据,数据时间为:" + message.MessageContent, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); string source = XMLHelper.GetMeterList(xmlSource); bool receiveSuccess = SQLiteHelper.InsertGatewayDataToDB(message, source); if (!receiveSuccess) { return; } //回送接收到数据 byte[] data = XMLHelper.GetXmlBytes(message, "energy_data", message.MessageAttribute, message.MessageContent, "OK"); session.Send(ToolUtil.AppendPacketHeader(3, data), 0, data.Length + 7); } else if (message.MessageAttribute == "finish") { Console.WriteLine("{0} -> 接收到:" + message.GatewayID + "网关发来的建筑" + message.BuildID + "断点续传完成标记。", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); //回送接收到数据 byte[] data = XMLHelper.GetXmlBytes(message, "energy_data", message.MessageAttribute, message.MessageContent, "OK"); session.Send(ToolUtil.AppendPacketHeader(3, data), 0, data.Length + 7); } }
void ProcessValidate(MessageInfo message, SHaiSession session) { string key = message.BuildID + "_" + message.GatewayID; if (message.MessageAttribute == "request") { Console.WriteLine("{0} -> 接收到:" + session.RemoteEndPoint.Address + ":" + session.RemoteEndPoint.Port + "身份认证请求,请求信息为:" + message.BuildID + "," + message.GatewayID, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); Guid guid = Guid.NewGuid(); string sequence = guid.ToString(); if (Runtime.m_GateWayInfo.ContainsKey(key)) { Runtime.m_GateWayInfo[key] = MD5Helper.GetMD5(Runtime.m_AESValue + sequence); } else { Runtime.m_GateWayInfo.Add(key, MD5Helper.GetMD5(Runtime.m_AESValue + sequence)); } byte[] data = XMLHelper.GetXmlBytes(message, "id_validate", "sequence", sequence); if (session != null && data.Length > 0) { session.Send(ToolUtil.AppendPacketHeader(1, data), 0, data.Length + 7); } } else if (message.MessageAttribute == "md5") { byte[] data; if (Runtime.m_GateWayInfo[key].ToUpper() == message.MessageContent.ToUpper()) { Console.WriteLine("{0} -> MD5信息比对成功:" + message.BuildID + "," + message.GatewayID, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); data = XMLHelper.GetXmlBytes(message, "id_validate", "result", "pass"); session.Send(ToolUtil.AppendPacketHeader(1, data), 0, data.Length + 7); } else { Console.WriteLine("{0} -> MD5信息比对失败:" + message.BuildID + "," + message.GatewayID, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); data = XMLHelper.GetXmlBytes(message, "id_validate", "result", "fail"); session.Send(ToolUtil.AppendPacketHeader(1, data), 0, data.Length + 7); } } }
void ProcessEnergyData(MessageInfo message, SHaiSession session, string xmlSource) { if (message.MessageAttribute == "report") { Console.WriteLine("{0} -> 接收到:" + message.GatewayID + "网关发来的建筑" + message.BuildID + "的定时上报的能耗数据,数据时间为:" + message.MessageContent, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); Runtime.m_Logger.Info("{0} -> 接收到:" + message.GatewayID + "网关发来的建筑" + message.BuildID + "的定时上报的能耗数据,数据时间为:" + message.MessageContent, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); //保存数据到文件中 //string source = XMLHelper.GetMeterList(xmlSource); //bool receiveSuccess = SQLiteHelper.InsertGatewayDataToDB(message,source); bool receiveSuccess = true; List <YangZhuChangMeter> meterList = XMLHelper.GetYangZhuChangMeters(xmlSource); try { int cnt = MySQLHelper.InsertRealTimeValues(Runtime.MySqlConnectString, meterList); if (cnt > 0) { receiveSuccess = true; } } catch (Exception e) { receiveSuccess = false; Runtime.m_Logger.Error("数据存储出错," + e.Message); } if (!receiveSuccess) { return; } //回送接收到数据 byte[] data = XMLHelper.GetXmlBytes(message, "energy_data", message.MessageAttribute, message.MessageContent, "OK"); session.Send(ToolUtil.AppendPacketHeader(3, data), 0, data.Length + 7); } else if (message.MessageAttribute == "continuous") { Console.WriteLine("{0} -> 接收到:" + message.GatewayID + "网关发来的建筑" + message.BuildID + "的断点续传的能耗数据,数据时间为:" + message.MessageContent, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); //string source = XMLHelper.GetMeterList(xmlSource); //bool receiveSuccess = SQLiteHelper.InsertGatewayDataToDB(message, source); bool receiveSuccess = true; List <YangZhuChangMeter> meterList = XMLHelper.GetYangZhuChangMeters(xmlSource); try { int cnt = MySQLHelper.InsertRealTimeValues(Runtime.MySqlConnectString, meterList); if (cnt > 0) { receiveSuccess = true; } } catch (Exception e) { receiveSuccess = false; Runtime.m_Logger.Error("数据存储出错," + e.Message); } if (!receiveSuccess) { return; } //回送接收到数据 byte[] data = XMLHelper.GetXmlBytes(message, "energy_data", message.MessageAttribute, message.MessageContent, "OK"); session.Send(ToolUtil.AppendPacketHeader(3, data), 0, data.Length + 7); } else if (message.MessageAttribute == "finish") { Console.WriteLine("{0} -> 接收到:" + message.GatewayID + "网关发来的建筑" + message.BuildID + "断点续传完成标记。", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); //回送接收到数据 byte[] data = XMLHelper.GetXmlBytes(message, "energy_data", message.MessageAttribute, message.MessageContent, "OK"); session.Send(ToolUtil.AppendPacketHeader(3, data), 0, data.Length + 7); } }