Ejemplo n.º 1
0
        public void Initialize(IClientDeviceConnection brokerConnection)
        {
            // Creating a air conditioner device.
            _clientDevice = DeviceFactory.CreateClientDevice("air-conditioner");

            // Creating properties.
            _turnOnOfProperty = _clientDevice.CreateClientChoiceProperty(new ClientPropertyMetadata {
                PropertyType = PropertyType.Command, NodeId = "general", PropertyId = "turn-on-off", Format = "ON,OFF"
            });
            _actualState = _clientDevice.CreateClientChoiceProperty(new ClientPropertyMetadata {
                PropertyType = PropertyType.State, NodeId = "general", PropertyId = "actual-state", Format = "ON,OFF,STARTING", InitialValue = "OFF"
            });
            _actualState.PropertyChanged += (sender, e) => {
                _log.Info($"{_clientDevice.DeviceId}: property {_actualState.PropertyId} changed to {_actualState.Value}.");
            };

            _inletTemperature = _clientDevice.CreateClientNumberProperty(new ClientPropertyMetadata {
                PropertyType = PropertyType.State, NodeId = "general", PropertyId = "actual-air-temperature", DataType = DataType.Float, InitialValue = "0"
            });
            _inletTemperature.PropertyChanged += (sender, e) => {
                // Simulating some overheated dude.
                if (_inletTemperature.Value > 25)
                {
                    _log.Info($"{_clientDevice.Name}: getting hot in here, huh?.. Let's try turning air conditioner on.");
                    if (_actualState.Value != "ON")
                    {
                        _turnOnOfProperty.Value = "ON";
                    }
                }
            };

            // Initializing all the Homie stuff.
            _clientDevice.Initialize(brokerConnection);
        }
Ejemplo n.º 2
0
        public void Initialize(IClientDeviceConnection brokerConnection, ClientDeviceMetadata deviceMetadata)
        {
            _clientDevice = DeviceFactory.CreateClientDevice(deviceMetadata);

            for (var i = 0; i < _clientDevice.Nodes.Length; i++)
            {
                Debug.Print($"Iterating over nodes. Currently: \"{_clientDevice.Nodes[i].Name}\" with {_clientDevice.Nodes[i].Properties.Length} properties.");

                foreach (var property in _clientDevice.Nodes[i].Properties)
                {
                    property.PropertyChanged += (sender, e) => {
                        Debug.WriteLine($"Value of property \"{property.Name}\" changed to \"{property.RawValue}\".");
                    };
                }
            }

            // Initializing all the Homie stuff.
            _clientDevice.Initialize(brokerConnection);
        }
        public void Initialize(IClientDeviceConnection _brokerConnection)
        {
            // Creating a air conditioner device.
            _clientDevice = DeviceFactory.CreateClientDevice("lightbulb");

            // Creating properties.
            _color = _clientDevice.CreateClientColorProperty(new ClientPropertyMetadata {
                PropertyType = PropertyType.Parameter, NodeId = "general", PropertyId = "color", Format = "rgb", InitialValue = "0,0,0"
            });
            _color.PropertyChanged += (sender, e) => {
                if (_color.Value.RedValue > 0)
                {
                    Console.WriteLine("Me no like red!");
                    _color.Value = HomieColor.FromRgbString("0,128,128");
                }
            };

            // Initializing all the Homie stuff.
            _clientDevice.Initialize(_brokerConnection);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Initializes the entire Client Device tree: actually creates internal property variables, subscribes to topics and so on. This method must be called, or otherwise entire Client Device tree will not work.
        /// </summary>
        public void Initialize(IClientDeviceConnection broker)
        {
            base.Initialize(broker, NLog.LogManager.GetCurrentClassLogger());

            // Initializing properties. They will start using broker immediatelly.
            foreach (ClientPropertyBase property in _properties)
            {
                property.Initialize(this);
            }

            var homieTopic = $"{_baseTopic}/{DeviceId}/$homie";
            ActionStringDelegate handlerForTopicHomie = delegate(string value) {
                HomieVersion = value;
                RaisePropertyChanged(this, new PropertyChangedEventArgs(nameof(HomieVersion)));
            };

            InternalGeneralSubscribe(homieTopic, handlerForTopicHomie);

            var nameTopic = $"{_baseTopic}/{DeviceId}/$name";
            ActionStringDelegate handlerForTopicName = delegate(string value) {
                Name = value;
                RaisePropertyChanged(this, new PropertyChangedEventArgs(nameof(Name)));
            };

            InternalGeneralSubscribe(nameTopic, handlerForTopicName);

            var stateTopic = $"{_baseTopic}/{DeviceId}/$state";
            ActionStringDelegate handlerForTopicState = delegate(string value) {
                if (Helpers.TryParseHomieState(value, out var parsedState))
                {
                    State = parsedState;
                    RaisePropertyChanged(this, new PropertyChangedEventArgs(nameof(State)));
                }
                ;
            };

            InternalGeneralSubscribe(stateTopic, handlerForTopicState);
        }