@@ -87,7 +87,7 @@ export class OneWireTemperature extends OneWireDeviceBase {
87
87
if ( typeof opts . calibration !== this . options . calibration ) this . options . calibration = opts . calibration ;
88
88
if ( typeof opts . readInterval === 'number' ) this . options . readInterval = opts . readInterval ;
89
89
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 ) ;
91
91
// RSG - need to figure out permissions to make this work.
92
92
try {
93
93
@@ -99,10 +99,10 @@ export class OneWireTemperature extends OneWireDeviceBase {
99
99
// RSG - need to figure out permissions to make this work.
100
100
try {
101
101
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 )
103
103
}
104
104
}
105
- catch ( err ) { logger . error ( ` ${ this . device . name } - EXPECTED - cannot set resolution. ${ err } ` ) }
105
+ catch ( err ) { throw ( err ) }
106
106
return Promise . resolve ( this ) ;
107
107
}
108
108
catch ( err ) {
@@ -141,17 +141,20 @@ export class OneWireTemperature extends OneWireDeviceBase {
141
141
public async alarmSearch ( ) {
142
142
// to implement; is an alarm triggered?
143
143
}
144
- protected async setAlarms ( val : string ) {
144
+ protected async setAlarms ( alarmLow : number , alarmHigh : number ) {
145
145
try {
146
146
let lowLimit = Math . round ( utils . convert . temperature . convertUnits ( - 100 , 'C' , this . values . units ) ) ;
147
147
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 } ` ) ;
150
152
if ( Controller . isMock ( ) ) {
151
- this . options . resolution = val ;
153
+ this . options . alarmLow = alarmLow ;
154
+ this . options . alarmHigh = alarmHigh ;
152
155
}
153
156
else {
154
- await this . writeFile ( `alarms` , val ) ;
157
+ await this . writeFile ( `alarms` , ` ${ alarmLowC } ${ alarmHighC } ` ) ;
155
158
await this . getAlarms ( ) ;
156
159
}
157
160
@@ -209,17 +212,18 @@ export class OneWireTemperature extends OneWireDeviceBase {
209
212
210
213
} catch ( err ) { return Promise . reject ( err ) ; }
211
214
}
212
- public setUnits ( value : string ) : Promise < boolean > {
215
+ public async setUnits ( value : string ) : Promise < boolean > {
213
216
try {
214
217
if ( ! [ 'C' , 'F' , 'K' ] . includes ( value . toUpperCase ( ) ) ) return Promise . reject ( new Error ( `Cannot set units to ${ value } ` ) ) ;
215
218
let prevUnits = this . values . units || 'C' ;
216
219
this . options . units = this . values . units = value . toUpperCase ( ) ;
217
220
this . values . temp = utils . convert . temperature . convertUnits ( this . values . temp , prevUnits , this . values . units ) + this . options . calibration ;
218
221
this . options . alarmLow = Math . round ( utils . convert . temperature . convertUnits ( this . options . alarmLow , 'c' , this . values . units ) ) ;
219
222
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 ) ;
220
224
webApp . emitToClients ( 'oneWireDataValues' , { id : this . device . id , typeId : this . device . typeId , values : this . values } ) ;
221
225
}
222
- catch ( err ) { this . logError ( err ) ; }
226
+ catch ( err ) { this . logError ( err ) ; return Promise . reject ( err ) ; }
223
227
}
224
228
225
229
protected pollDeviceInformation ( ) {
0 commit comments