public async Task <bool> Initialize() { moduleClient = await WrappedModuleClient.Create(); var acm0 = OpenSerialPort("/dev/ttyACM0"); if (acm0 != null) { Logger.LogInfo("Successfully opened /dev/ACM0"); serialPort = acm0; } else { Logger.LogInfo("ACM0 not available, opening serial port ACM1"); var acm1 = OpenSerialPort("/dev/ttyACM1"); if (acm1 != null) { Logger.LogInfo("Successfully opened /dev/ACM1"); serialPort = acm1; } else { return(false); } } return(true); }
/// <summary> /// Initializes the ModuleClient and sets up the callback to receive /// messages containing temperature information /// </summary> static async Task Init() { // Use Mqtt as it is more reliable than ampq MqttTransportSettings mqttSetting = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only); ITransportSettings[] settings = { mqttSetting }; // Open a connection to the Edge runtime ModuleClient ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings); await ioTHubModuleClient.OpenAsync(); wrappedModuleClient = new WrappedModuleClient(ioTHubModuleClient); Logger.LogInfo("IoT Hub module client initialized."); // get module twin settings var moduleTwin = await ioTHubModuleClient.GetTwinAsync(); await OnDesiredPropertiesUpdate(moduleTwin.Properties.Desired, ioTHubModuleClient); // Attach a callback for updates to the module twin's desired properties. await ioTHubModuleClient.SetDesiredPropertyUpdateCallbackAsync(OnDesiredPropertiesUpdate, null); // Register callback to be called when a message is received by the module await ioTHubModuleClient.SetInputMessageHandlerAsync(TrackerModule.TelemetryInputName, ProcessTelemetry, ioTHubModuleClient); // Register callback to be called when a message is received by the module await ioTHubModuleClient.SetInputMessageHandlerAsync(TrackerModule.BalloonInputName, ProcessBalloonData, ioTHubModuleClient); }
private void ReceiptTimerCallback(object state) { Logger.LogWarning("Timer fired - did not receive expected balloon message"); UpdatePacketReceiveHistory(false); var trackerMessage = new TrackerMessage() { Location = this.Location, DeviceName = this.DeviceName, FlightId = this.FlightId, PacketReceivedPercentage = CalculatePacketReceivedPercentage() }; try { var moduleClient = WrappedModuleClient.Create().Result; Message trackerMessageRaw = new Message(trackerMessage.ToRawBytes()); moduleClient.SendEventAsync(TrackerOutputName, trackerMessageRaw).Wait(); } catch (Exception ex) { // Todo - wire in with application insights Logger.LogWarning($"Error Transmitter tracker message: {ex.Message}"); } }
/// <summary> /// Initializes the ModuleClient and sets up the callback to receive /// messages containing temperature information /// </summary> static async Task Init() { try { moduleClient = await WrappedModuleClient.Create(); } catch (Exception ex) { Logger.LogFatalError("Failed to create module client."); Logger.LogException(ex); // Throw to make module shutdown and get restarted by the iot edge agent. throw ex; } Logger.LogInfo("Created and connected Module Client. "); // Register callback to be called when a message is received by the module await moduleClient.moduleClient.SetInputMessageHandlerAsync(BalloonModule.TelemetryInputName, ProcessTelemetry, moduleClient.moduleClient); transmitTimer = new Timer(TransmitTimerCallback, null, transmitInterval, transmitInterval); }