Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/v0.5.0 #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v0.5.0 (28.05.2023)
* Implement new CONFIG action to allow overriding configuration without starting the timer.

## v0.4.1 (27.05.2023)
* Bugfix: When the timer was in a paused state and received an override configuration, it failed to update the visible timer progress.

Expand Down
22 changes: 11 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-controltimer",
"version": "0.4.1",
"version": "0.5.0",
"description": "A controllable Node-RED timer node. Start, Stop, Reset, Pause and Continue the timer. Use the timer as a loop or a delay.",
"scripts": {
"build": "scripty",
Expand Down
34 changes: 29 additions & 5 deletions src/controltimer.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
isResetActionEnabled: { value: <%= defaults.isResetActionEnabled %> },
isPauseActionEnabled: { value: <%= defaults.isPauseActionEnabled %> },
isContinueActionEnabled: { value: <%= defaults.isContinueActionEnabled %> },
isConfigActionEnabled: { value: <%= defaults.isConfigActionEnabled %> },
actionPropertyNameType: { value: '<%= defaults.actionPropertyNameType %>' },
actionPropertyName: { value: '<%= defaults.actionPropertyName %>', required: true, validate: RED.validators.typedInput('actionPropertyNameType') },
startActionNameType: { value: '<%= defaults.startActionNameType %>' },
Expand All @@ -51,12 +52,14 @@
pauseActionName: { value: '<%= defaults.pauseActionName %>', required: true, validate: RED.validators.typedInput('pauseActionNameType') },
continueActionNameType: { value: '<%= defaults.continueActionNameType %>' },
continueActionName: { value: '<%= defaults.continueActionName %>', required: true, validate: RED.validators.typedInput('continueActionNameType') },
configActionNameType: { value: '<%= defaults.configActionNameType %>' },
configActionName: { value: '<%= defaults.configActionName %>', required: true, validate: RED.validators.typedInput('configActionNameType') },
},
inputs: 1,
outputs: 2,
icon: "controltimer.png",
label: function () {
return this.name || `${this.timerDuration} ${this.timerDurationUnit} ${this.timerType === '<%= TIMER_TYPE.DELAY %>' ? '<%= TIMER_TYPE.DELAY %>' : '<%= TIMER_TYPE.LOOP %>'}`;
return this.name || `${this.timerDuration} ${this.timerDurationUnit} ${this.timerType === '<%= TimerType.DELAY %>' ? '<%= TimerType.DELAY %>' : '<%= TimerType.LOOP %>'}`;
},
labelStyle: function () {
return this.name ? 'node_label_italic' : '';
Expand Down Expand Up @@ -166,12 +169,14 @@
initializeNodeRedCheckbox('isResetActionEnabled');
initializeNodeRedCheckbox('isPauseActionEnabled');
initializeNodeRedCheckbox('isContinueActionEnabled');
initializeNodeRedCheckbox('isConfigActionEnabled');
initializeNodeRedTypedTextInput('actionPropertyName', ['str']);
initializeNodeRedTypedTextInput('startActionName', ['str']);
initializeNodeRedTypedTextInput('stopActionName', ['str']);
initializeNodeRedTypedTextInput('resetActionName', ['str']);
initializeNodeRedTypedTextInput('pauseActionName', ['str']);
initializeNodeRedTypedTextInput('continueActionName', ['str']);
initializeNodeRedTypedTextInput('configActionName', ['str']);
}

