public static void Main() { try { var timeSet = false; NTP.UpdateTimeFromNtpServer("pool.ntp.org", 1); ReadConfiguration(); ReadValueCache(System.DateTime.Today.ToString("ddMMyyyy")); loggingEndpoint = HttpClient.GetIPEndPoint(loggingHostName, loggingPortNumber); s0Port = new InterruptPort(Pins.GPIO_PIN_D12, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeLow); s0Port.OnInterrupt += new NativeEventHandler(S0PulseReceived); s0Port.EnableInterrupt(); var messageReader = new P1MessageReader(); messageReader.MessageReceived += new P1MessageReader.MessageReceivedDelegate(messageReader_MessageReceived); messageReader.Start(); while (true) { Thread.Sleep(60000); // Resync time and s0Counter at 3 o'clock at night if (!timeSet && System.DateTime.Now.Hour == 3) { timeSet = NTP.UpdateTimeFromNtpServer("pool.ntp.org", 1); s0Counter = 0; } else if (timeSet && System.DateTime.Now.Hour > 3) { timeSet = false; } CacheValuesOnSd(System.DateTime.Today.ToString("ddMMyyyy")); } } catch (Exception ex) { Debug.Print(ex.ToString()); } }
public static void Main() { try { var timeSet = false; NTP.UpdateTimeFromNtpServer("pool.ntp.org", 1); ReadConfiguration(); ReadValueCache(); loggingEndpoint = HttpClient.GetIPEndPoint(loggingHostName, loggingPortNumber); s0Port = new InterruptPort(Pins.GPIO_PIN_D12, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeLow); s0Port.OnInterrupt += new NativeEventHandler(S0PulseReceived); s0Port.EnableInterrupt(); var messageReader = new P1MessageReader(); messageReader.MessageReceived += new P1MessageReader.MessageReceivedDelegate(messageReader_MessageReceived); messageReader.Start(); while (true) { Thread.Sleep(60000); // Resync time and s0Counter at 3 o'clock at night if (!timeSet && System.DateTime.Now.Hour == 3) { timeSet = NTP.UpdateTimeFromNtpServer("pool.ntp.org", 1); s0Counter = 0; } else if (timeSet && System.DateTime.Now.Hour > 3) { timeSet = false; } CacheValuesOnSd(); } } catch (Exception ex) { Debug.Print(ex.ToString()); } }
static void messageReader_MessageReceived(object sender, P1MessageReader.MessageReceivedEventArgs e) { new Thread(delegate { try { StringBuilder content = new StringBuilder(512); content.AppendLine("{"); content.AppendLine("\"ApiKey\": \"" + apiKey + "\","); content.AppendLine("\"Timestamp\": \"" + e.Data.LogMoment.ToString("yyyy-MM-ddTHH:mm:ss") + "\","); content.AppendLine("\"E1\": \"" + e.Data.E1.ToString() + "\","); content.AppendLine("\"E2\": \"" + e.Data.E2.ToString() + "\","); content.AppendLine("\"E1Retour\": \"" + e.Data.E1Retour.ToString() + "\","); content.AppendLine("\"E2Retour\": \"" + e.Data.E2Retour.ToString() + "\","); content.AppendLine("\"CurrentTariff\": \"" + e.Data.CurrentTariff.ToString() + "\","); content.AppendLine("\"CurrentUsage\": \"" + e.Data.CurrentUsage.ToString() + "\","); content.AppendLine("\"CurrentRetour\": \"" + e.Data.CurrentRetour.ToString() + "\","); var gasValue = ""; if (e.Data.LastGasTransmit != null) gasValue = e.Data.LastGasTransmit; content.AppendLine("\"GasMeasurementMoment\": \"" + gasValue + "\","); content.AppendLine("\"GasMeasurementValue\": \"" + e.Data.Gas.ToString() + "\","); content.AppendLine("\"PvProductionCounter\": \"" + s0Counter.ToString() + "\""); content.AppendLine("}"); // produce request using (Socket connection = HttpClient.Connect(loggingEndpoint, 2000)) { if (connection != null) HttpClient.SendRequest(connection, loggingHostName, "POST /api/logentries HTTP/1.1", content.ToString()); else Debug.Print("Unable to connect"); } } catch (Exception ex) { Debug.Print("ERROR in posting data: " + ex.StackTrace); } }).Start(); new Thread(delegate { try { WriteToSdCard(e.Data); } catch (Exception ex) { Debug.Print("ERROR in writing to SDCard: " + ex.StackTrace); } }).Start(); }