Ejemplo n.º 1
0
        public Sensor(string sensorType, string sensorUnit, AggregatedSensors parentAs)
        {
            Type = sensorType;
            Unit = sensorUnit;
            _parentAS = parentAs;
            LastValue = -100;
            LastUpdated = DateTime.Now;
            Offline = false;

            SensorDatas = new ObservableCollection<SensorData>();
            _maxElements = 100;
            UpdatePeriod = TimeSpan.FromSeconds(10);
        }
        public OverviewViewModel(MainWindow mainWindow)
        {
            MqttReceivedData = new MQTTCollection(this);
            SensorsList = new ObservableCollection<AggregatedSensors>();

            ChartIntervalType = DateTimeIntervalType.Auto;

            //Creating the data layer
            MqttDl = new MqttDataLayer("WPF Client", "192.168.2.122");
            MqttDl.OnConnected += MqttDL_OnConnected;
            MqttDl.Connect();

            As1 = new AggregatedSensors("cabinet", "Cabinet");
            As1.AddSensor("temperature", "C");
            As1.AddSensor("humidity", "%");

            SensorsList.Add(As1);

            MeasuredData=new MeasuredData();

            As2 = new AggregatedSensors("cave", "Cave");
            As2.AddSensor("temperature", "C");
            As2.AddSensor("humidity", "%");

            SensorsList.Add(As2);

            As3 = new AggregatedSensors("outside", "Outside");
            As3.AddSensor("temperature", "C");
            As3.AddSensor("humidity", "%");
            SensorsList.Add(As3);

            As4 = new AggregatedSensors("sleeping_room", "Sleeping room");
            As4.AddSensor("temperature", "C");
            As4.AddSensor("humidity", "%");

            SensorsList.Add(As4);
            //note that the sensors listen to the collection events not the data layer events
            //the collection takes care to parse the received data so that the agregated sensors can use it directly
            MqttReceivedData.DataReceived += As1.MqttReceivedData;
            MqttReceivedData.DataReceived += As2.MqttReceivedData;
            MqttReceivedData.DataReceived += As3.MqttReceivedData;
            MqttReceivedData.DataReceived += As4.MqttReceivedData;

            As1.NewReading += MeasuredData.NewReading;
            As2.NewReading += MeasuredData.NewReading;
            As3.NewReading += MeasuredData.NewReading;
            As4.NewReading += MeasuredData.NewReading;

            Dht22SensorControl dht22;
            foreach (var aggregatedSensor in SensorsList)
            {
                dht22 = new Dht22SensorControl();
                dht22.DataContext = aggregatedSensor;
                dht22.Margin = new Thickness(5);
                Binding myBinding = new Binding
                {
                    Source = aggregatedSensor.Sensors["temperature"],
                    Path = new PropertyPath("LastUpdated"),
                    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                };

                dht22.SetBinding(Dht22SensorControl.LastUpdatedProperty, myBinding);
                    mainWindow.SensorsContainerPanel.Children.Add(dht22);

            }
        }