Exemplo n.º 1
0
        /// <summary>
        ///  查询控制指令
        /// </summary>
        /// <param name="controlQueries"></param>
        public static XResponseMessage ProcessControlQueries(IEnumerable <ControlQuery> controlQueries)
        {
            var sw = new Stopwatch();

            sw.Start();
            if (controlQueries == null)
            {
                throw new ArgumentNullException("controlQueries");
            }
            var enumerable = controlQueries as ControlQuery[] ?? controlQueries.ToArray();

            if (enumerable.Any())
            {
                var facCodes = controlQueries.Select(que => que.FacilityCode);
                //var devCmds = _redis.GetVals<DeviceControlCommandDto>("deviceControlCommand", DataType.Protobuf).FindAll(cmd=>cmd.DeviceSerialnum.EqualIgnoreCase());
                var devControlCmds = facCodes.SelectMany(facCode =>
                                                         _deviceControlCommandService.GetDeviceControlCmdsByFacilityId(facCode));

                var cmds = new List <ControlCmd>();
                if (devControlCmds != null && devControlCmds.Count() > 0)
                {
                    devControlCmds.ForEach(async cmdDb =>
                    {
                        if (cmdDb.Status == (int)DeviceCommandTypeEnum.Created)
                        {
                            //if (cmdDb != null)
                            //{
                            var devControlConmand        = await _deviceControlCommandService.GetControlCommandByDeviceIdAsny(cmdDb.Serialnum);
                            devControlConmand.Status     = (int)DeviceCommandTypeEnum.Getted;             //接收到来自客户端的控制指令请求的时候,状态变为1
                            devControlConmand.UpdateTime = DateTime.Now;
                            await _deviceControlCommandService.AddControlCommandAsny(devControlConmand);  //添加控制指令
                                                                                                          //}

                            var cmd = new ControlCmd
                            {
                                Serialnum    = cmdDb.Serialnum,
                                FacilityCode = (await _deviceService.GetDeviceByIdAsny(cmdDb.DeviceSerialnum)).FacilitySerialnum,
                                DeviceCode   = cmdDb.DeviceSerialnum,
                                Command      = Convert.ToInt32(cmdDb.Command),
                                Time         = cmdDb.CreateTime,
                                ContinueTime = cmdDb.ControlContinueTime,
                            };
                            cmds.Add(cmd);
                        }
                    });
                }

                sw.Stop();
                ServiceLogger.Current.WriteDebugLog("处理控制指令花费的时间为:{0}", sw.ElapsedMilliseconds);
                return(ResultHelper.CreateMessage("", ErrorType.NoError, cmds));
            }
            return(ResultHelper.CreateMessage("无控制指令", ErrorType.NoError));
        }
Exemplo n.º 2
0
        public async Task <IEnumerable <ControlCmd> > ProcessAsync(IEnumerable <ControlQuery> controlQueries)
        {
            try
            {
                //var sw = new Stopwatch();
                //sw.Start();
                if (controlQueries == null)
                {
                    throw new ArgumentNullException(nameof(controlQueries));
                }
                var queries    = controlQueries as ControlQuery[] ?? controlQueries.ToArray();
                var enumerable = controlQueries as ControlQuery[] ?? queries.ToArray();
                if (!enumerable.Any())
                {
                    return(null);
                }
                var facCodes       = queries.Select(que => que.FacilityCode);
                var devControlCmds = new List <DeviceControlCommandDto>();
                var codes          = facCodes as string[] ?? facCodes.ToArray();
                if (codes.Any())
                {
                    codes.ToList().ForEach(facCode =>
                    {
                        var devCmd = _controlCommandService.GetDeviceControlCmdsByFacilityId(facCode);
                        if (devCmd != null && devCmd.Count > 0)
                        {
                            devControlCmds.AddRange(devCmd);
                        }
                    });
                }
                //var devControlCmds =
                //    facCodes.SelectMany(facCode => _controlCommandService.GetDeviceControlCmdsByFacilityId(facCode).ToList());
                var cmds = new List <ControlCmd>();
                if (!devControlCmds.Any() || devControlCmds.Count <= 0)
                {
                    return(null);
                }
                devControlCmds.ForEach(async cmdDb =>
                {
                    if (cmdDb.Status != (int)DeviceCommandTypeEnum.Created)
                    {
                        return;
                    }
                    var devControlConmand =
                        await _controlCommandService.GetDeviceControlCmdByIdAsny(cmdDb.Serialnum);
                    devControlConmand.Status = (int)DeviceCommandTypeEnum.Getted;
                    //接收到来自客户端的控制指令请求的时候,状态变为1
                    devControlConmand.UpdateTime = DateTime.Now;
                    var result = _controlCommandService.UpdateControlCommandAsny(devControlConmand);
                    var device = await _deviceService.GetDeviceByIdAsny(cmdDb.DeviceSerialnum);
                    //根据设备编码获取控制设备
                    var cmd = new ControlCmd
                    {
                        Serialnum    = cmdDb.Serialnum,
                        FacilityCode = device.FacilitySerialnum,
                        DeviceCode   = cmdDb.DeviceSerialnum,
                        Command      = Convert.ToInt32(cmdDb.Command),
                        Time         = cmdDb.CreateTime,
                        ContinueTime = cmdDb.ControlContinueTime,
                    };
                    cmds.Add(cmd);
                    LogHelper.Info("[查询控制指令]设备{0}指令:{1}", cmd.DeviceCode, cmd.Command);
                });
                //sw.Stop();
                //ServiceLogger.Current.WriteDebugLog("处理控制指令花费的时间为:{0}", sw.ElapsedMilliseconds);
                return(cmds);
            }
            catch (Exception ex)
            {
                LogHelper.Error("查询控制指令出错,错误内容:{0}", ex.ToString());

                return(null);
            }
        }