/// <summary>
        /// 检测需要用铱星方式发送的命令
        /// </summary>
        public void CheckIridiumCommand()
        {
            if (null == _server)
            {
                return;
            }

            using (var bll = new CommandBLL())
            {
                var list = bll.FindList(f => f.ScheduleTime >= DateTime.Now.AddSeconds(-30) &&
                                        f.Status == (byte)CommandStatus.WaitingForSatellite && f.ActualSendTime == (DateTime?)null).ToList();
                if (null != list && list.Count > 0)
                {
                    HandleIridiumCommand(list.First(), bll);
                }
            }
        }
Example #2
0
        /// <summary>
        /// 处理GSM命令回复状态
        /// </summary>
        /// <param name="tx300"></param>
        private void HandleGsmCommandResponsed(TX300 tx300)
        {
            using (var bll = new CommandBLL())
            {
                // 查找定期时间内发送的相同命令记录,直取最后一条命令
                var cmd = bll.FindList <TB_Command>(f => f.DestinationNo.Equals(tx300.TerminalID) &&
                                                    f.Command.Equals("0x" + CustomConvert.IntToDigit(tx300.CommandID, CustomConvert.HEX, 4)) &&
                                                    f.ScheduleTime >= DateTime.Now.AddMinutes(tx300.ProtocolType == Protocol.ProtocolTypes.SATELLITE ? -60 : -3),
                                                    "ScheduleTime", true).FirstOrDefault();

                if (null != cmd)
                {
                    bll.Update(f => f.id == cmd.id, act =>
                    {
                        act.Status = (byte)CommandStatus.Returned;
                    });
                }
                //bll.Update(f => f.DestinationNo == tx300.TerminalID &&
                //    f.Command == "0x" + CustomConvert.IntToDigit(tx300.CommandID, CustomConvert.HEX, 4) &&
                //    f.ScheduleTime >= DateTime.Now.AddMinutes(-3) //&&
                //    //GsmStatus.Skip(2).Contains(f.Status.Value),
                //    , act => { act.Status = (byte)CommandStatus.Returned; });
            }
        }
Example #3
0
        /// <summary>
        /// 查询命令历史记录
        /// </summary>
        /// <param name="security">是否查询保安命令</param>
        /// <returns></returns>
        private string HandleCommandHistoryRequest(bool security)
        {
            var ret = "[]";

            try
            {
                var id = ParseInt(Utility.Decrypt(data));
                using (var ebll = new EquipmentBLL())
                {
                    var obj = ebll.Find(f => f.id == id && f.Deleted == false);
                    if (null != obj)
                    {
                        // 终端不为空时才查询
                        if ((int?)null != obj.Terminal)
                        {
                            var start = DateTime.Parse(GetParamenter("start") + " 00:00:00");
                            var end   = DateTime.Parse(GetParamenter("end") + " 23:59:59");
                            var sim   = obj.TB_Terminal.Sim;
                            if (sim[0] == '8' && sim[1] == '9' && sim.Length < 11)
                            {
                                sim += "000";
                            }
                            // 查询的命令
                            //string _command = "";
                            Command command = null;
                            if (!string.IsNullOrEmpty(cmd))
                            {
                                command = CommandUtility.GetCommand(cmd);
                                //_command = command.Code;
                            }
                            using (var bll = new CommandBLL())
                            {
                                var list = bll.FindList <TB_Command>(f => f.DestinationNo.Equals(sim) &&
                                                                     f.ScheduleTime >= start && f.ScheduleTime <= end && f.Command != "0xBB0F", "ScheduleTime", true);
                                if (security)
                                {
                                    list = list.Where(w => w.Command == "0x6007" || w.Command == "0x4000" || w.Command == "0x3000" || w.Command == "0xDD02");
                                }
                                else
                                {
                                    list = list.Where(w => w.Command != "0x6007" && w.Command != "0x4000" && w.Command != "0x3000" && w.Command != "0xDD02");
                                }
                                //&&
                                //    (string.IsNullOrEmpty(_command) ? f.u_sms_command.IndexOf("0x") >= 0 : f.u_sms_command.IndexOf(_command) >= 0),
                                //    "u_sms_schedule_time", true);
                                if (null != command)
                                {
                                    list = list.Where(w => w.Command.IndexOf(command.Code) >= 0);
                                    if (security && command.Code.Equals("6007"))
                                    {
                                        list = list.Where(w => w.Content.Substring(w.Content.Length - 2) == command.Param);
                                    }
                                    else if (security && command.Code.Equals("3000"))
                                    {
                                        list = list.Where(w => w.Content.Substring(w.Content.Length - 4, 2) == command.Param);
                                    }
                                    else if (security && command.Code.Equals("4000"))
                                    {
                                        list = list.Where(w => w.Content.Substring(w.Content.Length - 4, 2) == command.Param);
                                    }
                                }
                                if (list.Count() > 0)
                                {
                                    // 将command_id替换
                                    //List<Command> commands = CommandUtility.GetCommand();
                                    foreach (var record in list)
                                    {
                                        string param = "";
                                        if (record.Command == "0x6007")
                                        {
                                            param = record.Content.Substring(record.Content.Length - 2);
                                        }
                                        if (record.Command == "0x3000")
                                        {
                                            param = record.Content.Substring(record.Content.Length - 4, 2);
                                        }
                                        if (record.Command == "0x4000")
                                        {
                                            param = record.Content.Substring(record.Content.Length - 4, 2);
                                        }
                                        if (record.Command == "0xDD02")
                                        {
                                            param = record.Content.Substring(record.Content.Length - 8, 2);
                                        }
                                        Command _cmd   = CommandUtility.GetCommand(record.Command.Replace("0x", ""), param);
                                        var     func   = (EquipmentFunctional)obj.Functional;
                                        var     called = (func == EquipmentFunctional.Mechanical || func == EquipmentFunctional.Electric) ? "Equipment" : "Loader";
                                        record.Command = (null == _cmd ? "" : _cmd.Title.Replace("Equipment", called).Replace("Loader", called));
                                        // 加入命令发送者  2015/09/18 10:36
                                        record.Content = (int?)null == record.SendUser ? "Server" : record.TB_Account.Name;
                                    }
                                }
                                ret = JsonConverter.ToJson(list);
                            }
                        }
                        else
                        {
                            ret = ResponseMessage(-1, "No terminal bond with this equipment.");
                        }
                    }
                    else
                    {
                        ret = ResponseMessage(-1, "Equipment is not exist.");
                    }
                }
            }
            catch (Exception e)
            { ret = ResponseMessage(-1, "Handle History request error:" + e.Message); }
            return(ret);
        }
