Ejemplo n.º 1
0
        private async Task PostToSensorCommunity(string HTdata, string PMdata)
        {
            try
            {
                SensorCommunityHttpClient.CancelPendingRequests();

                using (StringContent content = new StringContent(HTdata, Encoding.UTF8, "application/json"))
                {
                    // BME280 use 11   for SHT31 use 7
                    content.Headers.Add("X-pin", "7");
                    content.Headers.Add("X-Sensor", $"{SensorID}");

                    using (HttpResponseMessage response = await SensorCommunityHttpClient.PostAsync("", content))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            Sup.LogTraceInfoMessage($"PostToSensorCommunity : OK - {response.StatusCode} - {await response.Content.ReadAsStringAsync()}");
                        }
                        else
                        {
                            Sup.LogTraceErrorMessage($"PostToSensorCommunity : Error: {response.StatusCode} - {await response.Content.ReadAsStringAsync()}");
                        }
                    }
                }

                using (StringContent content = new StringContent(PMdata, Encoding.UTF8, "application/json"))
                {
                    content.Headers.Add("X-pin", "1");   // BME280 use 11   for SHT31 use 7
                    content.Headers.Add("X-Sensor", $"{SensorID}");

                    using (HttpResponseMessage response = await SensorCommunityHttpClient.PostAsync("", content))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            Sup.LogTraceInfoMessage($"PostToSensorCommunity : OK - {response.StatusCode} - {await response.Content.ReadAsStringAsync()}");
                        }
                        else
                        {
                            Sup.LogTraceErrorMessage($"PostToSensorCommunity : Error: {response.StatusCode} - {await response.Content.ReadAsStringAsync()}");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Sup.LogTraceErrorMessage($"PostToSensorCommunity : Exception - {e.Message}");
            }

            return;
        }
Ejemplo n.º 2
0
        public PMS1003Device(Support s, string Name)
        {
            Sup = s;
            Sup.LogTraceInfoMessage($"DoPMS1003Device: Constructor...{Name}");

            try
            {
                SensorUsed = (SerialSensorsSupported)Enum.Parse(typeof(SerialSensorsSupported), Name, true);
                Port       = new DefinitionSerialPort(Sup, Name);
                Valid      = true;
            }
            catch (Exception e) when(e is ArgumentException || e is ArgumentNullException)
            {
                // We arrive here if the sensor does not exist in the Enum definition
                Sup.LogTraceErrorMessage($"Serial: Exception on Serial Port definitions in Inifile : {e.Message}");
                Sup.LogTraceErrorMessage("No use continuing when the particle sensor is not there - trying anyway");
                Valid = false;
            }
        }// PMS1003 Constructor
Ejemplo n.º 3
0
        public List <I2cSensordata> ObservationList  = new List <I2cSensordata>(); // The list to create the minutevalues. Filled in DoWork(), Cleared in SetMinuteValuesFromObservations()


        public I2C(Support s, string Name)
        {
            Sup = s;

            Sup.LogDebugMessage($"I2C Constructor {Name}");

            // Make all addresses
            i2cAddress[(int)I2cSensorsSupported.SHT31] = 0x44;

            // Now create the actual device
            try
            {
                SensorUsed = (I2cSensorsSupported)Enum.Parse(typeof(I2cSensorsSupported), Name, true);
                switch (SensorUsed)
                {
                case I2cSensorsSupported.SHT31:
                    Sup.LogDebugMessage("I2C Constructor: Creating SHT31 sensor");
                    Sensor = new SHT31Device(Sup, Name);
                    break;

                case I2cSensorsSupported.Simulator:
                    Sup.LogDebugMessage("I2C Constructor: Creating SHT31 sensor");
                    Sensor = new I2cSimulatorDevice(Sup, Name);
                    break;

                default:
                    Sup.LogDebugMessage($"I2C Constructor: I2c sensor not implemented {SensorUsed}");
                    Sensor = new I2cDummyDevice(Sup, Name);
                    break;
                }
                Sensor.Start();
            }
            catch (Exception e) when(e is ArgumentException || e is ArgumentNullException)
            {
                // We arrive here if the sensor does not exist in the Enum definition
                Sup.LogTraceErrorMessage($"I2C Constructor: Exception on parsing I2cDevice Name : {e.Message}");
                Sup.LogTraceErrorMessage("Either an error in naming or driver not implemented.");
                Sup.LogTraceErrorMessage("Replacing this device by a Dummy Driver. Continuing...");
                Sensor = new I2cDummyDevice(Sup, Name);
            }
        }// Constructor
