Skip to content

Commit

Permalink
sensor.js->ds18b20.js. Reenable sensor configuration (still need to a…
Browse files Browse the repository at this point in the history
…pply config to running jobs at startup)
  • Loading branch information
cwilling committed Sep 9, 2017
1 parent cbea9a6 commit 4a87b34
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 96 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ SERVER_FILES = \
src/scripts/brewable.js \
src/scripts/modules/configuration.js \
src/scripts/modules/cpuinfo.js \
src/scripts/modules/sensor.js \
src/scripts/modules/ds18b20.js \
src/scripts/modules/fhem.js \
src/scripts/modules/gpioworker.js \
src/scripts/modules/jobprocessor.js \
Expand All @@ -12,7 +14,6 @@ SERVER_FILES = \
src/scripts/modules/requestHandlers.js \
src/scripts/modules/router.js \
src/scripts/modules/sainsmartrelay.js \
src/scripts/modules/sensor.js \
src/scripts/modules/server.js

CLIENT_FILES = \
Expand Down
95 changes: 95 additions & 0 deletions src/scripts/modules/ds18b20.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import fs from 'fs';

const DSNAME = Symbol();
const FUDGE = Symbol();

// ds18b20Device object
class ds18b20Device {
constructor (val) {
//this.name = val;
//this.id = val;
this[DSNAME] = val;
this[FUDGE] = parseFloat(0.7);

console.log("ds18b20Device constructor() name = " + this.name);

//console.log('New ds18b20Device with id = ' + this.id + ', fudge = ' + this.fudge);
}

// Return a list of sensor devices
static sensors() {
var deviceDirectory = '/sys/bus/w1/devices/w1_bus_master1/w1_master_slaves';
var returnList = [];
var data = fs.readFileSync(deviceDirectory, 'utf8');
var devs = data.split('\n');
devs.pop();
for (var i=0;i<devs.length;i++) {
//console.log("substr = " + devs[i].substr(-8));
if (devs[i].substr(-8) != "00000000") returnList.push(devs[i]);
}
return returnList;
}

set name (val) {}
get name () { return this[DSNAME]; }
set id (val) {}
get id () { return this[DSNAME]; }

set fudge (fudgeFactor) {
this[FUDGE] = fudgeFactor;
}
get fudge () {
return this[FUDGE];
}

get temp () {
var dpath = '/sys/bus/w1/devices/' + this.id + '/w1_slave';
var data = fs.readFileSync(dpath, 'utf8');
console.log('(ds18b20Device) ' + this.id + ' data = ' + data);
console.log('(ds18b20Device) fudge ' + this.fudge);
console.log('(ds18b20Device) ' + parseFloat(data.split(' ')[20].split('=')[1]) / 1000);
return parseFloat(this.fudge) + parseFloat(data.split(' ')[20].split('=')[1]) / 1000;
}

getTempAsync (callback) {
var dpath = '/sys/bus/w1/devices/' + this.id + '/w1_slave';
var id = this.id;
var fudge = this.fudge;
fs.readFile(dpath, 'utf8', function (err, data) {
if (err) {
console.log('Error reading device data: ' + dpath);
} else {
var result = parseFloat(fudge) + parseFloat(data.split(' ')[20].split('=')[1]) / 1000;
callback(result, id);
}
});
}

}
export default ds18b20Device;


/*
ds18b20Device.prototype.getTempAsync = function (callback) {
var dpath = '/sys/bus/w1/devices/' + this.id + '/w1_slave';
var id = this.id;
var fudge = parseFloat(this.fudge);
fs.readFile(dpath, 'utf8', function (err, data) {
if (err) {
console.log('Error reading device data: ' + dpath);
} else {
var result = parseFloat(fudge) + parseFloat(data.split(' ')[20].split('=')[1]) / 1000;
callback(result, id);
}
});
};
ds18b20Device.prototype.getTemp = function () {
var dpath = '/sys/bus/w1/devices/' + this.id + '/w1_slave';
var data = fs.readFileSync(dpath, 'utf8');
console.log("getTemp(): " + parseFloat(data.split(' ')[20].split('=')[1]) / 1000);
return (parseFloat(this.fudge) +parseFloat(data.split(' ')[20].split('=')[1]) / 1000);
};
*/

/* ex:set ai shiftwidth=2 inputtab=spaces smarttab noautotab: */
27 changes: 18 additions & 9 deletions src/scripts/modules/gpioworker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fs from "fs";
import events from "events";
var eventEmitter = new events.EventEmitter();

