Skip to content
This repository was archived by the owner on Jun 2, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions ContosoThings/Content/Site.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 6 additions & 2 deletions ContosoThings/Controllers/HubManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -50,7 +54,7 @@ public void Refresh(TimeSpan ageAllowed)
{
this.hubs.Clear();

List<ContosoThingsCore.Hub> hubs = TableStorageProvider.GetAllHubs();
List<ContosoThingsCore.Hub> hubs = storageProvider.GetAllHubs();
this.hubs.AddRange(hubs);

LastLoaded = DateTime.Now;
Expand Down Expand Up @@ -87,7 +91,7 @@ public Hub GetHub(string hubId)

public void Save(Hub h)
{
TableStorageProvider.AddHub(h);
storageProvider.AddHub(h);
}
}
}
2 changes: 1 addition & 1 deletion ContosoThings/Controllers/HubsApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
17 changes: 13 additions & 4 deletions ContosoThings/Views/Hub/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,24 @@

@*Dimmable Light*@
<div class="ct-slider-container" ng-if="thing.ThingsType === 2">
<div class="text-xs-center dim-percentage">{{thing.Dim}}%</div>
<div ui-slider class="ct-slider" min="0" max="100" ng-model="thing.Dim" ng-mouseup="dim(hubDetail.Id, thing)"></div>
<div class="text-xs-center dim-percentage">
{{thing.Dim}}%
</div>
<div ui-slider class="ct-slider" min="0" max="100" ng-model="thing.Dim" ng-mouseup="dim(hubDetail.Id, thing, 'Dim')"></div>
</div>

@*Colored Light*@
<div class="ct-slider-container" ng-if="thing.ThingsType === 3">
<div class="text-xs-center dim-percentage">
<div class="dropdown">
{{thing.Dim}}%
<span class="ct-color-display" ng-style="{'background-color': thing.ColorRGB}"></span>
<button class="btn btn-default dropdown-toggle" ng-style="{'background-color':thing.ColorRGB}" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"></button>
<ul class="dropdown-menu" >
<li><a href="#" ng-click="setThingProperty(hubDetail.Id, thing, 'ColorRGB','#ff0000');"><div class="ct-color-display" ng-style="{'background-color':'#ff0000'}"></div>Red</a></li>
<li><a href="#" ng-click="setThingProperty(hubDetail.Id, thing, 'ColorRGB','#0000ff');"><div class="ct-color-display" ng-style="{'background-color':'#0000ff'}"></div>Blue</a></li>
<li><a href="#" ng-click="setThingProperty(hubDetail.Id, thing, 'ColorRGB','#ffff00');"><div class="ct-color-display" ng-style="{'background-color':'#ffff00'}"></div>Yellow</a></li>
<li><a href="#" ng-click="setThingProperty(hubDetail.Id, thing, 'ColorRGB','#00ff00');"><div class="ct-color-display" ng-style="{'background-color':'#00ff00'}"></div>Green</a></li>
<li><a href="#" ng-click="setThingProperty(hubDetail.Id, thing, 'ColorRGB','#ffffff');"><div class="ct-color-display" ng-style="{'background-color':'#ffffff'}"></div>White</a></li>
</ul>
</div>
<div ui-slider class="ct-slider" min="0" max="100" ng-model="thing.Dim" ng-mouseup="dim(hubDetail.Id, thing, 'Dim')"></div>
<span class="ct-color-text">{{thing.ColorRGB}}</span>
Expand Down
3 changes: 3 additions & 0 deletions ContosoThings/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<!-- !Must be configured! - AuthKeyword is used on the Login page and passed in the auth header on REST API calls -->
<add key="AuthKeyword" value="" />
<!-- !Must be configured! - TableStorageConnectionString is the Connection String for an Azure storage account where the hubs and devices are stored -->
<add key="TableStorageConnectionString" value="" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
Expand Down
21 changes: 12 additions & 9 deletions ContosoThingsConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");

Expand All @@ -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<Hub> hubs = TableStorageProvider.GetAllHubs();
//TableStorageProvider storageProvider = new TableStorageProvider(TableStorageConnectionString);
//CreateHub1(storageProvider);
//CreateHub2(storageProvider);

//List<Hub> hubs = storageProvider.GetAllHubs();

Console.WriteLine("done");
Console.ReadLine();
}
Expand Down
1 change: 1 addition & 0 deletions ContosoThingsCore/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public ContosoLightColor() : base() { }
public ContosoLightColor(string name) : base(name)
{
ThingsType = ThingsType.LightColor;
ColorRGB = "#FF0000";
}

public override string ToString()
Expand Down
40 changes: 22 additions & 18 deletions ContosoThingsCore/Providers/TableStorageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -51,7 +46,7 @@ public static void AddHub(Hub hub, string partition = "Test")
}
}

public static List<Hub> GetAllHubs(string partition = "Test")
public List<Hub> GetAllHubs(string partition = "Test")
{
List<Hub> toReturn = new List<Hub>();

Expand All @@ -67,7 +62,7 @@ public static List<Hub> 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<HubWrapper>(partition, rowKey);
Expand All @@ -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)
{
Expand Down