Created
November 8, 2016 02:14
-
-
Save clemensv/d7290cc02b820cea1a845690ee30c149 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<style> | |
body { | |
font-family: Tahoma, Geneva, sans-serif; | |
} | |
div { | |
display: inline; | |
} | |
</style> | |
<script> | |
function updateStats(memuse) { | |
document.getElementById('x').innerHTML = memuse.x; | |
document.getElementById('y').innerHTML = memuse.y; | |
document.getElementById('z').innerHTML = memuse.z; | |
} | |
var host = window.document.location.host.replace(/:.*/, ''); | |
var ws = new WebSocket('wss://cvrelaywus.servicebus.windows.net:443/$hc/public?sb-hc-action=connect');// &sb-hc-token={{token}}'); | |
ws.onmessage = function (event) { | |
updateStats(JSON.parse(event.data)); | |
}; | |
</script> | |
</head> | |
<body> | |
<strong>Accel</strong><br> | |
X: <div id='x'></div><br> | |
Y: <div id='y'></div><br> | |
Z: <div id='z'></div><br> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Windows.Devices.Sensors; | |
using Microsoft.Azure.Relay; | |
using Newtonsoft.Json; | |
namespace SensorFeedServer | |
{ | |
class Program | |
{ | |
private List<HybridConnectionStream> connections = new List<HybridConnectionStream>(); | |
private AccelerometerReading lastReading; | |
static void Main(string[] args) | |
{ | |
new Program().Run().GetAwaiter().GetResult(); | |
} | |
async Task Run() | |
{ | |
var accel = Accelerometer.GetDefault(); | |
accel.ReadingChanged += (Accel_ReadingChanged); | |
Uri ns = new Uri("sb://cvrelaywus.servicebus.windows.net/public"); | |
TokenProvider tp = TokenProvider.CreateSharedAccessSignatureTokenProvider("listen", "...key..."); | |
var hcl = new HybridConnectionListener(ns, tp); | |
await hcl.OpenAsync(CancellationToken.None); | |
Console.WriteLine("ready"); | |
do | |
{ | |
var cxn = await hcl.AcceptConnectionAsync(); | |
cxn.WriteMode = WriteMode.Text; | |
Console.Write("+"); | |
this.connections.Add(cxn); | |
} while (true); | |
} | |
private void Accel_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args) | |
{ | |
var reading = args.Reading; | |
if ( this.lastReading != null ) | |
{ | |
if (Math.Abs(lastReading.AccelerationX-reading.AccelerationX) < 0.1 && | |
Math.Abs(lastReading.AccelerationY - reading.AccelerationY) < 0.1 && | |
Math.Abs(lastReading.AccelerationZ - reading.AccelerationZ) < 0.1 ) | |
{ | |
return; | |
} | |
} | |
this.lastReading = args.Reading; | |
Console.Write("."); | |
var r = new | |
{ | |
x = reading.AccelerationX, | |
y = reading.AccelerationY, | |
z = reading.AccelerationZ | |
}; | |
var j = JsonConvert.SerializeObject(r); | |
foreach (var cxn in new List<HybridConnectionStream>(connections)) | |
{ | |
try | |
{ | |
var u = Encoding.UTF8.GetBytes(j); | |
cxn.Write(u,0,u.Length); | |
} | |
catch (Exception) | |
{ | |
Console.Write("-"); | |
connections.Remove(cxn); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment