/// <summary> /// Loads or reloads the <see cref="device"/> object. /// </summary> public void LoadDeviceInfo() { lastLoadAttempt = timer.ElapsedMilliseconds; lock (myLock) { lastLoadAttempt = timer.ElapsedMilliseconds; device = null; int tries = 0; int maxTries = 5; while (device == null && tries < maxTries) { if (tries > 0) { Logger.Info("Unable to find BroadLink device \"" + name + "/" + host + "\" after " + tries + " scan" + (tries == 1 ? "" : "s") + ". " + (tries + 1 < maxTries ? "Trying again." : "Aborting BroadLinkController load.")); } device = ScanForDevice(host); tries++; } if (device != null) { try { device.AuthorizeAsync().Wait(); } catch (Exception ex) { Logger.Debug(ex); device = null; } } } }
private static async Task TestCodes(RMDevice remote) { string?fileName = Program.QueryFileName(); if (fileName == null) { return; } Dictionary <string, string> dictionary = JsonSerializer.Deserialize <Dictionary <string, string> >(File.ReadAllText(fileName, Encoding.UTF8)) !; while (true) { if (Program.Query("Command name?") is not string name) { return; } if (!dictionary.TryGetValue(name, out string?text)) { Console.Error.WriteLine($"Command {name} not found"); continue; } await remote.SendData(ByteArray.FromHex(text)); await remote.WaitForAck(); } }
private static async Task LearnCodes(RMDevice device) { string?fileName = Program.QueryFileName(); if (fileName == null) { return; } Dictionary <string, string> dictionary = new(); while (true) { if (Query("Command name?") is not string name) { break; } await device.BeginLearning(); await device.WaitForAck(); byte[] data = await device.WaitForData(); dictionary[name] = data.ToHex(); } File.WriteAllText(fileName, JsonSerializer.Serialize(dictionary), Encoding.UTF8); }
public RMDevice DiscoverDevice() { RMDevice discovered = null; EventHandler <BroadlinkDevice> handler = (s, e) => { discovered = e as RMDevice; }; discoverClient.DeviceHandler += handler; discoverClient.DiscoverAsync().Wait(); for (int i = 0; i < DefaultWaitTime; i++) { if (discovered != null) { return(discovered); } Task.Delay(1000).Wait(); } discoverClient.DeviceHandler -= handler; return(discovered); }
public static RMDevice WaitUntilReady(this RMDevice device) { if (!device.IsEventsReady) { bool ready = false; EventHandler handler = (sen, er) => { ready = true; }; device.OnDeviceReady += handler; //e.OnTemperature += RMDevice_OnTemperature; //e.OnRawData += RMDevice_OnRawData; //e.OnRawRFDataFirst += RMDevice_OnRawRFDataFirst; //e.OnRawRFDataSecond += RMDevice_OnRawRFDataSecond; //e.OnSentDataCallback += RMDevice_OnSentDataCallback; device.IsEventsReady = true; device.AuthorizeAsync().Wait(); for (int i = 0; i < 20 && !ready; i++) { Task.Delay(1000).Wait(); } device.OnDeviceReady -= handler; if (!ready) { Console.WriteLine("Device was not ready in the requested time"); } } return(device); }
private async void btnConnect_Click(object senderBtn, EventArgs eBtn) { btnConnect.Enabled = false; if (cmbDevices.Items.Count > 0) { try { DiscoverClient.Dispose(); DiscoverClient = null; } catch (Exception) { } } if (cmbDevices.Items.Count == 0) { if (DiscoverClient == null) { DiscoverClient = new Client(); DiscoverClient.DeviceHandler += Client_DeviceHandler; } HelperMy.Notification(Color.Gray, "Cihazlar aranıyor..."); await DiscoverClient.DiscoverAsync(); btnConnect.Text = "Tarama Yap"; } else if (cmbDevices.SelectedItem is RMDevice secim) { HelperMy.Notification(Color.Gray, "Bağlantı kuruluyor..."); RMDevice = secim as RMDevice; if (!RMDevice.IsEventsReady) { RMDevice.OnDeviceReady += RMDevice_OnDeviceReady; RMDevice.OnTemperature += RMDevice_OnTemperature; RMDevice.OnRawData += RMDevice_OnRawData; RMDevice.OnRawRFDataFirst += RMDevice_OnRawRFDataFirst; RMDevice.OnRawRFDataSecond += RMDevice_OnRawRFDataSecond; RMDevice.OnSentDataCallback += RMDevice_OnSentDataCallback; RMDevice.IsEventsReady = true; } await RMDevice.AuthorizeAsync(); KomutYukle(); btnMenuOgren.Enabled = cmbKomutListe.Enabled = btnIR_Learn.Enabled = btnRF_Learn.Enabled = btnLearnCancel.Enabled = btnKomutGonder.Enabled = btnKomutlariKaydet.Enabled = btnIceAktar.Enabled = true; btnConnect.Text = "Bağlanıldı"; } else { HelperMy.Notification(Color.Red, "Yazılıma uyumlu cihaz tespit edilemedi!"); } btnConnect.Enabled = true; }
public async Task Search() { EventHandler <BroadlinkDevice> handler = (s, e) => { rmDevice = e as RMDevice; }; discoverClient.DeviceHandler += handler; rmDevice = null; discoverClient.DiscoverAsync().Wait(); for (int i = 0; i < DefaultWaitTime && rmDevice == null; i++) { await Task.Delay(1000); } discoverClient.DeviceHandler -= handler; }
public static void SendCommand(this RMDevice sender, string command) { sender.SendRemoteCommandAsync(command.HexToBytes()).Wait(); }