Ejemplo n.º 1
0
        public bool StartPair(string frontendUrl, string redirectUrl, NodeConfig conf, string selfUrl, NodePairingBackend.OnPairedDelegate OnPairedcb, NodePairingBackend.OnPairingFailedDelegate OnPairingFailedCb)
        {
            lock (this)
            {
                //pairing already in progress
                if (host != null || NancyPairingModule.backend != null)
                    return false;

                //sanity checks
                if (string.IsNullOrWhiteSpace(selfUrl))
                    return false;

                this.conf = conf;

                //pairing backend            
                NancyPairingModule.backend = new Yodiwo.NodeLibrary.Pairing.NodePairingBackend(frontendUrl, conf, OnPairedcb, OnPairingFailedCb);
                startHttpServer(selfUrl);
                return true;
            }
        }
Ejemplo n.º 2
0
        //------------------------------------------------------------------------------------------------------------------------
        private void InitYodiwoConnection()
        {
            //create node configuration
            NodeConfig conf = new NodeConfig()
            {
                Name = "Clone of " + ActiveCfg.IBMInfo.IOTFDeviceName,
                YpServer = ActiveCfg.YodiwoInfo.YodiwoApiServer,
                YpchannelPort = Convert.ToInt32(ActiveCfg.YodiwoInfo.YPChannelPort),
                FrontendServer = ActiveCfg.YodiwoInfo.YodiwoRestUrl,
                CanSolveGraphs = false,
                SecureYpc = ActiveCfg.YodiwoInfo.SecureYPC
            };
            //prepare node graphmanager module
            var nodeGraphManager = new Yodiwo.NodeLibrary.Graphs.NodeGraphManager(
                                                new Type[]
                                                    {
                                                        typeof(Yodiwo.Logic.BlockLibrary.Basic.Librarian)
                                                    });
            //create the proxy node
            proxyNode = new Yodiwo.NodeLibrary.Node(conf, Things, null, null, null, NodeGraphManager: nodeGraphManager);
            //set transport
            proxyNode.Transport = Transport.YPCHANNEL;

            //register cbs
            proxyNode.OnChangedState += OnChangedStateCb;
            proxyNode.OnTransportConnected += OnTransportConnectedCb;
            proxyNode.OnTransportDisconnected += OnTransportDisconnectedCb;
            proxyNode.OnTransportError += OnTransportErrorCb;
            proxyNode.OnUnexpectedMessage = OnUnexpectedMessageCb;
            proxyNode.OnThingActivated += OnThingActivatedCb;

            //use nodekey to setup things
            proxyNode.SetupNodeKeys(ActiveCfg.YodiwoInfo.YodiwoNodeKey, ActiveCfg.YodiwoInfo.YodiwoSecretKey);

            //connect
            proxyNode.Connect();
            RegisterThingStateCbs();
        }
Ejemplo n.º 3
0
        public void Start()
        {
            YConfig = Config.Init();
            ActiveCfg = YConfig.GetActiveConf();

            NodeConfig conf = new NodeConfig()
            {
                uuid = ActiveCfg.Uuid,
                Name = "SkyWriter Node",
                MqttBrokerHostname = ActiveCfg.MqttBrokerHostname,
                MqttUseSsl = ActiveCfg.MqttUseSsl,
                YpServer = ActiveCfg.ApiServer,
                YpchannelPort = ActiveCfg.YpchannelPort,
                SecureYpc = ActiveCfg.YpchannelSecure,
                FrontendServer = ActiveCfg.FrontendServer,
                CanSolveGraphs = false,// deactivate for GrovePi
            };

            //prepare pairing module
            var pairmodule = new Yodiwo.NodeLibrary.Pairing.NancyPairing.NancyPairing();

            //create node
            node = new Yodiwo.NodeLibrary.Node(conf,
                                                pairmodule,
                                                null, null,
                                                NodeGraphManager: null
                                                );
            Helper.node = node;
            //set TransPort
            node.Transport = transport;

            //register callbacks for grovepi node
            node.OnChangedState += OnChangedStateCb;
            node.OnNodePaired += OnPaired;
            node.OnTransportConnected += OnTransportConnectedCb;
            node.OnTransportDisconnected += OnTransportDisconnectedCb;
            node.OnTransportError += OnTransportErrorCb;
            node.OnUnexpectedMessage += OnUnexpectedMessageCb;
            node.OnThingActivated += OnThingActivatedCb;
            node.OnThingDeactivated += OnThingDeactivatedCb;

            //register port event handlers

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

            //add things
            Helper.CreateThings(this.pysharp, node);

            //connect
            node.Connect();
        }
