Skip to content

Commit f209fb4

Browse files
committed
Merge branch 'develop'
2 parents 5d5be1d + 59bd852 commit f209fb4

File tree

12 files changed

+137
-24
lines changed

12 files changed

+137
-24
lines changed

Makefile

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
SHELL = /bin/bash
22

3+
SERVER_FILES = \
4+
src/scripts/brewable.js \
5+
src/scripts/modules/configuration.js \
6+
src/scripts/modules/cpuinfo.js \
7+
src/scripts/modules/fhem.js \
8+
src/scripts/modules/gpioworker.js \
9+
src/scripts/modules/jobprocessor.js \
10+
src/scripts/modules/jsogpio.js \
11+
src/scripts/modules/queue.js \
12+
src/scripts/modules/requestHandlers.js \
13+
src/scripts/modules/router.js \
14+
src/scripts/modules/sainsmartrelay.js \
15+
src/scripts/modules/sensor.js \
16+
src/scripts/modules/sensorLister.js \
17+
src/scripts/modules/server.js
18+
19+
CLIENT_FILES = \
20+
src/scripts/status.js \
21+
322
TEST_FILES = test-status.js \
423
src/scripts/modules/jsogpio.js \
524
src/scripts/modules/cpuinfo.js \
@@ -48,15 +67,15 @@ default.conf: default.conf.in
4867
node_modules:
4968
npm install
5069

51-
client: node_modules
70+
client: node_modules $(CLIENT_FILES)
5271
./node_modules/.bin/rollup --config client.config.js
5372
touch client
5473

5574
test.js: $(TEST_FILES)
5675
./node_modules/.bin/rollup --config test.config.js
5776
chmod a+x test.js
5877

59-
server: node_modules src/scripts/brewable.js
78+
server: node_modules $(SERVER_FILES)
6079
patch -p0 < websocket-no-binaries.diff
6180
./node_modules/.bin/rollup --config server.config.js
6281
patch -p0 -R < websocket-no-binaries.diff

client.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import cssnext from 'postcss-cssnext';
1515
import cssnano from 'cssnano';
1616

