Exemplo n.º 1
0
        /// <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];
                    }
                });
            }
        }
Exemplo n.º 2
0
        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)); }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 添加或查询相同device id的tracker
        /// </summary>
        /// <param name="device"></param>
        /// <returns>返回具有相同device id的tracker或新建一个tracker</returns>
        private TB_Tracker addTracker(string device, TrackerBLL bll)
        {
            if (string.IsNullOrEmpty(device))
            {
                return(null);
            }

            var tracker = bll.Find(f => f.DeviceId.Equals(device) && f.Deleted == false);

            if (null == tracker)
            {
                if (string.IsNullOrEmpty(TrackerNumberPrefix))
                {
                    TrackerNumberPrefix = ConfigurationManager.AppSettings["TRACKER_NUMBER_PREFIX"];
                }
                // 生成一个新的tracker并与当前账户绑定
                tracker = bll.FindList <TB_Tracker>(f => f.SimCard.StartsWith(TrackerNumberPrefix) && f.Deleted == false, "SimCard", true).FirstOrDefault();
                string number;
                if (null == tracker)
                {
                    number = TrackerNumberPrefix + "0000";
                }
                else
                {
                    var old = int.Parse(tracker.SimCard) + 1;
                    number = old.ToString();
                }
                tracker          = bll.GetObject();
                tracker.SimCard  = number;
                tracker.DeviceId = device;
                tracker          = bll.Add(tracker);
                // 保存tracker绑定历史记录
                SaveHistory(new TB_AccountHistory()
                {
                    Account  = null,
                    ActionId = new ActionBLL().Find(f => f.Name.Equals("AddNewTracker")).id,
                    ObjectA  = string.Format("tracker: {0}, device: {1}", tracker.SimCard, tracker.DeviceId)
                });
                return(tracker);
            }
            return(tracker);
        }
Exemplo n.º 4
0
        /// <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;
                }
            }
        }
Exemplo n.º 5
0
        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);
                }
            }
        }