Skip to content

Commit

Permalink
Made working SignalR hub on the server + connection on Construct Studio
Browse files Browse the repository at this point in the history
  • Loading branch information
tylercamp committed Nov 15, 2014
1 parent 5aa32f9 commit 63d7e4b
Show file tree
Hide file tree
Showing 31 changed files with 3,199 additions and 189 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@
<Reference Include="Microsoft.Owin.Diagnostics">
<HintPath>..\packages\Microsoft.Owin.Diagnostics.2.0.1\lib\net40\Microsoft.Owin.Diagnostics.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin.Host.HttpListener">
<HintPath>..\packages\Microsoft.Owin.Host.HttpListener.2.0.1\lib\net45\Microsoft.Owin.Host.HttpListener.dll</HintPath>
<Reference Include="Microsoft.Owin.Host.HttpListener, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.Owin.Host.HttpListener.3.0.0\lib\net45\Microsoft.Owin.Host.HttpListener.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin.Hosting">
<HintPath>..\packages\Microsoft.Owin.Hosting.2.0.1\lib\net45\Microsoft.Owin.Hosting.dll</HintPath>
Expand Down Expand Up @@ -125,9 +126,9 @@
<Compile Include="IModel.cs" />
<Compile Include="ItemAdapter.cs" />
<Compile Include="ItemPersistor.cs" />
<None Include="ItemRealtimeStreamer.cs">
<Compile Include="ItemRealtimeStreamer.cs">
<SubType>Code</SubType>
</None>
</Compile>
<Compile Include="LiveCollection.cs" />
<Compile Include="Model.cs" />
<Compile Include="MsSql\ExecuteMsSqlScript.cs" />
Expand All @@ -140,6 +141,7 @@
<DependentUpon>TemplateCodeResources.resx</DependentUpon>
</Compile>
<Compile Include="MsSql\TABLENAMETrigger.cs" />
<Compile Include="OwinSignalRConfigurator.cs" />
<Compile Include="PersistenceEventManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="MsSql\SqlServerPropertyValuePersistor.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,37 @@
using System.Linq;
using System.Text;
using System.Threading;
using SignalR.Hubs;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using Microsoft.Owin.Hosting;
using Owin;

