Inheritance: IYConfigEntry
Ejemplo n.º 1
0
        public YConfig <RaspNodeConfig> InitConfig()
        {
            var absoluteCfgFile = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/" + this.cfgFile;

            var yconfig = new YConfig <RaspNodeConfig>(absoluteCfgFile);

            try
            {
                if (yconfig.Retrieve())
                {
                    Console.WriteLine("Config file found: {0}", absoluteCfgFile);
                    return(yconfig);
                }
                Console.WriteLine("Config retrieval failed; falling back to defaults");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Loading default configuration");
            }

            //create default conf
            RaspNodeConfig cfg = new RaspNodeConfig();

            cfg.FrontendServer     = "https://cyan.yodiwo.com";
            cfg.ApiServer          = "api.yodiwo.com";
            cfg.LocalWebServer     = "http://localhost:3339";
            cfg.Uuid               = "1337Raspberry";
            cfg.MqttBrokerHostname = "api.yodiwo.com";
            cfg.YpchannelPort      = Yodiwo.API.Plegma.Constants.YPChannelPort;
            cfg.MqttUseSsl         = false;
            cfg.YpchannelSecure    = false;

            //add new active conf and save to disk
            yconfig.AddActiveConf("default", cfg);
            yconfig.Save();

            return(yconfig);
        }
Ejemplo n.º 2
0
        public YConfig<RaspNodeConfig> InitConfig()
        {
            var absoluteCfgFile = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/" + this.cfgFile;

            var yconfig = new YConfig<RaspNodeConfig>(absoluteCfgFile);

            try
            {
                if (yconfig.Retrieve())
                {
                    Console.WriteLine("Config file found: {0}", absoluteCfgFile);
                    return yconfig;
                }
                Console.WriteLine("Config retrieval failed; falling back to defaults");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Loading default configuration");
            }

            //create default conf
            RaspNodeConfig cfg = new RaspNodeConfig();
            cfg.FrontendServer = "https://cyan.yodiwo.com";
            cfg.ApiServer = "api.yodiwo.com";
            cfg.LocalWebServer = "http://localhost:3339";
            cfg.Uuid = "1337Raspberry";
            cfg.MqttBrokerHostname = "api.yodiwo.com";
            cfg.YpchannelPort = Yodiwo.API.Plegma.Constants.YPChannelPort;
            cfg.MqttUseSsl = false;
            cfg.YpchannelSecure = false;

            //add new active conf and save to disk
            yconfig.AddActiveConf("default", cfg);
            yconfig.Save();

            return yconfig;
        }
