// called when a device is unplugged public void deviceRemoval(YModule m) { //Cycle on All DropDown gauges for (int j = 0; j < gauges.GetLength(0); j++) { ComboBox d = gauges[j].getDropDown(); int selected = d.SelectedIndex; // cycle on all gauge dropdown items for (int i = d.Items.Count - 1; i >= 0; i--) { //search for function stored in the drop down list MyTemperature mt = (MyTemperature)d.Items[i]; YTemperature t = mt.getYTemperature(); // test if the fucntion parent module is the the one removed // note : it's too late to use get_module on t, so with use // a little trick: with stored the module serial in the function // userdate. if ((string)t.get_userData() == m.get_serialNumber()) { // remove it from the drop down d.Items.RemoveAt(i); // selected index update if (selected == i) { selected--; } if (selected >= d.Items.Count) { selected = d.Items.Count - 1; } } } if (selected >= 0) { d.SelectedIndex = selected; } } // disable empty dropdowns for (int j = 0; j < gauges.GetLength(0); j++) { ComboBox d = gauges[j].getDropDown(); if (d.Enabled && d.Items.Count <= 0) { d.Enabled = false; } } }
// poll temperature function for all gauges and // update UI acordingly private void refreshUI() { for (int j = 0; j < gauges.GetLength(0); j++) { ComboBox d = gauges[j].getDropDown(); if (d.Enabled) { if (d.SelectedIndex >= 0) { MyTemperature mt = (MyTemperature)d.Items[d.SelectedIndex]; YTemperature t = mt.getYTemperature(); gauges[j].setValue(t.get_currentValue()); } } gauges[j].refresh(); } }
// called when a device is plugged public void deviceArrival(YModule m) { // new device arrived, lets check if any Ytemperature function are hosted int fctCount = m.functionCount(); YTemperature t; // list the functions availble on the device for (int i = 0; i < fctCount; i++) { string fname = m.functionId(i); if (fname.StartsWith("temperature")) { t = YTemperature.FindTemperature(m.get_serialNumber() + '.' + fname); // used in deviceRemoval t.set_userData(t.get_module().get_serialNumber()); // ok temperature found, lets add it to all gauges control dropdown for (int j = 0; j < gauges.GetLength(0); j++) { ComboBox d = gauges[j].getDropDown(); MyTemperature mt = new MyTemperature(t); d.Items.Add(mt); } } } // enable the dropdowns if not empty for (int j = 0; j < gauges.GetLength(0); j++) { ComboBox d = gauges[j].getDropDown(); if (!d.Enabled && d.Items.Count > 0) { if (j < d.Items.Count) { d.SelectedIndex = j; d.Enabled = true; } } } }