Beispiel #1
0
        public LgPreset(LgPreset preset) : this()
        {
            id    = GetNewId();
            name  = preset.name + " (copy)";
            appId = preset.appId;

            foreach (var step in preset.steps)
            {
                steps.Add(step);
            }
        }
Beispiel #2
0
        private LgPreset GeneratePreset(string name, string step = null, string appId = null, Keys key = Keys.None)
        {
            var preset = new LgPreset();

            var cvt      = new KeysConverter();
            var shortcut = key != Keys.None ? (string)cvt.ConvertTo(key, typeof(string)) : null;

            preset.name     = name;
            preset.appId    = appId;
            preset.shortcut = shortcut;
            if (appId == null)
            {
                preset.steps.Add(step == null ? name : step);
            }

            return(preset);
        }
Beispiel #3
0
        private async Task ExecuteSteps(LgTvApi api, LgPreset preset)
        {
            var mouse = await api.GetMouse();

            foreach (var step in preset.steps)
            {
                var keySpec = step.Split(':');
                var key     = keySpec[0].ToUpper();
                if (_invokableActions.ContainsKey(key))
                {
                    continue;
                }

                if (keySpec.Length == 2)
                {
                    SendKey(mouse, key, int.Parse(keySpec[1]));
                }
                else
                {
                    SendKey(mouse, key);
                }
            }
        }
Beispiel #4
0
        public async Task <bool> ApplyPreset(LgPreset preset, bool reconnect = false)
        {
            var hasApp = !string.IsNullOrEmpty(preset.appId);

            var hasWOL = preset.steps.Any(s => s.Equals("WOL", StringComparison.OrdinalIgnoreCase));

            if (hasWOL)
            {
                var connected = await WakeAndConnectToSelectedDevice(0);

                if (!connected)
                {
                    return(false);
                }
            }

            for (var tries = 0; tries <= 1; tries++)
            {
                if (!await Connected(reconnect || tries == 1))
                {
                    return(false);
                }

                if (hasApp)
                {
                    try
                    {
                        await _lgTvApi.LaunchApp(preset.appId);
                    }
                    catch (Exception ex)
                    {
                        string logMessage = ex.ToLogString(Environment.StackTrace);
                        Logger.Error("Error while launching app: " + logMessage);

                        if (tries == 0)
                        {
                            continue;
                        }
                        return(false);
                    }

                    if (_justWokeUp)
                    {
                        _justWokeUp = false;
                        await Task.Delay(1000);
                    }
                }

                if (preset.steps.Any())
                {
                    if (hasApp)
                    {
                        await Task.Delay(1500);
                    }
                    try
                    {
                        await ExecuteSteps(_lgTvApi, preset);
                    }
                    catch (Exception ex)
                    {
                        string logMessage = ex.ToLogString(Environment.StackTrace);
                        Logger.Error("Error while executing steps: " + logMessage);

                        if (tries == 0)
                        {
                            continue;
                        }
                        return(false);
                    }
                }

                return(true);
            }

            return(true);
        }
Beispiel #5
0
        public LgPreset Clone()
        {
            var preset = new LgPreset(this);

            return(preset);
        }