Created
January 22, 2020 15:59
-
-
Save WesleyE/887f33cacec31bb321c5deafa13e5c63 to your computer and use it in GitHub Desktop.
HysteresisController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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