Esempio n. 1
0
        //public ActionResult SaveChanges([Bind("Name", "ArmingCode", "Host", "Port", "User", "Password")] AlarmSystemInfo alarmInfo)
        public ActionResult SaveChanges(AlarmSystemInfo alarmInfo)
        {
            if (ModelState.IsValid)
            {
                myAlarmSystem.Name       = alarmInfo.Name;
                myAlarmSystem.ArmingCode = alarmInfo.ArmingCode;

                myAlarmSystem.Host = alarmInfo.Host;
                myAlarmSystem.Port = alarmInfo.Port;
                myAlarmSystem.User = alarmInfo.User;
                if (!String.IsNullOrEmpty(alarmInfo.Password))
                {
                    myAlarmSystem.Password = alarmInfo.Password;
                }

                myAlarmSystem.j64Server = alarmInfo.j64Host;
                myAlarmSystem.j64Port   = alarmInfo.j64Port;

                for (int i = 0; i < Request.Form["partition.Id"].Count; i++)
                {
                    var partition = myAlarmSystem.PartitionList.Find(p => p.Id.ToString() == Request.Form["partition.Id"][i]);

                    partition.Name = Request.Form["partition.Name"][i];
                    partition.SHMIntegrationEnabled = Convert.ToBoolean(Request.Form["partition.SHMIntegrationEnabled"][i]);
                }

                for (int i = 0; i < Request.Form["zone.Id"].Count; i++)
                {
                    var zone = myAlarmSystem.ZoneList.Find(p => p.Id.ToString() == Request.Form["zone.Id"][i]);

                    zone.Name     = Request.Form["zone.Name"][i];
                    zone.ZoneType = (ZoneType)Enum.Parse(typeof(ZoneType), Request.Form["zone.ZoneType"][i]);
                }

                AlarmSystemRepository.Save(myAlarmSystem);

                // this will update the existing devices on the smartthings hub
                SmartThingsRepository.InstallDevices(this.Request.Host.Value);

                // Add the partition/zone info to the alarm model we are returning
                alarmInfo = new AlarmSystemInfo(myAlarmSystem);
            }

            return(View("index", alarmInfo));
        }
 public IActionResult SyncSmartThings()
 {
     SmartThingsRepository.InstallDevices(j64Config, Request.Host.Value);
     return(View("Index", j64Config));
 }
 public IActionResult SyncSmartThings()
 {
     SmartThingsRepository.InstallDevices(myj64Config, Request.Host.Value);
     return(View("Edit", CreateViewMode()));
 }
Esempio n. 4
0
        public IActionResult Authorized(string code)
        {
            OauthInfo oai = OauthRepository.Get();

            if (code == null)
            {
                return(View(oai));
            }

            oai.authCode = code;

            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();

            var url = "https://graph.api.smartthings.com/oauth/token";

            List <KeyValuePair <string, string> > parms = new List <KeyValuePair <string, string> >();

            parms.Add(new KeyValuePair <string, string>("grant_type", "authorization_code"));
            parms.Add(new KeyValuePair <string, string>("code", oai.authCode));
            parms.Add(new KeyValuePair <string, string>("client_id", oai.clientKey));
            parms.Add(new KeyValuePair <string, string>("client_secret", oai.secretKey));
            string authorizedUrl = "http://" + this.Request.Host.Value + this.Url.Content("~/OAuth/Authorized");

            parms.Add(new KeyValuePair <string, string>("redirect_uri", authorizedUrl));

            var content  = new System.Net.Http.FormUrlEncodedContent(parms);
            var response = client.PostAsync(url, content);

            response.Wait();

            if (response.Result.StatusCode != System.Net.HttpStatusCode.OK)
            {
                ViewData.Add("GetTokenError", "Get Auth Code Error: " + response.Result.StatusCode.ToString());
                return(View(oai));
            }

            // Save the interim result
            var val = JsonConvert.DeserializeObject <Dictionary <string, string> >(response.Result.Content.ReadAsStringAsync().Result);

            oai.accessToken      = val["access_token"];
            oai.expiresInSeconds = Convert.ToInt32(val["expires_in"]);
            oai.tokenType        = val["token_type"];
            OauthRepository.Save(oai);

            // Get the endpoint info
            client = new System.Net.Http.HttpClient();
            url    = "https://graph.api.smartthings.com/api/smartapps/endpoints";

            System.Net.Http.HttpRequestMessage msg = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url);
            msg.Headers.Add("Authorization", $"Bearer {oai.accessToken}");

            response = client.SendAsync(msg);
            response.Wait();

            if (response.Result.StatusCode != System.Net.HttpStatusCode.OK)
            {
                ViewData.Add("GetTokenError", "Get EndPoints Error: " + response.Result.StatusCode.ToString());
                return(View(oai));
            }

            string jsonString = response.Result.Content.ReadAsStringAsync().Result;

            oai.endpoints = JsonConvert.DeserializeObject <List <OauthEndpoint> >(jsonString);

            OauthRepository.Save(oai);

            // Prepare the smart app to be called from the local traffic
            SmartThingsRepository.PrepTheInstall(j64Config);

            // Send all of the default devices to the smart app
            SmartThingsRepository.InstallDevices(j64Config, Request.Host.Value);

            // all done!
            return(View(oai));
        }