Exemplo n.º 1
0
        private static DataStore SetDefaults(DataStore store)
        {
            store.InsertItem("dsIp", "0.0.0.0");
            BaseDevice myDevice = new SideKick(GetLocalIpAddress());

            myDevice.Initialize();
            var bList = HueBridge.FindBridges();
            var bData = bList.Select(lb => new BridgeData(lb.IpAddress, lb.BridgeId)).ToList();

            store.InsertItem("myDevice", myDevice);
            store.InsertItem("emuType", "SideKick");
            store.InsertItem("bridges", bData);
            store.InsertItem("devices", Array.Empty <BaseDevice>());
            return(store);
        }
Exemplo n.º 2
0
        public IActionResult Action(string action, string value = "")
        {
            var message = "Unrecognized action";
            var store   = DreamData.GetStore();

            Console.WriteLine($@"{action} fired.");

            if (action == "connectDreamScreen")
            {
                List <BaseDevice> dev;
                using (var ds = new DreamClient()) {
                    dev = ds.FindDevices().Result;
                }

                store.Dispose();
                return(new JsonResult(dev));
            }

            if (action == "authorizeHue")
            {
                var        doAuth    = true;
                var        bridges   = store.GetItem <List <BridgeData> >("bridges");
                BridgeData bd        = null;
                var        bridgeInt = -1;

                if (!string.IsNullOrEmpty(value))
                {
                    var bCount = 0;
                    foreach (var b in bridges)
                    {
                        if (b.Ip == value)
                        {
                            bd        = b;
                            bridgeInt = bCount;
                            doAuth    = b.Key == null || b.User == null;
                        }

                        bCount++;
                    }
                }

                if (doAuth)
                {
                    var appKey = HueBridge.CheckAuth(value).Result;
                    if (appKey != null && bd != null)
                    {
                        message            = "Success: Bridge Linked.";
                        bd.Key             = appKey.StreamingClientKey;
                        bd.User            = appKey.Username;
                        bridges[bridgeInt] = bd;
                        store.ReplaceItem("bridges", bridges, true);
                    }
                    else
                    {
                        message = "Error: Press the link button";
                    }
                }
                else
                {
                    message = "Success: Bridge Already Linked.";
                }
            }
            else if (action == "findHue")
            {
                var bridges = HueBridge.FindBridges(3);
                if (bridges != null)
                {
                    store.ReplaceItem("bridges", bridges, true);
                }
                else
                {
                    message = "Error: No bridge found.";
                }
            }

            Console.WriteLine(message);
            store.Dispose();
            return(new JsonResult(message));
        }
Exemplo n.º 3
0
        public IActionResult GetJson()
        {
            Console.WriteLine(@"GetJson Called.");
            var store = DreamData.GetStore();

            if (DreamData.GetItem <List <BridgeData> >("bridges") != null)
            {
                var bridges    = store.GetItem <List <BridgeData> >("bridges");
                var newBridges = HueBridge.FindBridges();
                var nb         = new List <BridgeData>();
                var update     = false;
                if (bridges.Count > 0)
                {
                    foreach (var b in bridges)
                    {
                        if (b.Key != null && b.User != null)
                        {
                            var hb = new HueBridge(b);
                            b.SetLights(hb.GetLights());
                            b.SetGroups(hb.ListGroups().Result);
                            update = true;
                        }

                        nb.Add(b);
                    }
                }

                foreach (var bb in newBridges)
                {
                    var exists = false;
                    foreach (var b in bridges)
                    {
                        if (bb.BridgeId == b.Id)
                        {
                            exists = true;
                        }
                    }

                    if (!exists)
                    {
                        Console.WriteLine($@"Adding new bridge at {bb.IpAddress}.");
                        nb.Add(new BridgeData(bb.IpAddress, bb.BridgeId));
                        update = true;
                    }
                }


                if (update)
                {
                    bridges = nb;
                    store.ReplaceItem("bridges", bridges, true);
                }
            }


            if (store.GetItem("dsIp") == "0.0.0.0")
            {
                var dc = new DreamClient();
                dc.FindDevices().ConfigureAwait(true);
                dc.Dispose();
            }

            store.Dispose();
            return(Content(DreamData.GetStoreSerialized(), "application/json"));
        }