namespace Construct.Server.Models.Data
{
[HubName("ItemStreamHub")]
public class ItemRealtimeStreamer : IDisposable
public class ItemRealtimeStreamer : Hub, IDisposable
{
private Thread dataStreamThread;
private SignalR.Hosting.Self.Server signalrServer;
private bool isRunning = false;

public ItemRealtimeStreamer(String streamUri)
{
signalrServer = new SignalR.Hosting.Self.Server(streamUri);
signalrServer.MapHubs();
signalrServer.Start();
WebApp.Start(streamUri);

isRunning = true;
dataStreamThread = new Thread(() => DataStreamThread(this));
dataStreamThread.Start();
}

public void Dispose()
public override Task OnConnected()
{
return base.OnConnected();
}

public void Dispose()
{

try
{
signalrServer.Stop();
}
catch (Exception e) { }

signalrServer = null;
}

public void ProcessItemPayload(String jsonPayload)
Expand All @@ -50,9 +49,12 @@ public void AddDataSubscription(String client, Guid sensorId, Guid propertyId)

private static void DataStreamThread(ItemRealtimeStreamer streamer)
{
Random random = new Random();
var hubContext = GlobalHost.ConnectionManager.GetHubContext<ItemRealtimeStreamer>();
while (streamer.isRunning)
{
Thread.Sleep(1);
hubContext.Clients.All.newData("Property Type", "Source ID", random.NextDouble());
Thread.Sleep(200);
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion Construct3/Construct3/Construct.Server.Models.Data/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class Model : Models.Model, IModel

private DataStoreConnectionPool connectionPool;
private SwitchingItemPersistor itemPersistor;
private ItemRealtimeStreamer itemStreamer;

private bool isDatabaseReachable = false;

Expand Down Expand Up @@ -146,11 +147,13 @@ public Model(Uri serverServiceUri, string connectionString, Models.IServer serve
InitializeSerializationAssistant();

InitializePropertyValueServicesAndBuildEndpointDictionary();

InitializeModelCache();

connectionPool = new DataStoreConnectionPool(connectionString);
itemPersistor = new SwitchingItemPersistor(assistant, connectionString, "ItemsCache");
// SigR port on 15999
itemStreamer = new ItemRealtimeStreamer("http://*:15999/00000000-0000-0000-0000-000000000000/Data/");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Owin;

namespace Construct.Server.Models.Data
{
class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<package id="Microsoft.AspNet.SignalR.SelfHost" version="2.1.2" targetFramework="net45" />
<package id="Microsoft.Owin" version="2.0.1" targetFramework="net45" />
<package id="Microsoft.Owin.Diagnostics" version="2.0.1" targetFramework="net45" />
<package id="Microsoft.Owin.Host.HttpListener" version="2.0.1" targetFramework="net45" />
<package id="Microsoft.Owin.Host.HttpListener" version="3.0.0" targetFramework="net45" />
<package id="Microsoft.Owin.Hosting" version="2.0.1" targetFramework="net45" />
<package id="Microsoft.Owin.Security" version="2.0.1" targetFramework="net45" />
<package id="Microsoft.Owin.SelfHost" version="2.0.1" targetFramework="net45" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
<SignManifests>true</SignManifests>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Owin.Host.HttpListener">
<HintPath>..\packages\Microsoft.Owin.Host.HttpListener.3.0.0\lib\net45\Microsoft.Owin.Host.HttpListener.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Data" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Owin.Host.HttpListener" version="3.0.0" targetFramework="net45" />
<package id="NLog" version="3.1.0.0" targetFramework="net40" requireReinstallation="True" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Owin.Host.HttpListener, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.Owin.Host.HttpListener.3.0.0\lib\net45\Microsoft.Owin.Host.HttpListener.dll</HintPath>
</Reference>
<Reference Include="NLog, Version=3.1.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\NLog.3.1.0.0\lib\net40\NLog.dll</HintPath>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Owin.Host.HttpListener" version="3.0.0" targetFramework="net45" />
<package id="NLog" version="3.1.0.0" targetFramework="net40" requireReinstallation="True" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
<SccLocalPath>SAK</SccLocalPath>
<SccAuxPath>SAK</SccAuxPath>
<SccProvider>SAK</SccProvider>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -41,6 +43,13 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.AspNet.SignalR.Client">
<HintPath>..\packages\Microsoft.AspNet.SignalR.Client.2.1.2\lib\net45\Microsoft.AspNet.SignalR.Client.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Data" />
Expand Down Expand Up @@ -85,11 +94,14 @@
<ItemGroup>
<Compile Include="BooleanVisualizer.cs" />
<Compile Include="IntVisualizer.cs" />
<Compile Include="IStreamDataSource.cs" />
<Compile Include="Labeler.xaml.cs">
<DependentUpon>Labeler.xaml</DependentUpon>
</Compile>
<Compile Include="LabelerViewModel.cs" />
<Compile Include="RadGridVisualizer.cs" />
<Compile Include="SignalRStreamDataSource.cs" />
<Compile Include="StreamDataRouter.cs" />
<Compile Include="TimebarBooleanVisualizer.cs" />
<Compile Include="TimebarIntVisualizer.cs" />
<Compile Include="RadChartVisualization.xaml.cs">
Expand Down Expand Up @@ -147,6 +159,7 @@
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="app.config" />
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down Expand Up @@ -176,6 +189,13 @@
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Construct.UX.Views.Visualizations
{
interface IStreamDataSource
{
// Property ID / Source ID / Data
event Action<String, String, object> OnData;

void Start();
void Stop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Construct.UX.ViewModels.Visualizations.VisualizationsServiceReference;
using Microsoft.AspNet.SignalR.Client;

namespace Construct.UX.Views.Visualizations
{
// Intended to connect only to Construct-hosted SigR hub
class SignalRStreamDataSource : IStreamDataSource
{
public event Action<String, String, object> OnData;

public String SourceHostName { get; private set; }
public String DataUri { get; private set; }

private HubConnection hubConnection;

public SignalRStreamDataSource(String dataHostname)
{
SourceHostName = dataHostname;
DataUri = "http://" + SourceHostName + ":15999/00000000-0000-0000-0000-000000000000/Data";

hubConnection = new HubConnection(DataUri);
IHubProxy dataProxy = hubConnection.CreateHubProxy("ItemStreamHub");
dataProxy.On<string, string, double>("newData", DispatchData);
}

public void Start()
{
hubConnection.Start();
}

public void Stop()
{
hubConnection.Stop();
}

private void DispatchData(String propertyName, String sourceName, double data)
{

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Construct.UX.Views.Visualizations
{
class StreamDataRouter
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<TabControl Grid.Row="0" Grid.Column="0">
<TabItem Header="Create">
<StackPanel>
<Button Content="Begin Streaming" Name="StreamButton" Click="StreamButton_Click"></Button>

<Label>Name</Label>
<TextBox Name="nameTextBox" TextChanged="NameChanged"></TextBox>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -121,6 +122,7 @@ private void NotifyPropertyChanged(string propertyName)
}

private Uri serverServiceUri = null;
private IStreamDataSource streamDataSource;


public View(ApplicationSessionInfo sessionInfo)
Expand All @@ -129,6 +131,8 @@ public View(ApplicationSessionInfo sessionInfo)
InitializeComponent();
InitializeViewModel(sessionInfo);

streamDataSource = new SignalRStreamDataSource(sessionInfo.HostName);

this.DataContext = this;
InitializeMembers();

Expand Down Expand Up @@ -344,5 +348,10 @@ private void DataTypeComboBoxDropDownOpened(object sender, EventArgs e)
InitializeMembers();
}

private void StreamButton_Click(object sender, RoutedEventArgs e)
{
streamDataSource.Start();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.SignalR.Client" version="2.1.2" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
</packages>
Binary file not shown.
Loading

0 comments on commit 63d7e4b

Please sign in to comment.