1717
export default {
18-
entry: 'status.js',
18+
entry: 'src/scripts/status.js',
1919
targets: [
2020
{ dest: 'build/js/brewableclientbundle.js', format: 'iife' },
2121
],

server.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default {
1414
entry: 'src/scripts/brewable.js',
1515
dest: 'build/js/brewableserverbundle.js',
1616
format: 'cjs',
17-
external: [ 'os', 'path', 'buffer', 'http', 'https', 'events', 'util', 'tty', 'net', 'url', 'fs', 'crypto' ],
17+
external: [ 'os', 'path', 'buffer', 'http', 'https', 'events', 'util', 'tty', 'net', 'url', 'fs', 'crypto', 'querystring' ],
1818
banner: '#!/usr/bin/env node',
1919
sourceMap: 'inline',
2020
plugins: [

src/scripts/brewable.js

100755100644
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import start from "./modules/server";
33
import route from "./modules/router";
44
import { index as rhindex } from "./modules/requestHandlers";
55
import { favicon as rhfavicon } from "./modules/requestHandlers";
6+
import { fhem as rhfhem } from "./modules/requestHandlers";
67
import { status as rhstatus } from "./modules/requestHandlers";
78
import { ws as rhws } from "./modules/requestHandlers";
89
import Queue from "./modules/queue";
@@ -101,6 +102,7 @@ handle["/index"] = rhindex;
101102
handle["/index.htm"] = rhindex;
102103
handle["/index.html"] = rhindex;
103104
handle["/favicon.ico"] = rhfavicon;
105+
handle["/fhem"] = rhfhem;
104106
handle["/status.js"] = rhstatus;
105107
handle["/ws"] = rhws;
106108

src/scripts/modules/configuration.js

100755100644
Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
//import fs from "fs";
1+
import fs from "fs";
22
import mkdirp from "mkdirp";
3-
//import path from 'path';
4-
var fs = require('fs');
5-
var path =require('path');
6-
//var mkdirp = require('mkdirp');
3+
import path from 'path';
74
var home = require('os').homedir();
85

96

@@ -12,7 +9,13 @@ var defaultConfigValues = function() {
129
'sensorFudgeFactors' : {},
1310
'multiSensorMeanWeight' : parseInt(50),
1411
'relayDelayPostON' : parseInt(180),
15-
'relayDelayPostOFF' : parseInt(480)
12+
'relayDelayPostOFF' : parseInt(480),
13+
'iSpindelA' : {
14+
"name":"iSpindel",
15+
"calfactor0":0.00438551,
16+
"calfactor1":0.13647658,
17+
"calfactor2":6.968821422
18+
}
1619
};
1720
};
1821

@@ -64,6 +67,13 @@ Configuration.prototype.loadConfigFromFile = function () {
6467
}
6568
};
6669

70+
/* Save the user's configuration
71+
*/
72+
Configuration.prototype.saveConfigToFile = function () {
73+
console.log("saveConfigToFile()");
74+
fs.writeFileSync(this._configFileName, JSON.stringify(this._configuration));
75+
};
76+
6777
Configuration.prototype.getConfiguration = function () {
6878
return this._configuration;
6979
};
@@ -85,6 +95,8 @@ Configuration.prototype.dir = function (topic) {
8595
};
8696

8797
Configuration.prototype.updateFudgeEntry = function (sensorIds) {
98+
console.log("updateFudgeEntry()");
99+
88100
var config = this._configuration;
89101
/* Case 1 - Simple conversion
90102
from: single 'global' fudge factor (applying to all sensors)
@@ -141,7 +153,7 @@ Configuration.prototype.updateFudgeEntry = function (sensorIds) {
141153
};
142154

143155
Configuration.prototype.setMultiSensorMeanWeight = function (value) {
144-
console.log("updateMultiSensorMeanWeight(" + value + ")");
156+
console.log("setMultiSensorMeanWeight(" + value + ")");
145157
var config = this._configuration;
146158
if (value < 0 ) {
147159
value = 0;
@@ -152,6 +164,24 @@ Configuration.prototype.setMultiSensorMeanWeight = function (value) {
152164
fs.writeFileSync(this._configFileName, JSON.stringify(config));
153165
};
154166

167+
Configuration.prototype.setRelayDelayPost = function (onoff, value) {
168+
console.log("setRelayDelayPost(" + onoff + ", " + value + ")");
169+
if (onoff == 'relayDelayPostON' || onoff == 'relayDelayPostOFF' ) {
170+
var config = this._configuration;
171+
config[onoff] = value;
172+
fs.writeFileSync(this._configFileName, JSON.stringify(config));
173+
} else {
174+
console.log("Unkown config item (" + onoff + ")");
175+
}
176+
};
177+
178+
Configuration.prototype.setFudgeFactor = function (key, val) {
179+
//console.log("setFudgeFactor() " + key + ", val " + val);
180+
181+
var config = this._configuration;
182+
config.sensorFudgeFactors[key] = val;
183+
fs.writeFileSync(this._configFileName, JSON.stringify(config));
184+
};
155185

156186
/********
157187
atest = new Configuration();

src/scripts/modules/gpioworker.js

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var sensorDevices = [];
2323
var sensorResults = [];
2424
var sensorsRead = 0;
2525

26+
2627
function gpioWorker (input_queue, output_queue) {
2728
this.input_queue = input_queue;
2829
this.output_queue = output_queue;
@@ -48,6 +49,10 @@ function gpioWorker (input_queue, output_queue) {
4849
// Relay device
4950
this.relay = new Relay();
5051
//this.relay.testConnected();
52+
for (var i=0;i<this.relay.deviceCount();i++) {
53+
this.relay.setDelaySetValue(i+1, 'on_time', this.configuration['relayDelayPostON']);
54+
this.relay.setDelaySetValue(i+1, 'off_time', this.configuration['relayDelayPostOFF']);
55+
}
5156

5257
// Temperature sensors
5358
sensorDevices = this.sensorDevices();
@@ -57,7 +62,7 @@ function gpioWorker (input_queue, output_queue) {
5762
this.configObj.updateFudgeEntry(sensorDevices);
5863
console.log("Using configuration: " + JSON.stringify(this.configuration));
5964

60-
// Set sensor fudge according to configuration
65+
// Now set sensor fudge according to updated configuration
6166
sensorDevices.forEach( function (sensor) {
6267
sensor.setFudge(parseFloat(config['sensorFudgeFactors'][sensor.getId()]));
6368
});
@@ -125,16 +130,30 @@ function gpioWorker (input_queue, output_queue) {
125130
}
126131
}.bind(this));
127132

128-
// eventEmitter WAS global (from index.js)
129133
eventEmitter.on('sensor_read', allSensorsRead);
130134
eventEmitter.on('msg_waiting', this.processMessage.bind(this));
131135

136+
eventEmitter.on('fhem_reading', function(obj) {
137+
this.fhemReading(obj);
138+
}.bind(this));
139+
132140
}
133141
//module.exports = gpioWorker;
134142
//export default gpioWorker;
135-
//export default gpioWorker;
136143
export { gpioWorker, eventEmitter };
137144

145+
gpioWorker.prototype.fhemReading = function (reading) {
146+
//console.log("fhemReading() " + JSON.stringify(reading));
147+
var name = reading.name;
148+
var tilt = reading.tilt;
149+
var temp = reading.temp;
150+
var batt = reading.batt;
151+
console.log("fhem() " + name + ", " + tilt + ", " + temp + ", " + batt);
152+
153+
//var plato = calcPlato(tilt, temp);
154+
//console.log("plato = " + plato);
155+
};
156+
138157
gpioWorker.prototype.sensorDevices = function () {
139158
var deviceList = [];
140159

@@ -145,6 +164,13 @@ gpioWorker.prototype.sensorDevices = function () {
145164
for (var z=0;z<sensorList.length;z++) {
146165
deviceList.push(new sensordevice(sensorList[z]));
147166
}
167+
168+
// Sort the device list by (slightly mangled) id's
169+
deviceList.sort(function(a, b) {
170+
var aval = parseInt(a.id.substr(0,a.id.search("-")),16) + parseInt(a.id.substr(a.id.search("-") + 1),16);
171+
var bval = parseInt(b.id.substr(0,b.id.search("-")),16) + parseInt(b.id.substr(b.id.search("-") + 1),16);
172+
return aval - bval;
173+
});
148174
return deviceList;
149175
};
150176

@@ -320,11 +346,25 @@ gpioWorker.prototype.config_change = function (msg) {
320346
item.setFudge(msg.data['fudge']);
321347
}
322348
});
349+
//this.configuration.sensorFudgeFactors[msg.data['sensorFudgeFactors']] = msg.data['fudge'];
350+
this.configObj.setFudgeFactor(msg.data['sensorFudgeFactors'], msg.data['fudge']);
323351
} else if (keys[0] == 'multiSensorMeanWeight') {
324352
this.configObj.setMultiSensorMeanWeight(msg.data['multiSensorMeanWeight']);
353+
} else if (keys[0] == 'relayDelayPostON' || keys[0] == 'relayDelayPostOFF') {
354+
for (var i=0;i<this.relay.deviceCount();i++) {
355+
if (keys[0] == 'relayDelayPostON') {
356+
this.relay.setDelaySetValue(i+1, 'on_time', msg.data[keys[0]]);
357+
} else {
358+
this.relay.setDelaySetValue(i+1, 'off_time', msg.data[keys[0]]);
359+
}
360+
}
361+
//this.configuration[keys[0]] = msg.data[keys[0]];
362+
this.configObj.setRelayDelayPost(keys[0], msg.data[keys[0]]);
325363
} else {
326364
console.log("config_change(): " + keys[0] + " = " + msg.data[keys[0]]);
327365
}
366+
console.log("this.configuration = " + JSON.stringify(this.configuration));
367+
//this.configObj.saveConfigToFile();
328368
};
329369

330370
gpioWorker.prototype.list_sensors = function (msg) {

src/scripts/modules/requestHandlers.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var fs = require("fs");
22
var path = require('path');
3+
import { newReading } from './fhem.js';
34

45
function index(response) {
56
console.log("Request handler 'index' was called.");
@@ -38,6 +39,24 @@ function favicon(response) {
3839
});
3940
}
4041

42+
function fhem(response, query) {
43+
console.log("Request handler 'fhem' was called with query: " + query);
44+
newReading(query);
45+
/*
46+
fs.readFile(path.join(__dirname, "../../../favicon.ico"), function (err, data) {
47+
if(err){
48+
response.writeHead(404);
49+
response.write("Not Found!");
50+
} else {
51+
response.writeHead(200, {"Content-Type": "image/x-icon"});
52+
response.write(data);
53+
}
54+
response.end();
55+
});
56+
*/
57+
response.end();
58+
}
59+
4160
function status(response) {
4261
console.log("Request handler 'status' was called.");
4362
//fs.readFile(path.join(__dirname, "../../../status.js"), function (err, data) {
@@ -80,6 +99,6 @@ function ws(response) {
8099
response.end();
81100
}
82101

83-
export { status, css, index, favicon, ws };
102+
export { status, css, index, favicon, ws, fhem };
84103

85104
/* ex:set ai shiftwidth=2 inputtab=spaces smarttab noautotab: */

src/scripts/modules/router.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
function route(handle, pathname, response) {
2-
console.log("About to route a request for " + pathname);
1+
function route(handle, pathname, query, response) {
2+
console.log("About to route a request for " + pathname + " with query: " + query);
33
if (typeof handle[pathname] === 'function') {
4-
handle[pathname](response);
4+
handle[pathname](response, query);
55
} else {
66
console.log("No request handler found for " + pathname);
77
response.writeHead(404, {"Content-Type": "text/plain"});

src/scripts/modules/sensor.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ SensorDevice.prototype.getId = function () {
2020
SensorDevice.prototype.getTempAsync = function (callback) {
2121
var dpath = '/sys/bus/w1/devices/' + this.id + '/w1_slave';
2222
var id = this.id;
23+
var fudge = this.fudge;
2324
fs.readFile(dpath, 'utf8', function (err, data) {
2425
if (err) {
2526
console.log('Error reading device data: ' + dpath);
2627
} else {
27-
var result = parseFloat(data.split(' ')[20].split('=')[1]) / 1000;
28+
var result = fudge + parseFloat(data.split(' ')[20].split('=')[1]) / 1000;
2829
callback(id, result);
2930
}
3031
});
@@ -34,7 +35,7 @@ SensorDevice.prototype.getTemp = function () {
3435
var dpath = '/sys/bus/w1/devices/' + this.id + '/w1_slave';
3536
var data = fs.readFileSync(dpath, 'utf8');
3637
//console.log('(SensorDevice)' + this.id + ' data = ' + data);
37-
return parseFloat(data.split(' ')[20].split('=')[1]) / 1000;
38+
return this.fudge + parseFloat(data.split(' ')[20].split('=')[1]) / 1000;
3839
};
3940

4041
SensorDevice.prototype.setFudge = function (fudgeFactor) {

src/scripts/modules/sensorLister.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var fs =require('fs');
1+
import fs from "fs";
22

33
// Return a list of sensor devices
44
export default function sensors() {

0 commit comments

Comments
 (0)