public override async Task <bool> PushCommand(ExecuteCommand execCommand)
        {
            bool result = false;

            try
            {
                var device = execCommand?.Command?.Device;

                if (device != null)
                {
                    var wsChannelId = GetActiveChannelName(execCommand.SourceChannelId, device.NodeId);

                    if (!string.IsNullOrEmpty(wsChannelId))
                    {
                        var start = MsgLogger.BeginTimeMeasure();

                        if (ConnectionManager != null && ConnectionManager.IsConnected(wsChannelId))
                        {
                            if (await ConnectionManager.Send(wsChannelId, _userIdentity, execCommand))
                            {
                                result = true;
                            }
                        }

                        MsgLogger.EndTimeMeasure($"{GetType().Name} - PushCommand", start, $"push command='{execCommand.Command.Name}' to device='{device.Family}':0x{device.NodeId}");
                    }
                }
            }
            catch (Exception e)
            {
                MsgLogger.Exception($"{GetType().Name} - PushCommand", e);
            }

            return(result);
        }
        public override async Task <bool> PushCommand(ExecuteCommand execCommand)
        {
            bool result = false;

            try
            {
                var device = execCommand?.Command?.Device;

                if (device != null)
                {
                    var start = MsgLogger.BeginTimeMeasure();

                    if (device.ChannelLocalHost != EltraUdpConnector.LocalHost)
                    {
#if _UDP
                        var udpConnection = ConnectionManager.GetConnection <UdpClientConnection>(CommandExecUuid);

                        if (udpConnection == null)
                        {
                            var udpServerConnection = new UdpClientConnection()
                            {
                                ChannelName = "ExecuteCommander",
                                UniqueId    = CommandExecUuid,
                                Url         = device.ChannelLocalHost
                            };

                            await ConnectionManager.Connect(udpServerConnection);
                        }
                        else if (udpConnection.Url != device.ChannelLocalHost)
                        {
                            udpConnection.Url = device.ChannelLocalHost;

                            await udpConnection.Connect();
                        }
#endif
                    }

                    if (ConnectionManager != null && ConnectionManager.IsConnected(CommandExecUuid))
                    {
                        if (await ConnectionManager.Send(CommandExecUuid, UserIdentity, execCommand))
                        {
                            result = true;
                        }
                    }

                    MsgLogger.EndTimeMeasure($"{GetType().Name} - PushCommand", start, $"push command='{execCommand.Command.Name}' to device='{device.Family}':0x{device.NodeId}");
                }
            }
            catch (Exception e)
            {
                MsgLogger.Exception($"{GetType().Name} - PushCommand", e);
            }

            return(result);
        }
예제 #3
0
        public async Task <int> ExecuteCommands(List <ExecuteCommand> executeCommands)
        {
            int result = 0;

            var start = MsgLogger.BeginTimeMeasure();

            try
            {
                int executedCommandCount = 0;

                foreach (var executeCommand in executeCommands)
                {
                    var device = FindDevice(executeCommand.NodeId);

                    if (device != null)
                    {
                        if (await ExecuteCommand(device, executeCommand))
                        {
                            executedCommandCount++;
                        }
                    }
                    else
                    {
                        MsgLogger.WriteError($"{GetType().Name} - ExecuteCommands", $"device with node id = {executeCommand.NodeId} not found!");
                    }
                }

                result = executedCommandCount;
            }
            catch (Exception e)
            {
                MsgLogger.Exception($"{GetType().Name} - ExecuteCommands", e);
            }

            MsgLogger.EndTimeMeasure($"{GetType().Name} - ExecuteCommands", start, $"commands executed, count = {executeCommands.Count}");

            return(result);
        }
