From 83e45c4e15a0f49f9399c3a45860089918225e70 Mon Sep 17 00:00:00 2001 From: Noel Anderson Date: Thu, 18 May 2017 10:35:04 -0700 Subject: [PATCH] Remove hardcode Azure Table Connection String. Fix some exceptions when using the console to set device state. Add a simple colour picker to the colour light --- ContosoThings/Content/Site.css | 10 +++-- ContosoThings/Controllers/HubManager.cs | 8 +++- .../Controllers/HubsApiController.cs | 2 +- ContosoThings/Views/Hub/Index.cshtml | 17 ++++++-- ContosoThings/Web.config | 3 ++ ContosoThingsConsole/Program.cs | 21 +++++----- ContosoThingsCore/Models.cs | 1 + .../Providers/TableStorageProvider.cs | 40 ++++++++++--------- 8 files changed, 65 insertions(+), 37 deletions(-) diff --git a/ContosoThings/Content/Site.css b/ContosoThings/Content/Site.css index 964312c..77d9fbf 100644 --- a/ContosoThings/Content/Site.css +++ b/ContosoThings/Content/Site.css @@ -157,9 +157,13 @@ small { } .ct-color-display { - width:15px; - height:15px; - display:inline-block; + width: 15px; + height: 10px; + display: inline-block; + border-width: 1px; + border-color: darkgray; + border-style: solid; + margin-right: 5px; } .ct-switch > label { diff --git a/ContosoThings/Controllers/HubManager.cs b/ContosoThings/Controllers/HubManager.cs index 92d37ee..a592905 100644 --- a/ContosoThings/Controllers/HubManager.cs +++ b/ContosoThings/Controllers/HubManager.cs @@ -15,10 +15,14 @@ public class HubManager : Controller public static HubManager Instance { get { return instance; } } internal Microsoft.AspNet.SignalR.IHubContext hub; + internal TableStorageProvider storageProvider; private HubManager() { // load the signalr hub which is used to signal to the webui something has changed hub = Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager.GetHubContext("NotificationHub"); + + string TableStorageConnectionString = System.Configuration.ConfigurationManager.AppSettings["TableStorageConnectionString"]; + storageProvider = new TableStorageProvider(TableStorageConnectionString); Refresh(); } @@ -50,7 +54,7 @@ public void Refresh(TimeSpan ageAllowed) { this.hubs.Clear(); - List hubs = TableStorageProvider.GetAllHubs(); + List hubs = storageProvider.GetAllHubs(); this.hubs.AddRange(hubs); LastLoaded = DateTime.Now; @@ -87,7 +91,7 @@ public Hub GetHub(string hubId) public void Save(Hub h) { - TableStorageProvider.AddHub(h); + storageProvider.AddHub(h); } } } \ No newline at end of file diff --git a/ContosoThings/Controllers/HubsApiController.cs b/ContosoThings/Controllers/HubsApiController.cs index 416817b..f3b4dbc 100644 --- a/ContosoThings/Controllers/HubsApiController.cs +++ b/ContosoThings/Controllers/HubsApiController.cs @@ -146,7 +146,7 @@ public object SetValue(HttpRequestMessage request) hub.Clients.All.refresh(); // update hub in storage - TableStorageProvider.AddHub(h); + HubManager.Instance.Save(h); return newThing; } diff --git a/ContosoThings/Views/Hub/Index.cshtml b/ContosoThings/Views/Hub/Index.cshtml index 8454d5d..04b18b2 100644 --- a/ContosoThings/Views/Hub/Index.cshtml +++ b/ContosoThings/Views/Hub/Index.cshtml @@ -79,15 +79,24 @@ @*Dimmable Light*@
-
{{thing.Dim}}%
-
+
+ {{thing.Dim}}% +
+
@*Colored Light*@
-
+
{{thing.ColorRGB}} diff --git a/ContosoThings/Web.config b/ContosoThings/Web.config index 74379b1..3405080 100644 --- a/ContosoThings/Web.config +++ b/ContosoThings/Web.config @@ -9,7 +9,10 @@ + + + diff --git a/ContosoThingsConsole/Program.cs b/ContosoThingsConsole/Program.cs index c395e9e..955625d 100644 --- a/ContosoThingsConsole/Program.cs +++ b/ContosoThingsConsole/Program.cs @@ -11,8 +11,10 @@ namespace ContosoThingsConsole { class Program { + // !Must be configured! - TableStorageConnectionString is the Connection String for an Azure storage account where the hubs and devices are stored + static string TableStorageConnectionString = ""; - public static void CreateHub1() + public static void CreateHub1(TableStorageProvider storageProvider) { ContosoSwitch cs1 = new ContosoSwitch("Humidifier"); ContosoLight cl1 = new ContosoLight("Family Lamp 1"); @@ -36,15 +38,15 @@ public static void CreateHub1() //Console.WriteLine(h.ToString()); File.WriteAllText("hub.json", saveString); - TableStorageProvider.AddHub(h); - + storageProvider.AddHub(h); + Hub h2 = Hub.Load(saveString); Console.WriteLine(h2); } - public static void CreateHub2() + public static void CreateHub2(TableStorageProvider storageProvider) { Hub h = new Hub("Contoso Big Hub"); @@ -62,18 +64,19 @@ public static void CreateHub2() h.AddThing(new ContosoLightDimmable("Guest Bedroom Lamp")); h.AddThing(new ContosoLightDimmable("Guest Bedroom Chandelier")); - TableStorageProvider.AddHub(h); + storageProvider.AddHub(h); Console.WriteLine(h); } static void Main(string[] args) { - //CreateHub1(); - //CreateHub2(); - - //List hubs = TableStorageProvider.GetAllHubs(); + //TableStorageProvider storageProvider = new TableStorageProvider(TableStorageConnectionString); + //CreateHub1(storageProvider); + //CreateHub2(storageProvider); + //List hubs = storageProvider.GetAllHubs(); + Console.WriteLine("done"); Console.ReadLine(); } diff --git a/ContosoThingsCore/Models.cs b/ContosoThingsCore/Models.cs index 6d05518..56bce7a 100644 --- a/ContosoThingsCore/Models.cs +++ b/ContosoThingsCore/Models.cs @@ -123,6 +123,7 @@ public ContosoLightColor() : base() { } public ContosoLightColor(string name) : base(name) { ThingsType = ThingsType.LightColor; + ColorRGB = "#FF0000"; } public override string ToString() diff --git a/ContosoThingsCore/Providers/TableStorageProvider.cs b/ContosoThingsCore/Providers/TableStorageProvider.cs index 5bbc316..e663780 100644 --- a/ContosoThingsCore/Providers/TableStorageProvider.cs +++ b/ContosoThingsCore/Providers/TableStorageProvider.cs @@ -6,38 +6,33 @@ using System.Web; using ContosoThingsCore; + namespace ContosoThingsCore.Providers { public class TableStorageProvider { - static string lockObject = "lock"; - - static string ConnectionString = "DefaultEndpointsProtocol=https;AccountName=contosothings;AccountKey=J7+5GGTqfZIEG2pE+UGRZ1vgw9tBWPcWfsYC+GVH7hFebF62Vkb4U4a9L9M1S3y4Ecuuag68wUDKz/3MXodOWQ=="; + string lockObject = "lock"; + CloudStorageAccount storageAccount = null; + CloudTableClient tableClient = null; + CloudTable table = null; - static CloudStorageAccount storageAccount = null; - static CloudTableClient tableClient = null; - static CloudTable table = null; - static TableStorageProvider() + public TableStorageProvider(string TableStorageConnectionString) { // Retrieve the storage account from the connection string. - storageAccount = CloudStorageAccount.Parse(ConnectionString); + storageAccount = CloudStorageAccount.Parse(TableStorageConnectionString); // Create the table client. tableClient = storageAccount.CreateCloudTableClient(); - - - // Create the CloudTable object that represents the "people" table. - table = tableClient.GetTableReference("Hubs"); - + // Create the CloudTable object that represents the "hubs" table. + table = tableClient.GetTableReference("hubs"); if (!table.Exists()) { table.Create(); } } - - public static void AddHub(Hub hub, string partition = "Test") + public void AddHub(Hub hub, string partition = "Test") { lock (lockObject) { @@ -51,7 +46,7 @@ public static void AddHub(Hub hub, string partition = "Test") } } - public static List GetAllHubs(string partition = "Test") + public List GetAllHubs(string partition = "Test") { List toReturn = new List(); @@ -67,7 +62,7 @@ public static List GetAllHubs(string partition = "Test") return toReturn; } - public static Hub GetHub(string rowKey, string partition = "Test") + public Hub GetHub(string rowKey, string partition = "Test") { // Create a retrieve operation that takes a customer entity. TableOperation retrieveOperation = TableOperation.Retrieve(partition, rowKey); @@ -86,7 +81,16 @@ public static Hub GetHub(string rowKey, string partition = "Test") return null; } } - public static void DeleteHub(string rowKey, string partition) + public void DeleteHub(Hub hub) + { + lock (lockObject) + { + HubWrapper hubWrapper = new HubWrapper(hub); + DeleteHub(hubWrapper.RowKey); + } + } + + public void DeleteHub(string rowKey, string partition = "Test") { lock (lockObject) {