示例#1
0
    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;
    }