The following errors were encountered: Please rectify them and try again.

Using a JSON string as a configuration source for a .NET Core project

I recently had a project at work that I wanted to pass in a JSON string as a means of configuration. There is a .NET package that will read in a JSON file, but not one to read in a string. So below you will find a set of classes I made to take in a JSON string and parse it in to the application config using the Newtonsoft JSON library.

  1. public static class ConfigurationExtensions
  2. {
  3. public static IConfigurationBuilder AddJsonString(this IConfigurationBuilder configurationBuilder, string json)
  4. {
  5. configurationBuilder.Add(new JsonStringConfigurationSource(json));
  6. return configurationBuilder;
  7. }
  8. }
  10. public class JsonStringConfigurationSource : IConfigurationSource
  11. {
  12. private readonly string _json;
  14. public JsonStringConfigurationSource(string json) => _json = json;
  16. public IConfigurationProvider Build(IConfigurationBuilder builder) => new JsonStringConfigurationProvider(_json);
  17. }
  19. public class JsonStringConfigurationProvider : ConfigurationProvider

Posted on Wednesday the 3rd of October 2018


The Home Automation Saga

Home Automation UIHome Automation UI

Since my posts heading in to the world of home automation I've gone on a bit of a saga. I've abandoned Home Assistant, as I wasn't happy with the way it functioned; I found creating rules for it far too obtuse if you wanted to do anything more than turn things on and off based on a single event. My response to this was, of course, to write my own software for it.

The idea behind my implementation is to simply be able to set rules up through a GUI. There are 3 main parts to it: the core backend, the web interface, and the components. It is currently in active development, and by no means complete, but functions enough to have fully replaced Home Assistant for my needs.

The backend is written in C# (Mono compatible) and uses a simple message bus system to allow each object to communicate. It has triggers which contain a set of conditions and will either match against all of them or any of them. When the conditions for the trigger is met, it then performs an action on a device (currently only switches). Conditions can be set up to compare against time, numbers, or words.

Posted on Saturday the 30th of September 2017


A basic timer for the Arduino

For one of my Sonoff switches I'm using a timer to ping my PC so it switches everything off with my PC (see code). Originally, for the sake of time (forgive the pun), I ripped code from this page. However, it was a bit more complicated to use that I anticipated and resetting the timer interval didn't always work (I don't know if it ever did, tbh). So I wrote my own, with significantly fewer lines. The only major disadvantage is that you can't register multiple timers under one loop method.

Anyway, here it is:

  1. #ifndef _TIMER_h
  2. #define _TIMER_h
  4. #if defined(ARDUINO) && ARDUINO >= 100
  5. #include "arduino.h"
  6. #else
  7. #include "WProgram.h"
  8. #endif
  9. #include <functional>
  11. class Timer
  12. {
  13. protected:
  14. int _interval;
  15. std::function<void()> _callback;
  16. unsigned long _lastRunTime;
  17. bool _enabled;
  18. void constructor(int interval, std::function<void()> callback, bool startImmediately);

Posted on Saturday the 25th of February 2017


DIY Home Automation: Update

It's been nearly a year since my last post. I had no idea it'd been that long!

When I last posted I was trying to get some control over the boiler in my home. This sort of worked for a while until the relay in the receiver unit seized. It was replaced with a new unit that's totally different. It uses the 868MHz frequency instead of 433MHz. I did get a transceiver, going by the catchy name of nRF905, to attempt to communicate with the new receiver unit, but failed to get it working with the RPi. I have not had another attempt at it for a while.

The code for the thermostat Pi has not gone unloved, however. See here for the code as it currently stands. The main change is that it can now publish to MQTT, and that I've massively sped up reading from the sensor.

Further towards the home automation goal I've found these Sonoff units. They use an ESP8266 chip which can run Arduino code and they also expose the headers to load new code up on to the chip, which makes them excellent for hacking. I've cooked up my own code for these that uses MQTT for communication. MQTT is excellent for this, and I've made the firmware so it's easily controllable from any source, and will keep its status up to date. I currently use MQTT Dash on my phone and Home Assistant on a Pi to control things. I'm not massively satisfied by Home Assistant however so, being me, I might make my own alternative. Currently I have 3 Sonoff in-line switches wired in to 3 lamps, and 2 wall socket switches.

Posted on Saturday the 28th of January 2017


The start of DIY home automation: replacing the thermostat, part 2

My DS18B20 sensors arrived the other day. Using Adafruit's tutorial on the sensor I was able to get a reading out of it in a matter of minutes. During Friday evening I was able to knock up a quick thermostat script.

  1. import time;
  2. import sys;
  3. import RPi.GPIO as GPIO;
  5. targetTemp = 21;
  6. sendPin = 24;
  7. sendAttempts = 3;
  8. tempSensor = '28-03157469ceff';
  9. onCode = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1];
  10. offCode = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 2, 1, 1, 1];
  13. def sendSignal(code):
  14. signal = False;
  15. for t in range(sendAttempts):
  16. for i in code:
  17. signal = not signal;

Posted on Saturday the 12th of March 2016


The start of DIY home automation: replacing the thermostat

