/// <summary> /// 更新或新建Tracker的基本信息 /// </summary> private void UpdateTrackerInfo(TX300 tx300, AsyncUserDataBuffer data, TrackerBLL bll) { var sim = GetSimFromData(tx300); var tracker = bll.Find(f => f.SimCard.Equals(sim)); if (null == tracker) { // 新增一个tracker tracker = bll.GetObject(); tracker.SimCard = sim; tracker.LastActionAt = data.ReceiveTime; tracker.Socket = data.SocketHandle; tracker.State = 1; bll.Add(tracker); } else { bll.Update(f => f.id == tracker.id, act => { act.LastActionAt = data.ReceiveTime; act.Socket = data.SocketHandle; if (tx300.CommandID == 0x7020 || tx300.CommandID == 0x7030 || tx300.CommandID == 0x7040) { act.CSQ = tx300.MsgContent[0]; } }); } }
/// <summary> /// 根据TX300数据包更新终端和设备的在线状态 /// </summary> /// <param name="tx300"></param> private void HandleTX300Status(TX300 tx300, AsyncUserDataBuffer data) { var sim = GetSimFromData(tx300); var equipment = EquipmentInstance.Find(f => f.TB_Terminal.Sim.Equals(sim)); var terminal = TerminalInstance.Find(f => f.Sim.Equals(sim)); // 终端不存在的话,不用再继续处理数据了 if (null == terminal) { return; } HandleOnline(sim, tx300.CommandID, data); if (tx300.CommandID != 0xAA00) { SaveTX300History(tx300, data.ReceiveTime, (null == equipment ? "" : EquipmentInstance.GetFullNumber(equipment))); // TX10G的数据 if (tx300.CommandID == 0x7010 || tx300.CommandID == 0x7020 || tx300.CommandID == 0x7030) { HandleTX10G(tx300, data); } else { // 根据命令的不同处理各个命令详情 HandleCommand(tx300, equipment, terminal); } } }
/// <summary> /// 处理收到的数据 /// </summary> /// <param name="data"></param> public void HandleData(AsyncUserDataBuffer data) { try { //if (data.PackageType == AsyncDataPackageType.UDP) { IP = data.IP; Port = data.Port; } bool handled = false; switch (data.DataType) { case AsyncUserDataType.ClientConnected: break; case AsyncUserDataType.ClientDisconnected: // 处理客户端断开连接的情况 HandleClientDisconnect(data.SocketHandle); break; case AsyncUserDataType.ReceivedData: var len = data.Buffer.Length; // 如果收到的数据长度小于TX300的包头则不用处理数据了 if (len >= Protocol.TX300.TX300Items.header_length && Protocol.ProtocolTypes.IsTX300(data.Buffer[2]) && Protocol.TerminalTypes.IsTX300(data.Buffer[3])) { handled = true; if (data.Buffer[0] != len) { HandleException("Data length(package length: " + data.Buffer[0] + ", buffer length: " + len + ") error.", CustomConvert.GetHex(data.Buffer)); } else { HandleReceivedData(data); } } else { // 增加未能处理的数据保存 2015/09/02 15:09 HandleException("Data is not TX protocol.", CustomConvert.GetHex(data.Buffer)); } break; } if (data.PackageType == AsyncDataPackageType.SAT) { if (!handled) { } data.Dispose(); data = null; } } catch (Exception e) { ShowUnhandledMessage(Now + e.Message + Environment.NewLine + e.StackTrace + Environment.NewLine + CustomConvert.GetHex(data.Buffer)); HandleException(e.Message + Environment.NewLine + e.StackTrace, CustomConvert.GetHex(data.Buffer)); } IP = ""; Port = 0; }
/// <summary> /// 更新在线时间和在线状态 /// </summary> private void HandleOnline(string sim, ushort CommandID, AsyncUserDataBuffer data) { using (var bll = new EquipmentBLL()) { bll.Update(f => f.TB_Terminal.Sim.Equals(sim), act => { act.IP = data.IP; act.Port = data.Port; act.Socket = data.SocketHandle; act.OnlineTime = data.ReceiveTime; // 处理当发送报警信息时设备已经是OFF状态的情况:不处理 if (act.OnlineStyle == (byte)LinkType.OFF && CommandID == 0x2000) { // 收到报警时不处理已经是OFF状态 } else { act.OnlineStyle = GetOnlineStyleByPackage(data.PackageType); } act.LastAction = "0x" + CustomConvert.IntToDigit(CommandID, CustomConvert.HEX, 4); act.LastActionBy = GetOnlineStyle(data.PackageType); act.LastActionTime = data.ReceiveTime; }); } using (var bll = new TerminalBLL()) { bll.Update(f => f.Sim.Equals(sim), act => { act.Socket = data.SocketHandle; if (act.OnlineStyle == (byte)LinkType.OFF && CommandID == 0x2000) { // 收到报警但此时已经是OFF状态时,不更新在线状态 } else { act.OnlineStyle = GetOnlineStyleByPackage(data.PackageType); } act.OnlineTime = data.ReceiveTime; }); } // 查看是否为TX10G的命令 if (CommandID >= 0x7000 && CommandID <= 0x7040) { using (var bll = new TrackerBLL()) { bll.Update(f => f.SimCard.Equals(sim), act => { // 标记为在线状态 act.State = 1; act.LastActionAt = DateTime.Now; act.Socket = data.SocketHandle; }); } } }
/// <summary> /// 更新在线时间和在线状态 /// </summary> private void HandleOnline(string sim, ushort CommandID, AsyncUserDataBuffer data) { EquipmentInstance.Update(f => f.TB_Terminal.Sim.Equals(sim), act => { act.IP = data.IP; act.Port = data.Port; act.Socket = data.SocketHandle; act.OnlineTime = data.ReceiveTime; act.OnlineStyle = (byte)(data.PackageType == AsyncDataPackageType.TCP ? LinkType.TCP : LinkType.UDP); act.LastAction = "0x" + CustomConvert.IntToDigit(CommandID, CustomConvert.HEX, 4); act.LastActionBy = data.PackageType == AsyncDataPackageType.TCP ? "TCP" : "UDP"; act.LastActionTime = data.ReceiveTime; }); TerminalInstance.Update(f => f.Sim.Equals(sim), act => { act.Socket = data.SocketHandle; act.OnlineStyle = (byte)(data.PackageType == AsyncDataPackageType.TCP ? LinkType.TCP : LinkType.UDP); act.OnlineTime = data.ReceiveTime; }); }
/// <summary> /// 处理收到的数据 /// </summary> /// <param name="data"></param> public void HandleData(AsyncUserDataBuffer data) { try { switch (data.DataType) { case AsyncUserDataType.ClientConnected: break; case AsyncUserDataType.ClientDisconnected: // 处理客户端断开连接的情况 HandleClientDisconnect(data.SocketHandle); break; case AsyncUserDataType.ReceivedData: var len = data.Buffer.Length; // 如果收到的数据长度小于TX300的包头则不用处理数据了 if (len >= TX300Items.header_length && Wbs.Protocol.ProtocolTypes.IsTX300(data.Buffer[2]) && Wbs.Protocol.TerminalTypes.IsTX300(data.Buffer[3])) { if (data.Buffer[0] != len) { HandleException("Data length(package length: " + data.Buffer[0] + ", buffer length: " + len + ") error.", CustomConvert.GetHex(data.Buffer)); } else { HandleReceivedData(data); } } break; } } catch (Exception e) { HandleException(e.StackTrace, CustomConvert.GetHex(data.Buffer)); } }
/// <summary> /// 处理TX10G的数据 /// </summary> /// <param name="tx300"></param> /// <param name="data"></param> private void HandleTX10G(TX300 tx300, AsyncUserDataBuffer data) { using (var bll = new TrackerBLL()) { // 更新或新建Tracker信息 UpdateTrackerInfo(tx300, data, bll); var sim = GetSimFromData(tx300); var tracker = bll.Find(f => f.SimCard.Equals(sim)); switch (tx300.CommandID) { case 0x7020: // 报警 Handle0x7020(tx300, tracker, bll); break; case 0x7030: // 位置信息 Handle0x7030(tx300, tracker); break; } } }
/// <summary> /// 根据TX300数据包更新终端和设备的在线状态 /// </summary> /// <param name="tx300"></param> private void HandleTX300Status(TX300 tx300, AsyncUserDataBuffer data) { var sim = GetSimFromData(tx300); using (var ebll = new EquipmentBLL()) { var equipment = ebll.Find(f => f.TB_Terminal.Sim.Equals(sim)); var terminal = new TerminalBLL().Find(f => f.Sim.Equals(sim)); SaveTerminalData(null == terminal ? -1 : terminal.id, sim, data.PackageType, tx300.TotalLength, true, data.ReceiveTime); // 终端不存在的话,不用再继续处理数据了 if (!IsTracker(tx300.CommandID)) { if (null == terminal) { return; } } HandleOnline(sim, tx300.CommandID, data); if (tx300.CommandID != 0xAA00) { // TX10G的数据 if (tx300.CommandID >= 0x7000 && tx300.CommandID <= 0x7040) { HandleTX10G(tx300, data); } else { SaveTX300History(tx300, data.ReceiveTime, (null == equipment ? "" : ebll.GetFullNumber(equipment))); // 根据命令的不同处理各个命令详情 HandleCommand(tx300, equipment, terminal); } } } }
/// <summary> /// 每5秒获取一次未处理的SMS数据 /// </summary> public void CheckSMSData() { if (_lastCheckSMSData.Subtract(DateTime.Now).Duration().TotalSeconds < 5) { return; } _lastCheckSMSData = DateTime.Now; using (var bll = new SmsBLL()) { var sms = bll.Find(f => f.Handled == false); if (null != sms) { using (var data = new AsyncUserDataBuffer()) { data.Buffer = Convert.FromBase64String(sms.Data); data.DataType = AsyncUserDataType.ReceivedData; data.IP = ""; data.PackageType = AsyncDataPackageType.SMS; data.Port = 0; data.ReceiveTime = sms.SendTime.Value; data.SocketHandle = 0; ShowUnhandledMessage(format("{0}SMS data from {1}: {2}", Now, sms.Sender, CustomConvert.GetHex(data.Buffer))); // 如果数据中的sim号码是000000000000则将sender直接放入其中 2015/09/02 15:18 if (data.Buffer[9] == 0 && data.Buffer[10] == 0) { byte[] t = SimToByte(sms.Sender); Buffer.BlockCopy(t, 0, data.Buffer, 9, 6); t = null; } HandleData(data); } bll.Update(f => f.id == sms.id, act => { act.Handled = true; }); } } }
/// <summary> /// 处理TX10G的数据 /// </summary> /// <param name="tx300"></param> /// <param name="data"></param> private void HandleTX10G(TX300 tx300, AsyncUserDataBuffer data) { }
/// <summary> /// 处理接收到的数据 /// </summary> /// <param name="data"></param> private void HandleReceivedData(AsyncUserDataBuffer data) { int index = 0, len = data.Buffer.Length; while (index < len) { byte[] temp = new byte[len - index]; Buffer.BlockCopy(data.Buffer, index, temp, 0, len - index); TX300 x300 = new TX300(); x300.Content = temp; x300.package_to_msg(); // 保存历史记录 //SaveTX300History(x300, data.ReceiveTime); // 更新设备状态 HandleTX300Status(x300, data); if (null != _server) { if (x300.CommandID == 0xCC00) { byte[] cc00 = new byte[23]; Buffer.BlockCopy(x300.Content, 0, cc00, 0, 17); cc00[0] = 0x17; cc00[7] = 0xFF; cc00[8] = 0xFF; cc00[17] = 0x07; WbsDateTime dt = new WbsDateTime(DateTime.Now); Buffer.BlockCopy(dt.DateTimeToByte, 0, cc00, 19, 4); dt = null; if (data.PackageType == AsyncDataPackageType.TCP) { _server.Send(data.SocketHandle, cc00); } else { _server.Send(data.Port, data.IP, cc00); } cc00 = null; } else { // 返回回执 TX300Resp resp = new TX300Resp(); resp.CommandID = x300.CommandID; resp.PackageID = x300.PackageID; resp.SequenceID = x300.SequenceID; resp.Status = 0; resp.Package(); if (data.PackageType == AsyncDataPackageType.TCP) { _server.Send(data.SocketHandle, resp.Content); } else { _server.Send(data.Port, data.IP, resp.Content); } } } HandleCommandResponsed(x300); index += x300.TotalLength; } }
/// <summary> /// 处理接收到的数据 /// </summary> /// <param name="data"></param> private void HandleReceivedData(AsyncUserDataBuffer data) { int index = 0, len = data.Buffer.Length; while (index < len) { byte[] temp = new byte[len - index]; Buffer.BlockCopy(data.Buffer, index, temp, 0, len - index); TX300 x300 = new TX300(); x300.Content = temp; temp = null; x300.package_to_msg(); // 保存历史记录 //SaveTX300History(x300, data.ReceiveTime); // 更新设备状态 HandleTX300Status(x300, data); // 处理命令回复 if (x300.CommandID != 0xBB0F) { // 不要处理0xBB0F的命令返回数据 HandleGsmCommandResponsed(x300); } // 是否需要返回包 bool isNeedResponse = false; // SMS消息不需要返回包 if (x300.ProtocolType > Protocol.ProtocolTypes.SMS) { if (x300.CommandID == 0xCC00 || x300.CommandID == 0xBB0F) { isNeedResponse = true; } } else { isNeedResponse = true; } if (null != _server && isNeedResponse) { if (x300.CommandID == 0xCC00) { byte[] cc00 = Get0xCC00(x300); var ret = 0; if (data.PackageType == AsyncDataPackageType.TCP) { ret = _server.Send(data.SocketHandle, cc00); } else { ret = _server.Send(Port, IP, cc00); } if (1 != ret) { ShowUnhandledMessage(format("{0}Cannot send data to ip:{1}({2}): {3} [{4}]", Now, IP, Port, CustomConvert.GetHex(cc00), data.PackageType)); } cc00 = null; } else if (x300.CommandID == 0xBB0F) { byte smsType = byte.Parse(ConfigurationManager.AppSettings["SMS_SUBMIT_TYPE"]); // 0xBB0F命令需要服务器发送时 // 2600200A0FBB1001000000000000000101383938363030363631353039313130303136343302 if (smsType == SMSUtility.SUBMIT_BY_DB) { string sender = x300.TerminalID; // 170020140FBB10FFFF0139535986930101089001495000 string cmd = "170020" + CustomConvert.GetHex(x300.TerminalType) + "0FBB10FFFF0" + sender + "01010" + sender; using (var bll = new TerminalBLL()) { var terminal = bll.Find(f => f.Sim.Equals(sender)); using (var cbll = new CommandBLL()) { var obj = cbll.GetObject(); obj.DestinationNo = sender; obj.Status = (byte)CommandStatus.Waiting; obj.Content = cmd; obj.Terminal = null == terminal ? (int?)null : terminal.id; obj.SendUser = null; obj = cbll.Add(obj); } } } } else { // 返回回执 TX300Resp resp = new TX300Resp(); resp.CommandID = x300.CommandID; resp.PackageID = x300.PackageID; resp.SequenceID = x300.SequenceID; resp.Status = 0; resp.Package(); var ret = 0; if (data.PackageType == AsyncDataPackageType.TCP) { ret = _server.Send(data.SocketHandle, resp.Content); } else { ret = _server.Send(Port, IP, resp.Content); } if (1 != ret) { ShowUnhandledMessage(format("{0}Cannot send data to {1}:{2}: {3} [{4}]", Now, IP, Port, CustomConvert.GetHex(resp.Content), data.PackageType)); } } } index += x300.TotalLength; x300 = null; } }