Skip to content

Commit a81e8a4

Browse files
committed
Fixed 1-wire temp conversion and alarm setting. #82
1 parent e8bc5e3 commit a81e8a4

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

one-wire/OneWireTemperature.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export class OneWireTemperature extends OneWireDeviceBase {
8787
if (typeof opts.calibration !== this.options.calibration) this.options.calibration = opts.calibration;
8888
if (typeof opts.readInterval === 'number') this.options.readInterval = opts.readInterval;
8989
this.options.readInterval = Math.max(1000, this.options.readInterval);
90-
if (typeof opts.units !== 'undefined' && this.options.units !== opts.units) this.setUnits(opts.units);
90+
if (typeof opts.units !== 'undefined' && this.options.units !== opts.units) await this.setUnits(opts.units);
9191
// RSG - need to figure out permissions to make this work.
9292
try {
9393

@@ -99,10 +99,10 @@ export class OneWireTemperature extends OneWireDeviceBase {
9999
// RSG - need to figure out permissions to make this work.
100100
try {
101101
if (typeof opts.alarmLow !== 'undefined' && typeof opts.alarmHigh !== 'undefined' && opts.alarmLow !== this.options.alarmLow || opts.alarmHigh !== this.options.alarmHigh) {
102-
await this.setAlarms(`${opts.alarmLow} ${opts.alarmHigh}`)
102+
await this.setAlarms(opts.alarmLow, opts.alarmHigh)
103103
}
104104
}
105-
catch (err) { logger.error(`${this.device.name} - EXPECTED - cannot set resolution. ${err}`) }
105+
catch (err) { throw(err)}
106106
return Promise.resolve(this);
107107
}
108108
catch (err) {
@@ -141,17 +141,20 @@ export class OneWireTemperature extends OneWireDeviceBase {
141141
public async alarmSearch() {
142142
// to implement; is an alarm triggered?
143143
}
144-
protected async setAlarms(val: string) {
144+
protected async setAlarms(alarmLow: number, alarmHigh: number) {
145145
try {
146146
let lowLimit = Math.round(utils.convert.temperature.convertUnits(-100, 'C', this.values.units));
147147
let highLimit = Math.round(utils.convert.temperature.convertUnits(100, 'C', this.values.units));
148-
let alarms = val.split(' ').map(el => utils.convert.temperature.convertUnits(parseInt(el, 10), this.values.units, 'C'));
149-
if (isNaN(alarms[0]) || isNaN(alarms[1]) || alarms[0] < lowLimit || alarms[0] > highLimit || alarms[1] < lowLimit || alarms[1] > highLimit && alarms[0] < alarms[1]) return Promise.reject(`${this.device.name} alarms must be between ${lowLimit} and ${highLimit}. Value provided: ${val}`);
148+
// let alarms = alarmLow.split(' ').map(el => utils.convert.temperature.convertUnits(parseInt(el, 10), this.values.units, 'C'));
149+
let alarmLowC = utils.convert.temperature.convertUnits(alarmLow, this.values.units, 'C');
150+
let alarmHighC = utils.convert.temperature.convertUnits(alarmHigh, this.values.units, 'C');
151+
if (isNaN(alarmLowC) || isNaN(alarmHighC) || alarmLowC < lowLimit || alarmLowC > highLimit || alarmHighC < lowLimit || alarmHighC > highLimit && alarmLowC < alarmHighC) return Promise.reject(`${this.device.name} alarms must be between ${lowLimit} and ${highLimit} celsius. Value provided: ${alarmLow} and ${alarmHigh}`);
150152
if (Controller.isMock()) {
151-
this.options.resolution = val;
153+
this.options.alarmLow = alarmLow;
154+
this.options.alarmHigh = alarmHigh;
152155
}
153156
else {
154-
await this.writeFile(`alarms`, val);
157+
await this.writeFile(`alarms`, `${alarmLowC} ${alarmHighC}`);
155158
await this.getAlarms();
156159
}
157160

@@ -209,17 +212,18 @@ export class OneWireTemperature extends OneWireDeviceBase {
209212

210213
} catch (err) { return Promise.reject(err); }
211214
}
212-
public setUnits(value: string): Promise<boolean> {
215+
public async setUnits(value: string): Promise<boolean> {
213216
try {
214217
if (!['C', 'F', 'K'].includes(value.toUpperCase())) return Promise.reject(new Error(`Cannot set units to ${value}`));
215218
let prevUnits = this.values.units || 'C';
216219
this.options.units = this.values.units = value.toUpperCase();
217220
this.values.temp = utils.convert.temperature.convertUnits(this.values.temp, prevUnits, this.values.units) + this.options.calibration;
218221
this.options.alarmLow = Math.round(utils.convert.temperature.convertUnits(this.options.alarmLow, 'c', this.values.units));
219222
this.options.alarmHigh = Math.round(utils.convert.temperature.convertUnits(this.options.alarmHigh, 'c', this.values.units));
223+
await this.setAlarms(this.options.alarmLow, this.options.alarmHigh);
220224
webApp.emitToClients('oneWireDataValues', { id: this.device.id, typeId: this.device.typeId, values: this.values });
221225
}
222-
catch (err) { this.logError(err); }
226+
catch (err) { this.logError(err); return Promise.reject(err); }
223227
}
224228

225229
protected pollDeviceInformation() {

0 commit comments

Comments
 (0)