예제 #1
0
        public void CreateHostColorPropertyValidateNodeIdInput()
        {
            foreach (var badTopicLevel in CommonStuff.BadTopicLevels)
            {
                Assert.That(() => _hostDevice.CreateHostColorProperty(PropertyType.Parameter, badTopicLevel, "good-id", "Friendly name"), Throws.ArgumentException);
            }

            foreach (var goodTopicLevel in CommonStuff.GoodTopicLevels)
            {
                Assert.DoesNotThrow(() => _hostDevice.CreateHostNumberProperty(PropertyType.Parameter, goodTopicLevel, "good-id", "Friendly name"));
            }
        }
        public void Initialize(ChannelConnectionOptions channelOptions)
        {
            _hostDevice = DeviceFactory.CreateHostDevice("lightbulb", "Colorful lightbulb");

            #region General node

            _hostDevice.UpdateNodeInfo("general", "General information and properties", "no-type");

            // I think properties are pretty much self-explanatory in this producer.
            _color = _hostDevice.CreateHostColorProperty(PropertyType.Parameter, "general", "color", "Color", ColorFormat.Rgb);
            _color.PropertyChanged += (sender, e) => {
                _log.Info($"Color changed to {_color.Value.ToRgbString()}");
            };
            _onOffSwitch = _hostDevice.CreateHostChoiceProperty(PropertyType.Parameter, "general", "is-on", "Is on", new[] { "OFF", "ON" }, "OFF");
            _onOffSwitch.PropertyChanged += (sender, e) => {
                // Simulating some lamp behaviour.
                if (_onOffSwitch.Value == "ON")
                {
                    _intensity.Value = 50;
                }
                else
                {
                    _intensity.Value = 0;
                }
            };

            _intensity = _hostDevice.CreateHostNumberProperty(PropertyType.Parameter, "general", "intensity", "Intensity", 0, "%");

            #endregion

            _broker.Initialize(channelOptions);
            _hostDevice.Initialize(_broker);
        }
        public void Initialize()
        {
            _mqttClient.Connect(MqttClientGuid);

            _hostDevice = DeviceFactory.CreateHostDevice("lightbulb", "Colorful lightbulb on ESP32");

            _hostDevice.UpdateNodeInfo("general", "General information and properties", "no-type");

            _color = _hostDevice.CreateHostColorProperty(PropertyType.Parameter, "general", "color", "Color", ColorFormat.Rgb);
            _color.PropertyChanged       += HandleColorPropertyChanged;
            _onOffSwitch                  = _hostDevice.CreateHostBooleanProperty(PropertyType.Parameter, "general", "turn-on-off", "Turn device on or off");
            _onOffSwitch.PropertyChanged += HandleOnOffSwitchPropertyChanged;
            _intensity = _hostDevice.CreateHostIntegerProperty(PropertyType.Parameter, "general", "intensity", "Intensity", 0, "%");
            _intensity.PropertyChanged += HandleIntensityPropertyChanged;

            _hostDevice.Initialize((topic, value, qosLevel, isRetained) => {
                _mqttClient.Publish(topic, Encoding.UTF8.GetBytes(value), 1, true);
            }, topic => {
                _mqttClient.Subscribe(new string[] { topic }, new byte[] { 1 });
            });
        }