示例#1
0
        public static void LogRefresh <T>(this ILogger <T> logger, CachedControlPair stale, LightControlPair refreshed)
        {
            if (stale.PowerState == refreshed.PowerState &&
                stale.AppControlState == refreshed.AppControlState &&
                stale.ResetOccurred == refreshed.ResetOccurred)
            {
                return;
            }
            var refreshMessage = $"Refreshed light | ID: {refreshed.Properties.Id} Name: {refreshed.Properties.Name}";

            if (stale.PowerState != refreshed.PowerState)
            {
                refreshMessage += $" | Power state changed from {stale.PowerState} to {refreshed.PowerState}";
            }
            if (stale.AppControlState != refreshed.AppControlState)
            {
                refreshMessage += $" | Control state changed from {stale.AppControlState} to {refreshed.AppControlState}";
            }
            if (refreshed.ResetOccurred != refreshed.ResetOccurred)
            {
                refreshMessage += refreshed.ResetOccurred ? $" | Light reset." : $" | Reset scheduled.";
            }
            logger.LogDebug(stale.ToString());
            logger.LogDebug(refreshed.ToString());
            logger.LogInformation(refreshMessage);
        }
示例#2
0
        public async Task Refresh(DateTime currentTime)
        {
            await clientManager.AssertConnected();

            var discoveredLights = await Discover();

            var excludedLights = appOptionsDelegate.CurrentValue.LightsToExclude;
            var syncCommands   = new Dictionary <string, LightCommand>();

            this.lights = this.lights.Trim(discoveredLights);
            foreach (var discoveredLight in discoveredLights)
            {
                var id         = discoveredLight.Id;
                var isExcluded = excludedLights.Any(x => x == discoveredLight.Id);
                if (!this.lights.ContainsKey(id))
                {
                    var light = new LightControlPair(discoveredLight);
                    light.Exclude(isExcluded);
                    this.lights.Add(id, light);
                }
                else
                {
                    var light      = this.lights[id];
                    var staleLight = new CachedControlPair(light);
                    light.Refresh(discoveredLight.State, currentTime);
                    light.Exclude(isExcluded);
                    //log new light ... reinvestigate cloning of some kind (create a barebones cached light model)
                    logger.LogRefresh(staleLight, light);
                    if (this.lights[id].RequiresSync(out LightCommand syncCommand))
                    {
                        syncCommand.TransitionTime = TimeSpan.FromSeconds(appOptionsDelegate.CurrentValue.StandardTransitionTime);
                        syncCommands.Add(id, syncCommand);
                    }
                }
            }
            if (syncCommands.Any())
            {
                await Synchronise(syncCommands, currentTime);
            }
            foreach (var idLightPair in lights)
            {
                if (idLightPair.Key != idLightPair.Value.Properties.Id)
                {
                    throw new InvalidOperationException();
                }
            }
            return;
        }