Ejemplo n.º 1
0
        private JsonObject GetControllerStatus()
        {
            var result = new JsonObject();

            result.SetNamedValue("Type", "HA4IoT.Status".ToJsonValue());
            result.SetNamedValue("Version", 1.ToJsonValue());

            var actuators = new JsonObject();

            foreach (var actuator in _controller.Actuators())
            {
                actuators.SetNamedValue(actuator.Id.Value, actuator.ExportStatusToJsonObject());
            }

            result.SetNamedValue("Actuators", actuators);

            var automations = new JsonObject();

            foreach (var automation in _controller.Automations())
            {
                automations.SetNamedValue(automation.Id.Value, automation.ExportStatusToJsonObject());
            }

            result.SetNamedValue("Automations", automations);

            var weatherStation = _controller.Device <IWeatherStation>();

            if (weatherStation != null)
            {
                result.SetNamedValue("WeatherStation", weatherStation.ExportStatusToJsonObject());
            }

            return(result);
        }
Ejemplo n.º 2
0
        public void ConnectActuators(IController controller)
        {
            if (controller == null)
            {
                throw new ArgumentNullException(nameof(controller));
            }

            foreach (var actuator in controller.Actuators <IActuator>())
            {
                OnActuatorConnecting(actuator);

                var binaryStateOutput = actuator as IBinaryStateOutputActuator;
                if (binaryStateOutput != null)
                {
                    HandleBinaryStateOutputActuator(binaryStateOutput);
                    continue;
                }

                var stateMachineOutput = actuator as StateMachine;
                if (stateMachineOutput != null)
                {
                    HandleStateMachineOutputActuator(stateMachineOutput);
                    continue;
                }

                var sensor = actuator as ISingleValueSensorActuator;
                if (sensor != null)
                {
                    OnSensorValueChanged(sensor, sensor.GetValue());
                    sensor.ValueChanged += (s, e) =>
                    {
                        OnSensorValueChanged(sensor, e.NewValue);
                    };

                    continue;
                }

                var motionDetector = actuator as IMotionDetector;
                if (motionDetector != null)
                {
                    motionDetector.GetMotionDetectedTrigger().Attach(() => OnMotionDetected(motionDetector));
                    continue;
                }

                var button = actuator as IButton;
                if (button != null)
                {
                    button.GetPressedShortlyTrigger().Attach(() => OnButtonPressed(button, ButtonPressedDuration.Short));
                    button.GetPressedLongTrigger().Attach(() => OnButtonPressed(button, ButtonPressedDuration.Long));
                }
            }
        }