initializeInputs();
Expand All @@ -180,11 +185,11 @@
$('#node-input-timerType').change(() => {
const selectedTimerType = $('#node-input-timerType').val();

if (selectedTimerType === '<%= TIMER_TYPE.LOOP %>') {
if (selectedTimerType === '<%= TimerType.LOOP %>') {
$('.loop-options').show();
}

if (selectedTimerType === '<%= TIMER_TYPE.DELAY %>') {
if (selectedTimerType === '<%= TimerType.DELAY %>') {
$('.loop-options').hide();
}
});
Expand Down Expand Up @@ -225,6 +230,7 @@
this.isStopActionEnabled = elementDefaults.isStopActionEnabled;
this.isPauseActionEnabled = elementDefaults.isPauseActionEnabled;
this.isContinueActionEnabled = elementDefaults.isContinueActionEnabled;
this.isConfigActionEnabled = elementDefaults.isConfigActionEnabled;
this.actionPropertyNameType = elementDefaults.actionPropertyNameType;
this.actionPropertyName = elementDefaults.actionPropertyName;
this.startActionNameType = elementDefaults.startActionNameType;
Expand All @@ -237,6 +243,8 @@
this.pauseActionName = elementDefaults.pauseActionName;
this.continueActionNameType = elementDefaults.continueActionNameType;
this.continueActionName = elementDefaults.continueActionName;
this.configActionNameType = elementDefaults.configActionNameType;
this.configActionName = elementDefaults.configActionName;

initializeInputs();
});
Expand Down Expand Up @@ -289,6 +297,7 @@
this.isStopActionEnabled = $('#node-input-isStopActionEnabled').is(":checked");
this.isPauseActionEnabled = $('#node-input-isPauseActionEnabled').is(":checked");
this.isContinueActionEnabled = $('#node-input-isContinueActionEnabled').is(":checked");
this.isConfigActionEnabled = $('#node-input-isConfigActionEnabled').is(":checked");
this.actionPropertyNameType = $('#node-input-actionPropertyNameType').val();
this.actionPropertyName = $('#node-input-actionPropertyName').val();
this.startActionNameType = $('#node-input-startActionNameType').val();
Expand All @@ -301,6 +310,8 @@
this.pauseActionName = $('#node-input-pauseActionName').val();
this.continueActionNameType = $('#node-input-continueActionNameType').val();
this.continueActionName = $('#node-input-continueActionName').val();
this.configActionNameType = $('#node-input-configActionNameType').val();
this.configActionName = $('#node-input-configActionName').val();
},
});
</script>
Expand Down Expand Up @@ -386,8 +397,8 @@
<div class="form-row controltimer-input-row">
<label for="node-input-timerType"><i class="fa fa-tag"></i> Timer type</label>
<select id="node-input-timerType">
<option value="<%= TIMER_TYPE.DELAY %>">Delay</option>
<option value="<%= TIMER_TYPE.LOOP %>">Loop</option>
<option value="<%= TimerType.DELAY %>">Delay</option>
<option value="<%= TimerType.LOOP %>">Loop</option>
</select>
</div>

Expand Down Expand Up @@ -573,6 +584,13 @@
</div>
</div>

<div class="form-row controltimer-input-row">
<label for="node-input-isConfigActionEnabled"><i class="fa fa-tag"></i> Is config action enabled</label>
<div class="controltimer-input-row-checkbox-wrapper">
<input type="checkbox" id="node-input-isConfigActionEnabled">
</div>
</div>

<div class="form-row controltimer-input-row">
<label for="node-input-actionPropertyName"><i class="fa fa-tag"></i> Action property name</label>
<input type="hidden" id="node-input-actionPropertyNameType">
Expand Down Expand Up @@ -608,6 +626,12 @@
<input type="hidden" id="node-input-continueActionNameType">
<input type="text" id="node-input-continueActionName" placeholder="Continue action name">
</div>

<div class="form-row controltimer-input-row">
<label for="node-input-configActionName"><i class="fa fa-tag"></i> Config action name</label>
<input type="hidden" id="node-input-configActionNameType">
<input type="text" id="node-input-configActionName" placeholder="Config action name">
</div>
</div>

<div class="form-row controltimer-input-row">
Expand Down
70 changes: 38 additions & 32 deletions src/controltimer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Node, NodeAPI, NodeMessageInFlow } from 'node-red';

import { constants, ControlTimerNodeDef, nodeName } from './node-config';
import { STATE, Timer } from './timer';
import { State, Timer } from './timer';

