Example #1
0
        // simulate config command  from iot central
        private static async Task <MethodResponse> OnConfigchanged(MethodRequest methodrequest, object usercontext)
        {
            ShowEventMessage?.Invoke("Receiving cloud to device message OnConfigchanged");
            try
            {
                dynamic payload = JObject.Parse(methodrequest.DataAsJson);
                if (payload.version > Version)
                {
                    Version = payload.version;
                    ShowEventMessage?.Invoke($"Configuration updated to version {payload.version}");
                    await SendMessageFromDeviceToCloud("config", $"Configuration updated to version {payload.version}").ConfigureAwait(false);
                }
                else
                {
                    ShowEventMessage?.Invoke($"no update required, already on {Version}");
                    await SendMessageFromDeviceToCloud("config", $"no update required, already on {Version}").ConfigureAwait(false);
                }
            }
            catch (Exception e)
            {
                await AzureIotCentralClient.SendMessageFromDeviceToCloud("alarm", $"Exception OnConfigchanged {e.Message} data {methodrequest.DataAsJson}").ConfigureAwait(false);
            }

            return(new MethodResponse(200));
        }
Example #2
0
        // processes deviceupdate command from iot central => disable/enable device
        private static async Task <MethodResponse> OnDeviceChanged(MethodRequest methodrequest, object usercontext)
        {
            ShowEventMessage?.Invoke("Receiving cloud to device message OnSensorChanged");
            try
            {
                dynamic payload = JObject.Parse(methodrequest.DataAsJson);

                String bluetoothaddress = payload.bluetoothaddress;
                String action           = payload.action;

                // invoke sensor changed delegate
                await(SensorChanged?.Invoke(bluetoothaddress, null, action)).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                // on exception inform iot central => disable/enable sensor
                await AzureIotCentralClient.SendMessageFromDeviceToCloud("alarm", $"Exception OnDeviceChanged {e.Message}  data {methodrequest.DataAsJson}").ConfigureAwait(false);
            }

            return(new MethodResponse(200));
        }
Example #3
0
        private async Task ReadValues()
        {
            if (Beacon == null)
            {
                return;
            }

            // read values fromm sensor
            DateTime timeStamp    = DateTime.Now;
            double   batteryLevel = await ReadBatteryValue().ConfigureAwait(true);

            double lux = await ReadLuminosityValue().ConfigureAwait(true);

            double temperature = await ReadTemperatureValue().ConfigureAwait(true);

            if (Beacon == null)
            {
                return;
            }

            // set values in beacon
            Beacon?.SetData(SensorType.BatteryPercent, batteryLevel);
            Beacon?.SetData(SensorType.Luminosity, lux);
            Beacon?.SetData(SensorType.Temperature, temperature);

            // do the ui stuff
            ThreadHelper.RunOnMainThread(() =>
            {
                Beacon.TimeStampReceived = timeStamp;
                Beacon?.UpdateUi();
            });

            // send to iot central if all sensor data are valid
            if (Beacon?.AllDataValid() == true)
            {
                await AzureIotCentralClient.SendSensorDataToIotCentral(Beacon, timeStamp).ConfigureAwait(false);
            }
        }