Ejemplo n.º 4
0
        public List <PMSensordata> ObservationList  = new List <PMSensordata>();      // The list to create the minutevalues

        public Serial(Support s, string Name)
        {
            Sup = s;

            Sup.LogDebugMessage($"Serial: Constructor...{Name}");
            try
            {
                SensorUsed = (SerialSensorsSupported)Enum.Parse(typeof(SerialSensorsSupported), Name, true);

                switch (SensorUsed)
                {
                case SerialSensorsSupported.PMS1003:
                    Sup.LogDebugMessage("Serial Constructor: Creating PMS1003 sensor");
                    Sensor = new PMS1003Device(Sup, Name);
                    break;

                case SerialSensorsSupported.Simulator:
                    Sup.LogDebugMessage("Serial Constructor: Creating PMS1003 sensor");
                    Sensor = new SerialSimulatorDevice(Sup, Name);
                    break;

                default:
                    Sup.LogDebugMessage($"Serial Constructor: Serial sensor not implemented {SensorUsed}");
                    Sensor = new SerialDummyDevice(Sup, Name);
                    break;
                }

                Sensor.Open(); // Also called Open in some circles
            }
            catch (Exception e) when(e is ArgumentException || e is ArgumentNullException)
            {
                // We arrive here if the sensor does not exist in the Enum definition
                Sup.LogTraceErrorMessage($"Serial Constructor: Exception on parsing Serial Device Name : {e.Message}");
                Sup.LogTraceErrorMessage("Either an error in naming or driver not implemented.");
                Sup.LogTraceErrorMessage("Replacing this device by a Dummy Driver. Continuing...");
                Sensor = new SerialDummyDevice(Sup, Name);
            }
        }// Serial Constructor
Ejemplo n.º 5
0
            internal DefinitionSerialPort(Support s, string Name)
            {
                Sup = s;

                Sup.LogDebugMessage($"DefinitionSerialPort Constructor...");

                try
                {
                    SerialPortUsed     = Sup.GetSensorsIniValue("PortDefinitions", $"{Name}_SerialPort", "/dev/ttyS0");
                    SerialBaudRate     = Convert.ToInt32(Sup.GetSensorsIniValue("PortDefinitions", $"{Name}_SerialBaudrate", "9600"));
                    SerialParity       = (Parity)Enum.Parse(typeof(Parity), Sup.GetSensorsIniValue("PortDefinitions", $"{Name}_SerialParity", "None"), true);
                    SerialDataBits     = Convert.ToInt32(Sup.GetSensorsIniValue("PortDefinitions", $"{Name}_SerialDataBits", "8"));
                    SerialNrOfStopBits = (StopBits)Enum.Parse(typeof(StopBits), Sup.GetSensorsIniValue("PortDefinitions", $"{Name}_SerialStopBits", "One"), true);
                }
                catch (Exception e) when(e is ArgumentException || e is ArgumentNullException || e is OverflowException || e is FormatException)
                {
                    Sup.LogTraceErrorMessage($"Serial: Exception on Serial Port definitions in Inifile : {e.Message}");
                }
            }
