public MqttDeviceEventArgs(string deviceDescription, Type deviceType,
                            MqttDeviceActions deviceAction, string message)
 {
     DeviceAction      = deviceAction;
     DeviceType        = deviceType;
     DeviceDescription = deviceDescription;
     Message           = message;
 }
Esempio n. 2
0
        private void ControlDeviceByMqttMessage(string deviceDescription, Type deviceType, MqttDeviceActions deviceAction, string message)
        {
            var device = (from x in AllDevices
                          where x.Description.Equals(deviceDescription) && x.GetType() == deviceType
                          select x).FirstOrDefault();

            if (device == null)
            {
                Logger.Fatal($"{deviceDescription} of type {deviceType.Name} could not be found in the configured device-collection");
                return;
            }

            switch (deviceAction)
            {
            case MqttDeviceActions.None:
                break;

            case MqttDeviceActions.Open:
                if (typeof(IShuttable).IsAssignableFrom(deviceType))
                {
                    var dev = device as IShuttable;
                    dev?.Open();
                }
                else
                {
                    Logger.Error($"{deviceDescription} does not implement {nameof(IShuttable)}");
                }
                break;

            case MqttDeviceActions.Close:
                if (typeof(IShuttable).IsAssignableFrom(deviceType))
                {
                    var dev = device as IShuttable;
                    dev?.Close();
                }
                else
                {
                    Logger.Error($"{deviceDescription} does not implement {nameof(IShuttable)}");
                }
                break;

            case MqttDeviceActions.Stop:
                if (typeof(IStoppable).IsAssignableFrom(deviceType))
                {
                    var dev = device as IStoppable;
                    dev?.Stop();
                }
                else
                {
                    Logger.Error($"{deviceDescription} does not implement {nameof(IStoppable)}");
                }
                break;

            case MqttDeviceActions.EnableDevice:
                if (typeof(ICanBeDisabled).IsAssignableFrom(deviceType))
                {
                    var dev = device as ICanBeDisabled;
                    if (dev != null)
                    {
                        dev.Disabled = false;
                        if (typeof(ISchedulable).IsAssignableFrom(deviceType))
                        {
                            var devSchedulable = dev as ISchedulable;
                            if (devSchedulable != null && devSchedulable.TimerEnabled)
                            {
                                _deviceScheduler.EnableJobsOfDevice(devSchedulable);
                            }
                        }
                    }
                }
                else
                {
                    Logger.Error($"{deviceDescription} does not implement {nameof(ICanBeDisabled)}");
                }
                break;

            case MqttDeviceActions.DisableDevice:
                if (typeof(ICanBeDisabled).IsAssignableFrom(deviceType))
                {
                    var dev = device as ICanBeDisabled;
                    if (dev != null)
                    {
                        dev.Disabled = true;
                        if (typeof(ISchedulable).IsAssignableFrom(deviceType))
                        {
                            var devSchedulable = dev as ISchedulable;
                            if (devSchedulable != null && devSchedulable.TimerEnabled)
                            {
                                _deviceScheduler.DisableJobsOfDevice(devSchedulable);
                            }
                        }
                    }
                }
                else
                {
                    Logger.Error($"{deviceDescription} does not implement {nameof(ICanBeDisabled)}");
                }
                break;

            case MqttDeviceActions.EnableEmergency:
                if (typeof(IEmergencyReceiver).IsAssignableFrom(deviceType))
                {
                    var dev = device as IEmergencyReceiver;
                    if (dev != null)
                    {
                        dev.EmergencyEnabled = true;
                        RegisterEmergencyDevice(dev);
                    }
                }
                else
                {
                    Logger.Error($"{deviceDescription} does not implement {nameof(IEmergencyReceiver)}");
                }
                break;

            case MqttDeviceActions.DisableEmergency:
                if (typeof(IEmergencyReceiver).IsAssignableFrom(deviceType))
                {
                    var dev = device as IEmergencyReceiver;
                    if (dev != null)
                    {
                        dev.EmergencyEnabled = false;
                        UnregisterEmergencyDevice(dev);
                    }
                }
                else
                {
                    Logger.Error($"{deviceDescription} does not implement {nameof(IEmergencyReceiver)}");
                }
                break;

            case MqttDeviceActions.EnableTimer:
                if (typeof(ISchedulable).IsAssignableFrom(deviceType))
                {
                    var dev = device as ISchedulable;
                    if (dev != null)
                    {
                        dev.TimerEnabled = true;
                        _deviceScheduler.EnableJobsOfDevice(dev);
                    }
                }
                else
                {
                    Logger.Error($"{deviceDescription} does not implement {nameof(ISchedulable)}");
                }
                break;

            case MqttDeviceActions.DisableTimer:
                if (typeof(ISchedulable).IsAssignableFrom(deviceType))
                {
                    var dev = device as ISchedulable;
                    if (dev != null)
                    {
                        dev.TimerEnabled = false;
                        _deviceScheduler.DisableJobsOfDevice(dev);
                    }
                }
                else
                {
                    Logger.Error($"{deviceDescription} does not implement {nameof(ISchedulable)}");
                }
                break;

            case MqttDeviceActions.SetOnTime:
                if (typeof(ISchedulable).IsAssignableFrom(deviceType))
                {
                    var      dev = device as ISchedulable;
                    TimeSpan res;
                    if (TimeSpan.TryParse(message, out res))
                    {
                        if (dev != null)
                        {
                            dev.OnTime = res;
                            if (dev is ISwitchable)
                            {
                                _deviceScheduler.UpdateTrigger(dev, JobActions.On);
                            }
                            else if (dev is IShuttable)
                            {
                                _deviceScheduler.UpdateTrigger(dev, JobActions.Open);
                            }
                            else
                            {
                                Logger.Fatal($"Jobaction not supported for the device {dev.GetType().Name}");
                            }
                        }
                    }
                    else
                    {
                        Logger.Error($"Time: {message} could not be parsed into a {nameof(TimeSpan)} ");
                    }
                }
                else
                {
                    Logger.Error($"{deviceDescription} does not implement {nameof(ISchedulable)}");
                }
                break;

            case MqttDeviceActions.SetOffTime:
                if (typeof(ISchedulable).IsAssignableFrom(deviceType))
                {
                    var      dev = device as ISchedulable;
                    TimeSpan res;
                    if (TimeSpan.TryParse(message, out res))
                    {
                        if (dev != null)
                        {
                            dev.OffTime = res;
                            if (dev is ISwitchable)
                            {
                                _deviceScheduler.UpdateTrigger(dev, JobActions.Off);
                            }
                            else if (dev is IShuttable)
                            {
                                _deviceScheduler.UpdateTrigger(dev, JobActions.Close);
                            }
                            else
                            {
                                Logger.Fatal($"Jobaction not supported for the device {dev.GetType().Name}");
                            }
                        }
                    }
                    else
                    {
                        Logger.Error($"Time: {message} could not be parsed into a {nameof(TimeSpan)} ");
                    }
                }
                else
                {
                    Logger.Error($"{deviceDescription} does not implement {nameof(ISchedulable)}");
                }
                break;

            case MqttDeviceActions.On:
                if (typeof(ISwitchable).IsAssignableFrom(deviceType))
                {
                    var dev = device as ISwitchable;
                    dev?.SwitchOn();
                }
                else
                {
                    Logger.Error($"{deviceDescription} does not implement {nameof(ISwitchable)}");
                }
                break;

            case MqttDeviceActions.Off:
                if (typeof(ISwitchable).IsAssignableFrom(deviceType))
                {
                    var dev = device as ISwitchable;
                    dev?.SwitchOff();
                }
                else
                {
                    Logger.Error($"{deviceDescription} does not implement {nameof(ISwitchable)}");
                }
                break;
            }
        }