Since forever I've always been enamoured with the idea of home automation, but it's always been beyond my reach for one reason or another. Lately the blocking factor has been that I'm renting so I can't really pull apart the makings of the house. Fortunately, however, the house I'm in now has a wireless thermostat, which means I can control the heating without any intrusive measures as it uses the 433MHz band.

I have managed to replicate the wireless signal that the thermostat sends to the boiler for both the on and off commands with a Raspberry Pi and a cheap 433MHz receiver and transmitter. This instructables article was instrumental in understanding what I needed to do in order to get the job done. This blog post was also very helpful. The code from the Instructables article was very useful in order to record and inspect the signal sent by the thermostat.

Posted on Sunday the 6th of March 2016


Displaying the keyboard on iOS when showing a textbox with AngularJS

Consider the following HTML and JavaScript:

  1. <div data-ng-controller="controller">
  2. <button data-ng-click="showTb=true">
  3. Show TextBox
  4. </button>
  6. <div data-ng-show="showTb">
  7. <form>
  8. <input type="text" />
  9. </form>
  10. </div>
  11. </div>

  1. angular.module('module')
  2. .controller("controller", [
  3. "$scope", "$timeout", function($scope, $timeout) {
  4. $scope.showTb=false;
  5. }
  6. ]);

This is a basic example of a simple AngularJS setup to show a textbox. Now we'll edit it to fire the focus event.

  1. <div data-ng-controller="controller">
  2. <button data-ng-click="displayTb()">
  3. Show TextBox
  4. </button>
  6. <div data-ng-show="showTb">
  7. <form>
  8. <input type="text" id="textBox" />
  9. </form>
  10. </div>
  11. </div>

  1. angular.module('module')
  2. .controller("controller", [
  3. "$scope", "$timeout", function($scope, $timeout) {
  4. $scope.showTb=false;
  6. var displayTb=function(){

Posted on Tuesday the 28th of July 2015


I decided to cycle from Macclesfield to Manchester, and back, using Sustrans routes. It was an interesting adventure.

For this trip to the National Cycling Centre I chose my Arkose. There was no reason for this trip other than wanting to and having nothing better to do. I loaded up my panniers, checked my tyre pressure, entered the route into my phone. Set to do as little on roads as possible I set off from my home just North of Macclesfield. A few suburban roads and then on to the Middlewood way for seven and a half miles to Marple. Through Marple I followed National Cycle Route 55 where it terminated in Stockport. So far the route had been rather uneventful. I did meet a family in Marple trying to follow the route and gave information which reassured them they were on the right path.

I made my way to Stockport Road West which I followed until I my turning down Welkin Road. This led me to my next bit of off road pathway, which took me under the M60. I wish I took some pictures because it was the the most decrepit looking part, with cycle gates (credit) standing independently off the edge of the tarmac. Graffiti donned the walls of the tunnels and dying foliage was creeping on to the pathway with bits of branch offering a different route than that designed. I felt like this was the perfect place to be jumped, so I kept a quick pace and didn't stop.

Posted on Sunday the 1st of June 2014


Stretching the WPF Expander Header

Header content left and stretchHeader content left and stretch

Here I'll outline a simple method for expanding the header in a WPF expander. The code I'll use below is from a test tool I'm prototyping.

The reason that the header doesn't expand is because the ContentPresenter inside the header has its HorizontalAlignment property to Left. We unfortunately can't set this to anything else in XAML without overriding the Control Template, which is a bit overkill for this simple task.

To make this work we want to override the Expander.HeaderTemplate property. To forward the binding we need to set the Header property to {Binding}.

  1. <Expander Header="{Binding}">
  2. <Expander.HeaderTemplate>
  3. <DataTemplate>
  4. <Grid Loaded="HeaderTemplateStretchHack">
  5. ...
  6. </Grid>
  7. </DataTemplate>
  8. </Expander.HeaderTemplate>
  9. </Expander>

You may notice that the root element inside the HeaderTemplate is a Grid. You can use other controls like Border, but they'll need to have a Loaded event. We use the loaded event to work up the UI tree and then set the HorizontalAlignment property of the ContentPresenter that contains our Grid to Stretch.

Posted on Wednesday the 27th of November 2013


Binding a WPF DataGrid to a dynamic class or type

Dynamically generating a classDynamically generating a class

After searching the internet for days on end trying to solve this one problem I found no answers. Nothing. Not even a hint. Luckily I had covered a particular topic in some lectures at university only a couple of weeks prior which would enable me to create a solution to this problem. I feel this problem shouldn't even exist. This problem is binding data of a dynamic type to a DataGrid in WPF.

There are many ways to generate dynamic types within .NET and C#. These, however, will not work with the WPF DataGrid thanks to the way the DataGrid reflects on types to generate itself. The DataGrid control is only able to gather information to render its columns and cells from non-dynamic types through reflection, which doesn't seem to support dynamic types.

Before I get on to the solution I'll put some context behind the problem I encountered; I needed to create a table generated from the attributes of XML nodes. There would be a node that contained a list of nodes each containing the same attributes, much like a table. The problem is that I needed a way to bind this data, and keep to MVVM as much as possible, but I didn't know before runtime what columns I would need. I tried many methods such as the dynamic type, as well as DynamicObject and ElastoObject classes, and none of these could be used with a DataGrid.

Posted on Wednesday the 27th of February 2013