Skip to content

Instantly share code, notes, and snippets.

@WesleyE
Created January 22, 2020 15:59
Show Gist options
  • Save WesleyE/887f33cacec31bb321c5deafa13e5c63 to your computer and use it in GitHub Desktop.
Save WesleyE/887f33cacec31bb321c5deafa13e5c63 to your computer and use it in GitHub Desktop.
HysteresisController
class HysteresisController {
/**
*
* @param {Object} options Options array
* @param {Function} onChange Function to call when the state should change
*/
constructor(options, onChange) {
this.targetTemperature = options.targetTemperature;
this.min = options.min;
this.max = options.max;
this.minBand = options.minBand;
this.maxBand = options.maxBand;
this.onChange = onChange;
this.previousState = 'off';
}
/**
* Calculates a new state and calls the callback on change. Call this
* as often as possible.
* @param {Number} currentTemperature The current measured temperature
*/
calculateState(currentTemperature) {
if(currentTemperature < this.min - this.minBand) {
if(this.previousState != 'off') {
this.previousState = 'off';
this.onChange('off');
}
} else if(currentTemperature > this.max - this.maxBand) {
if(this.previousState != 'on') {
this.previousState = 'on';
this.onChange('on');
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment