public byte[] ToArray() { var stack = new SimpleStack(); stack.Push(Index); stack.PushName(Device); stack.Push(Status); return stack.Data; }
public byte[] ToArray() { var stack = new SimpleStack(); stack.PushName(_pin); stack.Push(_value); stack.Push((byte)(_isActive ? 1 : 0)); return stack.Data; }
public byte[] ToArray() { var stack = new SimpleStack(); stack.Push(Index); stack.PushName(Pin); stack.Push(Value); return stack.Data; }
public byte[] ToArray() { var stack = new SimpleStack(); stack.PushName(_device); stack.Push(_action); stack.Push(_values); return stack.Data; }
public byte[] ToArray() { var stack = new SimpleStack(); stack.PushName(_device); stack.Push(_event); stack.Push(_status); stack.Push(_time); stack.Push(_values); return stack.Data; }
public byte[] ToArray() { var stack = new SimpleStack(); stack.PushName(Name); stack.Push(Description); stack.Push(Location); stack.Push(Address); stack.Push(Type); stack.PushName(Hardware); stack.PushName(Version); stack.Push(DigitalInputs); stack.Push(DigitalOutputs); stack.Push(AnalogInputs); stack.Push(CounterInputs); stack.Push(PwmOutputs); stack.Push(WiresCount); stack.Push(DevicesCount); stack.Push(SensorsCount); //stack.Push(SupportedCommands); stack.Push(ResetPin); return stack.Data; }
private IList<Wire> GetWires(IEnumerable<XElement> elements, Node node, BusController bus) { var wires = new List<Wire>(); foreach (var xwire in elements) { var input = xwire.Attribute("input").Value; var pin = node.Pins.FirstOrDefault(p => p.Name == input); if (pin == null) throw new WireException("Input pin not found"); var index = Convert.ToByte(xwire.Attribute("index").Value); var address = xwire.Attribute("address") != null && !string.IsNullOrEmpty(xwire.Attribute("address").Value) ? Address.Parse(Convert.ToUInt32(xwire.Attribute("address").Value)) : Address.Empty; var useInputData = xwire.Attribute("useInputData") != null && !string.IsNullOrEmpty(xwire.Attribute("useInputData").Value) && Convert.ToBoolean(xwire.Attribute("useInputData").Value); var cmdText = xwire.Attribute("command").Value.ToUpperInvariant(); byte cmd = 0; #region parse command name switch (cmdText) { case "PING": cmd = NodeCommands.CMD_PING; break; case "RESET": cmd = NodeCommands.CMD_RESET; break; case "READ_CONFIG": cmd = NodeCommands.CMD_READ_CONFIG; break; case "START": cmd = NodeCommands.CMD_START; break; case "STOP": cmd = NodeCommands.CMD_STOP; break; //Pins case "CHANGE_DIGITAL": cmd = NodeCommands.CMD_CHANGE_DIGITAL; break; case "TOGGLE_DIGITAL": cmd = NodeCommands.CMD_TOGGLE_DIGITAL; break; case "TIMED_DIGITAL": cmd = NodeCommands.CMD_TIMED_DIGITAL; break; case "DELAY_DIGITAL": cmd = NodeCommands.CMD_DELAY_DIGITAL; break; case "PULSE_DIGITAL": cmd = NodeCommands.CMD_PULSE_DIGITAL; break; case "CYCLE_DIGITAL": cmd = NodeCommands.CMD_CYCLE_DIGITAL; break; case "CHANGE_ALL_DIGITAL": cmd = NodeCommands.CMD_CHANGE_ALL_DIGITAL; break; case "CHANGE_PWM": cmd = NodeCommands.CMD_CHANGE_PWM; break; case "CHANGE_PIN": cmd = NodeCommands.CMD_CHANGE_PIN; break; case "DELAY_TOGGLE_DIGITAL": cmd = NodeCommands.CMD_DELAY_TOGGLE_DIGITAL; break; case "DELTA_PWM": cmd = NodeCommands.CMD_DELTA_PWM; break; case "FADE_PWM": cmd = NodeCommands.CMD_FADE_PWM; break; case "ACTIVATE": cmd = NodeCommands.CMD_ACTIVATE; break; case "DEACTIVATE": cmd = NodeCommands.CMD_DEACTIVATE; break; case "EXECUTE_DEVICE_ACTION": cmd = NodeCommands.CMD_EXECUTE_DEVICE_ACTION; break; case "PUSH_SENSOR_READ": cmd = NodeCommands.CMD_READ_SENSOR; break; } #endregion var dataText = xwire.Attribute("data") != null ? xwire.Attribute("data").Value : string.Empty; var data = Csv.CsvToList<byte>(dataText).ToArray(); //var trgText = xwire.Attribute("trigger") != null ? xwire.Attribute("trigger").Value : string.Empty; //var trgs = Csv.CsvToList<string>(trgText).ToArray(); //var trigger = WireTriggers.None; //foreach (var trg in trgs) //{ // trigger |= trg != null && Enum.IsDefined(typeof (WireTriggers), trg) // ? (WireTriggers) Enum.Parse(typeof (WireTriggers), trg) // : WireTriggers.None; //} var wire = new Wire(pin) { Index = index, Command = cmd, Address = address, UseInputData = useInputData, Parameters = data }; //Add wire trigger event //TODO configurable on Activate/deactivate/change wire.OnWireTriggered += (sender, args) => { var w = (Wire)sender; var stack = new SimpleStack(w.Parameters); if (w.UseInputData) { stack.Push(args.Source.Value); } if (w.Address == Address.Empty || w.Address == bus.Address) node.Execute(w.Address, w.Command, stack.Data); else bus.SendImmediate(w.Command, w.Address, stack.Data); }; wires.Add(wire); } Log.Debug(string.Format("Configured {0} wires", wires.Count)); return wires; }
public HBusProcessor(BusController hbus) { _bus = hbus; _bus.CommandReceived += OnCommandReceived; _bus.AckReceived += OnAckReceived; _scheduler = Scheduler.GetScheduler(); _hbusEvents = new Dictionary<string, IList<Event>>(); //Event from ep source OnSourceEvent = (@event, point) => { if (@event.Channel != Channel && !string.IsNullOrEmpty(@event.Channel)) return; var address = !string.IsNullOrEmpty(@event.Address) ? Address.Parse(@event.Address) : Address.BroadcastAddress; var stack = new SimpleStack(); ////Create new page event list //if (!_hbusEvents.ContainsKey(@event.Subscriber)) // _hbusEvents.Add(@event.Subscriber, new List<Event>()); switch (@event.Name) { case "node-subscribe": _bus.SendCommand(NodeCommands.CMD_ADD_NODE_LISTENER, address); break; case "node-unsubscribe": _bus.SendCommand(NodeCommands.CMD_DELETE_NODE_LISTENER, address); break; case "pin-activate": stack.PushName(@event.Source); _bus.SendCommand(NodeCommands.CMD_ACTIVATE, address, stack.Data); break; case "pin-deactivate": stack.PushName(@event.Source); _bus.SendCommand(NodeCommands.CMD_DEACTIVATE, address, stack.Data); break; case "pin-subscribe": stack.PushName(@event.Source); _bus.SendCommand(NodeCommands.CMD_ADD_PIN_LISTENER, address, stack.Data); break; case "pin-unsubscribe": stack.PushName(@event.Source); _bus.SendCommand(NodeCommands.CMD_DELETE_PIN_LISTENER, address, stack.Data); break; case "device-subscribe": stack.PushName(@event.Source); _bus.SendCommand(NodeCommands.CMD_ADD_DEVICE_LISTENER, address, stack.Data); break; case "device-unsubscribe": stack.PushName(@event.Source); _bus.SendCommand(NodeCommands.CMD_DELETE_DEVICE_LISTENER, address, stack.Data); break; case "sensor-subscribe": //This is extracted only for explantion //@event.Data could be used as it is var interval = @event.Data[0]; var expires = (ushort) (@event.Data[2] << 8 + @event.Data[1]); stack.PushName(@event.Source); stack.Push(interval); stack.Push(expires); _bus.SendCommand(NodeCommands.CMD_ADD_SENSOR_LISTENER, address, stack.Data); break; case "sensor-unsubscribe": stack.PushName(@event.Source); _bus.SendCommand(NodeCommands.CMD_DELETE_SENSOR_LISTENER, address, stack.Data); break; case "sensor-read": stack.PushName(@event.Source); _bus.SendCommand(NodeCommands.CMD_READ_SENSOR, address, stack.Data); break; //TODO: other HBus commands default: if (@event.Name.IndexOf("device-") == 0) { //Send device action var devaction = new DeviceAction(@event.Source, @event.Name.Substring(7), @event.Data); _bus.SendCommand(NodeCommands.CMD_EXECUTE_DEVICE_ACTION, address, devaction.ToArray()); } break; } }; //Error from ep source OnSourceError = (exception, sender) => { Log.Error("Error from source endpoint", exception); }; //Close connection with ep source OnSourceClose = (sender) => { //Close HBus endpoint Stop(); Log.Debug("closed on source close"); }; }
/// <summary> /// Serialize node status to byte array /// </summary> /// <returns>data stream (byte array)</returns> public byte[] ToArray() { var stack = new SimpleStack(); //Node global status stack.Push(Mask); stack.Push(Time); stack.Push((byte)NodeStatus); stack.Push((byte)BusStatus); stack.Push((byte)LastError); stack.Push(TotalErrors); stack.Push((byte)(Inputs!= null ? Inputs.Length : 0)); stack.Push((byte)(Outputs != null ? Outputs.Length : 0)); stack.Push((byte)(Analogs != null ? Analogs.Length : 0)); stack.Push((byte)(Counters != null ? Counters.Length : 0)); stack.Push((byte)(Pwms != null ? Pwms.Length : 0)); stack.Push((byte)(Devices != null ? Devices.Length : 0)); stack.Push((byte)(Sensors != null ? Sensors.Length : 0)); stack.PushName(LastActivatedInput); stack.PushName(LastActivatedOutput); //Inputs if ((Mask & 0x01) != 0 && Inputs != null && Inputs.Length > 0) foreach (var input in Inputs) stack.Push(input); //Outputs if ((Mask & 0x02) != 0 && Outputs != null && Outputs.Length > 0) foreach (var output in Outputs) stack.Push(output); //Analogs if ((Mask & 0x04) != 0 && Analogs != null && Analogs.Length > 0) foreach (var analog in Analogs) stack.Push(analog); //Counters if ((Mask & 0x08) != 0 && Counters != null && Counters.Length > 0) foreach (var counter in Counters) stack.Push(counter); //Pwms if ((Mask & 0x10) != 0 && Pwms != null && Pwms.Length > 0) foreach (var pwm in Pwms) stack.Push(pwm); //Devices if ((Mask & 0x20) != 0 && Devices != null && Devices.Length > 0) foreach (var device in Devices) stack.Push(device); //Sensors if ((Mask & 0x40) != 0 && Sensors != null && Sensors.Length > 0) foreach (var sensor in Sensors) stack.Push(sensor); return stack.Data; }
/// <summary> /// Convert shared information /// to transmit to other devices /// </summary> /// <returns></returns> public byte[] ToArray() { var stack = new SimpleStack(); stack.Push(Index); stack.PushName(Name); //stack.Push(Address); stack.Push(Description); stack.Push(Location); stack.Push(Class); stack.Push(Hardware); stack.Push(Version); stack.PushStringArray(Actions); return stack.Data; }