From e27add8da718602e19d1d38423e37efa69f0568e Mon Sep 17 00:00:00 2001 From: mmcw-dev Date: Thu, 19 Oct 2017 21:30:07 -0400 Subject: [PATCH] Refactor releaseWatch to use a seperate function to determine close state for the release switch --- lib/intval/index.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/intval/index.js b/lib/intval/index.js index 16e3d4c..c9b309c 100644 --- a/lib/intval/index.js +++ b/lib/intval/index.js @@ -172,7 +172,7 @@ class Intval { * * 1) If closed, start timer. * 2) If opened, check timer AND - * 3) If `press` (`NOW - this._state.release.time`) greater than minimum and less than `this._releaseSequence`, start frame + * 3) If `press` (`now - this._state.release.time`) greater than minimum and less than `this._releaseSequence`, start frame * 4) If `press` greater than `this._releaseSequence`, start sequence * * Button + 10K ohm resistor @@ -184,7 +184,7 @@ class Intval { * */ _watchRelease (err, val) { - const NOW = +new Date() + const now = +new Date() let press = 0 if (err) { return log.error(err) @@ -192,15 +192,14 @@ class Intval { log.info(`Release switch val: ${val}`) if (val === 0) { //closed - if ((!this._state.release.active && this._state.release.time === 0) || (this._state.release.active && (NOW - this._state.release.time) > (this._releaseSequence * 10)) - ) { - this._state.release.time = NOW + if (this._releaseClosedState(now)) { + this._state.release.time = now this._state.release.active = true //maybe unncecessary } } else if (val === 1) { //opened if (this._state.release.active) { - press = NOW - this._state.release.time + press = now - this._state.release.time if (press > this._releaseMin && press < this._releaseSequence) { this.frame() } else if (press >= this._releaseSequence) { @@ -213,6 +212,16 @@ class Intval { } log.info('completed if statement') } + + _releaseClosedState (now) { + if (!this._state.release.active && this._state.release.time === 0) { + return true + } + if (this._state.release.active && (now - this._state.release.time) > (this._releaseSequence * 10)) { + return true + } + return false + } /** * Set the default direction of the camera. * * forward = true