private async void Pair_ClickAsync(object sender, EventArgs e) { Serial.Write(Hub.device, "~stopQuatSender~"); while (!Serial.ReceivedMessageBool(Hub.device, "~stopingQuatSender~")) { ; } //Create a task that completes when/if a Sensor1 device is classified. Task s1PluggedIn = Task.Factory.StartNew(() => { while (Sensor1.device == null) { ; } }); await s1PluggedIn; //Start Sensor1 pair. Pairing.Pair(Sensor1.device); //Create a task that completes when/if a Sensor2 device is classifed Task s2PluggedIn = Task.Factory.StartNew(() => { while (Sensor2.device == null) { ; } }); await s2PluggedIn; //Start Sensor2 pair. Pairing.Pair(Sensor2.device); //Create a task that completes when/if a Hub device is classified Task hubPluggedIn = Task.Factory.StartNew(() => { while (Hub.device == null) { ; } }); await hubPluggedIn; //Hub Pair is last. Needs both Sensor 1 and Sensor 2 MacAdd. Pairing.Pair(Hub.device); }
//Async Pair Method public static async void Pair(Device currentDevice) { //Find and store device [Device.cs object and Parent Class's name] from serialPort param. string currentDeviceType = null; if (currentDevice.comPort == Sensor1.device.comPort) { currentDeviceType = "Sensor 1"; } if (currentDevice.comPort == Sensor2.device.comPort) { currentDeviceType = "Sensor 2"; } if (currentDevice.comPort == Hub.device.comPort) { currentDeviceType = "Hub"; } System.Diagnostics.Debug.WriteLine($"Starting {currentDeviceType} Pair...\n"); //System.Diagnostics.Debug.Write("\tSharing Key\n"); //Share Key bool shareKeySuccess = await Pairing.SharKeyAsync(currentDevice); //Share MacAdd(s) #region ShareMacAdds if (currentDeviceType == "Sensor 1") { //System.Diagnostics.Debug.WriteLine($"\tSharing Hub MacAdd to Sensor 1\n"); bool shareMacAddSuccess = await ShareMacAddAsync(currentDevice, false, false, true); } if (currentDeviceType == "Sensor 2") { //System.Diagnostics.Debug.WriteLine($"\tSharing Hub MacAdd to Sensor 2\n"); bool shareMacAddSuccess = await ShareMacAddAsync(currentDevice, false, false, true); } if (currentDeviceType == "Hub") { //Share Sensor1 MacAdd //System.Diagnostics.Debug.WriteLine($"\tSharing Sensor1 MacAdd to Hub\n"); bool shareSensor1MacAddSuccess = await ShareMacAddAsync(currentDevice, true, false, false); //Share Sensor2 MacAdd //System.Diagnostics.Debug.WriteLine($"\tSharing Sensor2 MacAdd to Hub\n"); bool shareSensor2MacAddSuccess = await ShareMacAddAsync(currentDevice, false, true, false); } #endregion ShareMacAdds //Restart Device Serial.Write(currentDevice, "~restart~"); //restart device. set key to key_from_eeprom. connect to hub via hubMacAdd_from_eeprom. while (!Serial.ReceivedMessageBool(currentDevice, "~restarting~")) { ; //wait for restart confirmation. } //if (currentDeviceType == "Sensor 1" || currentDeviceType == "Sensor 2") //{ // System.Diagnostics.Debug.WriteLine($"\t\tSafe to Remove {currentDeviceType}\n"); //} System.Diagnostics.Debug.WriteLine($"{currentDeviceType} Pair Complete."); }