Skip to content

Instantly share code, notes, and snippets.

View alistairjevans's full-sized avatar

Alistair Evans alistairjevans

View GitHub Profile
public class FeedHub : Hub
{
private readonly IMachineState machineState;
private readonly ISampleWriter sampleWriter;
public FeedHub(IMachineState machineState, ISampleWriter sampleWriter)
{
this.machineState = machineState;
this.sampleWriter = sampleWriter;
}
@alistairjevans
alistairjevans / startup.cs
Last active June 9, 2019 10:55
Adding signalr route
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//
// Omitted standard content for brevity...
//
app.UseSignalR(cfg => cfg.MapHub<FeedHub>("/feed"));
}
@alistairjevans
alistairjevans / DataController.cs
Created June 8, 2019 12:31
Pushing data to the signalr clients.
public class DataController : Controller
{
private readonly IMachineState machineState;
private readonly ISampleWriter sampleWriter;
private readonly IHubContext<FeedHub> feedHub;
public DataController(IMachineState machineState, ISampleWriter sampleWriter, IHubContext<FeedHub> feedHub)
{
this.machineState = machineState;
this.sampleWriter = sampleWriter;
@alistairjevans
alistairjevans / Index.cshtml
Last active June 8, 2019 12:57
Landing page for the rower app
@page
<html>
<head>
</head>
<body>
<div class="container">
<div class="lblSpeed text lbl">Speed:</div>
<div class="valSpeed text" id="currentSpeed"><!-- speed goes here --></div>
@alistairjevans
alistairjevans / site.js
Last active June 8, 2019 13:01
Initial app javascript
"use strict";
// Define my connection (note the /feed address to specify the hub)
var connection = new signalR.HubConnectionBuilder().withUrl("/feed").build();
// Get the elements I need
var speedValue = document.getElementById("currentSpeed");
var countValue = document.getElementById("currentCount");
var resetButton = document.getElementById("reset");
@alistairjevans
alistairjevans / site.js
Created June 8, 2019 13:12
Chart configuration
window.onload = function () {
var ctx = document.getElementById('chartCanvas').getContext('2d');
window.myChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [{
label: 'Speed',
data: []
}]
@alistairjevans
alistairjevans / site.js
Created June 8, 2019 13:16
Chart updates as data arrives
connection.on("newData", function (time, speed, count) {
// This subtract causes the data to be placed
// in the centre of the chart as it arrives,
// which I personally think looks better...
var dateValue = moment(time).subtract(5, 'seconds');
speedValue.innerText = speed;
countValue.innerText = count;
@alistairjevans
alistairjevans / program.cs
Created June 16, 2019 09:27
Load Data in Serial
async static Task Main(string[] args)
{
var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("https://myserver");
using (var fileSource = new StreamReader(File.OpenRead(@"C:\Data\Sources\myfile.csv")))
{
await StreamData(fileSource, httpClient, "/api/send");
}
@alistairjevans
alistairjevans / program.cs
Created June 16, 2019 10:04
Load data in parallel
private static async Task StreamDataInParallel(StreamReader fileSource, HttpClient httpClient, string path, int maxParallel)
{
var block = new ActionBlock<string>(
async json =>
{
await httpClient.PostAsync(path, new StringContent(json, Encoding.UTF8, "application/json"));
}, new ExecutionDataflowBlockOptions
{
// Tells the action block how many we want to run at once.
MaxDegreeOfParallelism = maxParallel,
@alistairjevans
alistairjevans / nlog.config
Created September 7, 2019 08:40
JSON NLog Config
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Info"
internalLogFile="c:\temp\internal-nlog.txt">
<!-- enable asp.net core layout renderers -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>