示例#1
0
        public async Task <bool> ExecuteCommands()
        {
            bool result = true;

            try
            {
                foreach (var deviceNode in SafeDevicesArray)
                {
                    var executeCommands = await DeviceControllerAdapter.PopCommands(deviceNode, ExecCommandStatus.Waiting);

                    foreach (var executeCommand in executeCommands)
                    {
                        if (!await ExecuteCommand(deviceNode, executeCommand))
                        {
                            result = false;
                        }
                    }

                    if (result)
                    {
                        result = DeviceControllerAdapter.Good;
                    }
                }
            }
            catch (Exception e)
            {
                MsgLogger.Exception($"{GetType().Name} - ExecuteCommands", e);
                result = false;
            }

            return(result);
        }
示例#2
0
        public async Task <bool> UnregisterDevice(EltraDevice device)
        {
            bool result = await DeviceControllerAdapter.UnregisterChannelDevice(device);

            if (result)
            {
                RemoveDevice(device);
            }

            return(result);
        }
示例#3
0
        public async Task <bool> RegisterDevice(EltraDevice device)
        {
            bool result = true;

            if (!await IsChannelRegistered())
            {
                if (await RegisterChannel())
                {
                    MsgLogger.WriteLine($"register channel='{Channel.Id}' success");
                }
                else
                {
                    MsgLogger.WriteError($"{GetType().Name} - RegisterDevice", $"register channel='{Channel.Id}' failed!");

                    result = false;
                }
            }

            if (result)
            {
                if (!await IsDeviceRegistered(device))
                {
                    result = await DeviceControllerAdapter.RegisterDevice(device);
                }
                else
                {
                    result = await DeviceControllerAdapter.UpdateDevice(device);
                }
            }

            if (result)
            {
                if (FindDevice(device.NodeId) == null)
                {
                    AddDevice(device);
                }
            }

            return(result);
        }
示例#4
0
        private async Task <bool> AnyDeviceUnRegistered()
        {
            bool result = false;

            try
            {
                foreach (var deviceNode in SafeDevicesArray)
                {
                    var status = await DeviceControllerAdapter.GetDeviceStatus(deviceNode);

                    if (status != DeviceStatus.Registered && status != DeviceStatus.Ready)
                    {
                        result = true;
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                MsgLogger.Exception($"{GetType().Name} - AnyDeviceUnRegistered", e);
            }

            return(result);
        }
示例#5
0
        public async Task <bool> IsDeviceRegistered(EltraDevice device)
        {
            var status = await DeviceControllerAdapter.GetDeviceStatus(device);

            return(status == DeviceStatus.Registered || status == DeviceStatus.Ready);
        }
示例#6
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);
        }
示例#7
0
        public override async Task <bool> Update()
        {
            bool result = false;

            try
            {
                MsgLogger.WriteDebug($"{GetType().Name} - Update", $"Is channel='{Channel.Id}' registered...");

                if (await IsChannelRegistered())
                {
                    MsgLogger.WriteDebug($"{GetType().Name} - Update", $"Is any channel='{Channel.Id}' device not registered...");

                    if (await AnyDeviceUnRegistered())
                    {
                        MsgLogger.WriteLine($"re-register channel='{Channel.Id}' devices");

                        if (!await DeviceControllerAdapter.RegisterDevices())
                        {
                            MsgLogger.WriteError($"{GetType().Name} - Update", $"register channel='{Channel.Id}' devices failed!");
                        }
                    }

                    MsgLogger.Write($"{GetType().Name} - Update", $"Updating channel='{Channel.Id}' status...");

                    result = await SetChannelStatus(ChannelStatus.Online);
                }
                else
                {
                    MsgLogger.WriteLine($"Registering channel='{Channel.Id}' ...");

                    if (await RegisterChannel())
                    {
                        MsgLogger.Write($"{GetType().Name} - Update", $"updating channel='{Channel.Id}' status ...");

                        result = await SetChannelStatus(ChannelStatus.Online);

                        if (result)
                        {
                            MsgLogger.WriteLine($"update channel='{Channel.Id}' status success");
                        }
                        else
                        {
                            MsgLogger.WriteError($"{GetType().Name} - Update", $"update channel='{Channel.Id}' status failed!");
                        }
                    }
                    else
                    {
                        MsgLogger.WriteError($"{GetType().Name} - Update", $"register channel='{Channel.Id}' failed!");
                    }

                    if (result)
                    {
                        MsgLogger.WriteLine($"Registering devices='{Channel.Id}' ...");

                        result = await DeviceControllerAdapter.RegisterDevices();

                        if (result)
                        {
                            MsgLogger.WriteLine($"register channel='{Channel.Id}' devices success");
                        }
                        else
                        {
                            MsgLogger.WriteError($"{GetType().Name} - Update", $"register channel='{Channel.Id}' devices failed!");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MsgLogger.Exception($"{GetType().Name} - AnyDeviceUnRegistered", e);
            }

            return(result);
        }
示例#8
0
 private void RegisterDeviceEvents(DeviceControllerAdapter deviceControllerAdapter)
 {
     deviceControllerAdapter.RegistrationStateChanged += OnDeviceRegistrationStateChanged;
 }
示例#9
0
 internal Task <bool> UploadPayload(DeviceToolPayload payload)
 {
     return(DeviceControllerAdapter.UploadPayload(payload));
 }
示例#10
0
 internal Task <bool> PayloadExists(DeviceToolPayload payload)
 {
     return(DeviceControllerAdapter.PayloadExists(payload));
 }