Beispiel #1
0
        private static void SampleSensorValues(object State)
        {
            measurementLed.High();
            try
            {
                lock (synchObject)
                {
                    DateTime Now = DateTime.Now;
                    Record   Rec, Rec2;

                    // Read sensors

                    temp  = (short)tmp102.ReadTemperatureRegister();
                    light = adc.ReadRegistersBinary() [0];

                    // Calculate average of last 10 measurements, to get smoother momentary values

                    sumTemp  -= tempAvgWindow [avgPos];
                    sumLight -= lightAvgWindow [avgPos];

                    tempAvgWindow [avgPos]  = temp;
                    lightAvgWindow [avgPos] = light;

                    sumTemp       += temp;
                    sumLight      += light;
                    motionDetected = motion.Value;

                    temperatureC = (sumTemp * 0.1 / 256.0);
                    lightPercent = (100.0 * 0.1 * sumLight) / 0x0fff;
                    avgPos       = (avgPos + 1) % 10;

                    // Update history

                    Rec = new Record(Now, temperatureC, lightPercent, motionDetected);                                  // Rank 0

                    perSecond.Add(Rec);
                    if (perSecond.Count > 1000)
                    {
                        perSecond.RemoveAt(0);
                    }

                    sumSeconds += Rec;
                    nrSeconds++;

                    if (Now.Second == 0)
                    {
                        Rec = sumSeconds / nrSeconds;                                   // Rank 1
                        perMinute.Add(Rec);
                        Rec.SaveNew();

                        if (perMinute.Count > 1000)
                        {
                            Rec2 = perMinute [0];
                            perMinute.RemoveAt(0);
                            Rec2.Delete();
                        }

                        sumMinutes += Rec;
                        nrMinutes++;

                        sumSeconds = null;
                        nrSeconds  = 0;

                        if (Now.Minute == 0)
                        {
                            Rec = sumMinutes / nrMinutes;
                            perHour.Add(Rec);
                            Rec.SaveNew();

                            if (perHour.Count > 1000)
                            {
                                Rec2 = perHour [0];
                                perHour.RemoveAt(0);
                                Rec2.Delete();
                            }

                            sumHours += Rec;
                            nrHours++;

                            sumMinutes = null;
                            nrMinutes  = 0;

                            if (Now.Hour == 0)
                            {
                                Rec = sumHours / nrHours;
                                perDay.Add(Rec);
                                Rec.SaveNew();

                                if (perDay.Count > 1000)
                                {
                                    Rec2 = perDay [0];
                                    perDay.RemoveAt(0);
                                    Rec2.Delete();
                                }

                                sumDays += Rec;
                                nrDays++;

                                sumHours = null;
                                nrHours  = 0;

                                if (Now.Day == 1)
                                {
                                    Rec = sumDays / nrDays;
                                    perMonth.Add(Rec);
                                    Rec.SaveNew();

                                    sumDays = null;
                                    nrDays  = 0;
                                }
                            }
                        }
                    }
                }

                errorLed.Low();
            } catch (Exception)
            {
                errorLed.High();
            } finally
            {
                measurementLed.Low();
            }
        }
        private static void SampleSensorValues(object State)
        {
            measurementLed.High();
            try
            {
                lock (synchObject)
                {
                    DateTime Now = DateTime.Now;
                    Record   Rec, Rec2;

                    // Read sensors

                    temp  = (short)tmp102.ReadTemperatureRegister();
                    light = adc.ReadRegistersBinary() [0];

                    // Calculate average of last 10 measurements, to get smoother momentary values

                    sumTemp  -= tempAvgWindow [avgPos];
                    sumLight -= lightAvgWindow [avgPos];

                    tempAvgWindow [avgPos]  = temp;
                    lightAvgWindow [avgPos] = light;

                    sumTemp       += temp;
                    sumLight      += light;
                    motionDetected = motion.Value;

                    temperatureC = (sumTemp * 0.1 / 256.0);
                    lightPercent = (100.0 * 0.1 * sumLight) / 0x0fff;
                    avgPos       = (avgPos + 1) % 10;

                    // Update history

                    Rec = new Record(Now, temperatureC, lightPercent, motionDetected);                                  // Rank 0

                    perSecond.Add(Rec);
                    if (perSecond.Count > 1000)
                    {
                        perSecond.RemoveAt(0);
                    }

                    sumSeconds += Rec;
                    nrSeconds++;

                    if (Now.Second == 0)
                    {
                        Rec = sumSeconds / nrSeconds;                                   // Rank 1
                        perMinute.Add(Rec);
                        Rec.SaveNew();

                        if (perMinute.Count > 1000)
                        {
                            Rec2 = perMinute [0];
                            perMinute.RemoveAt(0);
                            Rec2.Delete();
                        }

                        sumMinutes += Rec;
                        nrMinutes++;

                        sumSeconds = null;
                        nrSeconds  = 0;

                        if (Now.Minute == 0)
                        {
                            Rec = sumMinutes / nrMinutes;
                            perHour.Add(Rec);
                            Rec.SaveNew();

                            if (perHour.Count > 1000)
                            {
                                Rec2 = perHour [0];
                                perHour.RemoveAt(0);
                                Rec2.Delete();
                            }

                            sumHours += Rec;
                            nrHours++;

                            sumMinutes = null;
                            nrMinutes  = 0;

                            if (Now.Hour == 0)
                            {
                                Rec = sumHours / nrHours;
                                perDay.Add(Rec);
                                Rec.SaveNew();

                                if (perDay.Count > 1000)
                                {
                                    Rec2 = perDay [0];
                                    perDay.RemoveAt(0);
                                    Rec2.Delete();
                                }

                                sumDays += Rec;
                                nrDays++;

                                sumHours = null;
                                nrHours  = 0;

                                if (Now.Day == 1)
                                {
                                    Rec = sumDays / nrDays;
                                    perMonth.Add(Rec);
                                    Rec.SaveNew();

                                    sumDays = null;
                                    nrDays  = 0;
                                }
                            }
                        }
                    }

                    // Check pending events

                    PendingEvent Event;
                    int          i = 0;
                    int          c = pendingEvents.Count;

                    while (i < c)
                    {
                        Event = pendingEvents [i];

                        if (Event.Trigger(temperatureC, lightPercent, motionDetected))
                        {
                            pendingEvents.RemoveAt(i);
                            c--;

                            HttpGetSensorData(Event.Response, Event.ContentType, Event.ExportModule, new ReadoutRequest(ReadoutType.MomentaryValues));
                            Event.Response.SendResponse();                              // Flags the end of the response, and transmission of all to the recipient.
                        }
                        else
                        {
                            i++;
                        }
                    }
                }

                errorLed.Low();
            } catch (Exception)
            {
                errorLed.High();
            } finally
            {
                measurementLed.Low();
                RemoveOldSessions();
            }
        }