Ejemplo n.º 1
0
        public void AddOrUpdateSensor(Sensor sensor)
        {
            Sensor oldSensor = conn.Table<Sensor>().Where(x => x.NodeId == sensor.NodeId).FirstOrDefault(x => x.sensorId == sensor.sensorId);

            if (oldSensor != null)
                conn.InsertOrReplaceWithChildren(sensor, true);
            else
                conn.InsertWithChildren(sensor);
        }
 private void UpdateSensor(Sensor node)
 {
     ShowNodes();
 }
Ejemplo n.º 3
0
 private void OnSensorUpdated(Sensor sensor)
 {
     if (!updatedNodesId.Contains(sensor.ownerNodeId))
         updatedNodesId.Add(sensor.ownerNodeId);
 }
        private void DelayedSendTimer(object sender, object e)
        {
            if (!sendSensorsList.Any())
                return;

            //for prevent update from another thread
            Sensor[] sendSensors = new Sensor[sendSensorsList.Count];
            sendSensorsList.CopyTo(sendSensors);
            sendSensorsList.Clear();

            foreach (var sensor in sendSensors)
            {
                foreach (var data in sensor.sensorData)
                {
                    SendSensorState(sensor.ownerNodeId, sensor.sensorId, data);
                }
            }

        }
        private void AddSensor(Sensor sensor)
        {
            StackPanel oldPanel = GetSensorPanel(sensor.ownerNodeId, sensor.sensorId);
            if (oldPanel != null)
                sensorPanels.Remove(oldPanel);

            StackPanel nodePanel = GetNodePanel(sensor.ownerNodeId);
            StackPanel sensorPanel = CreateSensorPanel(sensor);
            nodePanel.Children.Add(sensorPanel);
            sensorPanels.Add(sensorPanel);
        }
        private void UpdateSensor(Sensor sensor)
        {
            //prevent slidres update when drug
            if (sensor.ownerNodeId == lastSendedNodeId
                && sensor.sensorId == lastSendedSensorId)
                return;

            StackPanel panel = GetSensorPanel(sensor.ownerNodeId, sensor.sensorId);
            UpdateSensorPanel(sensor, panel);
        }
        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 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 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 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 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 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;
        }
        private void UpdateSensorPanel(Sensor sensor, StackPanel sensorPanel)
        {
            sensorPanel.Children.Clear();

            string sType = MySensors.GetSimpleSensorType(sensor.GetSensorType());

            if (sensor.description != null)
                sType = String.Format("{0} {1}", sType, sensor.description);

            sensorPanel.Children.Add(new TextBlock { Text = sType, Margin = new Thickness(5), Foreground = new SolidColorBrush(Colors.Gray) });

            foreach (SensorData data in sensor.sensorData)
            {
                StackPanel dataPanel = CreateSensorData(data, sensor);
                sensorPanel.Children.Add(dataPanel);
            }
        }
        private StackPanel CreateSensorPanel(Sensor sensor)
        {
            StackPanel sensorPanel = new StackPanel()
            {

                Orientation = Orientation.Vertical,
                Margin = new Thickness(5),
                BorderThickness = new Thickness(1),
                BorderBrush = new SolidColorBrush(Colors.Black),
                Tag = String.Format("{0};{1}", sensor.ownerNodeId, sensor.sensorId)
            };

            UpdateSensorPanel(sensor, sensorPanel);

            return sensorPanel;
        }
Ejemplo n.º 15
0
        public Sensor AddSensor(int sensorId)
        {
            Sensor sensor = GetSensor(sensorId);
            if (sensor != null) return sensor;

            sensor = new Sensor(sensorId, this);
            sensors.Add(sensor);
            return sensor;
        }
        private StackPanel CreateSensor(Sensor sensor)
        {
            StackPanel sensorPanel = new StackPanel();

            sensorPanel.Orientation = Orientation.Vertical;
            sensorPanel.Margin = new Thickness(5);
            sensorPanel.BorderThickness = new Thickness(1);
            sensorPanel.BorderBrush = new SolidColorBrush(Colors.Gainsboro);
            sensorPanel.Background = new SolidColorBrush(Colors.Gray);

            sensorPanel.Children.Add(new TextBlock { Text = "Sensor id: " + sensor.sensorId, Margin = new Thickness(5), Foreground = new SolidColorBrush(Colors.Gainsboro) });

            string sType = (sensor.GetSensorType() == null) ? "unknown" : sensor.GetSensorType().ToString();

            sensorPanel.Children.Add(new TextBlock { Text = "Sensor type: " + sType, Margin = new Thickness(5), Foreground = new SolidColorBrush(Colors.Gainsboro) });

            if (sensor.description != null)
                sensorPanel.Children.Add(new TextBlock { Text = "Description: " + sensor.description, Margin = new Thickness(5), Foreground = new SolidColorBrush(Colors.Gainsboro) });

            foreach (SensorData data in sensor.sensorData)
            {
                StackPanel dataPanel = CreateSensorData(data);
                sensorPanel.Children.Add(dataPanel);
            }
            return sensorPanel;
        }