private void SendSensorState(int nodeId, int sensorId, SensorData data)
 {
     lastSendedNodeId = nodeId;
     lastSendedSensorId = sensorId;
     App.serialController.SendSensorState(nodeId, sensorId, data);
     lastSendedNodeId = -1;
     lastSendedSensorId = -1;
 }
Ejemplo n.º 2
0
        public void SendSensorState(int nodeId, int sensorId, SensorData data)
        {
            Sensor sensor = GetNode(nodeId).GetSensor(sensorId);
            sensor.AddOrUpdateData(data);

            Message message = new Message();
            message.ack = false;
            message.messageType = MessageType.C_SET;
            message.nodeId = nodeId;
            message.payload = data.state;
            message.sensorId = sensorId;
            message.subType = (int)data.dataType;
            message.isValid = true;
            SendMessage(message);

            if (OnSensorUpdatedEvent != null)
                OnSensorUpdatedEvent(sensor);
        }
        private void slider_ValueChanged(object sender, RoutedEventArgs e)
        {
            Slider slider = (Slider)sender;
            string tag = slider.Tag.ToString();
            string[] args = tag.Split(';');
            int nodeId = Int32.Parse(args[0]);
            int sensorId = Int32.Parse(args[1]);
            SensorDataType dataType;
            SensorDataType.TryParse(args[2], true, out dataType);
            string state = slider.Value.ToString();
            SensorData data = new SensorData(dataType, state);

            DelayedSendSensorState(nodeId, sensorId, data);
        }
        private void DelayedSendSensorState(int nodeId, int sensorId, SensorData data)
        {
            Sensor sensor = sendSensorsList
                .Where(x => x.ownerNodeId == nodeId)
                .FirstOrDefault(x => x.sensorId == sensorId);
            if (sensor == null)
            {
                sensor = new Sensor { sensorId = sensorId, ownerNodeId = nodeId };
                sendSensorsList.Add(sensor);
            }

            sensor.AddOrUpdateData(data);

        }
        private List<Slider> CreateRGBWSliders(Sensor sensor, SensorData data)
        {
            List<Slider> list = new List<Slider>();

            int[] rgbw = ColorUtils.ConvertRGBWHexStringToIntArray(data.state);

            for (int i = 0; i < 4; i++)
            {
                Slider slider = new Slider();
                slider.Tag = String.Format("{0};{1};{2};{3}",
                    sensor.ownerNodeId,
                    sensor.sensorId,
                    data.dataType.ToString(),
                    i);

                slider.HorizontalAlignment = HorizontalAlignment.Stretch;
                slider.Margin = new Thickness(5, 0, 5, 0);

                slider.Maximum = 255;
                slider.Value = rgbw[i];
                slider.ValueChanged += sliderRGBW_ValueChanged;

                list.Add(slider);
            }
            return list;
        }
        private void button_Click(object sender, RoutedEventArgs e)
        {
            ToggleButton button = (ToggleButton)sender;
            string tag = button.Tag.ToString();
            string[] args = tag.Split(';');
            int nodeId = Int32.Parse(args[0]);
            int sensorId = Int32.Parse(args[1]);
            SensorDataType dataType;
            SensorDataType.TryParse(args[2], true, out dataType);
            string state = (button.IsChecked.Value) ? "1" : "0";
            SensorData data = new SensorData(dataType, state);

            button.Content = (button.IsChecked.Value) ? "ON" : "OFF";

            SendSensorState(nodeId, sensorId, data);
        }
        private ToggleButton CreateButton(Sensor sensor, SensorData data)
        {
            ToggleButton button = new ToggleButton
            {
                HorizontalAlignment = HorizontalAlignment.Stretch,
                Margin = new Thickness(5),
               
            };
            button.Tag = String.Format("{0};{1};{2}",
                sensor.ownerNodeId,
                sensor.sensorId,
                data.dataType.ToString());

            button.Click += button_Click;
            button.IsChecked = data.state == "1";
            button.Content = (button.IsChecked.Value) ? "ON" : "OFF";

            return button;
        }
        private Slider CreateSlider(Sensor sensor, SensorData data)
        {
            Slider slider = new Slider();
            slider.Tag = String.Format("{0};{1};{2}",
                sensor.ownerNodeId,
                sensor.sensorId,
                data.dataType.ToString());

            slider.HorizontalAlignment = HorizontalAlignment.Stretch;
            slider.Margin = new Thickness(5, 0, 5, 0);

            slider.Maximum = 100;
            slider.Value = Int32.Parse(data.state);
            slider.ValueChanged += slider_ValueChanged;

            return slider;
        }
        private StackPanel CreateTextBox(Sensor sensor, SensorData data)
        {
            StackPanel textBoxGrid = new StackPanel();

            TextBox box = new TextBox()
            {
                HorizontalAlignment = HorizontalAlignment.Stretch,
                Margin = new Thickness(5,0, 5,0)
            };

            box.TextChanged += textBoxChanged;
            box.Tag = String.Format("{0};{1};{2}",
                sensor.ownerNodeId,
                sensor.sensorId,
                data.dataType.ToString());

            Button button = new Button
            {
                HorizontalAlignment = HorizontalAlignment.Stretch,
                Margin = new Thickness(5),
                Content = "Send"
            };

            button.Tag = String.Format("{0};{1};{2}",
                sensor.ownerNodeId,
                sensor.sensorId,
                data.dataType.ToString());
            button.Click += textBoxClick;



            textBoxGrid.Children.Add(box);
            textBoxGrid.Children.Add(button);

            return textBoxGrid;
        }
        private StackPanel CreateSensorData(SensorData data, Sensor sensor)
        {
            StackPanel dataPanel = new StackPanel();

            //ON-OFF BUTTON
            if (data.dataType == SensorDataType.V_TRIPPED
                || data.dataType == SensorDataType.V_STATUS
                || data.dataType == SensorDataType.V_LIGHT)
            {
                ToggleButton button = CreateButton(sensor, data);
                dataPanel.Children.Add(button);
            }

            //0-100% SLIDER
            else if (data.dataType == SensorDataType.V_PERCENTAGE
                || data.dataType == SensorDataType.V_DIMMER
                || data.dataType == SensorDataType.V_LIGHT_LEVEL)
            {
                Slider slider = CreateSlider(sensor, data);
                dataPanel.Children.Add(slider);
            }

            //RGB/RGBW SLIDERS
            else if (data.dataType == SensorDataType.V_RGB)
            {
                List<Slider> sliders = CreateRGBSliders(sensor, data);
                foreach (var slider in sliders)
                    dataPanel.Children.Add(slider);
            }
            else if (data.dataType == SensorDataType.V_RGBW)
            {
                List<Slider> sliders = CreateRGBWSliders(sensor, data);
                foreach (var slider in sliders)
                    dataPanel.Children.Add(slider);
            }

            //HUMIDITY
            else if (data.dataType == SensorDataType.V_HUM)
            {
                string s = String.Format("{0} %", data.state);
                dataPanel.Children.Add(new TextBlock { Text = s, Margin = new Thickness(5), Foreground = new SolidColorBrush(Colors.Black) });
            }

            //TEMPERATURE
            else if (data.dataType == SensorDataType.V_TEMP)
            {
                string s = String.Format("{0} C", data.state);
                dataPanel.Children.Add(new TextBlock { Text = s, Margin = new Thickness(5), Foreground = new SolidColorBrush(Colors.Black) });
            }

            //WATT
            else if (data.dataType == SensorDataType.V_WATT)
            {
                string s = String.Format("{0} Watt", data.state);
                dataPanel.Children.Add(new TextBlock { Text = s, Margin = new Thickness(5), Foreground = new SolidColorBrush(Colors.Black) });
            }

            //KWH
            else if (data.dataType == SensorDataType.V_KWH)
            {
                string s = String.Format("{0} KWH", data.state);
                dataPanel.Children.Add(new TextBlock { Text = s, Margin = new Thickness(5), Foreground = new SolidColorBrush(Colors.Black) });
            }

            //Volt
            else if (data.dataType == SensorDataType.V_VOLTAGE)
            {
                string s = String.Format("{0} V", data.state);
                dataPanel.Children.Add(new TextBlock { Text = s, Margin = new Thickness(5), Foreground = new SolidColorBrush(Colors.Black) });
            }

            //Current
            else if (data.dataType == SensorDataType.V_CURRENT)
            {
                string s = String.Format("{0} ma", data.state);
                dataPanel.Children.Add(new TextBlock { Text = s, Margin = new Thickness(5), Foreground = new SolidColorBrush(Colors.Black) });
            }

            //Armed/Bypassed
            else if (data.dataType == SensorDataType.V_ARMED)
            {
                string s = data.state == "1" ? "Armed" : "Bypassed";
                dataPanel.Children.Add(new TextBlock { Text = s, Margin = new Thickness(5), Foreground = new SolidColorBrush(Colors.Black) });
            }

            //Locked/Unlocked
            else if (data.dataType == SensorDataType.V_LOCK_STATUS)
            {
                string s = data.state == "1" ? "Locked" : "Unlocked";
                dataPanel.Children.Add(new TextBlock { Text = s, Margin = new Thickness(5), Foreground = new SolidColorBrush(Colors.Black) });
            }

            //IR Send
            else if (data.dataType == SensorDataType.V_IR_SEND)
            {
                StackPanel textEdit = CreateTextBox(sensor, data);
                dataPanel.Children.Add(textEdit);
            }

            //IR Received
            else if (data.dataType == SensorDataType.V_IR_RECEIVE)
            {
                string s = String.Format("Received: {0}", data.state);
                dataPanel.Children.Add(new TextBlock { Text = s, Margin = new Thickness(5), Foreground = new SolidColorBrush(Colors.Black) });
            }

            //SIMPLE TEXT
            else
            {
                string s = String.Format("{0}", data.state);
                dataPanel.Children.Add(new TextBlock { Text = s, Margin = new Thickness(5), Foreground = new SolidColorBrush(Colors.Black) });

            }

            return dataPanel;
        }
Ejemplo n.º 11
0
 public void AddOrUpdateData(SensorDataType dataType, string state)
 {
     SensorData data = GetData(dataType);
     if (data == null)
     {
         data = new SensorData(dataType, state);
         sensorData.Add(data);
     }
     else data.state = state;
 }
Ejemplo n.º 12
0
 public void AddOrUpdateData(SensorData newData)
 {
     AddOrUpdateData(newData.dataType.Value, newData.state);
 }
        private StackPanel CreateSensorData(SensorData data)
        {
            StackPanel dataPanel = new StackPanel();

            string s = String.Format("Data: {0}, State: {1}", data.dataType, data.state);
            dataPanel.Children.Add(new TextBlock { Text = s, Margin = new Thickness(5), Foreground = new SolidColorBrush(Colors.Gainsboro) });

            return dataPanel;
        }