Example #4
0
        /// <summary>
        /// 检索是否有TCP在线终端的命令待发送。
        /// 条件:
        /// 1、预定发送时间在30s以内
        /// 2、命令的状态为等待发送状态(等待发送或等待重发)
        /// 3、终端为TCP链接状态的
        /// </summary>
        /// <returns></returns>
        public void CheckGSMCommand()
        {
            if (null == _server)
            {
                return;
            }
            var bll = new CommandBLL();

            try
            {
                // 取出当前30s内的新命令
                var list = bll.FindList(f => f.ScheduleTime >= DateTime.Now.AddSeconds(-30) &&
                                        GsmStatus.Take(2).Contains(f.Status.Value) &&
                                        f.TB_Terminal.OnlineStyle != (byte)LinkType.SATELLITE).ToList();
                foreach (var cmd in list)
                {
                    var sim = cmd.DestinationNo;
                    if (sim[0] == '8' || sim[0] == '9')
                    {
                        sim = sim.Substring(0, 8);
                    }
                    // 0==链接不存在1=发送成功2=网络处理错误3=强制SMS发送
                    byte          ret = 0;
                    CommandStatus cs  = (CommandStatus)cmd.Status;
                    // 所有GSM命令都强制SMS方式发送
                    if (cs == CommandStatus.WaitingForSMS)
                    {
                        // 强制SMS发送的
                        ret = 3;
                    }
                    else
                    {
                        if (cmd.TB_Terminal.OnlineStyle == (byte)LinkType.TCP)
                        {
                            // 0==链接不存在1=发送成功2=网络处理错误
                            ret = _server.Send(cmd.TB_Terminal.Socket.Value, CustomConvert.GetBytes(cmd.Content));
                        }
                    }
                    if (ret != 1)
                    {
                        // 非强制SMS发送时,先保存一下命令的发送状态为等待SMS发送状态
                        if (ret < 3)
                        {
                            UpdateGsmCommand(cmd, CommandStatus.WaitingForSMS, bll);
                            ShowUnhandledMessage(Now + "Send Command(TCP: Fail, force this command to SMS): " + cmd.Content);
                        }
                        else
                        {
                            // url直链方式发送短信时,直接可以发送
                            byte type = byte.Parse(ConfigurationManager.AppSettings["SMS_SUBMIT_TYPE"]);
                            if (type == SMSUtility.SUBMIT_BY_URL)
                            {
                                // TCP链接丢失,重新用SMS方式发送
                                bool b = CommandUtility.SendSMSCommand(cmd);
                                if (b)
                                {
                                    SaveTerminalData((int?)null == cmd.Terminal ? -1 : cmd.Terminal.Value, sim, AsyncDataPackageType.SMS, 1, false, DateTime.Now);
                                }
                                ShowUnhandledMessage(Now + "Send Command(SMS: " + (b ? "Success" : "Fail") + "): " + cmd.Content);
                            }
                        }
                    }
                    else
                    {
                        // 保存TCP方式的命令发送结果
                        SaveTerminalData((int?)null == cmd.Terminal ? -1 : cmd.Terminal.Value, sim, AsyncDataPackageType.TCP, cmd.Content.Length / 2, false, DateTime.Now);
                        ShowUnhandledMessage(Now + "Send command(" + (1 == ret ? CommandStatus.SentByTCP : CommandStatus.TCPNetworkError) + "): " + cmd.Content);
                        UpdateGsmCommand(cmd, (1 == ret ? CommandStatus.SentByTCP : CommandStatus.TCPNetworkError), bll);
                    }
                }
                // 待发送的命令发送完之后,清理超时的命令
                ClearTimedoutCommands(bll);
            }
            finally { bll.Close(); }
        }