예제 #1
0
        public ActionResult Login(AuthModels.Login model)
        {
            if (ModelState.IsValid)
            {
                string token = null;
                try
                {
                    token = HarmonyLogin.GetUserAuthToken(model.Username, model.Password);
                }
                catch { }

                if (string.IsNullOrEmpty(token))
                {
                    ModelState.AddModelError("Username", "Invalid username or password");
                }

                if (ModelState.IsValid)
                {
                    using (var manager = new Manager())
                    {
                        var user = manager.Users
                            .FirstOrDefault(o => o.HarmonyUsername == model.Username);

                        if (user == null)
                        {
                            user = new User()
                            {
                                HarmonyUsername = model.Username
                            };
                        }

                        user.HarmonyPassword = "";
                        user.HarmonyToken = token;
                        user.AlexaToken = RandomString(50);
                        user.AlexaUserID = "";
                        user.Hostname = model.Hostname;

                        manager.SaveChanges();

                        string url = ConfigurationManager.AppSettings["AuthUrl"]
                            + "#state=" + Url.Encode(model.State)
                            + "&access_token=" + Url.Encode(user.AlexaToken)
                            + "&token_type=Bearer";

                        return Redirect(url);
                    }
                }
            }

            return View(model);
        }
예제 #2
0
        private HarmonyClient GetHarmonyClient(User user)
        {
            return ScopingModule.Application.Ensure<HarmonyClient>(user.ID.ToString(), () =>
            {
                var auth = new HarmonyAuthenticationClient(user.Hostname, 5222);

                string sessionToken = auth.SwapAuthToken(user.HarmonyToken);
                if (string.IsNullOrEmpty(sessionToken))
                {
                    throw new Exception("Could not swap token on Harmony Hub.");
                }

                var r = new HarmonyClient(user.Hostname, 5222, sessionToken);

                r.GetConfig();

                return r;
            });
        }
예제 #3
0
        private bool Command(User user, string name, Dictionary<string, string> values, out string speech)
        {
            bool success = false;
            int repeats = 1;
            speech = null;

            lock (_lock)
            {
                var client = GetHarmonyClient(user);
                var config = client.Config;
                string s;

                // Shortcuts
                switch (name)
                {
                    case "PlayIntent":
                        name = "ButtonIntent";
                        values["Button"] = "play";
                        speech = "Playing";
                        break;

                    case "PauseIntent":
                        name = "ButtonIntent";
                        values["Button"] = "pause";
                        speech = "Pausing";
                        break;

                    case "VolumeUpIntent":
                        name = "ButtonIntent";
                        values["Button"] = "volume up";
                        speech = "Volume Up";
                        repeats = 8;
                        break;

                    case "VolumeDownIntent":
                        name = "ButtonIntent";
                        values["Button"] = "volume down";
                        speech = "Volume Down";
                        repeats = 8;
                        break;
                }

                // Commands
                switch (name)
                {
                    case "ButtonIntent":
                        {
                            string buttonName = (values.TryGetValue("Button", out s) ? s : "Pause").ToLower();

                            if (config != null && config.activity != null)
                            {
                                client.GetCurrentActivity();

                                if (client.CurrentActivity != null)
                                {
                                    var activity = config.activity
                                        .FirstOrDefault(o => o.id == client.CurrentActivity);

                                    if (activity != null)
                                    {
                                        var buttons = activity.controlGroup
                                            .Where(o => o.function != null)
                                            .SelectMany(o => o.function)
                                            .Where(o => o.action != null)
                                            .ToList();

                                        //var allbuttons = config.activity
                                        //    .Where(o => o.controlGroup != null)
                                        //    .SelectMany(o => o.controlGroup)
                                        //    .Where(o => o.function != null)
                                        //    .SelectMany(o => o.function)
                                        //    .Where(o => o.action != null)
                                        //    .Select(o => o.label)
                                        //    .Concat(config.device
                                        //        .SelectMany(o => o.controlGroup)
                                        //        .Where(o => o.function != null)
                                        //        .SelectMany(o => o.function)
                                        //        .Where(o => o.action != null)
                                        //        .Select(o => o.label)
                                        //    )
                                        //    .Distinct()
                                        //    .OrderBy(o => o);

                                        //var buttonList = string.Join("\r\n", allbuttons);

                                        var button = buttons
                                            .OrderBy(o => {
                                                var label = (o.label ?? "")
                                                    .ToLower()
                                                    .Replace("direction ", "");

                                                return Distance(buttonName, label);
                                            })
                                            .FirstOrDefault();

                                        if (button != null)
                                        {
                                            var action = new JavaScriptSerializer().Deserialize<HarmonyIRCommandAction>(button.action);
                                            if (action != null)
                                            {
                                                for (int i = 0; i < repeats; i++)
                                                {
                                                    client.PressButton(action.deviceId, action.command);
                                                }

                                                speech = speech ?? "Pressing " + button.label;
                                                success = true;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        break;

                    case "ButtonOnDeviceIntent":
                        {
                            string deviceName = (values.TryGetValue("Device", out s) ? s : "").ToLower();
                            string buttonName = (values.TryGetValue("Button", out s) ? s : "Pause").ToLower();

                            if (config != null && config.device != null)
                            {
                                var device = config.device
                                    .OrderBy(o => Distance(buttonName, (o.label ?? "").ToLower()))
                                    .FirstOrDefault();

                                if (device != null)
                                {
                                    var buttons = device.controlGroup
                                        .Where(o => o.function != null)
                                        .SelectMany(o => o.function)
                                        .Where(o => o.action != null)
                                        .ToList();

                                    var button = buttons
                                        .OrderBy(o =>
                                        {
                                            var label = (o.label ?? "")
                                                .ToLower()
                                                .Replace("direction ", "");

                                            return Distance(buttonName, label);
                                        })
                                        .FirstOrDefault();

                                    if (button != null)
                                    {
                                        var action = new JavaScriptSerializer().Deserialize<HarmonyIRCommandAction>(button.action);
                                        if (action != null)
                                        {
                                            client.PressButton(action.deviceId, action.command);
                                            speech = speech ?? "Pressing " + button.label + " on the " + device.label;
                                            success = true;
                                        }
                                    }
                                }
                            }
                        }
                        break;

                    case "ActivityIntent":
                        {
                            string activityName = (values.TryGetValue("Activity", out s) ? s : "TV").ToLower();

                            if (config != null && config.activity != null)
                            {
                                var activity = config.activity
                                    .Where(o => o.id != "-1")
                                    .OrderBy(o => Distance(activityName, (o.label ?? "").ToLower()))
                                    .FirstOrDefault();

                                if (activity != null)
                                {
                                    client.StartActivity(activity.id);
                                    speech = "Starting the " + activity.label + " activity";
                                    success = true;
                                }
                            }
                        }
                        break;

                    case "SequenceIntent":
                        {
                            string sequenceName = (values.TryGetValue("Sequence", out s) ? s : "").ToLower();

                            if (config != null && config.sequence != null)
                            {
                                var sequence = config.sequence
                                    .OrderBy(o => Distance(sequenceName, (o.name ?? "").ToLower()))
                                    .FirstOrDefault();

                                if (sequence != null)
                                {
                                    int sequenceId;
                                    if (int.TryParse(sequence.id, out sequenceId))
                                    {
                                        client.Sequence(sequenceId);
                                        speech = "Running the " + sequence.name + " sequence";
                                        success = true;
                                    }
                                }
                            }
                        }
                        break;

                    case "OffIntent":
                        client.TurnOff();
                        speech = "Powering off";
                        break;
                }
            }

            return success;
        }