17 April 2014

Using property getter/setter functions in Angular binding

OK, is most circumstances there's a better way to do this, but sometimes life would be easier if we could have a getter function and a setter function in an Angular binding.

Here's a helper function to make it more convenient to wrap them up. It's a bit rough, but does the job.


/**
 * For use when you really want to intercept two-way Angular bindings.
 * (And usually there's another better way)
 *
 * Example
 * <input
 *     ng-model="asProp(getCheckbox, setCheckbox, someArg1, someArg2).value"
 *     type="checkbox" />
 * ...
 * $scope.asProp = lib.asProp;
 * $scope.getCheckbox = function(someArg1, someArg2) { return ... };
 * $scope.setCheckbox = function(newValue, someArg1, someArg2) { ... };
 *
 * Takes a getter and a setter function.
 * And optionally additional arguments.
 * Returns an object with a single property called 'value'.
 * 'value' uses the provided getter and setter functions.
 * Additional arguments are passed to the getter and setter:
 * getter(additionalArgs) -> currentValue
 * setter(newValue, additionalArgs)
 */
lib.asProp = function (getter, setter /* .. args */) {
 var res = {};
 var args = _.toArray(arguments).slice(2);
 Object.defineProperty(res, 'value', {
  get: function () {
   return getter.apply(null, args);
  },
  set: function (val) {
   var args2 = [val].concat(args);
   setter.apply(null, args2);
  },
  enumerable: true,
  configurable: true
 });
 return res;
};

4 January 2014

Aurora inverter USB connection

We had solar electric installed (yay!). We are using the Aurora PVI 4.2kW inverter, which appears to be a very well recommended unit, but there were a couple of challenges with trying to plug into the USB connection to download live data. Just thought I'd note them down here to hopefully save someone else a bit of time.

There are two pieces of software you need:
1. USB driver - makes the Aurora appear under Windows as a COM port. (Texas Instruments 3410 USB driver). We used this one instead of the Aurora one. "TI WDF USBUART Single Driver" from the software section of this page.
2. Aurora communicator software - Communicates with the Inverter to download and chart data.
(Unfortunately the inverter doesn't actually collect historical data, you need to essentially leave the program running and poll for it). Communicator can be downloaded from here. (select 'UK' as the country, then select Software - because not all countries have any 'software' listed against them).

Issue 1: My laptop is running Windows 7 (x64). For better or worse, the 64 bit version of Windows doesn't run unsigned device drivers. The driver may look like it has installed - but it won't run. The work-around for now is to boot Windows in the special mode to allow unsigned drivers. (Press F8 while the computer is booting, and select "Disable Driver Signature Enforcement."). Or see here for other options.

Issue 2: The Inverter itself needed to be assigned an "address" before it would talk to anything. Go to the inverter itself and use the menu options to go into the settings and set an address. Any number will do. We used "2". Seems to be necessary and in our case wasn't already preconfigured.

Next task: ditch the 70W laptop and start using a Rasberry Pi instead.