Skip to content

Commit

Permalink
clean up bracket change stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
alenia committed Oct 25, 2020
1 parent 8990f36 commit f5d9bb0
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/decoratedSensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,24 @@ export default class DecoratedSensor {
return this.formatReport(data);
}

//TODO: figure out how to get rid of the bracketChange flag? it has to do with threshold crossing direction
private updateAQIMonitoringState(AQI: number, forceReset = false) : { old: monitoringState; new: monitoringState, bracketChange: notifyBracket, changed: boolean } {
const oldThresholds : [number,number] | undefined = this.AQIThresholds ? [...this.AQIThresholds] : undefined;
const old : monitoringState = { thresholds: oldThresholds, notifyBracket: this.currentAQINotifyBracket }

// Need to calculate and expose whether above or below the PREVIOUS bracket
// Figure out how to refactor this later so it's clearer
const oldThresholdNotifyBracket = this.calculateAQINotifyBracket(AQI) || this.currentAQINotifyBracket;
const newBracketFromOldThresholds = this.calculateAQINotifyBracket(AQI) || old.notifyBracket;

const thresholds = this.calculateAQIThresholds(AQI, forceReset) || oldThresholds;
const thresholds = this.calculateAQIThresholds(AQI, forceReset) || old.thresholds;

const shouldChange = oldThresholdNotifyBracket !== this.currentAQINotifyBracket || thresholds !== oldThresholds;
const shouldChange = newBracketFromOldThresholds !== this.currentAQINotifyBracket || thresholds !== old.thresholds;

this.AQIThresholds = thresholds;

// only change the bracket if other things changed
const notifyBracket = shouldChange || forceReset ? this.calculateAQINotifyBracket(AQI) : this.currentAQINotifyBracket;
const notifyBracket = shouldChange || forceReset ? this.calculateAQINotifyBracket(AQI) : old.notifyBracket;
this.currentAQINotifyBracket = notifyBracket;

const stateChanges = { old, new: { thresholds, notifyBracket }, bracketChange: oldThresholdNotifyBracket, changed: shouldChange}
const stateChanges = { old, new: { thresholds, notifyBracket }, bracketChange: newBracketFromOldThresholds, changed: shouldChange}

if(stateChanges.changed) { console.log(this.name, 'monitoring state changing', AQI, stateChanges) }

Expand Down

0 comments on commit f5d9bb0

Please # to comment.