/// <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> /// 更新在线时间和在线状态 /// </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; }); } } }
private void HandleGetParameter(Api obj) { try { var acnt = ParseJson <Account>(obj.content); if (null != acnt) { Account resp = new Account(); resp.md5 = MQTT_SERVER; var device = acnt.device; using (var bll = new TrackerBLL()) { var tracker = bll.Find(f => f.DeviceId.Equals(device) && f.Deleted == false); if (null == tracker) { tracker = addTracker(acnt.device, bll); resp.data = tracker.SimCard; // 新登记的tracker没有session resp.session = ""; } else { // 查找绑定用户的登录session var user = tracker.TB_Account.FirstOrDefault(); if (null == user) { // 没有绑定用户时session为空 resp.session = ""; resp.device = ""; } else { resp.name = user.Code; // 已绑定过的用户返回session resp.session = user.DeviceLoginId; // 返回用户所在区域 resp.device = user.Belong; } resp.data = tracker.SimCard; bll.Update(f => f.id == tracker.id, act => { act.LastActionAt = DateTime.Now; }); } // get parameter的时候只返回设备与sim卡之间的绑定关系,没有账户的信息 ResponseData(0, JsonConverter.ToJson(resp), true); } } else { ResponseData(-1, "Can not handle your get_parameter request with error data."); } } catch (Exception e) { ResponseData(-1, string.Format("Can not handle your get_parameter request: {0}", e.Message)); } }
/// <summary> /// 处理报警信息 /// </summary> /// <param name="obj"></param> private void Handle0x7020(TX300 obj, TB_Tracker tracker, TrackerBLL bll) { _0x7020 x7020 = new _0x7020(); x7020.Content = obj.MsgContent; x7020.Unpackage(); var alarm = x7020.AlarmBIN; if (null != tracker) { bll.Update(f => f.id == tracker.id, act => { act.CSQ = x7020.CSQ; if (alarm[0] == '1') { // 终端后备电池耗光报警 act.BatteryAlarm = tracker.LastActionAt; } else if (alarm[1] == '1') { // 停车超时报警 act.ParkingAlarm = tracker.LastActionAt; } else if (alarm[2] == '1') { // 充电接线断开报警 act.ChargingAlarm = tracker.LastActionAt; } else if (alarm[2] == '0') { // 充电接线链接报警 act.ChargingAlarm = null; } }); } string type = ""; if (obj.TerminalType == TerminalTypes.TX10GAPP) { // app 端报告的报警信息 type = ((TX10GAlarms)x7020.Alarm).ToString().Replace("TX10GAlarms", ""); } else { // tx10g 报告的报警信息 type = alarm[0] == '1' ? "Battery OFF" : (alarm[1] == '1' ? "Parking Timeout" : (alarm[2] == '1' ? "Charge OFF" : (alarm[2] == '0' ? "Charge ON" : "Unknown"))); } string provider = "gps"; if (obj.TerminalType == TerminalTypes.TX10GAPP) { // tx10g app 的 packageid 和 packagelength 两个字节可以当作 provider 来区分 provider = GetTrackerProvider(obj.PackageID); } if (x7020.Position.Available) { SaveTrackerPosition(obj.TerminalID, (null == tracker ? "" : tracker.CarNumber), (null == tracker ? -1 : tracker.id), provider, x7020.Position, type, tracker.LastActionAt.Value); } }
/// <summary> /// 处理旧的链接或长时间未动的客户端节点 /// </summary> public void HandleOlderClients() { LastOlderLinkHandledTime = DateTime.Now; try { // 清理旧链接的方式改为用存储过程 2015/10/01 22:30 using (var edc = new EverdigmDataContext()) { int?arm2sleep = 0, sleep2blind = 0, sms2sleep = 0, tcpudp2sms = 0, satellite = 0, terminals = 0; var ret = edc.sp_ClearOldLinks(DateTime.Now, ref arm2sleep, ref sleep2blind, ref sms2sleep, ref tcpudp2sms, ref satellite, ref terminals); if (arm2sleep > 0 || sleep2blind > 0 || sms2sleep > 0 || tcpudp2sms > 0 || satellite > 0 || terminals > 0) { ShowUnhandledMessage( format("{0}HandleOlderClients(ret: {1}): ARM to Sleep: {2}, Sleep to Blind: {3}, SMS to Sleep: {4}, TCP/UDP to SMS: {5}", Now, ret, arm2sleep, sleep2blind, sms2sleep, tcpudp2sms)); } } // 所有更新集合到一个事务中2015/08/14 // 更新设备状态的同时也更新终端的状态 2015/09/22 09:30 // 更新设备连接状态 //EquipmentInstance.Update(f => f.OnlineStyle > (byte)LinkType.OFF && f.OnlineTime < DateTime.Now.AddMinutes(-75), act => //{ // act.Socket = 0; // act.Voltage = "G0000"; // act.Rpm = 0; // act.Signal = 0; // // 长时间主电报警的睡眠转为Off // if (act.OnlineTime < DateTime.Now.AddMinutes(-4320) && act.OnlineStyle == (byte)LinkType.SLEEP && // (act.Alarm.Substring(0, 1) == "1" || act.Alarm.Substring(15, 1) == "1")) // { // act.OnlineStyle = (byte)LinkType.OFF; // if ((int?)null != act.Terminal) // act.TB_Terminal.OnlineStyle = (byte)LinkType.OFF; // } // // 长时间睡眠的转为盲区 // else if (act.OnlineTime < DateTime.Now.AddMinutes(-10800) && act.OnlineStyle < (byte)LinkType.BLIND) // { // act.OnlineStyle = (byte)LinkType.BLIND; // if ((int?)null != act.Terminal) // act.TB_Terminal.OnlineStyle = (byte)LinkType.BLIND; // } // // 长时间SMS的转为睡眠 // else if (act.OnlineTime < DateTime.Now.AddMinutes(-720) && act.OnlineStyle < (byte)LinkType.SLEEP) // { // act.OnlineStyle = (byte)LinkType.SLEEP; // if ((int?)null != act.Terminal) // act.TB_Terminal.OnlineStyle = (byte)LinkType.SLEEP; // } // // 长时间没有UDP、TCP信息的转为SMS // else if (act.OnlineStyle < (byte)LinkType.SMS) // { // act.OnlineStyle = (byte)LinkType.SMS; // if ((int?)null != act.Terminal) // act.TB_Terminal.OnlineStyle = (byte)LinkType.SMS; // } // // 卫星超过2小时没有数据的,Eng. On更新为Eng. Off // else if (act.OnlineTime < DateTime.Now.AddMinutes(-120) && act.OnlineStyle == (byte)LinkType.SATELLITE // && act.Voltage != "G0000") // { act.Voltage = "G0000"; } //}); } catch (Exception e) { ShowUnhandledMessage(format("{0}HandleOlderClients error: {1}{2}StackTrace: {3}", Now, e.Message, Environment.NewLine, e.StackTrace)); } // 更新终端连接状态 //TerminalInstance.Update(f => f.OnlineStyle > (byte)LinkType.OFF && f.OnlineTime < DateTime.Now.AddMinutes(-75), act => //{ // act.Socket = 0; // if (act.OnlineTime < DateTime.Now.AddMinutes(-10800) && act.OnlineStyle < (byte)LinkType.BLIND) // { act.OnlineStyle = (byte)LinkType.BLIND; } // else if (act.OnlineTime < DateTime.Now.AddMinutes(-720) && act.OnlineStyle < (byte)LinkType.SLEEP) // { act.OnlineStyle = (byte)LinkType.SLEEP; } // else if (act.OnlineStyle < (byte)LinkType.SMS) // { act.OnlineStyle = (byte)LinkType.SMS; } //}); // 2015-04-08 16:40 更新:不处理已经为OFF状态的列表 // 处理旧的TCP链接为SMS链接(1小时15分钟之前有TCP数据来的链接会被置为SMS) //EquipmentInstance.Update(f => f.OnlineStyle > (byte)LinkType.OFF && f.OnlineStyle < (byte)LinkType.SMS && // f.OnlineTime < DateTime.Now.AddMinutes(-75), act => // { // act.Socket = 0; // act.OnlineStyle = (byte)LinkType.SMS; // act.Voltage = "G0000"; // act.Rpm = 0; // }); // 处理终端连接 //TerminalInstance.Update(f => f.OnlineStyle > (byte)LinkType.OFF && f.OnlineStyle < (byte)LinkType.SMS && // f.OnlineTime < DateTime.Now.AddMinutes(-75), act => // { // act.Socket = 0; // act.OnlineStyle = (byte)LinkType.SMS; // }); // 清理TX10G的旧链接记录 using (var bll = new TrackerBLL()) { bll.Update(f => f.State > 0 && f.LastActionAt < DateTime.Now.AddMinutes(-15), act => { // 删除socket act.Socket = 0; // 状态标记为offline状态 act.State = 0; }); } // 处理旧的SMS连接为SLEEP状态(SMS链接超过12小时的) //EquipmentInstance.Update(f => f.OnlineStyle > (byte)LinkType.OFF && f.OnlineStyle < (byte)LinkType.SLEEP && // f.OnlineTime < DateTime.Now.AddMinutes(-720), act => // { // act.OnlineStyle = (byte)LinkType.SLEEP; // act.Signal = 0; // act.Voltage = "G0000"; // }); // 处理终端连接 //TerminalInstance.Update(f => f.OnlineStyle > (byte)LinkType.OFF && f.OnlineStyle < (byte)LinkType.SLEEP && // f.OnlineTime < DateTime.Now.AddMinutes(-720), act => // { // act.OnlineStyle = (byte)LinkType.SLEEP; // }); // 处理旧的SLEEP连接为盲区(SLEEP超过7天的) //EquipmentInstance.Update(f => f.OnlineStyle > (byte)LinkType.OFF && f.OnlineStyle < (byte)LinkType.BLIND && // f.OnlineTime < DateTime.Now.AddMinutes(-10080), act => // { // act.OnlineStyle = (byte)LinkType.BLIND; // act.Signal = 0; // act.Voltage = "G0000"; // }); }
private void BindAccountWithTracker(TB_Account account, string device, AccountBLL abll) { if (string.IsNullOrEmpty(device)) { ResponseData(-1, "Cannot bind your device with error parameter."); return; } using (var bll = new TrackerBLL()) { // 查找相同deviceid的tracker var tracker = bll.Find(f => f.Deleted == false && f.DeviceId.Equals(device)); if (null != tracker) { string uuid = Guid.NewGuid().ToString(); // 查找这个设备是否已经绑定到别人账户上 var cnt = tracker.TB_Account.Count; if (cnt > 0) { Account exist = new Account() { name = account.Code, data = account.TB_Tracker.SimCard, // 每次绑定账户都生成新的session id session = uuid }; var user = tracker.TB_Account.FirstOrDefault(f => f.id == account.id); if (null != user) { abll.Update(u => u.id == user.id, act => { act.DeviceLoginId = uuid; }); exist.device = user.Belong; // 返回当前已经登录过的用户信息 ResponseData(0, JsonConverter.ToJson(exist), true); } else { ResponseData(-1, "Your device is already bound with another tms account."); } } else { // 该设备还未绑定任何人员 // 更改account的tracker信息 abll.Update(f => f.id == account.id, act => { act.Tracker = tracker.id; act.DeviceLoginId = uuid; }); // 保存tracker绑定历史记录 SaveHistory(new TB_AccountHistory() { Account = account.id, ActionId = new ActionBLL().Find(f => f.Name.Equals("BindTracker")).id, ObjectA = string.Format("tracker: {0}, device: {1}, account: {2}", tracker.SimCard, tracker.DeviceId, account.Code) }); ResponseData(0, JsonConverter.ToJson(new Account() { name = account.Code, data = tracker.SimCard, device = account.Belong, // 每次绑定账户都生成新的session id session = uuid }), true); } // 更新在线时间 bll.Update(f => f.id == tracker.id, act => { act.LastActionAt = DateTime.Now; }); } else { tracker = addTracker(device, bll); string uuid = Guid.NewGuid().ToString(); abll.Update(u => u.id == account.id, act => { act.DeviceLoginId = uuid; }); ResponseData(0, JsonConverter.ToJson(new Account() { name = account.Code, data = tracker.SimCard, device = account.Belong, // 每次绑定账户都生成新的session id session = uuid }), true); } } }