private async void Timer1_Tick()

in Tools/RIoTDemo/MainPage.cs [140:256]


        private async void Timer1_Tick(object sender, EventArgs e)
        {
            if (executing) return;
            executing = true;
            // do we have changes to the devices we're monitoring?
            var devices = await RegMgr.GetDevicesAsync(100);
            var demoDevices = devices.Where(x => x.Id.Contains(DeviceIDMatch));
            if(DeviceListHasChanged(CurrentDeviceList, demoDevices))
            {
                CurrentDeviceList = demoDevices;
                UpdateDevicePanes();
            }
            // else update status of what we have

            // First, get a batch of messages and post them to the controls

            for (int j = 0; j < Receivers.Length; j++)
            {
                while (true)
                {

                    var mx = Receivers[j].Receive(new TimeSpan(0, 0, 0, 0, 1));
                    if (mx == null) break;
                    var senderDeviceId = mx.SystemProperties["iothub-connection-device-id"];
                    var messageData = mx.GetBytes();
                    var message = JsonConvert.DeserializeObject<MyMessageType>(Encoding.ASCII.GetString(messageData));

                    var device = StatusPanes.Where(x => x.Id == senderDeviceId.ToString()).First();
                    string colorString = Enum.GetName(typeof(KnownColor), message.ColorVal);
                    device.NotifyNewMessage($"Count={message.MessageCount}, Color = {colorString}");
                    device.PicColor = (KnownColor)message.ColorVal;
                    device.LastMessageTime = DateTime.Now;
                    Debug.WriteLine(message.ToString());
                }
            }


            // Next 1) update the background color based on what version number the device *claims* it is
            //      2) If device firmware is wrong AND the last firmware change was >1 min ago, revoke the
            //              creds for the device
            foreach (var cc in this.Controls)
            {
                if (!(cc is DeviceStatus)) continue;
                var ds = (DeviceStatus)cc;

                string deviceId = ds.Id;
                var device = demoDevices.Where(x => x.Id == deviceId).First();
                var twx = await RegMgr.GetTwinAsync(deviceId);
                CurrentState s = CurrentState.Good;
                DateTime nowTIme = DateTime.Now;
                try
                {
                    if (!twx.Properties.Reported.Contains("VersionNumber")) s = CurrentState.BadFirmware;
                    if (!twx.Properties.Desired.Contains("VersionNumber")) s = CurrentState.BadFirmware;
                    if (s != CurrentState.BadFirmware)
                    {
                        var reported = (twx.Properties.Reported["VersionNumber"]);
                        var desired = (twx.Properties.Desired["VersionNumber"]);
                        if (reported == desired)
                        {
                            s = CurrentState.Good;
                        }
                        else
                        { 
                            s = CurrentState.OldFirmware;
                        }
                        // If the device is out-of-date, and the update was requested >1 min ago, revoke the creds
                        if(s== CurrentState.OldFirmware && nowTIme-LastUpdateTime> new TimeSpan(0,1,0))
                        {
                            device.Authentication.X509Thumbprint.PrimaryThumbprint = null;
                            s = CurrentState.BadFirmware;
                            await RegMgr.UpdateDeviceAsync(device);
                        }

                        if (twx.Properties.Reported.Contains("VersionNumber"))
                        {
                            var repX = (twx.Properties.Reported["VersionNumber"]);
                            ds.MyVersionNumber = repX;
                        }
                    }
                }
                catch (Exception ee)
                {
                    Debug.WriteLine($"Error getting twin info {ee.ToString()}");
                    s = CurrentState.BadFirmware;
                }

                // any messages in the last minute?
                if (nowTIme - ds.LastMessageTime > new TimeSpan(0, 0, 1, 0))
                {
                    s = CurrentState.BadFirmware;
                }

                ds.State = s;
                ds.UpdateGUI();
            }

            // Finally, tell devices that they're P0wned
            foreach (var cc in this.Controls)
            {
                if (!(cc is DeviceStatus)) continue;
                var ds = (DeviceStatus)cc;
                if (!ds.P0wnedStatusChanged) continue;
                ds.P0wnedStatusChanged = false;

                var twx = await RegMgr.GetTwinAsync(ds.Id);

                TwinCollection t = new TwinCollection();
                t["POwned"] = ds.AmIPOwned;
                twx.Properties.Desired = t;
                await RegMgr.UpdateTwinAsync(ds.Id, twx, twx.ETag);

            }

            executing = false;

        }