public static Cpu.AnalogChannel[] GetAnalogChannels(AnalogPorts port) { switch (port) { case AnalogPorts.Analog0: return new[] { Cpu.AnalogChannel.ANALOG_0, Cpu.AnalogChannel.ANALOG_1 }; case AnalogPorts.Analog1: return new[] { Cpu.AnalogChannel.ANALOG_1, Cpu.AnalogChannel.ANALOG_2 }; case AnalogPorts.Analog2: return new[] { Cpu.AnalogChannel.ANALOG_2, Cpu.AnalogChannel.ANALOG_3 }; case AnalogPorts.Analog3: return new[] { Cpu.AnalogChannel.ANALOG_3, Cpu.AnalogChannel.ANALOG_4 }; case AnalogPorts.Analog4: return new[] { Cpu.AnalogChannel.ANALOG_4, Cpu.AnalogChannel.ANALOG_5 }; } throw new ArgumentException("Invalid Analog port", "port"); }
/// <summary> /// Return true if IsAnalog /// </summary> /// <param name="portPin">Name of port and number of pin, Example: F3, A1</param> /// <returns></returns> public bool IsAnalog(String portPin) { return(AnalogPorts.Any(x => x == portPin)); }
public void Run(IBackgroundTaskInstance taskInstance) { var deviceId = ""; var deviceSettings = default(AutodiscoverResult); CameraStatus cameraStatus = CameraStatus.NotInstalled; GroveBaseHatStatus groveBaseHatStatus = GroveBaseHatStatus.NotInstalled; #region Set up device autodiscovery //keep autodiscovering until we get a connection string while (string.IsNullOrEmpty(deviceSettings?.ConnectionString) == true) { try { deviceId = DeviceIdService.GetDeviceId(); deviceSettings = AutodiscoverService.GetDeviceSettings(deviceId); LoggingService.Log($"Device settings retrieved for DeviceId:{deviceId}"); } catch (Exception ex) { LoggingService.Error($"Autodiscovery failed", ex); Thread.Sleep(30000); } } #endregion #region Set up IoT Hub Connection try { azureIoTHubClient = DeviceClient.CreateFromConnectionString(deviceSettings.ConnectionString, TransportType.Mqtt); } catch (Exception ex) { LoggingService.Error($"Azure IoT Hub connection failed", ex); //TODO: set up a way for this to retry instead of fail? return; } #endregion #region Report device startup SendPayloadToIoTHub(new DeviceEvent() { ApplicationStarted = true }); #endregion #region Retrieve device twin try { deviceTwin = azureIoTHubClient.GetTwinAsync().Result; } catch (Exception ex) { LoggingService.Error($"Azure IoT Hub device twin configuration retrieval failed", ex); //TODO: set up a way for this to retry instead of fail? return; } #endregion #region Report device properties try { TwinCollection reportedProperties; reportedProperties = new TwinCollection(); // This is from the OS reportedProperties["Timezone"] = TimeZoneSettings.CurrentTimeZoneDisplayName; reportedProperties["OSVersion"] = Environment.OSVersion.VersionString; reportedProperties["MachineName"] = Environment.MachineName; // This is from the application manifest Package package = Package.Current; PackageId packageId = package.Id; PackageVersion version = packageId.Version; reportedProperties["ApplicationDisplayName"] = package.DisplayName; reportedProperties["ApplicationName"] = packageId.Name; reportedProperties["ApplicationVersion"] = string.Format($"{version.Major}.{version.Minor}.{version.Build}.{version.Revision}"); // Unique identifier from the hardware SystemIdentificationInfo systemIdentificationInfo = SystemIdentification.GetSystemIdForPublisher(); using (DataReader reader = DataReader.FromBuffer(systemIdentificationInfo.Id)) { byte[] bytes = new byte[systemIdentificationInfo.Id.Length]; reader.ReadBytes(bytes); reportedProperties["SystemId"] = BitConverter.ToString(bytes); } azureIoTHubClient.UpdateReportedPropertiesAsync(reportedProperties).Wait(); } catch (Exception ex) { LoggingService.Error($"Azure IoT Hub device twin configuration retrieval failed", ex); //TODO: set up a way for this to retry instead of fail? return; } #endregion #region Wire up device reboot command handler try { azureIoTHubClient.SetMethodHandlerAsync("Restart", RestartAsync, null); } catch (Exception ex) { LoggingService.Error($"Azure IoT Hub device method handler configuration failed", ex); return; } #endregion #region Set up Grove Base Hat for RPI if installed if (deviceTwin.Properties.Desired.Contains("GroveBaseHatForRPIInstalled")) { groveBaseHatStatus = GroveBaseHatStatus.Installed; try { if (deviceTwin.Properties.Desired["GroveBaseHatForRPIInstalled"].value == true) { GroveBaseHatAnalogPorts = new AnalogPorts(); GroveBaseHatAnalogPorts.Initialise(); LoggingService.Log($"Grove Base Hat for RPI Firmware version:{GroveBaseHatAnalogPorts.Version()}"); bme280Sensor = new BME280(0x76); // Wire up the sensor update handler azureIoTHubClient.SetMethodHandlerAsync("SensorUpdate", SensorUpdateAsync, deviceId); // If the SensorUpdatePeriod greater than 0 start timer int sensorUpdatePeriod = deviceTwin.Properties.Desired["SensorUpdatePeriod"].value; LoggingService.Log($"Sensor update period:{sensorUpdatePeriod} seconds"); if (sensorUpdatePeriod > 0) { sensorUpdateTimer = new Timer(SensorUpdateTimerCallback, deviceId, SensorUpdateDuePeriod, new TimeSpan(0, 0, sensorUpdatePeriod)); groveBaseHatStatus = GroveBaseHatStatus.Automatic; } else { groveBaseHatStatus = GroveBaseHatStatus.Enabled; } } } catch (Exception ex) { LoggingService.Error($"Grove Base Hat for RPI sensor configuration failed", ex); return; } } SendPayloadToIoTHub(new DeviceStatus() { GroveBaseHatStatus = groveBaseHatStatus }); #endregion #region Set up Camera if installed if (deviceTwin.Properties.Desired.Contains("CameraInstalled")) { cameraStatus = CameraStatus.Installed; try { if (deviceTwin.Properties.Desired["CameraInstalled"].value == true) { mediaCapture = new MediaCapture(); mediaCapture.InitializeAsync().AsTask().Wait(); // Wire up the image update handler azureIoTHubClient.SetMethodHandlerAsync("ImageUpdate", ImageUpdateAsync, deviceId); // If the CameraUpdatePeriod greater than 0 start timer int imageUpdatePeriod = deviceTwin.Properties.Desired["ImageUpdatePeriod"].value; LoggingService.Log($"Image update period:{imageUpdatePeriod} seconds"); if (imageUpdatePeriod > 0) { ImageUpdatetimer = new Timer(ImageUpdateTimerCallback, deviceId, ImageUpdateDueDefault, new TimeSpan(0, 0, imageUpdatePeriod)); cameraStatus = CameraStatus.Automatic; } else { cameraStatus = CameraStatus.Enabled; } } } catch (Exception ex) { LoggingService.Error($"Image capture configuration failed", ex); } } SendPayloadToIoTHub(new DeviceStatus() { CameraStatus = cameraStatus }); #endregion //enable task to continue running in background backgroundTaskDeferral = taskInstance.GetDeferral(); }