|
1 | 1 | 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"; |
4 | 4 | import { utils } from "../boards/Constants";
|
5 | 5 |
|
6 |
| -import * as fs from 'fs'; |
7 |
| -import { GenericDeviceBase } from "./genericDevices"; |
8 | 6 | import { webApp } from "../web/Server";
|
9 | 7 |
|
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; |
15 | 64 | }
|
16 |
| - catch (err) { this.logError(err); Promise.reject(err); } |
17 |
| - finally { } |
| 65 | + return value; |
18 | 66 | }
|
19 | 67 | }
|
20 | 68 |
|
|
0 commit comments