/// <summary> /// Here we read out the datapoints and add the description to the dataGridView /// </summary> public void readDatapointDescriptions(Connector connector) { try { // change mouse cursor to busy this.Cursor = Cursors.WaitCursor; // get datapoint description BaosDatapointDescriptions baosDatapointDescriptions = new BaosDatapointDescriptions(connector); baosDatapointDescriptions.readFromDevice(); List <BaosDatapointDescription> descriptions = baosDatapointDescriptions.getDescriptions(); foreach (BaosDatapointDescription desc in descriptions) { BaosDatapoint datapoint = new BaosDatapoint(connector, desc.getId()); String size = desc.isBitType() ? string.Format("{0} Bits", desc.getValueTypeSizeBits()) : string.Format("{0} Bytes", desc.getValueTypeSizeBytes()); dataGridView.Rows.Add(datapoint.getId(), datapoint.getDescription(), size); } } catch (kdrive.KdriveException exception) { MessageBox.Show(exception.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } finally { // reset mouse cursor this.Cursor = Cursors.Default; } }
static void switchLight(Connector connector, bool enabled) { BaosDatapoint d = new BaosDatapoint(connector, 1); if (d.getBoolean() != enabled) { string description = d.getDescription(); string action = enabled ? "on" : "off"; Console.WriteLine("Switching {0} : {1}", description, action); d.setBoolean(enabled); } }
/// <summary> /// Sends the datapoint value write request to the BAOS device /// Our datapoint is a 1 bit boolean value, so we use the setBoolean function /// There are several other encoding functions available, please refer to the /// documentation /// </summary> private void sendBoolean(ushort id, bool enabled) { try { if (connector != null) { BaosDatapoint datapoint = new BaosDatapoint(connector, id); datapoint.setBoolean(enabled); } } catch (kdrive.KdriveException exception) { MessageBox.Show(exception.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
/// <summary> /// Reads the datapoint value from the BAOS device /// The response is returned via the BaosEvent event indication /// so we don't have to handle it here /// </summary> bool readFloat(ushort id) { bool success = false; try { if (connector != null) { BaosDatapoint baosDatapoint = new BaosDatapoint(connector, id); baosDatapoint.get2OctetFloat(); success = true; } } catch (kdrive.KdriveException exception) { MessageBox.Show(exception.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } return(success); }
/// <summary> /// This function will called in the main thread /// The datapoint indication event handler /// We show that the light has been switched /// by changing the image associated with the indicated channel /// </summary> private void onDatapointEvent(object sender, BaosDatapoint datapoint) { try { ushort dpId = datapoint.getId(); switch (dpId) { case (ushort)GroupObject.Ch1Status: case (ushort)GroupObject.Ch2Status: switchLight(dpId, datapoint.getBoolean()); break; case (ushort)GroupObject.Ch3Temperature: updateFloat(dpId, datapoint.get2OctetFloat()); break; } } catch (kdrive.KdriveException exception) { MessageBox.Show(exception.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
/// <summary> /// This function will called from the event handler. It is in the context of the notification thread. /// It is not safe here to update the gui, so we must invoke to run it in the main thread /// </summary> private void onDatapointEventInThreadContext(object sender, BaosDatapoint datapoint) { Invoke(new BaosEvent.DatapointEventHandler(onDatapointEvent), new object[] { sender, datapoint }); }