示例#1
0
        public Task StartCapture(DreamClient dreamClient, CancellationToken cancellationToken)
        {
            LogUtil.Write("Beginning capture process...");
            SetCapVars();
            return(Task.Run(() => {
                var autoEvent = new AutoResetEvent(false);
                saveTimer = new Timer(SaveFrame, autoEvent, 5000, 5000);
                LogUtil.WriteInc($"Starting capture task, setting sw and h to {_scaleWidth} and {_scaleHeight}");
                _splitter = new Splitter(_ledData, _scaleWidth, _scaleHeight);
                var wlArray = DataUtil.GetCollection <WLedData>("Dev_Wled");
                foreach (var wl in wlArray)
                {
                    _splitter.AddWled(wl);
                }
                LogUtil.Write("All wled devices added, executing main capture loop...");

                while (!cancellationToken.IsCancellationRequested)
                {
                    var frame = _vc.Frame;
                    if (frame == null)
                    {
                        SourceActive = false;
                        LogUtil.Write("Frame is null, dude.", "WARN");
                        continue;
                    }

                    if (frame.Cols == 0)
                    {
                        SourceActive = false;
                        LogUtil.Write("Frame has no columns, dude.", "WARN");
                        continue;
                    }
                    var warped = ProcessFrame(frame);
                    if (warped == null)
                    {
                        SourceActive = false;
                        LogUtil.Write("Unable to process frame, Dude.", "WARN");
                        continue;
                    }
                    _splitter.Update(warped);
                    SourceActive = !_splitter.NoImage;

                    var colors = _splitter.GetColors();
                    var sectors = _splitter.GetSectors();
                    var sectors3 = _splitter.GetSectorsV2();
                    var sectorsWled = _splitter.GetWledSectors();
                    if (_sendColors)
                    {
                        dreamClient.SendColors(colors, sectors, sectors3, sectorsWled);
                    }
                }

                saveTimer.Dispose();
                LogUtil.Write("Capture task completed!", "WARN");
                return Task.CompletedTask;
            }, cancellationToken));
        }
        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));
        }
        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"));
        }