Exemplo n.º 1
0
    private static void Main(string[] args)
    {
      XmlConfigurator.Configure();

#if DEBUG
      //set log level to DEBUG
      var logLevel = Level.Debug;
      ((Hierarchy)LogManager.GetRepository()).Root.Level = logLevel;
      ((Hierarchy)LogManager.GetRepository()).RaiseConfigurationChanged(EventArgs.Empty);
#endif

      Console.WriteLine("-----------------------------------------");
      Console.WriteLine("HBus Console node version {0}", Assembly.GetExecutingAssembly().GetName().Version);
#if ARTIK_DEMO_LOCAL
      Console.WriteLine("ARTIK DEMO with local node");
#endif
#if ARTIK_DEMO_RASPBERRY
      Console.WriteLine("ARTIK DEMO with raspberry node");
#endif
      Console.WriteLine("-----------------------------------------");

      //Startup parameters
      Console.WriteLine("Configuring node.");
      var defaultConfig = Convert.ToBoolean(ConfigurationManager.AppSettings["node.defaultConfiguration"]);
      var xconfig = ConfigurationManager.AppSettings["node.configurationFile"];

      //Create configurator
      _configurator = new Nodes.Configuration.XmlConfigurator(xconfig);

      //Configure all global objects
      _configurator.Configure(defaultConfig);

      //Get the node
      _node = _configurator.Node;
      _bus = _configurator.Bus;
#if DEBUG
      //Add handlers to bus
      _bus.OnMessageReceived += (source, message) =>
      {
        Console.WriteLine("Received message from {0} to {1} = {2} ({3})", message.Source, message.Destination, message.Command, message.Flags);
      };
#endif

      //Configure remote sensors for demo porpouses
#if ARTIK_DEMO_LOCAL || ARTIK_DEMO_RASPBERRY
      var addr7 = Address.Parse(7);
      _extSensors = new List<Sensor>
      {
        new Sensor { Name = "SN701", Address = addr7 }, //arduino node DHT11 temperature
        new Sensor { Name = "SN702", Address = addr7 }, //arduino node DHT11 humidity
        new Sensor { Name = "SN703", Address = addr7 } //arduino node photoresistor light
    };
#endif
      //Add handlers to sensor events
      _node.OnSensorRead += (arg1, arg2) =>
      {
        Console.WriteLine("Sensor read from {0} : {1} = {2} @ {3}", arg1, arg2.Name, arg2.Value, arg2.Time);
      };

      Console.WriteLine("Reset node.");
      //Full reset
      _node.Reset(true);

      Console.WriteLine("Start node.");
      //Start node
      _node.Start();

      Console.WriteLine("Node {0} started", _node.Name);
      Console.WriteLine("Press i [enter] for info.");
      Console.WriteLine("Press x [enter] to exit.");

      #region local test section
      //Main loop
      while (true)
      {
        var w = Console.ReadLine();
        Pin pin;
        Device device;

        switch (w)
        {
          case "0":
            _bus.SendCommand(NodeCommands.CMD_PING, Address.HostAddress);
            break;

#if ARTIK_DEMO_LOCAL || ARTIK_DEMO_RASPBERRY
          #region input pins
          case "1":
            pin = _node.Pins[0];
            pin.Activate();
            break;
          case "2":
            pin = _node.Pins[1];
            pin.Activate();
            break;
          case "3":
            pin = _node.Pins[2];
            pin.Activate();
            break;
          case "4":
            pin = _node.Pins[3];
            pin.Activate();
            break;
          case "5":
            pin = _node.Pins[4];
            pin.Activate();
            break;
          case "6":
            pin = _node.Pins[5];
            pin.Activate();
            break;
          case "7":
            pin = _node.Pins[6];
            pin.Activate();
            break;
          case "8":
            pin = _node.Pins[7];
            pin.Activate();
            break;
          #endregion

          #region output pins
          case "q":
            pin = _node.Pins[8];
            pin.Activate();
            break;
          case "w":
            pin = _node.Pins[9];
            pin.Activate();
            break;
          case "e":
            pin = _node.Pins[10];
            pin.Activate();
            break;
          case "r":
            pin = _node.Pins[11];
            pin.Activate();
            break;
          #endregion
#endif

          #region devices
#if ARTIK_DEMO_LOCAL || ARTIK_DEMO_RASPBERRY
          case "t":
            device = _node.Devices[0];
            device.ExecuteAction(new DeviceAction { Device = device.Name, Action = "open" });
            break;
          case "y":
            device = _node.Devices[0];
            device.ExecuteAction(new DeviceAction { Device = device.Name, Action = "close" });
            break;
          case "u":
            device = _node.Devices[1];
            device.ExecuteAction(new DeviceAction { Device = device.Name, Action = "open" });
            break;
          case "i":
            device = _node.Devices[1];
            device.ExecuteAction(new DeviceAction { Device = device.Name, Action = "close" });
            break;
#endif
          #endregion

          #region sensors
#if ARTIK_DEMO_LOCAL || ARTIK_DEMO_RASPBERRY
          case "a":// local sensor
            readSensor(_node.Sensors[0]);
            break;
          case "s":// local sensor
            readSensor(_node.Sensors[1]);
            break;
          case "d"://remote sensor
            readSensor(_extSensors[0]);
            break;
          case "f"://remote sensor
            readSensor(_extSensors[1]);
            break;
          case "g"://remote sensor
            readSensor(_extSensors[2]);
            break;
#endif
          #endregion

          case "h":
            ShowHelp();
            break;

          case "x":
            Console.WriteLine("Closing node...");
            _node.Close();
            _node = null;

            Console.WriteLine("Bye.");
            return;
        }

      }

    }
Exemplo n.º 2
0
    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");
      };
    }