// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddEntityFramework() .AddSqlServer() .AddDbContext <ApplicationDbContext>(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])); services.AddIdentity <ApplicationUser, IdentityRole>() .AddEntityFrameworkStores <ApplicationDbContext>() .AddDefaultTokenProviders(); services.AddMvc(); // Get the configuration info j64HarmonyGateway j64Config = j64HarmonyGatewayRepository.Read(); services.AddInstance <j64HarmonyGateway>(j64Config); // Get an auth token from the harmony "cloud" Hub myHub = new Hub(); bool connected = myHub.StartNewConnection(j64Config.Email, j64Config.Password, j64Config.HubAddress, j64Config.HubPort); // Add the hub as a service available to all of the controllers services.AddInstance <Hub>(myHub); }
public static void Save(j64HarmonyGateway j64Config) { using (StreamWriter file = System.IO.File.CreateText(HarmonyHubConfigurationFile)) { JsonSerializer serializer = new JsonSerializer(); serializer.Formatting = Formatting.Indented; serializer.Serialize(file, j64Config); } }
/// <summary> /// Install or Update Devices in the SmartThings App /// </summary> public static void PrepTheInstall(j64HarmonyGateway j64Config) { try { OauthInfo authInfo = OauthRepository.Get(); if (authInfo == null | authInfo.endpoints == null || authInfo.endpoints.Count == 0) { return; } // create a new identifier for this app! j64Config.j64AppId = Guid.NewGuid().ToString(); string url = authInfo.endpoints[0].uri + $"/prepInstall"; 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>("j64AppId", j64Config.j64AppId)); 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.IsSuccessStatusCode) { // Get the address of the local smart things hub var result = response.Result.Content.ReadAsStringAsync().Result; JObject ipInfo = JObject.Parse(result); j64Config.STHubAddress = (string)ipInfo["hubIP"]; j64Config.STHubPort = Convert.ToInt32((string)ipInfo["hubPort"]); if (j64Config.STHubAddress == "null") { j64Config.STHubAddress = null; j64Config.STHubPort = 0; } j64HarmonyGatewayRepository.Save(j64Config); } } catch (Exception) { } }
public static j64HarmonyGateway Read() { j64HarmonyGateway j64Config = new j64HarmonyGateway(); if (File.Exists(HarmonyHubConfigurationFile)) { using (StreamReader file = System.IO.File.OpenText(HarmonyHubConfigurationFile)) { JsonSerializer serializer = new JsonSerializer(); j64Config = (j64HarmonyGateway)serializer.Deserialize(file, typeof(j64HarmonyGateway)); } } else { j64Config.FavoriteChannels.Add(new FavoriteChannel() { Name = "Fox and Friends", Channel = "1004" }); j64Config.FavoriteChannels.Add(new FavoriteChannel() { Name = "One zero zero five", Channel = "1005" }); j64Config.FavoriteChannels.Add(new FavoriteChannel() { Name = "Ten Oh Eight", Channel = "1008" }); j64Config.FavoriteChannels.Add(new FavoriteChannel() { Name = "Eleven 20", Channel = "1120" }); j64Config.FavoriteChannels.Add(new FavoriteChannel() { Name = "History Channel", Channel = "1256" }); j64Config.FavoriteChannels.Add(new FavoriteChannel() { Name = "Food Network", Channel = "1452" }); j64Config.FavoriteChannels.Add(new FavoriteChannel() { Name = "Show Time", Channel = "1852" }); Save(j64Config); } return(j64Config); }
public static void Determinej64Address(string host, j64HarmonyGateway j64Config) { string[] h = host.Split(':'); if (h.Length > 1) { j64Config.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) { j64Config.j64Address = i.ToString(); break; } } }
public HomeController(j64HarmonyGateway hubConfig, Hub hub) { this.j64Config = hubConfig; this.myHub = hub; }
public FavoriteChannelController(j64HarmonyGateway j64Config, Hub hub) { this.j64Config = j64Config; myHub = hub; }
/// <summary> /// Install or Update Devices in the SmartThings App /// </summary> public static void InstallDevices(j64HarmonyGateway j64Config, string host) { OauthInfo authInfo = OauthRepository.Get(); // We can't sync if the IP has not yet been set if (String.IsNullOrEmpty(j64Config.STHubAddress) || j64Config.STHubAddress.Contains("TBD")) { return; } // Set the IP address of this server if it has not been set yet if (String.IsNullOrEmpty(j64Config.j64Address) || j64Config.j64Address.Contains("TBD")) { SmartThingsRepository.Determinej64Address(host, j64Config); } var url = $"http://{j64Config.STHubAddress}:{j64Config.STHubPort}"; var client = new System.Net.Http.HttpClient(); System.Net.Http.HttpRequestMessage msg = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, url); List <j64Device> l = new List <j64Device>(); l.Add(new j64Device() { Name = j64Config.SoundDeviceName, DeviceValue = null, DeviceType = j64DeviceType.Volume }); l.Add(new j64Device() { Name = j64Config.ChannelSurfDeviceName, DeviceValue = null, DeviceType = j64DeviceType.Surfing }); l.Add(new j64Device() { Name = j64Config.VcrPauseDeviceName, DeviceValue = "pause", DeviceType = j64DeviceType.VCR }); foreach (var fc in j64Config.FavoriteChannels) { l.Add(new j64Device() { Name = fc.Name, DeviceValue = fc.Channel.ToString(), DeviceType = j64DeviceType.Channel }); } l.Add(new j64Device() { Name = j64Config.LastChannelDeviceName, DeviceValue = "previous", DeviceType = j64DeviceType.Channel }); foreach (var cc in j64Config.CustomCommands) { l.Add(new j64Device() { Name = cc.CommandName, DeviceValue = cc.CommandName, DeviceType = j64DeviceType.CustomCommand }); } var request = new MyRequest <List <j64Device> >() { j64Ip = j64Config.j64Address, j64Port = j64Config.j64Port, j64AppId = j64Config.j64AppId, Route = "/installAllDevices", Payload = l }; string json = JsonConvert.SerializeObject(request, Formatting.None); msg.Content = new System.Net.Http.StringContent(json); msg.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); try { var response = client.SendAsync(msg); response.Wait(); if (response.Result.IsSuccessStatusCode) { var result = response.Result.Content.ReadAsStringAsync(); } } catch (Exception) { // TODO: display an appropriate error message! } }
public FirstTimeConfigController(j64HarmonyGateway hubConfig, Hub hub) { this.hubConfig = hubConfig; this.myHub = hub; }
public DeviceNamesController(j64HarmonyGateway j64Config, Hub hub) { myj64Config = j64Config; myHub = hub; }
public OAuthController(j64HarmonyGateway j64Config, Hub hub, IHostingEnvironment env) { this.j64Config = j64Config; this.myEnv = env; this.myHub = hub; }
public HubsAndGatewaysController(j64HarmonyGateway j64Config, Hub hub) { this.j64Config = j64Config; this.myHub = hub; }
public CustomCommandController(j64HarmonyGateway j64Config, Hub hub) { this.j64Config = j64Config; myHub = hub; }