type NodeMessage = NodeMessageInFlow;

Expand All @@ -27,25 +27,25 @@ module.exports = function (RED: NodeAPI): void {
});

timer.on('state', ({ state, progress }) => {
if (state === STATE.IDLE) {
if (state === State.IDLE) {
node.status({ fill: 'grey', shape: 'ring', text: 'Idle' });
}

if (state === STATE.RUNNING) {
if (state === State.RUNNING) {
node.status({ fill: 'green', shape: 'dot', text: `Running${progress}` });
}

if (state === STATE.STOPPED) {
if (state === State.STOPPED) {
node.status({ fill: 'red', shape: 'dot', text: 'Stopped' });
}

if (state === STATE.PAUSED) {
if (state === State.PAUSED) {
node.status({ fill: 'yellow', shape: 'dot', text: `Paused${progress}` });
}
});

timer.on(STATE.STOPPED, () => node.send([null, getHaltedMessage()]));
timer.on(STATE.PAUSED, () => node.send([null, getHaltedMessage()]));
timer.on(State.STOPPED, () => node.send([null, getHaltedMessage()]));
timer.on(State.PAUSED, () => node.send([null, getHaltedMessage()]));
timer.on('timer', () => node.send([getTriggerMessage(), null]));
timer.on('loop-timeout', () => node.send([null, getMessage(config.loopTimeoutMessage)]));
timer.on('loop-max-iterations', () => node.send([null, getMessage(config.loopMaxIterationsMessage)]));
Expand All @@ -58,44 +58,39 @@ module.exports = function (RED: NodeAPI): void {
const isPauseActionMessage = message[config.actionPropertyName] === config.pauseActionName && config.isPauseActionEnabled;
const isContinueActionMessage = message[config.actionPropertyName] === config.continueActionName && config.isContinueActionEnabled;
const isStopActionMessage = message[config.actionPropertyName] === config.stopActionName && config.isStopActionEnabled;
const isUnknownMessage = !(isStartActionMessage || isResetActionMessage || isPauseActionMessage || isContinueActionMessage || isStopActionMessage);
const isConfigActionMessage = message[config.actionPropertyName] === config.configActionName && config.isConfigActionEnabled;
const isUnknownMessage = !(
isStartActionMessage ||
isResetActionMessage ||
isPauseActionMessage ||
isContinueActionMessage ||
isStopActionMessage ||
isConfigActionMessage
);

const timerTypeOverride = message[constants.timerTypeOverridePropertyName] ?? null;
const timerDurationOverride = message[constants.timerDurationOverridePropertyName] ?? null;
const timerDurationUnitOverride = message[constants.timerDurationUnitOverridePropertyName] ?? null;
const isOverrideMessage = timerTypeOverride !== null && timerDurationOverride !== null && timerDurationUnitOverride !== null;
const hasConfigOverride = timerTypeOverride !== null && timerDurationOverride !== null && timerDurationUnitOverride !== null;

if (isConfigActionMessage && !hasConfigOverride) {
done(new Error('Config override not found or is invalid'));
return;
}

if (isStartActionMessage && isOverrideMessage) {
if (!isUnknownMessage && hasConfigOverride) {
timer.setConfigOverride({
timerType: timerTypeOverride,
duration: timerDurationOverride,
durationUnit: timerDurationUnitOverride,
});
}

if (timer.getState() === STATE.PAUSED) {
if (isStartActionMessage && config.continueTimerOnReceivalOfStartAction) {
timer.continue();
done();
return;
}
}

if (timer.getState() !== STATE.RUNNING) {
if (isStartActionMessage || (isUnknownMessage && config.startTimerOnReceivalOfUnknownMessage)) {
timer.start();
done();
return;
}
}

if (
isResetActionMessage ||
(isUnknownMessage && config.resetTimerOnReceivalOfUnknownMessage) ||
(isStartActionMessage && config.resetTimerOnReceivalOfStartAction) ||
(isStartActionMessage && isOverrideMessage)
(isStartActionMessage || (isUnknownMessage && config.startTimerOnReceivalOfUnknownMessage)) &&
(timer.state === State.IDLE || timer.state === State.STOPPED)
) {
timer.reset();
timer.start();
done();
return;
}
Expand All @@ -112,11 +107,22 @@ module.exports = function (RED: NodeAPI): void {
return;
}

if (isContinueActionMessage) {
if (isContinueActionMessage || (timer.state === State.PAUSED && isStartActionMessage && config.continueTimerOnReceivalOfStartAction)) {
timer.continue();
done();
return;
}

if (
isResetActionMessage ||
(isUnknownMessage && config.resetTimerOnReceivalOfUnknownMessage) ||
(isStartActionMessage && config.resetTimerOnReceivalOfStartAction) ||
(isStartActionMessage && hasConfigOverride)
) {
timer.reset();
done();
return;
}
});

node.on('close', (done) => {
Expand Down
13 changes: 10 additions & 3 deletions src/node-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NodeDef } from 'node-red';

import { DurationUnit, TIMER_TYPE } from './timer';
import { DurationUnit, TimerType } from './timer';

export const nodeName = 'controltimer';
export type ControlTimerNodeDef = NodeDef & Props;
Expand All @@ -18,10 +18,11 @@ export type StopActionNameType = 'str';
export type ResetActionNameType = 'str';
export type PauseActionNameType = 'str';
export type ContinueActionNameType = 'str';
export type ConfigActionNameType = 'str';

export interface Props {
name: string;
timerType: TIMER_TYPE;
timerType: TimerType;

timerDurationUnit: DurationUnit;
timerDurationType: TimerDurationType;
Expand Down Expand Up @@ -56,6 +57,7 @@ export interface Props {
isResetActionEnabled: boolean;
isPauseActionEnabled: boolean;
isContinueActionEnabled: boolean;
isConfigActionEnabled: boolean;
actionPropertyNameType: ActionPropertyNameType;
actionPropertyName: string;
startActionNameType: StartActionNameType;
Expand All @@ -68,11 +70,13 @@ export interface Props {
pauseActionName: string;
continueActionNameType: ContinueActionNameType;
continueActionName: string;
configActionNameType: ConfigActionNameType;
configActionName: string;
}

export const defaults: Props = {
name: '',
timerType: TIMER_TYPE.DELAY,
timerType: TimerType.DELAY,

timerDurationUnit: DurationUnit.SECOND,
timerDurationType: 'num',
Expand Down Expand Up @@ -107,6 +111,7 @@ export const defaults: Props = {
isResetActionEnabled: true,
isPauseActionEnabled: true,
isContinueActionEnabled: true,
isConfigActionEnabled: true,
actionPropertyNameType: 'str',
actionPropertyName: 'payload',
startActionNameType: 'str',
Expand All @@ -119,6 +124,8 @@ export const defaults: Props = {
pauseActionName: 'PAUSE',
continueActionNameType: 'str',
continueActionName: 'CONTINUE',
configActionNameType: 'str',
configActionName: 'CONFIG',
};

export const constants = {
Expand Down
4 changes: 2 additions & 2 deletions src/render-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import * as fs from 'fs';
import * as path from 'path';

import { defaults, nodeName } from './node-config';
import { DurationUnit, TIMER_TYPE } from './timer';
import { DurationUnit, TimerType } from './timer';

(() => {
const srcEjsPath = path.resolve('src/controltimer.ejs');
const ejsTemplate = fs.readFileSync(srcEjsPath, 'utf8');
const nodeHtml = ejs.render(ejsTemplate, { defaults, nodeName, TIMER_TYPE, DurationUnit });
const nodeHtml = ejs.render(ejsTemplate, { defaults, nodeName, TimerType, DurationUnit });
const distHtmlPath = path.resolve('dist/controltimer.html');
fs.writeFileSync(distHtmlPath, nodeHtml);
})();
Loading