Ejemplo n.º 3
0
        public void Start()
        {
            #region Configurations

            this.YConfig = this.InitConfig();
            this.ActiveCfg = this.YConfig.GetActiveConf();
            NodeConfig conf = new NodeConfig()
            {
                uuid = ActiveCfg.Uuid,
                Name = "RaspberryNode",
                MqttBrokerHostname = ActiveCfg.MqttBrokerHostname,
                MqttUseSsl = ActiveCfg.MqttUseSsl,
                YpServer = ActiveCfg.ApiServer,
                YpchannelPort = ActiveCfg.YpchannelPort,
                SecureYpc = ActiveCfg.YpchannelSecure,
                FrontendServer = ActiveCfg.FrontendServer
            };

            #endregion

            #region Things setup

            #region Setup Led1 thing
            {
                var thing = Led1Thing = new Yodiwo.API.Plegma.Thing()
                {
                    Type = "yodiwo.input.leds.simple",
                    ThingKey = new ThingKey(NodeKey, GenerateThingID()),
                    Name = "Raspberry Led 1",
                    Config = null,
                    UIHints = new ThingUIHints()
                    {
                        IconURI = "/Content/RaspberryNode/img/icon-thing-led.png",
                    },
                };
                thing.Ports = new List<Yodiwo.API.Plegma.Port>()
                {
                    new Yodiwo.API.Plegma.Port()
                    {
                        ioDirection = Yodiwo.API.Plegma.ioPortDirection.Input,
                        Name = "LedState",
                        State = "0",
                        Type = Yodiwo.API.Plegma.ePortType.Boolean,
                        PortKey = PortKey.BuildFromArbitraryString("$ThingKey$", "0")
                    }
                };
                Things.Add(thing.ThingKey, thing);
                PkeyToLed.Add(thing.Ports[0].PortKey, LedPin.Led1);
            }
            #endregion

            #region Setup Led2 thing
            {
                var thing = Led2Thing = new Yodiwo.API.Plegma.Thing()
                {
                    Type = "yodiwo.input.leds.simple",
                    ThingKey = new ThingKey(NodeKey, GenerateThingID()),
                    Name = "Raspberry Led 2",
                    Config = null,
                    UIHints = new ThingUIHints()
                    {
                        IconURI = "/Content/RaspberryNode/img/icon-thing-led.png",
                    },
                };
                thing.Ports = new List<Yodiwo.API.Plegma.Port>()
                {
                    new Yodiwo.API.Plegma.Port()
                    {
                        ioDirection = Yodiwo.API.Plegma.ioPortDirection.Input,
                        Name = "LedState",
                        State = "0",
                        Type = Yodiwo.API.Plegma.ePortType.Boolean,
                        PortKey = PortKey.BuildFromArbitraryString("$ThingKey$", "0")
                    }
                };
                Things.Add(thing.ThingKey, thing);
                PkeyToLed.Add(thing.Ports[0].PortKey, LedPin.Led2);
            }
            #endregion

            #region Setup Led3 thing
            {
                var thing = Led3Thing = new Yodiwo.API.Plegma.Thing()
                {
                    Type = "yodiwo.input.leds.simple",
                    ThingKey = new ThingKey(NodeKey, GenerateThingID()),
                    Name = "Raspberry Led 3",
                    Config = null,
                    UIHints = new ThingUIHints()
                    {
                        IconURI = "/Content/RaspberryNode/img/icon-thing-led.png",
                    },
                };
                thing.Ports = new List<Yodiwo.API.Plegma.Port>()
                {
                    new Yodiwo.API.Plegma.Port()
                    {
                        ioDirection = Yodiwo.API.Plegma.ioPortDirection.Input,
                        Name = "LedState",
                        State = "0",
                        Type = Yodiwo.API.Plegma.ePortType.Boolean,
                        PortKey = PortKey.BuildFromArbitraryString("$ThingKey$", "0")
                    }
                };
                Things.Add(thing.ThingKey, thing);
                PkeyToLed.Add(thing.Ports[0].PortKey, LedPin.Led3);
            }
            #endregion

            #region Setup Led4 thing
            {
                var thing = Led4Thing = new Yodiwo.API.Plegma.Thing()
                {
                    Type = "yodiwo.input.leds.simple",
                    ThingKey = new ThingKey(NodeKey, GenerateThingID()),
                    Name = "Raspberry Led 4",
                    Config = null,
                    UIHints = new ThingUIHints()
                    {
                        IconURI = "/Content/RaspberryNode/img/icon-thing-led.png",
                    },
                };
                thing.Ports = new List<Yodiwo.API.Plegma.Port>()
                {
                    new Yodiwo.API.Plegma.Port()
                    {
                        ioDirection = Yodiwo.API.Plegma.ioPortDirection.Input,
                        Name = "LedState",
                        State = "0",
                        Type = Yodiwo.API.Plegma.ePortType.Boolean,
                        PortKey = PortKey.BuildFromArbitraryString("$ThingKey$", "0")
                    }
                };
                Things.Add(thing.ThingKey, thing);
                PkeyToLed.Add(thing.Ports[0].PortKey, LedPin.Led4);
            }
            #endregion

            #region Setup Led5 thing
            {
                var thing = Led5Thing = new Yodiwo.API.Plegma.Thing()
                {
                    Type = "yodiwo.input.leds.simple",
                    ThingKey = new ThingKey(NodeKey, GenerateThingID()),
                    Name = "Raspberry Led 5",
                    Config = null,
                    UIHints = new ThingUIHints()
                    {
                        IconURI = "/Content/RaspberryNode/img/icon-thing-led.png",
                    },
                };
                thing.Ports = new List<Yodiwo.API.Plegma.Port>()
                {
                    new Yodiwo.API.Plegma.Port()
                    {
                        ioDirection = Yodiwo.API.Plegma.ioPortDirection.Input,
                        Name = "LedState",
                        State = "0",
                        Type = Yodiwo.API.Plegma.ePortType.Boolean,
                        PortKey = PortKey.BuildFromArbitraryString("$ThingKey$", "0")
                    }
                };
                Things.Add(thing.ThingKey, thing);
                PkeyToLed.Add(thing.Ports[0].PortKey, LedPin.Led5);
            }
            #endregion

            #endregion

            #region Node construction
            //prepare pairing module
            var pairmodule = new Yodiwo.Node.Pairing.NancyPairing.NancyPairing();
            //prepare node graph manager module
            var nodeGraphManager = new Yodiwo.NodeLibrary.Graphs.NodeGraphManager(
                                                new Type[]
                                                    {
                                                        typeof(Yodiwo.Logic.BlockLibrary.Basic.Librarian),
                                                        typeof(Yodiwo.Logic.BlockLibrary.Extended.Librarian),
                                                    });
            //create node
            Node = new Yodiwo.NodeLibrary.Node(conf,
                                                Things.Values.ToList(),
                                                pairmodule,
                                                null, null,
                                                NodeGraphManager: nodeGraphManager
                                                //MqttTransport: typeof(Yodiwo.NodeLibrary.Transports.MQTT)
                                                );

            #endregion

            #region Register port event handlers

            Node.PortEventHandlers[Led1Thing.Ports[0]] = data =>
            {
                var ledState = data.ParseToBool();
                var led = PkeyToLed.TryGetOrDefault(Led1Thing.Ports[0].PortKey, LedPin.Unknown);
                Console.WriteLine("==> Rx port event msg for led {0}", led);
                if (led != LedPin.Unknown)
                {
                    SetLedState(led, ledState);
                }
            };

            Node.PortEventHandlers[Led2Thing.Ports[0]] = data =>
            {
                var ledState = data.ParseToBool();
                var led = PkeyToLed.TryGetOrDefault(Led2Thing.Ports[0].PortKey, LedPin.Unknown);
                Console.WriteLine("==> Rx port event msg for led {0}", led);
                if (led != LedPin.Unknown)
                {
                    SetLedState(led, ledState);
                }
            };

            Node.PortEventHandlers[Led3Thing.Ports[0]] = data =>
            {
                var ledState = data.ParseToBool();
                var led = PkeyToLed.TryGetOrDefault(Led3Thing.Ports[0].PortKey, LedPin.Unknown);
                Console.WriteLine("==> Rx port event msg for led {0}", led);
                if (led != LedPin.Unknown)
                {
                    SetLedState(led, ledState);
                }
            };

            Node.PortEventHandlers[Led4Thing.Ports[0]] = data =>
            {
                var ledState = data.ParseToBool();
                var led = PkeyToLed.TryGetOrDefault(Led4Thing.Ports[0].PortKey, LedPin.Unknown);
                Console.WriteLine("==> Rx port event msg for led {0}", led);
                if (led != LedPin.Unknown)
                {
                    SetLedState(led, ledState);
                }
            };

            Node.PortEventHandlers[Led5Thing.Ports[0]] = data =>
            {
                var ledState = data.ParseToBool();
                var led = PkeyToLed.TryGetOrDefault(Led5Thing.Ports[0].PortKey, LedPin.Unknown);
                Console.WriteLine("==> Rx port event msg for led {0}", led);
                if (led != LedPin.Unknown)
                {
                    SetLedState(led, ledState);
                }
            };

            #endregion

            #region Register callbacks

            Node.OnChangedState += OnChangedStateCb;
            Node.OnNodePaired += OnPairedCb;
            Node.OnTransportConnected += OnTransportConnectedCb;
            Node.OnTransportDisconnected += OnTransportDisconnectedCb;
            Node.OnTransportError += OnTransportErrorCb;
            Node.OnUnexpectedMessage = OnUnexpectedMessageCb;

            #endregion

            #region Pairing/Connect

            if (String.IsNullOrWhiteSpace(ActiveCfg.NodeKey))
            {
                DebugEx.TraceLog("Starting pairing procedure.");
                var task = Node.StartPairing(ActiveCfg.FrontendServer, null, ActiveCfg.LocalWebServer);
            }
            else
            {
                Node.SetupNodeKeys(ActiveCfg.NodeKey, ActiveCfg.NodeSecret);
                DebugEx.TraceLog("Node already paired: NodeKey = "
                    + ActiveCfg.NodeKey + ", NodeSecret = ", ActiveCfg.NodeSecret);

                Node.Connect();
            }

            #endregion
        }
