Skip to content

Commit 736cd76

Browse files
committed
Added Shart equations #69
Allow incorrect feed values #71
1 parent 14b2b6a commit 736cd76

File tree

4 files changed

+148
-20
lines changed

4 files changed

+148
-20
lines changed

boards/Constants.ts

+5
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ export class Utils {
233233
let tK = (beta * rtemp) / (beta + (rtemp * Math.log(resistance / ref)));
234234
return this.convert.temperature.convertUnits(tK, 'k', units);
235235
},
236+
shart3: (resistance: number, sA: number, sB: number, sC: number, units:string): number => {
237+
let rlog = Math.log(resistance);
238+
let tK = 1 / (sA + sB * rlog + sC * rlog * rlog * rlog);
239+
return this.convert.temperature.convertUnits(tK, 'k', units);
240+
},
236241
convertUnits: (val: number, from: string, to: string) => {
237242
if (typeof val !== 'number') return null;
238243
let fn = this.convert.temperature[from.toLowerCase()];

devices/temperature.json

+72-2
Original file line numberDiff line numberDiff line change
@@ -842,8 +842,8 @@
842842
"name": "10k Temperature Probe",
843843
"input": "ohms",
844844
"precision": 2,
845-
"deviceClass": "GenericDeviceBase",
846-
"module": "./genericDevices",
845+
"deviceClass": "Thermistor10k",
846+
"module": "./Temperature",
847847
"convertValue": "device.values.inputUnits = device.options.inputType === 'raw' ? '' : device.options.inputType === 'volt' ? 'volts' : device.options.inputResistanceUnits === 1000 ? 'kOhms': 'ohms'; device.values.units = device.options.units; device.values.maxVal = (device.options.inputType === 'raw') ? (1 << device.options.inputBitness) : device.options.inputType === 'volt' ? device.options.vccRef : 10000; device.values.resistance = (device.options.inputType === 'ohms') ? device.values.adcValue * device.options.inputResistanceUnits : (10000 * device.values.adcValue) / (device.values.maxVal - device.values.adcValue); device.values.temperature = (Math.round(maps.thermistor10k.interpolate(device.values.resistance, device.values.units || 'F') * 100)/100) + (device.options.calibration || 0);",
848848
"inputs": [
849849
{
@@ -1331,6 +1331,76 @@
13311331
}
13321332
]
13331333
},
1334+
{
1335+
"field": {
1336+
"type": "fieldset",
1337+
"legend": "Calculation",
1338+
"cssClass": "pnl-calculation"
1339+
},
1340+
"options": [
1341+
{
1342+
"field": {
1343+
"type": "pickList",
1344+
"labelText": "",
1345+
"binding": "options.calcType",
1346+
"value": "interpolate",
1347+
"required": true,
1348+
"canEdit": false,
1349+
"inputAttrs": {
1350+
"style": {
1351+
"width": "12rem"
1352+
}
1353+
},
1354+
"labelAttrs": {
1355+
"style": {
1356+
"hidden": true,
1357+
"width": "0rem"
1358+
}
1359+
},
1360+
"style": {
1361+
"display": "block"
1362+
},
1363+
"columns": [
1364+
{
1365+
"binding": "val",
1366+
"hidden": true,
1367+
"text": "Val",
1368+
"style": {
1369+
"whiteSpace": "nowrap"
1370+
}
1371+
},
1372+
{
1373+
"binding": "name",
1374+
"text": "Type",
1375+
"hidden": true,
1376+
"style": {
1377+
"whiteSpace": "nowrap"
1378+
}
1379+
},
1380+
{
1381+
"binding": "desc",
1382+
"text": "Method",
1383+
"style": {
1384+
"whiteSpace": "nowrap"
1385+
}
1386+
}
1387+
],
1388+
"items": [
1389+
{
1390+
"val": "interpolate",
1391+
"name": "10k thermistor table",
1392+
"desc": "10k thermistor table"
1393+
},
1394+
{
1395+
"val": "shart",
1396+
"name": "Steinhart-Hart Equation",
1397+
"desc": "Steinhart-Hart Equation"
1398+
}
1399+
]
1400+
}
1401+
}
1402+
]
1403+
},
13341404
{
13351405
"field": {
13361406
"type": "fieldset",

generic/Temperature.ts

+59-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,68 @@
11
import { logger } from "../logger/Logger";
2-
import { DeviceBinding } from "../boards/Controller";
3-
import { setTimeout, clearTimeout } from "timers";
2+
import { GenericDeviceBase } from "./genericDevices";
3+
import { AnalogDevices } from "../devices/AnalogDevices";
44
import { utils } from "../boards/Constants";
55

6-
import * as fs from 'fs';
7-
import { GenericDeviceBase } from "./genericDevices";
86
import { webApp } from "../web/Server";
97

10-
export class Themistor10k extends GenericDeviceBase {
11-
protected logError(err, msg?: string) { logger.error(`${this.device.name} ${typeof msg !== 'undefined' ? msg + ' ' : ''}${typeof err !== 'undefined' ? err.message : ''}`); }
12-
public async setValues(vals): Promise<any> {
13-
try {
14-
return Promise.resolve(this.values);
8+
export class Thermistor10k extends GenericDeviceBase {
9+
public setValue(prop, value) {
10+
let replaceSymbols = /(?:\]\.|\[|\.)/g
11+
let _prop = prop.indexOf(',') > -1 ? prop.replace(replaceSymbols, ',').split(',') : prop;
12+
// Execute a function, load a module, or ...
13+
let dt = this.device.getDeviceType();
14+
let val = value;
15+
if (typeof dt.inputs !== 'undefined') {
16+
let inp = dt.inputs.find(x => x.name === prop);
17+
if (typeof inp !== 'undefined') {
18+
switch (inp.dataType) {
19+
case 'number':
20+
if (typeof value.value !== 'undefined') val = value.value;
21+
else if (typeof value.adcValue !== 'undefined') val = value.adcValue;
22+
23+
}
24+
}
25+
}
26+
27+
//let obj = this.device.values;
28+
// for (let i = 0; i < _prop.length; i++) {
29+
// obj = obj[_prop[i]];
30+
// }
31+
// obj = value;
32+
this.device.values[_prop] = val;
33+
this.convertValue(val);
34+
webApp.emitToClients('genericDataValues', { id: this.device.id, typeId: this.device.typeId, values: this.values });
35+
this.emitFeeds();
36+
}
37+
public convertValue(value: number) {
38+
let device = this.device;
39+
let maps = AnalogDevices.maps;
40+
device.values.inputUnits = device.options.inputType === 'raw' ? '' : device.options.inputType === 'volt' ? 'volts' : device.options.inputResistanceUnits === 1000 ? 'kOhms' : 'ohms';
41+
device.values.units = device.options.units;
42+
device.values.maxVal = (device.options.inputType === 'raw') ? (1 << device.options.inputBitness) : device.options.inputType === 'volt' ? device.options.vccRef : 10000;
43+
switch (device.options.inputType) {
44+
case 'ohms':
45+
device.values.resistance = device.values.adcValue * device.options.inputResistanceUnits;
46+
break;
47+
case 'kohms':
48+
device.values.resistance = (10000 * device.values.adcValue) / (device.values.maxVal - device.values.adcValue);
49+
break;
50+
}
51+
switch (device.options.calcType) {
52+
case 'shart':
53+
device.values.tempK = utils.convert.temperature.shart3(device.values.resistance, 0.001125308852122, 0.000234711863267, 0.000000085663516, 'K');
54+
device.values.tempC = utils.convert.temperature.convertUnits(device.values.tempK, 'K', 'C');
55+
device.values.tempF = utils.convert.temperature.convertUnits(device.values.tempK, 'K', 'F');
56+
device.values.temperature = utils.convert.temperature.convertUnits(device.values.tempK, 'K', device.values.units || 'F') + (device.options.calibration || 0);
57+
break;
58+
default:
59+
device.values.tempK = (Math.round(maps['thermistor10k'].interpolate(device.values.resistance, 'K') * 100) / 100);
60+
device.values.tempC = utils.convert.temperature.convertUnits(device.values.tempK, 'K', 'C');
61+
device.values.tempF = utils.convert.temperature.convertUnits(device.values.tempK, 'K', 'F');
62+
device.values.temperature = utils.convert.temperature.convertUnits(device.values.tempK, 'K', device.values.units || 'F') + (device.options.calibration || 0);
63+
break;
1564
}
16-
catch (err) { this.logError(err); Promise.reject(err); }
17-
finally { }
65+
return value;
1866
}
1967
}
2068

pages/widgets.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -601,14 +601,19 @@ var dataBinder = {
601601
if (typeof val === 'number') return val;
602602
else if (typeof val === 'undefined' || val === null) return;
603603
else if (typeof val.getTime === 'function') return val.getTime();
604-
var tval = val.replace(/[^0-9\.\-]+/g, '');
605-
var v;
606-
if (tval.indexOf('.') !== -1) {
607-
v = parseFloat(tval);
608-
v = v.round(tval.length - tval.indexOf('.'));
604+
try {
605+
var tval = val.replace(/[^0-9\.\-]+/g, '');
606+
var v;
607+
if (tval.indexOf('.') !== -1) {
608+
v = parseFloat(tval);
609+
v = v.round(tval.length - tval.indexOf('.'));
610+
}
611+
else v = parseInt(tval, 10);
612+
return v;
613+
} catch (err) {
614+
console.log({ msg: 'Error parsing number', val: val, err: err });
615+
return NaN;
609616
}
610-
else v = parseInt(tval, 10);
611-
return v;
612617
},
613618
formatDuration: function (dur) {
614619
var fmt = '';

0 commit comments

Comments
 (0)