|
| 1 | +var OWL = require('./node-owlintuition/owl.js'); |
| 2 | +var util = require('util'); |
| 3 | +var http = require('http'); |
| 4 | +var url = require('url'); |
| 5 | +var config = require('config'); |
| 6 | +var querystring = require('querystring'); |
| 7 | + |
| 8 | +// Get the config into some handy vars |
| 9 | +var feeds = config.get('feeds'); |
| 10 | +var nodes = config.get('nodes'); |
| 11 | + |
| 12 | +var debug = config.has('debug') && config.get('debug'); |
| 13 | +var log = debug ? console.log : function () {}; |
| 14 | + |
| 15 | +var owl = new OWL(); |
| 16 | +log("Starting monitor"); |
| 17 | +owl.monitor(); |
| 18 | + |
| 19 | +owl.on('electricity', function( event ) { |
| 20 | + data = JSON.parse(event); |
| 21 | + log( "electricity = " + util.inspect(data, {"depth": null}) ); |
| 22 | + packet = { |
| 23 | + signalRssi: data.signal.rssi, |
| 24 | + signalLqi: data.signal.lqi, |
| 25 | + battery: data.battery, |
| 26 | + ch1Current: data.channels['0'][0].current, |
| 27 | + ch2Current: data.channels['1'][0].current, |
| 28 | + ch3Current: data.channels['2'][0].current, |
| 29 | + ch1Day: data.channels['0'][1].day, |
| 30 | + ch2Day: data.channels['1'][1].day, |
| 31 | + ch3Day: data.channels['2'][1].day |
| 32 | + } |
| 33 | + reportToEmon(nodes.electricity, packet); |
| 34 | +}); |
| 35 | + |
| 36 | +owl.on('heating', function( event ) { |
| 37 | + data = JSON.parse(event); |
| 38 | + log( "heating = " + util.inspect(data, {"depth": null}) ); |
| 39 | + packet = { |
| 40 | + signalRssi: data.signal.rssi, |
| 41 | + signalLqi: data.signal.lqi, |
| 42 | + battery: data.battery, |
| 43 | + tempCurrent: data.temperature.current, |
| 44 | + tempRequired: data.temperature.required, |
| 45 | + state: data.temperature.state, |
| 46 | + flags: data.temperature.flags |
| 47 | + } |
| 48 | + reportToEmon(nodes.heating, packet); |
| 49 | +}); |
| 50 | + |
| 51 | +owl.on('weather', function( event ) { |
| 52 | + log( "weather = " + util.inspect(JSON.parse(event), {"depth": null}) ); |
| 53 | +}); |
| 54 | + |
| 55 | +owl.on('solar', function( event ) { |
| 56 | + log( "solar = " + util.inspect(JSON.parse(event), {"depth": null}) ); |
| 57 | +}); |
| 58 | + |
| 59 | +function reportToEmon(node, packet) |
| 60 | +{ |
| 61 | + log( "node = " + node ); |
| 62 | + log( "packet = " + util.inspect(packet, {"packet": null}) ); |
| 63 | + for(var i = 0; i < feeds.length; i++) { |
| 64 | + postData(feeds[i].url, node, feeds[i].key, packet); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +function postData(urlString, node, key, packet) |
| 69 | +{ |
| 70 | + urlParsed = url.parse(urlString); |
| 71 | + |
| 72 | + // EmonCMS does not look to like properly encoded query strings... |
| 73 | + var post_data = 'json='+JSON.stringify(packet)+ |
| 74 | + '&apikey='+key+ |
| 75 | + '&node='+node; |
| 76 | + |
| 77 | + log( "post_data = '" + post_data + "'"); |
| 78 | + |
| 79 | + // An object of options to indicate where to post to |
| 80 | + var post_options = { |
| 81 | + host: urlParsed.host, |
| 82 | + port: '80', |
| 83 | + path: urlParsed.path+"?"+post_data, |
| 84 | + method: 'GET', |
| 85 | + }; |
| 86 | + |
| 87 | + // Set up the request |
| 88 | + var post_req = http.request(post_options, function(res) { |
| 89 | + res.setEncoding('utf8'); |
| 90 | + res.on('data', function (chunk) { |
| 91 | + log('Response: ' + chunk); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + post_req.on('error', function(e) { |
| 96 | + log("Got error: " + e.message); |
| 97 | + }); |
| 98 | + post_req.end(); |
| 99 | +} |
0 commit comments