import SensorDevice from "./sensor";
import ds18b20Device from "./ds18b20";
import fhemDevice from "./fhem";
import Relay from "./sainsmartrelay";
import Configuration from "./configuration";
Expand Down Expand Up @@ -56,7 +56,7 @@ function gpioWorker (input_queue, output_queue) {

// Now set sensor fudge according to updated configuration
sensorDevices.forEach( function (sensor) {
sensor.setFudge(parseFloat(config['sensorFudgeFactors'][sensor.getId()]));
sensor.fudge = parseFloat(config['sensorFudgeFactors'][sensor.id]);
});

// Populate this.jobs from saved data
Expand Down Expand Up @@ -155,9 +155,9 @@ gpioWorker.prototype.sensorDevices = function () {

// Obtain list of available sensor ids
// & keep array (sensorDevices) of sensor objects
var sensorList = SensorDevice.sensors();
var sensorList = ds18b20Device.sensors();
for (var z=0;z<sensorList.length;z++) {
deviceList.push(new SensorDevice(sensorList[z]));
deviceList.push(new ds18b20Device(sensorList[z]));
}

// Sort the device list by (slightly mangled) id's
Expand Down Expand Up @@ -185,7 +185,7 @@ gpioWorker.prototype.updateDevices = function () {

sensorDevices.forEach( function(item) {
//console.log("EEEEE " + item.id);
item.getTempAsync(function (id, result) {
item.getTempAsync(function (result, id) {
var sensor_result = {id:id,result:result};
//console.log("BBBB " + id + " " + result);
sensorResults.push(sensor_result);
Expand All @@ -196,7 +196,7 @@ gpioWorker.prototype.updateDevices = function () {
/*
for (var i=0;i<sensorDevices.length;i++) {
item = sensorDevices[i];
item.getTempAsync(function (id, result) {
item.getTempAsync(function (result, id) {
var result = {id:id,result:result};
//console.log("BBBB " + id + " " + result);
sensorResults.push(result);
Expand Down Expand Up @@ -344,16 +344,25 @@ gpioWorker.prototype.toggle_relay = function (msg) {

gpioWorker.prototype.config_change = function (msg) {
console.log("config_change() Rcvd: " + JSON.stringify(msg.data));
var i;
var keys = Object.keys(msg.data);

if (keys[0] == 'sensorFudgeFactors') {
console.log("config_change(): " + keys[0] + " = " + msg.data['sensorFudgeFactors'] + " (" + msg.data['fudge'] + ")");
sensorDevices.forEach( function(item) {
if (item.getId() == msg.data['sensorFudgeFactors']) {
item.setFudge(msg.data['fudge']);
if (item.id == msg.data['sensorFudgeFactors']) {
//item.setFudge(msg.data['fudge']);
item.fudge = msg.data['fudge'];
}
});
//this.configuration.sensorFudgeFactors[msg.data['sensorFudgeFactors']] = msg.data['fudge'];
this.configObj.setFudgeFactor(msg.data['sensorFudgeFactors'], msg.data['fudge']);

// Apply to running jobs too
for (i=0;i<this.runningJobs.length;i++ ) {
console.log("Dealing with job: " + this.runningJobs[i].name());
this.runningJobs[i].resetFudges(msg.data);
}
} else if (keys[0] == 'iSpindels') {
console.log("config_change() iSpindels: " + msg.data['iSpindels'] + " (" + msg.data['timeout'] + ")");

Expand All @@ -373,7 +382,7 @@ gpioWorker.prototype.config_change = function (msg) {
} else if (keys[0] == 'multiSensorMeanWeight') {
this.configObj.setMultiSensorMeanWeight(msg.data['multiSensorMeanWeight']);
} else if (keys[0] == 'relayDelayPostON' || keys[0] == 'relayDelayPostOFF') {
for (var i=0;i<this.relay.deviceCount();i++) {
for (i=0;i<this.relay.deviceCount();i++) {
if (keys[0] == 'relayDelayPostON') {
this.relay.setDelaySetValue(i+1, 'on_time', msg.data[keys[0]]);
} else {
Expand Down
46 changes: 30 additions & 16 deletions src/scripts/modules/jobprocessor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var os = require('os');
var fs = require('fs');
var path = require('path');
import os from 'os';
import fs from 'fs';
import path from 'path';


function JobProcessor(options) {
Expand Down Expand Up @@ -66,14 +66,14 @@ function JobProcessor(options) {
var jSensors = {};
//options.parent.sensorDevices().forEach( function (sensor, index) {
options.parent.sensorDevices().forEach( function (sensor) {
//console.log("ID = " + sensor.getId());
//console.log("ID = " + sensor.id);
//console.log("jobSensorIds (jsIds): " + JSON.stringify(jsIds));
if (jsIds.indexOf(sensor.getId()) > -1 ) {
jSensors[sensor.getId()] = sensor;
if (jsIds.indexOf(sensor.id) > -1 ) {
jSensors[sensor.id] = sensor;
}
});
this.jobSensors = jSensors;
//console.log("jobSensors: " + JSON.stringify(this.jobSensors));
console.log("jobSensors: " + JSON.stringify(this.jobSensors));

var jRelays;
if ( (isNewJob) )
Expand Down Expand Up @@ -148,10 +148,11 @@ function JobProcessor(options) {
//this.jobSensorIds.forEach( function (sensor, index) {
this.jobSensorIds.forEach( function (sensor) {
job_status['sensors'].push(sensor);
//console.log("jobStatus(): " + JSON.stringify(jSensors));
job_status[sensor] = jSensors[sensor].getTemp();
console.log("jobStatus() jSensors: " + JSON.stringify(jSensors));
//job_status[sensor] = jSensors[sensor].getTemp();
job_status[sensor] = jSensors[sensor].temp;
});
//console.log("job_status: " + JSON.stringify(job_status));
console.log("job_status: " + JSON.stringify(job_status));
//console.log("jobRelays: " + JSON.stringify(this.jobRelays));
//this.jobRelays.forEach( function (relay, index) {
this.jobRelays.forEach( function (relay) {
Expand Down Expand Up @@ -209,10 +210,10 @@ JobProcessor.prototype.jobStatus = function (nowTime, obj) {
//obj.jobSensorIds.forEach( function (sensor, index) {
obj.jobSensorIds.forEach( function (sensor) {
job_status['sensors'].push(sensor);
job_status[sensor] = obj.jobSensors[sensor].getTemp();
//job_status[sensor] = obj.jobSensors[sensor].getTemp();
job_status[sensor] = obj.jobSensors[sensor].temp;
});
//console.log("job_status: " + JSON.stringify(job_status));
//obj.jobRelays.forEach( function (relay, index) {
obj.jobRelays.forEach( function (relay) {
if (obj.relay.isOn(parseInt(relay.split(' ')[1])) ) {
job_status[relay] = 'ON';
Expand Down Expand Up @@ -260,7 +261,7 @@ JobProcessor.prototype.validateSensors = function (sensorIds) {
//console.log("sensorDevices = " + JSON.stringify(this.sensorDevices()));
//this.sensorDevices().forEach( function (item, index) {
this.sensorDevices().forEach( function (item) {
var sid = item.getId();
var sid = item.id;
valid_ids.push(sid);
//console.log("Found sensor: " + sid);
if (sensorIds.indexOf(sid) > -1) {
Expand Down Expand Up @@ -290,6 +291,16 @@ JobProcessor.prototype.report = function () {
//console.log(JSON.stringify(this.history));
};

JobProcessor.prototype.resetFudges = function (data) {
//console.log("Resetting fudges for job: " + this.name() + " with data: " + JSON.stringify(data));
for (var sensor in this.jobSensors) {
if (this.jobSensors[sensor].name == data["sensorFudgeFactors"]) {
this.jobSensors[sensor].fudge = data["fudge"];
console.log("resetFudges() reset = " + this.jobSensors[sensor].name + " with " + data["fudge"]);
}
}
};

/* Return the target temperature for a given time */
JobProcessor.prototype.target_temperature = function (current_time) {
/* First generate an array of target temps at accumulated time */
Expand Down Expand Up @@ -530,10 +541,13 @@ JobProcessor.prototype.temperatureAdjust = function (target) {
take the first sensor's reading into account at all.
*/
if (this.jobSensorIds.length == 1) {
temp = this.jobSensors[this.jobSensorIds[0]].getTemp();
//temp = this.jobSensors[this.jobSensorIds[0]].getTemp();
temp = this.jobSensors[this.jobSensorIds[0]].temp;
} else if (this.jobSensorIds.length > 1) {
var temp0 = parseFloat(this.jobSensors[this.jobSensorIds[0]].getTemp());
var temp1 = parseFloat(this.jobSensors[this.jobSensorIds[1]].getTemp());
//var temp0 = parseFloat(this.jobSensors[this.jobSensorIds[0]].getTemp());
//var temp1 = parseFloat(this.jobSensors[this.jobSensorIds[1]].getTemp());
var temp0 = parseFloat(this.jobSensors[this.jobSensorIds[0]].temp);
var temp1 = parseFloat(this.jobSensors[this.jobSensorIds[1]].temp);
var mswm = parseFloat(this.parent.configuration['multiSensorMeanWeight']);
//console.log("temperatureAdjust() mswm = " + mswm);
temp = (temp1 * mswm + temp0 * (100-mswm))/100.0;
Expand Down
69 changes: 0 additions & 69 deletions src/scripts/modules/sensor.js

This file was deleted.

4 changes: 3 additions & 1 deletion src/scripts/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ window.onload = function () {

var configItemSensorName = document.createElement('DIV');
configItemSensorName.id = 'configItemSensorName_' + isp_sensor.name;
configItemSensorName.className = 'configItemSensorName';
configItemSensorName.className = 'configItemSensorName unselectable';
configItemSensorName.textContent = isp_sensor.name;
var configItemSensorIspindel = document.createElement('INPUT');
configItemSensorIspindel.id = 'configItemSensorIspindel_' + isp_sensor.name;
Expand Down Expand Up @@ -1237,6 +1237,8 @@ window.onload = function () {
configItemDataValue.appendChild(configItemSensorName);
configItemDataValue.appendChild(configItemSensorFudge);
configItemData.appendChild(configItemDataValue);

configItemDataValue.blur();
}
} else if (key == "iSpindels") {
console.log("Configure ISpindels");
Expand Down

0 comments on commit 4a87b34

Please sign in to comment.