Ejemplo n.º 1
0
        public static AlarmSystem Determinej64ServerAddress(string hostString)
        {
            string[] h         = hostString.Split(':');
            string   j64Server = h[0];
            int      j64Port   = 80;

            if (h.Length > 1)
            {
                j64Port = Convert.ToInt32(h[1]);
            }

            var hostName = System.Net.Dns.GetHostEntryAsync(System.Net.Dns.GetHostName());

            hostName.Wait();
            foreach (var i in hostName.Result.AddressList)
            {
                if (i.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    j64Server = i.ToString();
                    break;
                }
            }

            // Save the info
            var asi = AlarmSystemRepository.Get();

            asi.j64Server = j64Server;
            asi.j64Port   = j64Port.ToString();
            AlarmSystemRepository.Save(asi);

            return(asi);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Install or Update Devices in the SmartThings App
        /// </summary>
        public static void InstallDevices(string hostString)
        {
            try
            {
                var alarm = AlarmSystemRepository.Get();
                if (string.IsNullOrEmpty(alarm.j64Server) || string.IsNullOrEmpty(alarm.j64Port))
                {
                    Determinej64ServerAddress(hostString);
                }

                OauthInfo authInfo = OauthRepository.Get();

                if (authInfo == null | authInfo.endpoints == null || authInfo.endpoints.Count == 0)
                {
                    MyLogger.LogError("OAuth endpoints have not been created. Cannot update smart things at this time");
                    return;
                }
                string url = authInfo.endpoints[0].uri + $"/installDevices";

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

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

                List <KeyValuePair <string, string> > parms = new List <KeyValuePair <string, string> >();
                parms.Add(new KeyValuePair <string, string>("j64Server", alarm.j64Server));
                parms.Add(new KeyValuePair <string, string>("j64Port", alarm.j64Port));
                parms.Add(new KeyValuePair <string, string>("j64UserName", "admin"));
                parms.Add(new KeyValuePair <string, string>("j64Password", "Admin_01"));
                msg.Content = new System.Net.Http.FormUrlEncodedContent(parms);
                var response = client.SendAsync(msg);
                response.Wait();

                if (response.Result.StatusCode != System.Net.HttpStatusCode.Created)
                {
                    MyLogger.LogError($"Error installing smart things devices.  StatusCode was {response.Result.StatusCode}");
                }
            }
            catch (Exception ex)
            {
                MyLogger.LogError($"Error installing smart things devices.  Exception was {MyLogger.ExMsg(ex)}");
            }
        }