예제 #4
0
        private async Task <bool> ExecuteCommand(EltraDevice device, ExecuteCommand executeCommand)
        {
            bool result = false;

            try
            {
                if (executeCommand != null && _executeCommandCache.CanExecute(executeCommand))
                {
                    var s = MsgLogger.BeginTimeMeasure();

                    var sourceChannelId = executeCommand.SourceChannelId;
                    var commandName     = executeCommand.Command?.Name;

                    var deviceCommand = device.FindCommand(commandName);

                    if (deviceCommand != null)
                    {
                        if (await DeviceControllerAdapter.SetCommandStatus(executeCommand, ExecCommandStatus.Executing))
                        {
                            try
                            {
                                MsgLogger.WriteDebug($"{GetType().Name} - ExecuteCommand", $"Clone Command '{commandName}'");

                                var clonedDeviceCommand = deviceCommand.Clone();

                                if (clonedDeviceCommand != null)
                                {
                                    MsgLogger.WriteDebug($"{GetType().Name} - ExecuteCommand", $"Sync Command '{commandName}'");

                                    clonedDeviceCommand.Sync(executeCommand.Command);

                                    MsgLogger.WriteDebug($"{GetType().Name} - ExecuteCommand", $"Execute Command '{commandName}', channel '{executeCommand.SourceChannelId}'");

                                    try
                                    {
                                        var start = MsgLogger.BeginTimeMeasure();

                                        result = clonedDeviceCommand.Execute(executeCommand.SourceChannelId, executeCommand.SourceLoginName);

                                        MsgLogger.EndTimeMeasure($"{GetType().Name} - ExecuteCommand", start, $"command '{executeCommand.Command.Name}' executed, result = {result}");
                                    }
                                    catch (Exception e)
                                    {
                                        MsgLogger.Exception($"{GetType().Name} - ExecuteCommand", e);
                                    }

                                    if (result)
                                    {
                                        var start = MsgLogger.BeginTimeMeasure();

                                        MsgLogger.WriteDebug($"{GetType().Name} - ExecuteCommand", $"Sync Response Command '{commandName}'");

                                        executeCommand.Command?.Sync(clonedDeviceCommand);

                                        MsgLogger.WriteDebug($"{GetType().Name} - ExecuteCommand", $"Push Response for Command '{commandName}'");

                                        executeCommand.SourceChannelId = Channel.Id;
                                        executeCommand.TargetChannelId = sourceChannelId;

                                        result = await DeviceControllerAdapter.PushCommand(executeCommand, ExecCommandStatus.Executed);

                                        if (result)
                                        {
                                            MsgLogger.WriteDebug($"{GetType().Name} - ExecuteCommand", $"Command '{commandName}' successfully processed!");
                                        }
                                        else
                                        {
                                            MsgLogger.WriteError($"{GetType().Name} - ExecuteCommand", $"Set command '{commandName}' status to exectuted failed!");
                                        }

                                        MsgLogger.EndTimeMeasure($"{GetType().Name} - ExecuteCommand", start, $"command '{executeCommand.Command.Name}' state synchronized, result = {result}");
                                    }
                                    else
                                    {
                                        var command = executeCommand.Command;

                                        MsgLogger.WriteError($"{GetType().Name} - ExecuteCommand",
                                                             command != null
                                                ? $"Command '{command.Name}' uuid '{executeCommand.CommandId}' execution failed!"
                                                : $"Command '?' uuid '{executeCommand.CommandId}' execution failed!");

                                        executeCommand.SourceChannelId = Channel.Id;
                                        executeCommand.TargetChannelId = sourceChannelId;

                                        await DeviceControllerAdapter.SetCommandStatus(executeCommand,
                                                                                       ExecCommandStatus.Failed);
                                    }
                                }
                                else
                                {
                                    await DeviceControllerAdapter.SetCommandStatus(executeCommand, ExecCommandStatus.Failed);

                                    MsgLogger.WriteError($"{GetType().Name} - ExecuteCommand", $"Command '{commandName}' cloning failed!");
                                }
                            }
                            catch (Exception e)
                            {
                                await DeviceControllerAdapter.SetCommandStatus(executeCommand,
                                                                               ExecCommandStatus.Failed);

                                MsgLogger.Exception($"{GetType().Name} - ExecuteCommand", e);
                            }
                        }
                        else
                        {
                            MsgLogger.WriteError($"{GetType().Name} - ExecuteCommand", $"Set Command '{commandName}' status to executing failed!");
                        }
                    }
                    else
                    {
                        MsgLogger.WriteError($"{GetType().Name} - ExecuteCommand", $"Command '{commandName}' not found!");
                    }

                    MsgLogger.EndTimeMeasure($"{GetType().Name} - ExecuteCommand", s, $"command '{executeCommand.Command?.Name}' executed, result = {result}");
                }
            }
            catch (Exception e)
            {
                result = false;
                MsgLogger.Exception($"{GetType().Name} - ExecuteCommand", e);
            }

            return(result);
        }