Ejemplo n.º 4
0
        public void Start()
        {
            YConfig = Config.Init();
            ActiveCfg = YConfig.GetActiveConf();

            NodeConfig conf = new NodeConfig()
            {
                uuid = ActiveCfg.Uuid,
                Name = "Raspberry Node",
                MqttBrokerHostname = ActiveCfg.MqttBrokerHostname,
                MqttUseSsl = ActiveCfg.MqttUseSsl,
                YpServer = ActiveCfg.ApiServer,
                YpchannelPort = ActiveCfg.YpchannelPort,
                SecureYpc = ActiveCfg.YpchannelSecure,
                FrontendServer = ActiveCfg.FrontendServer,
                CanSolveGraphs = false,// deactivate for GrovePi
            };

            //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,
                                                Helper.GatherThings(this.pysharp),
                                                pairmodule,
                                                null, null

                                                );
            Helper.node = node;
            //set TransPort
            node.Transport = transport;

            //register callbacks for grovepi node
            node.OnChangedState += OnChangedStateCb;
            node.OnNodePaired += OnPaired;
            node.OnTransportConnected += OnTransportConnectedCb;
            node.OnTransportDisconnected += OnTransportDisconnectedCb;
            node.OnTransportError += OnTransportErrorCb;
            node.OnUnexpectedMessage = OnUnexpectedMessageCb;
            node.OnUnexpectedRequest = HandleUnknownRequest;
            node.OnThingActivated += OnThingActivatedCb;

            //register port event handlers
            RegisterHandlers();


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

            //connect
            node.Connect();
        }
Ejemplo n.º 5
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.º 6
0
        public void Start()
        {
            #region Configurations

            this.YConfig = this.InitConfig();
            this.ActiveCfg = this.YConfig.GetActiveConf();
            NodeConfig conf = new NodeConfig()
            {
                uuid = ActiveCfg.Uuid,
                Name = "RaspPiGpioNode",
                YpServer = ActiveCfg.ApiServer,
                YpchannelPort = ActiveCfg.YpchannelPort,
                SecureYpc = ActiveCfg.YpchannelSecure,
                FrontendServer = ActiveCfg.FrontendServer,
                CanSolveGraphs = false,
                Pairing_NoUUIDAuthentication = true
            };

            if (String.IsNullOrWhiteSpace(ActiveCfg.NodeKey))
            {
                _IsFirstConnectionAfterPairing = true;
            }

            #endregion

            #region Node construction
            //create node
            Node = new Yodiwo.NodeLibrary.Node(conf,
                                               new Yodiwo.NodeLibrary.Pairing.NancyPairing.NancyPairing(),
                                               DataLoad: null,
                                               DataSave: null,
                                               nodeType: eNodeType.EndpointSingle
                                               );
            #endregion

            #region Register callbacks
            Node.OnNodePaired += Node_OnPairedCb;
            Node.OnNodeUnpaired += Node_OnNodeUnpairedCb;
            Node.OnTransportConnected += Node_OnTransportConnectedCb;
            Node.OnTransportDisconnected += Node_OnTransportDisconnectedCb;
            Node.OnTransportError += Node_OnTransportErrorCb;
            #endregion

            #region Do Pairing/Connection
            if (String.IsNullOrWhiteSpace(ActiveCfg.NodeKey))
            {
                //if nodekey null, start pairing
                DebugEx.TraceLog("Starting pairing procedure.");
                var task = Node.StartPairing(ActiveCfg.FrontendServer, null, ActiveCfg.LocalWebServer);
                task.Wait();
            }
            #endregion

            //already paired, connect
            Node.SetupNodeKeys(ActiveCfg.NodeKey, ActiveCfg.NodeSecret.ToSecureString());
            DebugEx.TraceLog($"Node already paired: NodeKey={ActiveCfg.NodeKey}, NodeSecret={ActiveCfg.NodeSecret.Substring(0, 8)}...");

            _SetupThings();

            foreach (var thing in ThingIdToThing.Values)
            {
                Node.PortEventHandlers[thing.Ports[0]] = (data, isEvent) =>
                {
                    Console.WriteLine("PorteventMsg Rx (isEvent={2}) for thingUID {0} with data {1}", thing.ThingKey.Split('-').Last(), data, isEvent);
                    if (isEvent)
                    {
                        var gpiopin = ThingIdToPin.TryGetOrDefault(thing.ThingKey.Split('-').Last(), GpioPin.Unknown);
                        if (gpiopin != GpioPin.Unknown)
                        {
                            Console.WriteLine("Setting pin {0} to {1}", gpiopin, data);
                            gpioPinsConnection[GpioToPin.TryGetOrDefault(gpiopin)] = data.ParseToBool();
                        }
                    }
                };
            }

            //connect
            Node.Connect();
        }