public void UpdateDevice(ArduinoDevice device) { //check pins foreach (var dpin in device.Pins) { //check that pins have the required capability if (!IsPinCapable(dpin)) { throw new Exception("Cannot update device " + device.Name + " as pin " + dpin.PinNumber + " board does not support capability"); } //check no conflict with existing pin usage foreach (var dev in _devices.Values) { if (dev != device && !dev.IsPinCompatible(dpin)) { throw new Exception("Cannot update device " + device.Name + " as it is not pin-compatible with " + dev.Name); } } } //maybe this was a new pin so needs to be added to the _pin2device maaping foreach (var dpin in device.Pins) { if (!_pin2device.ContainsKey(dpin.PinNumber)) { _pin2device[dpin.PinNumber] = new List <ArduinoDevice>(); } if (!_pin2device[dpin.PinNumber].Contains(device)) { _pin2device[dpin.PinNumber].Add(device); } } }
public void AddDeviceCommands(ArduinoDevice device) { Message.AddValue("DeviceID", device.ID); var cms = device.GetCommands(); Message.AddValue("DeviceCommands", cms.Select(i => i.CommandAlias).ToList()); }
virtual protected bool HandleADMDeviceCommand(ArduinoDeviceManager adm, String deviceID, String command, List <Object> args, Message response) { if (adm == null) { throw new Exception("No ADM provided"); } if (!adm.HasDevice(deviceID)) { throw new Exception(String.Format("Device {0} has not been added to ADM", deviceID)); } bool respond = true; ArduinoDevice device = null; MessageSchema schema = new ADMService.MessageSchema(response); switch (command) { case "list-commands": device = adm.GetDevice(deviceID); schema.AddDeviceCommands(device); break; case "status": device = adm.GetDevice(deviceID); if (device.BoardID == 0) { throw new Exception(String.Format("Device {0} does not have a board ID", deviceID)); } AddADMRequest(adm, adm.RequestStatus(device.BoardID), response.Target); respond = false; break; default: var commands = command.Split(','); foreach (var cmd in commands) { var tcmd = cmd.Trim(); if (tcmd.ToLower().IndexOf("wait") == 0) { int delay = tcmd.Length > 4 ? System.Convert.ToInt16(tcmd.Substring(4, tcmd.Length - 4)) : 200; System.Threading.Thread.Sleep(delay); } else { byte tag = adm.IssueCommand(deviceID, tcmd, args); if (tag > 0) { AddADMRequest(adm, tag, response.Target); respond = false; } } } break; } return(respond); }
public void AddDevice(ArduinoDevice dev) { if (dev == null) { return; } if (!Devices.Contains(dev)) { Devices.Add(dev); } }
public void PrepareForBroadcast(ArduinoDeviceManager adm) { ADMMessage message = (ADMMessage)Message; message.AddValue("BoardID", adm.BoardID); ArduinoDevice dev = null; if (message.TargetID > 0) { dev = adm.GetDeviceByBoardID(message.TargetID); } else if (message.Sender != null && message.Sender != String.Empty) { dev = adm.GetDevice(message.Sender); } message.AddValue(DEVICE_ID, dev != null ? dev.ID : ""); message.AddValue(DEVICE_NAME, dev != null ? dev.Name : ""); }
public ArduinoDevice AddDevice(ArduinoDevice device) { if (device.ID == null) { throw new Exception("Cannot add this device as it does not have a valid ID"); } if (State != ADMState.DEVICE_READY) { throw new Exception("Cannot add device " + device.ID + " as ADM state must be DEVICE_READY ... currently " + State); } if (_devices.ContainsKey(device.ID)) { throw new Exception("Cannot add this device because there is already a device with ID " + device.ID); } if (_devices.Count >= MaxDevices) { throw new Exception(String.Format("Cannot add this device because there are already a max of {0} devices.", MaxDevices)); } //if no board ID is given then autogenerate one if (device.BoardID == 0) { if (_device2boardID.ContainsKey(device.ID)) { device.BoardID = _device2boardID[device.ID]; } else { if (_device2boardID.Count == 255) { throw new Exception("Cannot automatically assign Board ID to device " + device.ID); } device.BoardID = (byte)(_device2boardID.Count + 1); } } if (device.BoardID > 0 && _device2boardID.ContainsKey(device.ID) && _device2boardID[device.ID] == device.BoardID) { throw new Exception("Cannot add this device because there is already a device using board ID " + device.BoardID); } foreach (var dpin in device.Pins) { //check that pins have the required capability if (!IsPinCapable(dpin)) { throw new Exception("Cannot add device " + device.Name + " as pin " + dpin.PinNumber + " board does not support capability"); } //check no conflict with existing pin usage foreach (var dev in _devices.Values) { if (!dev.IsPinCompatible(dpin)) { throw new Exception("Cannot add device " + device.Name + " as it is not pin-compatible with " + dev.Name); } } } device.Mgr = this; _devices[device.ID] = device; if (device.BoardID > 0) { _device2boardID[device.ID] = device.BoardID; _boardID2device[device.BoardID] = device; } foreach (var dpin in device.Pins) { if (!_pin2device.ContainsKey(dpin.PinNumber)) { _pin2device[dpin.PinNumber] = new List <ArduinoDevice>(); } if (_pin2device[dpin.PinNumber].Contains(device)) { throw new Exception("Device manager cannot contain the same device"); } _pin2device[dpin.PinNumber].Add(device); } //configure the pins on the board //TODO: analog pins foreach (var dpin in device.Pins) { switch (dpin.Mode) { case PinMode.DigitalInput: case PinMode.DigitalOutput: SetDigitalPinMode(dpin.PinNumber, dpin.Mode); if (dpin.InitialValue != -1) { SetDigitalPin(dpin.PinNumber, dpin.InitialValue > 0); } break; } } return(device); }