Ejemplo n.º 4
0
        public void Start()
        {
            #region Configurations

            this.YConfig   = this.InitConfig();
            this.ActiveCfg = this.YConfig.GetActiveConf();
            NodeConfig conf = new NodeConfig()
            {
                uuid = ActiveCfg.Uuid,
                Name = "RaspberryNode",
                MqttBrokerHostname = ActiveCfg.MqttBrokerHostname,
                MqttUseSsl         = ActiveCfg.MqttUseSsl,
                YpServer           = ActiveCfg.ApiServer,
                YpchannelPort      = ActiveCfg.YpchannelPort,
                SecureYpc          = ActiveCfg.YpchannelSecure,
                FrontendServer     = ActiveCfg.FrontendServer
            };

            #endregion

            #region Things setup

            #region Setup Led1 thing
            {
                var thing = Led1Thing = new Yodiwo.API.Plegma.Thing()
                {
                    Type     = "yodiwo.input.leds.simple",
                    ThingKey = new ThingKey(NodeKey, GenerateThingID()),
                    Name     = "Raspberry Led 1",
                    Config   = null,
                    UIHints  = new ThingUIHints()
                    {
                        IconURI = "/Content/RaspberryNode/img/icon-thing-led.png",
                    },
                };
                thing.Ports = new List <Yodiwo.API.Plegma.Port>()
                {
                    new Yodiwo.API.Plegma.Port()
                    {
                        ioDirection = Yodiwo.API.Plegma.ioPortDirection.Input,
                        Name        = "LedState",
                        State       = "0",
                        Type        = Yodiwo.API.Plegma.ePortType.Boolean,
                        PortKey     = PortKey.BuildFromArbitraryString("$ThingKey$", "0")
                    }
                };
                Things.Add(thing.ThingKey, thing);
                PkeyToLed.Add(thing.Ports[0].PortKey, LedPin.Led1);
            }
            #endregion

            #region Setup Led2 thing
            {
                var thing = Led2Thing = new Yodiwo.API.Plegma.Thing()
                {
                    Type     = "yodiwo.input.leds.simple",
                    ThingKey = new ThingKey(NodeKey, GenerateThingID()),
                    Name     = "Raspberry Led 2",
                    Config   = null,
                    UIHints  = new ThingUIHints()
                    {
                        IconURI = "/Content/RaspberryNode/img/icon-thing-led.png",
                    },
                };
                thing.Ports = new List <Yodiwo.API.Plegma.Port>()
                {
                    new Yodiwo.API.Plegma.Port()
                    {
                        ioDirection = Yodiwo.API.Plegma.ioPortDirection.Input,
                        Name        = "LedState",
                        State       = "0",
                        Type        = Yodiwo.API.Plegma.ePortType.Boolean,
                        PortKey     = PortKey.BuildFromArbitraryString("$ThingKey$", "0")
                    }
                };
                Things.Add(thing.ThingKey, thing);
                PkeyToLed.Add(thing.Ports[0].PortKey, LedPin.Led2);
            }
            #endregion

            #region Setup Led3 thing
            {
                var thing = Led3Thing = new Yodiwo.API.Plegma.Thing()
                {
                    Type     = "yodiwo.input.leds.simple",
                    ThingKey = new ThingKey(NodeKey, GenerateThingID()),
                    Name     = "Raspberry Led 3",
                    Config   = null,
                    UIHints  = new ThingUIHints()
                    {
                        IconURI = "/Content/RaspberryNode/img/icon-thing-led.png",
                    },
                };
                thing.Ports = new List <Yodiwo.API.Plegma.Port>()
                {
                    new Yodiwo.API.Plegma.Port()
                    {
                        ioDirection = Yodiwo.API.Plegma.ioPortDirection.Input,
                        Name        = "LedState",
                        State       = "0",
                        Type        = Yodiwo.API.Plegma.ePortType.Boolean,
                        PortKey     = PortKey.BuildFromArbitraryString("$ThingKey$", "0")
                    }
                };
                Things.Add(thing.ThingKey, thing);
                PkeyToLed.Add(thing.Ports[0].PortKey, LedPin.Led3);
            }
            #endregion

            #region Setup Led4 thing
            {
                var thing = Led4Thing = new Yodiwo.API.Plegma.Thing()
                {
                    Type     = "yodiwo.input.leds.simple",
                    ThingKey = new ThingKey(NodeKey, GenerateThingID()),
                    Name     = "Raspberry Led 4",
                    Config   = null,
                    UIHints  = new ThingUIHints()
                    {
                        IconURI = "/Content/RaspberryNode/img/icon-thing-led.png",
                    },
                };
                thing.Ports = new List <Yodiwo.API.Plegma.Port>()
                {
                    new Yodiwo.API.Plegma.Port()
                    {
                        ioDirection = Yodiwo.API.Plegma.ioPortDirection.Input,
                        Name        = "LedState",
                        State       = "0",
                        Type        = Yodiwo.API.Plegma.ePortType.Boolean,
                        PortKey     = PortKey.BuildFromArbitraryString("$ThingKey$", "0")
                    }
                };
                Things.Add(thing.ThingKey, thing);
                PkeyToLed.Add(thing.Ports[0].PortKey, LedPin.Led4);
            }
            #endregion

            #region Setup Led5 thing
            {
                var thing = Led5Thing = new Yodiwo.API.Plegma.Thing()
                {
                    Type     = "yodiwo.input.leds.simple",
                    ThingKey = new ThingKey(NodeKey, GenerateThingID()),
                    Name     = "Raspberry Led 5",
                    Config   = null,
                    UIHints  = new ThingUIHints()
                    {
                        IconURI = "/Content/RaspberryNode/img/icon-thing-led.png",
                    },
                };
                thing.Ports = new List <Yodiwo.API.Plegma.Port>()
                {
                    new Yodiwo.API.Plegma.Port()
                    {
                        ioDirection = Yodiwo.API.Plegma.ioPortDirection.Input,
                        Name        = "LedState",
                        State       = "0",
                        Type        = Yodiwo.API.Plegma.ePortType.Boolean,
                        PortKey     = PortKey.BuildFromArbitraryString("$ThingKey$", "0")
                    }
                };
                Things.Add(thing.ThingKey, thing);
                PkeyToLed.Add(thing.Ports[0].PortKey, LedPin.Led5);
            }
            #endregion

            #endregion

            #region Node construction
            //prepare pairing module
            var pairmodule = new Yodiwo.Node.Pairing.NancyPairing.NancyPairing();
            //prepare node graph manager module
            var nodeGraphManager = new Yodiwo.NodeLibrary.Graphs.NodeGraphManager(
                new Type[]
            {
                typeof(Yodiwo.Logic.BlockLibrary.Basic.Librarian),
                typeof(Yodiwo.Logic.BlockLibrary.Extended.Librarian),
            });
            //create node
            Node = new Yodiwo.NodeLibrary.Node(conf,
                                               Things.Values.ToList(),
                                               pairmodule,
                                               null, null,
                                               NodeGraphManager: nodeGraphManager
                                               //MqttTransport: typeof(Yodiwo.NodeLibrary.Transports.MQTT)
                                               );

            #endregion

            #region Register port event handlers

            Node.PortEventHandlers[Led1Thing.Ports[0]] = data =>
            {
                var ledState = data.ParseToBool();
                var led      = PkeyToLed.TryGetOrDefault(Led1Thing.Ports[0].PortKey, LedPin.Unknown);
                Console.WriteLine("==> Rx port event msg for led {0}", led);
                if (led != LedPin.Unknown)
                {
                    SetLedState(led, ledState);
                }
            };

            Node.PortEventHandlers[Led2Thing.Ports[0]] = data =>
            {
                var ledState = data.ParseToBool();
                var led      = PkeyToLed.TryGetOrDefault(Led2Thing.Ports[0].PortKey, LedPin.Unknown);
                Console.WriteLine("==> Rx port event msg for led {0}", led);
                if (led != LedPin.Unknown)
                {
                    SetLedState(led, ledState);
                }
            };

            Node.PortEventHandlers[Led3Thing.Ports[0]] = data =>
            {
                var ledState = data.ParseToBool();
                var led      = PkeyToLed.TryGetOrDefault(Led3Thing.Ports[0].PortKey, LedPin.Unknown);
                Console.WriteLine("==> Rx port event msg for led {0}", led);
                if (led != LedPin.Unknown)
                {
                    SetLedState(led, ledState);
                }
            };

            Node.PortEventHandlers[Led4Thing.Ports[0]] = data =>
            {
                var ledState = data.ParseToBool();
                var led      = PkeyToLed.TryGetOrDefault(Led4Thing.Ports[0].PortKey, LedPin.Unknown);
                Console.WriteLine("==> Rx port event msg for led {0}", led);
                if (led != LedPin.Unknown)
                {
                    SetLedState(led, ledState);
                }
            };

            Node.PortEventHandlers[Led5Thing.Ports[0]] = data =>
            {
                var ledState = data.ParseToBool();
                var led      = PkeyToLed.TryGetOrDefault(Led5Thing.Ports[0].PortKey, LedPin.Unknown);
                Console.WriteLine("==> Rx port event msg for led {0}", led);
                if (led != LedPin.Unknown)
                {
                    SetLedState(led, ledState);
                }
            };

            #endregion

            #region Register callbacks

            Node.OnChangedState          += OnChangedStateCb;
            Node.OnNodePaired            += OnPairedCb;
            Node.OnTransportConnected    += OnTransportConnectedCb;
            Node.OnTransportDisconnected += OnTransportDisconnectedCb;
            Node.OnTransportError        += OnTransportErrorCb;
            Node.OnUnexpectedMessage      = OnUnexpectedMessageCb;

            #endregion

            #region Pairing/Connect

            if (String.IsNullOrWhiteSpace(ActiveCfg.NodeKey))
            {
                DebugEx.TraceLog("Starting pairing procedure.");
                var task = Node.StartPairing(ActiveCfg.FrontendServer, null, ActiveCfg.LocalWebServer);
            }
            else
            {
                Node.SetupNodeKeys(ActiveCfg.NodeKey, ActiveCfg.NodeSecret);
                DebugEx.TraceLog("Node already paired: NodeKey = "
                                 + ActiveCfg.NodeKey + ", NodeSecret = ", ActiveCfg.NodeSecret);

                Node.Connect();
            }

            #endregion
        }