Exemplo n.º 1
0
        public async Task StartNodeBinding(IApiHandler api, IWebSocketHandler socket)
        {
            apiHandler    = api;
            socketHandler = socket;
            //apiHandler.DataNodeChanged += HandleNodeChange;
            try
            {
                var nodes = await apiHandler.GetNodes();

                //nodes.Select(GetNodeInstance);
                foreach (var node in nodes.OrderBy(d => d.Name))
                {
                    GetNodeInstance(node);
                    //Nodes.Add(realNode);
                }

                var containers = await apiHandler.GetContainers();

                foreach (var cnt in containers)
                {
                    var ncnt = new NodeContainer();
                    ncnt.Parse(cnt);
                    Containers.Add(ncnt);
                }
            }

            catch (HttpRequestException ex)
            {
                App.ShowConnectionErrorMessage("Unable to fetch data over http", ex);
                //await App.Current.MainPage.DisplayAlert("Connection", "Unable to fetch data over http\n\r"+ex.Message, "Wait...");
                ScheduleNewDataLoad();
                return;
                //return null;
            }
            try
            {
                socket.Subscribe("homeninja/conatiners", (JsonMessage message) =>
                {
                    var node = message.Data as JContainer;
                    if (node != null)
                    {
                        var dataCnt = node.ToObject <IEnumerable <DataNodeContainer> >();
                        foreach (var n in dataCnt)
                        {
                            GetNodeContainerInstance(n);
                        }
                    }
                });

                socket.Subscribe("homeninja/nodechange", (JsonMessage message) =>
                {
                    var node = message.Data as JContainer;
                    if (node != null)
                    {
                        var dataNodes = node.ToObject <IEnumerable <DataNode> >();
                        foreach (var n in dataNodes)
                        {
                            GetNodeInstance(n);
                        }
                    }
                });
                await socket.StartListening();
            }
            catch (WebSocketException ex) {
                App.ShowConnectionErrorMessage("Unable to fetch data over websocket", ex);
                ScheduleNewDataLoad();
            }
        }