private void GetSensorReadings()
        {
            string    pirCurrentState = "U";
            EventData eventMessage;
            //Message message;
            MsgBody tmpMsg = new MsgBody(msg);

            try
            {
                // Initialize if needed
                if (pir == null)
                {  // Assume that initialization is needed
                    InitGPIO();
                }

                if (pir != null)
                {  // Only if we are intialized
                    // Get Current IP and MAC (May have changed!)
                    tmpMsg.ID = GetSensorId();
                    tmpMsg.Ip = GetCurrentIpv4Address();

                    // Check PIR Status (High == Movement)
                    // if it is high, then motion was detected
                    if (pir.Read() == GpioPinValue.High)
                    {
                        pirCurrentState = "O";
                    }
                    else
                    {
                        pirCurrentState = "F";
                    }

                    // Handle state changes with smoothing
                    if (pirCurrentState != state.PendingStatus || state.PendingStatus == "")
                    {  // A change. Start counting smoothing time. (Ie time with the same state required for change)
                        state.PendingStatusTime = DateTime.Now;
                        state.PendingStatus     = pirCurrentState;
                    }

                    if (state.PendingStatus != tmpMsg.Status &&                                                                                    // This is a Status change AND....
                        (tmpMsg.Status == "" ||                                                                                                    // this is the first run OR ...
                         (state.PendingStatus == "O" && (DateTime.Now - state.PendingStatusTime).TotalSeconds >= settings.timerStateOSmoothing) || // Pending status is "Occupied" and O-Smoothing-time has passed OR ...
                         (state.PendingStatus == "F" && (DateTime.Now - state.PendingStatusTime).TotalSeconds >= settings.timerStateFSmoothing)))
                    {                                                                                                                              // Pending status is "Free" and F-Smoothing-time has passed
                        if (state.PendingStatus == "O")
                        {
                            LedSetState(ledRed, true);  // Turn on RED LED
                        }
                        else
                        {
                            LedSetState(ledRed, false); // Turn off RED LED
                        }

                        tmpMsg.Status = state.PendingStatus; // Set the new status to the message
                        tmpMsg.Change = "T";                 // This is a change of status (T)rue

                        // Create brokered message
                        eventMessage = new EventData(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(tmpMsg)));
                        //*checking message in consoleTONE* Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, JsonConvert.SerializeObject(tmpMsg));
                        // Send to event hub
                        _eventHub.Send(eventMessage);
                        //SendDeviceToCloudMessagesAsync(tmpMsg);

                        // Save any changes (to this last so that any exception does not save changes.)
                        msg.setMsgBody(tmpMsg);
                        state.LastSendTime = DateTime.Now;

                        // Toggle green led to show a message has been sent
                        LedToggleGreen();
                    }
                    else if ((DateTime.Now - state.LastSendTime).TotalSeconds > settings.timerKeepAliveIntervall)
                    {                        // No Change of status. Is it time fot "is alive"?
                        tmpMsg.Change = "F"; // Status change (F)alse. (Set if we will send (is alive)

                        // Create brokered message
                        eventMessage = new EventData(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(tmpMsg)));

                        //*IotHubTONE* SendDeviceToCloudMessagesAsync(tmpMsg);
                        // Send to IoT hub
                        //private static async void SendDeviceToCloudMessagesAsync(Message m)
                        //{

                        //    await deviceClient.SendEventAsync(m);
                        //    Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString);

                        //}

                        _eventHub.Send(eventMessage);

                        // Save any changes (to this last so that any exception does not save changes.)
                        msg.setMsgBody(tmpMsg);
                        state.LastSendTime = DateTime.Now;

                        // Toggle green led to show a message has been sent
                        LedToggleGreen();
                    }
                }
            }
            catch (Exception exception)
            {
                //Status not written
            }
        }
示例#2
0
        private void Timer_Tick(object sender, object e)
        {
            string  pirCurrentState = "U";
            MsgBody tmpMsg          = new MsgBody(msg);

            try
            {
                // Initialize if needed
                if (pir == null)
                {  // Assume that initialization is needed
                    InitGPIO();
                }

                if (pir != null)
                {  // Only if we are intialized
                    // Get Current IP and MAC (May have changed!)
                    tmpMsg.ID = GetSensorId();
                    tmpMsg.Ip = GetCurrentIpv4Address();

                    // Check PIR Status (High == Movement)
                    // if it is high, then motion was detected
                    if (pir.Read() == GpioPinValue.High)
                    {
                        pirCurrentState = "O";
                    }
                    else
                    {
                        pirCurrentState = "F";
                    }

                    // Handle state changes with smoothing
                    if (pirCurrentState != state.PendingStatus || state.PendingStatus == "")
                    {  // A change. Start counting smoothing time. (Ie time with the same state required for change)
                        state.PendingStatusTime = DateTime.Now;
                        state.PendingStatus     = pirCurrentState;
                    }

                    if (state.PendingStatus != tmpMsg.Status &&                                                                                      // This is a Status change AND....
                        (tmpMsg.Status == "" ||                                                                                                      // this is the first run OR ...
                         (state.PendingStatus == "x  " && (DateTime.Now - state.PendingStatusTime).TotalSeconds >= settings.timerStateOSmoothing) || // Pending status is "Occupied" and O-Smoothing-time has passed OR ...
                         (state.PendingStatus == "F" && (DateTime.Now - state.PendingStatusTime).TotalSeconds >= settings.timerStateFSmoothing)))
                    {                                                                                                                                // Pending status is "Free" and F-Smoothing-time has passed
                        if (state.PendingStatus == "O")
                        {
                            LedSetState(ledRed, true);  // Turn on RED LED
                            LedSetState(ledGreen, false);
                        }
                        else
                        {
                            LedSetState(ledRed, false); // Turn off RED LED
                            LedSetState(ledGreen, true);
                        }

                        tmpMsg.Status = state.PendingStatus; // Set the new status to the message
                        tmpMsg.Change = "T";                 // This is a change of status (T)rue
                        tmpMsg.TS     = DateTime.Now.ToString();

                        SendDeviceToCloudMessagesAsync(tmpMsg);

                        // Save any changes (to this last so that any exception does not save changes.)
                        msg.setMsgBody(tmpMsg);
                        state.LastSendTime = DateTime.Now;

                        // Toggle green led to show a message has been sent
                        //LedToggleGreen();
                    }
                    else if ((DateTime.Now - state.LastSendTime).TotalSeconds > settings.timerKeepAliveIntervall)
                    {                        // No Change of status. Is it time fot "is alive"?
                        tmpMsg.Change = "F"; // Status change (F)alse. (Set if we will send (is alive)
                        tmpMsg.TS     = DateTime.Now.ToString();

                        // Send message to IoT hub
                        SendDeviceToCloudMessagesAsync(tmpMsg);

                        // Save any changes (to this last so that any exception does not save changes.)
                        msg.setMsgBody(tmpMsg);
                        state.LastSendTime = DateTime.Now;

                        // Toggle green led to show a message has been sent
                        LedToggleGreen();
                    }
                }
            }
            catch (Exception exception)
            {
                //Status not written
            }

            //if (Convert.ToString(ReceiveC2dAsync().Result) == "Received")
            //led.Write(GpioPinValue.Low);
        }