// Real usage example public virtual void BaseSetupRecommendation() { // Unique name for the application which communicates with Tradfri gateway string applicationName = "UnitTestApp"; TradfriController controller = new TradfriController("GatewayName", "IP"); // This line should only be called once per applicationName // Gateway generates one appSecret key per applicationName TradfriAuth appSecret = controller.GenerateAppSecret("PSK", applicationName); // You should now save programatically appSecret.PSK value and use it // when connection to your gateway every other time controller.ConnectAppKey(appSecret.PSK, applicationName); }
private async void Main_Load(object sender, EventArgs e) { //preload colors combobox cmbColors.DisplayMember = "ColorName"; //cmbColors.SelectedValueChanged += (s, ev) => cmbColors.SelectedText = s.ToString(); cmbColors.ValueMember = "ColorId"; //cmbColors.SelectedValueChanged += (s, ev) => cmbColors.SelectedText = s.ToString(); foreach (FieldInfo p in typeof(TradfriColors).GetFields()) { object v = p.GetValue(null); // static classes cannot be instanced, so use null... cmbColors.Items.Add(new { ColorName = p.Name, ColorId = v }); } // prepare/load settings userData = loadUserData(); // connect tradfriController = new TradfriController(Properties.Settings.Default.gatewayName, Properties.Settings.Default.gatewayIp); if (string.IsNullOrWhiteSpace(Properties.Settings.Default.appSecret)) { using (EnterGatewayPSK form = new EnterGatewayPSK(Properties.Settings.Default.appName)) { DialogResult result = form.ShowDialog(); if (result == DialogResult.OK) { // generating appSecret on gateway - appSecret is connected with the appName so you must use a combination // Gateway generates one appSecret key per applicationName TradfriAuth appSecret = tradfriController.GenerateAppSecret(form.AppSecret, Properties.Settings.Default.appName); // saving programatically appSecret.PSK value to settings Properties.Settings.Default.appSecret = appSecret.PSK; Properties.Settings.Default.Save(); } else { MessageBox.Show("You haven't entered your Gateway PSK so applicationSecret can't be generated on gateway." + Environment.NewLine + "You can also enter it manually into app.config if you have one for your app already.", "Error acquiring appSecret", MessageBoxButtons.OK, MessageBoxIcon.Error); this.Close(); } } } string secret = Properties.Settings.Default.appSecret; // connection to gateway tradfriController.ConnectAppKey(Properties.Settings.Default.appSecret, Properties.Settings.Default.appName); List <TradfriDevice> devices = new List <TradfriDevice>(await tradfriController.GatewayController.GetDeviceObjects()).OrderBy(x => x.DeviceType.ToString()).ToList(); List <TradfriDevice> lights = devices.Where(i => i.DeviceType.Equals(DeviceType.Light)).ToList(); // set datasource for dgv dgvDevices.DataSource = devices; dgvDevices.AutoGenerateColumns = true; // temporary disable autosave on rowSelectionChange loadingSelectedRows = true; //reselect rows from settings if (userData.SelectedDevices.Count > 0 && devices.Count > 0) { foreach (DataGridViewRow row in dgvDevices.Rows) { if (userData.SelectedDevices.Any(x => x == (long)row.Cells["ID"].Value)) { row.Selected = true; } } } // re-enable autosave on rowSelectionChange loadingSelectedRows = false; if (lights.Count > 0) { // acquire first and last lights from grid TradfriDevice firstLight = lights.FirstOrDefault(); TradfriDevice lastLight = lights.LastOrDefault(); // function handler for observe events void ObserveLightEvent(TradfriDevice x) { TradfriDevice eventDevice = devices.Where(d => d.ID.Equals(x.ID)).SingleOrDefault(); eventDevice = x; if (dgvDevices.SelectedRows.Count > 0) { foreach (object item in dgvDevices.SelectedRows) { if (((TradfriDevice)((DataGridViewRow)item).DataBoundItem).ID.Equals(eventDevice.ID)) { this.Invoke((MethodInvoker) delegate { Debug.WriteLine($"{DateTime.Now} - triggered: {x.DeviceType}, {x.Name}, {x.LightControl[0].State}, {x.LightControl[0].Dimmer}"); lbxLog.Items.Add($"{DateTime.Now} - triggered: {x.DeviceType}, {x.Name}, {x.LightControl[0].State}, {x.LightControl[0].Dimmer}"); lbxLog.SelectedIndex = lbxLog.Items.Count - 1; trbBrightness.Value = Convert.ToInt16(eventDevice.LightControl[0].Dimmer * (double)10 / 254); }); } } } }; ObserveLightDelegate lightDelegate = new ObserveLightDelegate(ObserveLightEvent); // observe first light from grid tradfriController.DeviceController.ObserveDevice(firstLight, x => lightDelegate(x)); // observe last light from grid if the bulbs are different if (firstLight.ID != lastLight.ID) { tradfriController.DeviceController.ObserveDevice(lastLight, x => lightDelegate(x)); } } }