Ejemplo n.º 6
0
        public void DoAirLink()
        {
            // For Airquality (AirNow) and nowscast documentation: see Program.cs
            // We get here from the main loop once per minute

            Sup.LogTraceInfoMessage($"DoAirLink: Adding minutevalues to the averageslists...");

            TemperatureC = I2cDevice.MinuteValues.TemperatureC;
            TemperatureF = I2cDevice.MinuteValues.TemperatureF;
            Humidity     = I2cDevice.MinuteValues.Humidity;

            PM1_last = SerialDevice.MinuteValues.Pm1_atm;

            // 14.8 + (0.3834 * PM25) + (-0.1498 * rHum) + (-0.1905 * temperature)
            if (DoCalibrated)
            {
                PM25_last = 14.8 + 0.3834 * SerialDevice.MinuteValues.Pm25_atm + -0.1498 * Humidity + -0.1905 * TemperatureC;
            }
            else
            {
                PM25_last = SerialDevice.MinuteValues.Pm25_atm;
            }

            // 14.7 + (0.3151 * PM10) + (-0.0948 * rHum) + (0.2445 * temperature)
            if (DoCalibrated)
            {
                PM10_last = 14.7 + 0.3151 * SerialDevice.MinuteValues.Pm10_atm + -0.0948 * Humidity + 0.2445 * TemperatureC;
            }
            else
            {
                PM10_last = SerialDevice.MinuteValues.Pm10_atm;
            }

            if (PM25_last_1_hourList.Count == 60)
            {
                PM25_last_1_hourList.RemoveAt(0);
            }
            PM25_last_1_hourList.Add(PM25_last);
            Sup.LogTraceInfoMessage($"DoAirLink: PM25_last_1_hourList - count: {PM25_last_1_hourList.Count} / Average: {PM25_last_1_hourList.Average():F1}");

            if (PM25_last_3_hourList.Count == 3 * 60)
            {
                PM25_last_3_hourList.RemoveAt(0);
            }
            PM25_last_3_hourList.Add(PM25_last);
            Sup.LogTraceInfoMessage($"DoAirLink: PM25_last_3_hourList - count: {PM25_last_3_hourList.Count} / Average {PM25_last_3_hourList.Average():F1}");

            if (PM25_last_24_hourList.Count == 24 * 60)
            {
                PM25_last_24_hourList.RemoveAt(0);
            }
            PM25_last_24_hourList.Add(PM25_last);
            Sup.LogTraceInfoMessage($"DoAirLink: PM25_last_24_hourList - count: {PM25_last_24_hourList.Count} / Average {PM25_last_24_hourList.Average():F1}");

            if (PM10_last_1_hourList.Count == 60)
            {
                PM10_last_1_hourList.RemoveAt(0);
            }
            PM10_last_1_hourList.Add(PM10_last);
            Sup.LogTraceInfoMessage($"DoAirLink: PM10_last_1_hourList - count: {PM10_last_1_hourList.Count} / Average {PM10_last_1_hourList.Average():F1}");

            if (PM10_last_3_hourList.Count == 3 * 60)
            {
                PM10_last_3_hourList.RemoveAt(0);
            }
            PM10_last_3_hourList.Add(PM10_last);
            Sup.LogTraceInfoMessage($"DoAirLink: PM10_last_3_hourList - count: {PM10_last_3_hourList.Count} / Average {PM10_last_3_hourList.Average():F1}");

            if (PM10_last_24_hourList.Count == 24 * 60)
            {
                PM10_last_24_hourList.RemoveAt(0);
            }
            PM10_last_24_hourList.Add(PM10_last);
            Sup.LogTraceInfoMessage($"DoAirLink: PM10_last_24_hourList - count: {PM10_last_24_hourList.Count} / Average {PM10_last_24_hourList.Average():F1}");

#if PARANOIA
            // Do a paranoia check on the queue lengths: this should not be excuted in a release version
            // Print message to logfile if such thing occurs. Note that above the checks are on equal of the count value.
            // As soon as the value goes above, it is an uncorrectable issue.
            if (PM25_last_1_hourList.Count > 60)
            {
                Sup.LogTraceErrorMessage($"DoAirLink: PM25_last_1_hourList - count: {PM25_last_1_hourList.Count} / Average: {PM25_last_1_hourList.Average():F1}");
            }
            if (PM25_last_3_hourList.Count > 3 * 60)
            {
                Sup.LogTraceErrorMessage($"DoAirLink: PM25_last_3_hourList - count: {PM25_last_3_hourList.Count} / Average: {PM25_last_3_hourList.Average():F1}");
            }
            if (PM25_last_24_hourList.Count > 24 * 60)
            {
                Sup.LogTraceErrorMessage($"DoAirLink: PM25_last_24_hourList - count: {PM25_last_24_hourList.Count} / Average: {PM25_last_24_hourList.Average():F1}");
            }
            if (PM10_last_1_hourList.Count > 60)
            {
                Sup.LogTraceErrorMessage($"DoAirLink: PM10_last_1_hourList - count: {PM10_last_1_hourList.Count} / Average: {PM10_last_1_hourList.Average():F1}");
            }
            if (PM10_last_3_hourList.Count > 3 * 60)
            {
                Sup.LogTraceErrorMessage($"DoAirLink: PM10_last_3_hourList - count: {PM10_last_3_hourList.Count} / Average: {PM10_last_3_hourList.Average():F1}");
            }
            if (PM10_last_24_hourList.Count > 24 * 60)
            {
                Sup.LogTraceErrorMessage($"DoAirLink: PM10_last_24_hourList - count: {PM10_last_24_hourList.Count} / Average: {PM10_last_24_hourList.Average():F1}");
            }
#endif

            NowCast25 = CalculateNowCast(PM25_last_24_hourList);
            NowCast10 = CalculateNowCast(PM10_last_24_hourList);

            return;
        }