Esempio n. 1
0
        public bool CanExecuteAction(DeviceAction action)
        {
            IDeviceCommand command = null;

            return(_commands.TryGet(c => c.Action == action, out command) &&
                   CanApplyCommand(command));
        }
Esempio n. 2
0
        public static void getResponseData(IAsyncResult ar)
        {
            try
            {
                //Socket clientSocket = (Socket)ar.AsyncState;
                object[] array        = (object[])ar.AsyncState;
                Socket   clientSocket = (Socket)array[0];
                clientSocket.EndSend(ar);
                byte[] byteData = new byte[1024];
                clientSocket.Receive(byteData);

                string strReceived = Encoding.UTF8.GetString(byteData);

                Array.Clear(byteData, 0, byteData.Length);
                int i = strReceived.IndexOf("\0");
                if (i > 0)
                {
                    string data = strReceived.Substring(0, i);
                    Debug.WriteLine(" Data => SP: " + data);
                    //todo here should deal with the received string
                    IDeviceCommand cmd = (IDeviceCommand)array[1];
                    cmd.callBack(data);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("getResponseData => " + e.Message);
            }
        }
Esempio n. 3
0
 void 查询电机状态(IPEndPoint ipEndPoint)
 {
     DeviceCommandManager.setCommandCallback(enumDeviceCommand.查询电机状态,
                                             (data) =>
     {
         Debug.WriteLine("电机状态 => " + data);
         IDeviceCommand idc = DeviceCommandManager.getDeivceCommand(enumDeviceCommand.查询电机状态);
         if (null != idc)
         {
             LightState ls = idc.parseResponse(data);
             if (null != ls)
             {
                 if (ls.State)
                 {
                     string log = "电机已经打开";
                     this.add_log(log);
                     Debug.WriteLine(log);
                     EngineService.last_effective_command = new command("open", "");
                 }
                 else
                 {
                     string log = "电机已经关闭";
                     this.add_log(log);
                     Debug.WriteLine(log);
                     EngineService.last_effective_command = new command("close", "");
                 }
             }
         }
     });
     DeviceCommandManager.executeCommand(enumDeviceCommand.查询电机状态, ipEndPoint);
 }
Esempio n. 4
0
 void 查询风扇状态(IPEndPoint ipEndPoint)
 {
     DeviceCommandManager.setCommandCallback(enumDeviceCommand.查询风扇状态,
                                             (data) =>
     {
         Debug.WriteLine("风扇状态 => " + data);
         IDeviceCommand idc = DeviceCommandManager.getDeivceCommand(enumDeviceCommand.查询风扇状态);
         if (null != idc)
         {
             LightState ls = idc.parseResponse(data);
             if (null != ls)
             {
                 if (ls.State)
                 {
                     string log = "风扇已经打开";
                     this.add_log(log);
                     Debug.WriteLine(log);
                     //FanService.last_effective_command = new command("open", "");
                     command cmd      = new command(stateName.打开, "");
                     cmd.TargetDevice = TargetDeiveName.电风扇;
                     cmd.Initializing = "true";
                     this.sendInitialInfo(ws, cmd);
                 }
                 else
                 {
                     string log = "风扇已经关闭";
                     this.add_log(log);
                     Debug.WriteLine(log);
                     FanService.last_effective_command = new command("close", "");
                 }
             }
         }
     });
     DeviceCommandManager.executeCommand(enumDeviceCommand.查询风扇状态, ipEndPoint);
 }
Esempio n. 5
0
        void 检查风扇状态(IPEndPoint ipEndPoint)
        {
            DeviceCommandManager.setCommandCallback(enumDeviceCommand.查询风扇状态,
                                                    (data) =>
            {
                Debug.WriteLine("风扇状态 => " + data);
                IDeviceCommand idc = DeviceCommandManager.getDeivceCommand(enumDeviceCommand.查询风扇状态);
                if (null != idc)
                {
                    LightState ls = idc.parseResponse(data);
                    if (null != ls)
                    {
                        switch (ls.State)
                        {
                        case true:
                            if (myCommand != null)
                            {
                                myCommand.Name = stateName.打开;
                            }
                            this.invokeCallback(myCommand);
                            break;

                        case false:
                            if (myCommand != null)
                            {
                                myCommand.Name = stateName.关闭;
                            }
                            this.invokeCallback(myCommand);
                            break;
                        }
                    }
                }
            });
            DeviceCommandManager.executeCommand(enumDeviceCommand.查询风扇状态, ipEndPoint, 1000);
        }
Esempio n. 6
0
        protected override byte[] ParseSend(IDeviceCommand command)
        {
            var request = command.CommandStartChar + command.RequestCommand + command.CommandEndChar +
                          CalculateLrc(command.RequestCommand + command.CommandEndChar);
            var data = Encoding.ASCII.GetBytes(request);

            return(data);
        }
Esempio n. 7
0
        void 检查电机状态(IPEndPoint ipEndPoint)
        {
            DeviceCommandManager.setCommandCallback(enumDeviceCommand.查询电机状态,
                                                    (data) =>
            {
                Debug.WriteLine("黄灯状态 => " + data);
                IDeviceCommand idc = DeviceCommandManager.getDeivceCommand(enumDeviceCommand.查询电机状态);
                if (null != idc)
                {
                    LightState ls = idc.parseResponse(data);
                    if (null != ls && last_command != null)
                    {
                        bool temp = (last_command.Name == "open") ? true : false;
                        if (ls.State == temp)   //灯现在的状态和操作目标状态相同,同为关或者开
                        {
                            switch (last_command.Name)
                            {
                            case "open":
                                Debug.WriteLine(string.Format("{0} 打开电机", last_command.Commander));
                                command ncOpen         = new command("open", string.Format("{0}打开了电机", last_command.Commander));
                                last_effective_command = ncOpen;
                                recently_broadcast     = JsonConvert.SerializeObject(ncOpen);
                                this.Broadcast(recently_broadcast);
                                break;

                            case "close":
                                Debug.WriteLine(string.Format("{0} 关闭电机", last_command.Commander));
                                command ncClose        = new command("close", string.Format("{0}关闭了电机", last_command.Commander));
                                last_effective_command = ncClose;
                                recently_broadcast     = JsonConvert.SerializeObject(ncClose);
                                this.Broadcast(recently_broadcast);
                                break;
                            }
                        }
                        else   //说明设备没有响应,通知客户端
                        {
                            switch (ls.State)
                            {
                            case true:
                                command ncOpen     = new command("open", "操作失败");
                                recently_broadcast = JsonConvert.SerializeObject(ncOpen);
                                this.Send(recently_broadcast);       //只通知本人
                                Debug.WriteLine("关闭电机操作失败");
                                break;

                            case false:
                                command ncClose    = new command("close", "操作失败");
                                recently_broadcast = JsonConvert.SerializeObject(ncClose);
                                this.Send(recently_broadcast);
                                Debug.WriteLine("打开电机操作失败");
                                break;
                            }
                        }
                    }
                }
            });
            DeviceCommandManager.executeCommand(enumDeviceCommand.查询电机状态, ipEndPoint, 1000);
        }
Esempio n. 8
0
        void ¼ì²éµÆ״̬(IPEndPoint ipEndPoint)
        {
            DeviceCommandManager.setCommandCallback(enumDeviceCommand.²éѯ»ÆµÆ״̬,
                                                    (data) =>
            {
                Debug.WriteLine("»ÆµÆ״̬ => " + data);
                IDeviceCommand idc = DeviceCommandManager.getDeivceCommand(enumDeviceCommand.²éѯ»ÆµÆ״̬);
                if (null != idc)
                {
                    LightState ls = idc.parseResponse(data);
                    if (null != ls && last_command != null)
                    {
                        bool temp = (last_command.Name == "open") ? true : false;
                        if (ls.State == temp)   //µÆÏÖÔÚµÄ״̬ºÍ²Ù×÷Ä¿±ê״̬Ïàͬ£¬Í¬Îª¹Ø»òÕß¿ª
                        {
                            switch (last_command.Name)
                            {
                            case "open":
                                Debug.WriteLine(string.Format("{0} ´ò¿ª»ÆµÆ", last_command.Commander));
                                command ncOpen         = new command("open", string.Format("{0}´ò¿ªÁ˵Æ", last_command.Commander));
                                last_effective_command = ncOpen;
                                recently_broadcast     = JsonConvert.SerializeObject(ncOpen);
                                this.Broadcast(recently_broadcast);
                                break;

                            case "close":
                                Debug.WriteLine(string.Format("{0} ¹Ø±Õ»ÆµÆ", last_command.Commander));
                                command ncClose        = new command("close", string.Format("{0}¹Ø±ÕÁ˵Æ", last_command.Commander));
                                last_effective_command = ncClose;
                                recently_broadcast     = JsonConvert.SerializeObject(ncClose);
                                this.Broadcast(recently_broadcast);
                                break;
                            }
                        }
                        else   //˵Ã÷É豸ûÓÐÏìÓ¦£¬Í¨Öª¿Í»§¶Ë
                        {
                            switch (ls.State)
                            {
                            case true:
                                command ncOpen     = new command("open", "²Ù×÷ʧ°Ü");
                                recently_broadcast = JsonConvert.SerializeObject(ncOpen);
                                this.Send(recently_broadcast);       //ֻ֪ͨ±¾ÈË
                                Debug.WriteLine("¹Ø±Õ»ÆµÆ²Ù×÷ʧ°Ü");
                                break;

                            case false:
                                command ncClose    = new command("close", "²Ù×÷ʧ°Ü");
                                recently_broadcast = JsonConvert.SerializeObject(ncClose);
                                this.Send(recently_broadcast);
                                Debug.WriteLine("´ò¿ª»ÆµÆ²Ù×÷ʧ°Ü");
                                break;
                            }
                        }
                    }
                }
            });
            DeviceCommandManager.executeCommand(enumDeviceCommand.²éѯ»ÆµÆ״̬, ipEndPoint, 1000);
        }
Esempio n. 9
0
        public static IDeviceCommand getDeivceCommand(enumDeviceCommand name)
        {
            IDeviceCommand idc = DeviceCommandList.Find((cmd) =>
            {
                return(cmd.Name == name.ToString());
            });

            return(idc);
        }
Esempio n. 10
0
        public static IDeviceCommand getDeivceCommandWithResponseOf(string res)
        {
            IDeviceCommand idc = DeviceCommandList.Find((cmd) =>
            {
                return(cmd.itsMyResponsibility(res));
            });

            return(idc);
        }
Esempio n. 11
0
        public static void executeCommand(enumDeviceCommand name, IPEndPoint ipEndPoint)
        {
            IDeviceCommand cmd = null;

            cmd = getDeivceCommand(name);
            if (cmd != null)
            {
                cmd.sendCommand(ipEndPoint);
            }
        }
Esempio n. 12
0
        public static void setCommandCallback(enumDeviceCommand name, Action <string> _callback)
        {
            IDeviceCommand cmd = null;

            cmd = getDeivceCommand(name);
            if (cmd != null)
            {
                cmd.Callback = _callback;
            }
        }
Esempio n. 13
0
        public static void sendCommand(IDeviceCommand cmd, IPEndPoint ipEndPoint)
        {
            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            string strcmd       = cmd.getCmd();

            Debug.WriteLine("=> " + strcmd);
            byte[] _byteData = Encoding.UTF8.GetBytes(strcmd);

            clientSocket.BeginSendTo(_byteData, 0, _byteData.Length, SocketFlags.None,
                                     ipEndPoint, null, null);
            //clientSocket.BeginSendTo(_byteData, 0, _byteData.Length, SocketFlags.None,
            //    ipEndPoint, getResponseData, new object[] { clientSocket, cmd });
        }
Esempio n. 14
0
 public async Task <bool> TryExecuteCommand(IDeviceCommand deviceCommand, IRuntimeDevice runtimeDevice)
 {
     deviceCommand.IsCommandStartSending = true;
     deviceCommand.CurrectCommandStateChanged?.Invoke();
     _dataWritingService.SetDevice(runtimeDevice);
     if (!await _dataWritingService.WriteValues(deviceCommand.EntityMetadata, deviceCommand.CommandTags,
                                                deviceCommand.CommandValues))
     {
         return(false);
     }
     deviceCommand.IsCommandSent = true;
     deviceCommand.CurrectCommandStateChanged?.Invoke();
     return(true);
 }
Esempio n. 15
0
        public IDeviceCommand CreateStopAllStartersCommand(IRuntimeDevice runtimeDevice)
        {
            var           starters   = runtimeDevice.StartersOnDevice;
            List <bool>   boolValues = new List <bool>();
            List <string> tags       = new List <string>();

            starters.ForEach((starter =>
            {
                tags.Add(DeviceStringKeys.DeviceCommandsTagKeys.COMMAND_SWITCHING_STARTER + starter.ChannelNumber);
                boolValues.Add(false);
            }));
            IDeviceCommand command = CreateLightingCommandByTagsAndValues(tags, boolValues, GetLightingEntityMetadata(runtimeDevice), CommandTypesEnum.OFF, starters);

            return(command);
        }
Esempio n. 16
0
        public static void matchCommand(CommandMatch _command)
        {
            if (null == _command)
            {
                return;
            }
            IDeviceCommand idc = DeviceCommandList.Find((_temp) =>
            {
                return(_temp.getCmd() == _command.cmd);
            });

            if (null != idc)
            {
                idc.Name = _command.name;
            }
        }
Esempio n. 17
0
 public static void matchCommand(List <CommandMatch> _commandList)
 {
     if (null == _commandList)
     {
         return;
     }
     //根据设备协议匹配名字
     foreach (CommandMatch cmd in _commandList)
     {
         IDeviceCommand idc = DeviceCommandList.Find((_temp) =>
         {
             return(_temp.getCmd() == cmd.cmd);
         });
         if (null != idc)
         {
             idc.Name = cmd.name;
         }
     }
 }
Esempio n. 18
0
        public IDeviceCommand CreateStopEveninglightingCommand(IRuntimeDevice runtimeDevice)
        {
            var starters = runtimeDevice.StartersOnDevice.Where((starter =>
                                                                 starter.StarterLightingMode == LightingModeEnum.ECONOMY_NIGHTLIGHTING));
            List <bool>   boolValues = new List <bool>();
            List <string> tags       = new List <string>();

            starters.ForEach((starter =>
            {
                tags.Add(DeviceStringKeys.DeviceCommandsTagKeys.COMMAND_MANUALMODE_STARTER + starter.ChannelNumber);
                boolValues.Add(true);
                tags.Add(DeviceStringKeys.DeviceCommandsTagKeys.COMMAND_SWITCHING_STARTER + starter.ChannelNumber);
                boolValues.Add(false);
            }));

            IDeviceCommand command = CreateLightingCommandByTagsAndValues(tags, boolValues, GetLightingEntityMetadata(runtimeDevice), CommandTypesEnum.OFF, starters);

            return(command);
        }
Esempio n. 19
0
 public CommandResponse Send(IDeviceCommand command, CancellationToken cancellationToken)
 {
     lock (_sendLock)
     {
         var nextCommand = (int)(Math.Max(0, (CommandIntervalMs - (DateTime.UtcNow - _lastCommand).TotalMilliseconds)));
         _currentCommand = command;
         _commandInterval.WaitOne(nextCommand);
         _currentCommand.SendTime = DateTime.UtcNow;
         _channel.Send(ParseSend(_currentCommand));
         _lastCommand = DateTime.UtcNow;
         var isSignalled = _commandWait.WaitOne(CommandTimeout, cancellationToken);
         if (isSignalled)
         {
             _currentCommand.ReceiveTime = DateTime.UtcNow;
             var commandResponse = _currentCommand.CommandResponse;
             commandResponse.ReceiveTime = _currentCommand.ReceiveTime;
             _currentCommand             = null;
             return(commandResponse);
         }
         Trace.TraceError("TIMEOUT");
         _currentCommand = null;
         return(null);
     }
 }
Esempio n. 20
0
        void parseCommand(string strReceived)
        {
            if (strReceived.Length <= 0 || (strReceived.Length % 2) != 0)
            {
                return;
            }
            for (int i = 0; i < strReceived.Length; i += 2)
            {
                string         temp = strReceived.Substring(0, i + 2);
                IDeviceCommand cmd  = DeviceCommandManager.getDeivceCommandWithResponseOf(temp);
                if (cmd != null)
                {
                    Debug.WriteLine("parseCommand ok  => " + temp);
                    cmd.callBack(temp);

                    builder.Remove(0, i + 2);
                    parseCommand(strReceived.Substring(i + 1));
                }
                else
                {
                    Debug.WriteLine("parseCommand null  => " + temp);
                }
            }
        }
Esempio n. 21
0
 public async Task SendCommand(IDeviceCommand command)
 {
     var message = new Message(Encoding.ASCII.GetBytes(command.ToMessageJson()));
     await serviceClient.SendAsync(command.DeviceId, message);
 }
Esempio n. 22
0
 public void SetCommands(IDeviceCommand command)
 {
     command.SetCreateCommand <string, Uri>(.)
     ;
     command.SetParseCommand(.);
 }
Esempio n. 23
0
 private bool CanApplyCommand(IDeviceCommand command)
 {
     return(_states.CanFire(command.Action) && command.CanExecute(_device));
 }
Esempio n. 24
0
        public static void sendCommand(IDeviceCommand cmd, IPEndPoint ipEndPoint)
        {
            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            string strcmd = cmd.getCmd();
            Debug.WriteLine("=> " + strcmd);
            byte[] _byteData = Encoding.UTF8.GetBytes(strcmd);

            clientSocket.BeginSendTo(_byteData, 0, _byteData.Length, SocketFlags.None,
                            ipEndPoint, null, null);
            //clientSocket.BeginSendTo(_byteData, 0, _byteData.Length, SocketFlags.None,
            //    ipEndPoint, getResponseData, new object[] { clientSocket, cmd });
        }
Esempio n. 25
0
 protected abstract byte[] ParseSend(IDeviceCommand command);