Skip to content

Commit

Permalink
Behaviour of status indicator changed and option for init messages added
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Knabe committed Sep 24, 2020
1 parent cc9627e commit 01c1bcb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

## [Unreleased]

### Added
- Option for sending init-messages after deploy

### Changed
- Node icon is now SVG
- Status indicator below node now flashes in cycles

## [v1.1.0] - 2020-09-22

Expand Down
39 changes: 26 additions & 13 deletions clock-generator.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
name: { value: '' },
topic: { value: '' },
period: { value: '1', validate: RED.validators.regex( /^[0-9]+$/ ) },
output: { value: '1' }
output: { value: '1' },
init: { value: false }
},
inputs: 1,
inputLabels: 'input',
Expand Down Expand Up @@ -39,35 +40,47 @@
</div>
<div class="form-row">
<label for="node-input-output"><i class="fa fa-fw fa-envelope"></i> Output</label>
<select id="node-input-output">
<select id="node-input-output" style="width: 35%;">
<option value="1">true / false</option>
<option value="2">1 / 0</option>
</select>
</div>
<br />
<div class="form-row">
<label for="node-input-init"><i class="fa fa-fw fa-rocket"></i> Initialization</label>
<input type="checkbox" id="node-input-init" style="width: auto;" />
</div>
</script>

<script type="text/html" data-help-name="clock-generator">
<p>
A controllable clock generator that runs through the individual clock cycles with the set period duration.
A controllable clock generator that runs through the individual clock cycles with the set
period duration.
</p>
<p>
The clock generator starts when receiving <code>1</code> or <code>true</code> as value of
<code>msg.payload</code>. <code>0</code> or <code>false</code> as value of <code>msg.payload</code>
stops the cyclic clock output again.
</p>
<p>
The clock generator starts when receiving <code>1</code> or <code>true</code> as value of <code>msg.payload</code>.
<code>0</code> or <code>false</code> as value of <code>msg.payload</code> stops the cyclic clock output again.
The values for <b>Name</b> and <b>Topic</b> can be set in the properties. These are also part
of an outgoing message (<code>msg.name</code> and <code>msg.topic</code>).
</p>
<p>
The values for <b>Name</b> and <b>Topic</b> can be set in the properties. These are also part of an outgoing message
(<code>msg.name</code> and <code>msg.topic</code>).
Setting the <b>Period</b> value defines the length of a clock cycle. Whole seconds can be
specified here.
</p>
<p>
Setting the <b>Period</b> value defines the length of a clock cycle. Whole seconds can be specified here.
The <b>Output</b> parameter specifies the data type of <code>msg.payload</code> in an outgoing
message. You can choose between <code>0</code> / <code>false</code> and <code>1</code> / <code>true</code>.
A second output delivers an inverted signal.
</p>
<p>
The <b>Output</b> parameter specifies the data type of <code>msg.payload</code> in an outgoing message.
You can choose between <code>0</code> / <code>false</code> and <code>1</code> / <code>true</code>. A second output
delivers an inverted signal.
If <b>Initialization</b> is set, a message with the initial payload is sent by each output
after the deploy.
</p>
<p>
The outgoing message also contains <code>msg.timestamp</code>, which specifies the time of the status change in
milliseconds since 1.1.1970.
The outgoing messages also contain <code>msg.timestamp</code>, which specifies the time of
the status change in milliseconds since 1.1.1970.
</p>
</script>
36 changes: 23 additions & 13 deletions clock-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@ module.exports = RED => {
RED.nodes.createNode( this, config );

let timeout;
let timestamp = 0;
let timestamp;

const getPayload = status => config.output === '1' ? !status ? false : true : !status ? 0 : 1;
const getPayload = value => config.output == 1 ? !value ? false : true : !value ? 0 : 1;

const setStatus = ( value, flash ) => {
if( timestamp === 0 ) {
this.status( { fill: 'grey', shape: 'dot', text: 'inactive' } );
} else if( flash ) {
this.status( { fill: 'green', shape: 'dot', text: `active: ${ getPayload( value ) } / ${ getPayload( !value ) }` } );
setTimeout( setStatus, 250, value, false );
} else {
this.status( { fill: 'grey', shape: 'dot', text: `active: ${ getPayload( value ) } / ${ getPayload( !value ) }` } );
}
};

const start = () => {
if( timestamp === 0 ) {
Expand All @@ -21,28 +32,27 @@ module.exports = RED => {
}
};

const update = status => {
const update = value => {
msg = {
name: config.name,
topic: config.topic,
timestamp: new Date().getTime(),
payload: getPayload( status )
name: config.name,
topic: config.topic,
payload: getPayload( value ),
timestamp: new Date().getTime()
};

this.send( [ msg, { ...msg, payload: getPayload( !status ) } ] );
setStatus( value, true );
this.send( [ msg, { ...msg, payload: getPayload( !value ) } ] );

if( timestamp !== 0 ) {
while( timestamp <= msg.timestamp ) timestamp += config.period * 500;
timeout = setTimeout( update, timestamp - msg.timestamp, !status );

this.status( { fill: 'green', shape: 'dot', text: `active: ${ msg.payload } / ${ getPayload( !status ) }` } );
timeout = setTimeout( update, timestamp - msg.timestamp, !value );
} else {
clearTimeout( timeout );
this.status( { fill: 'grey', shape: 'dot', text: 'inactive' } );
}
};

this.status( { fill: 'grey', shape: 'dot', text: 'inactive' } );
timestamp = !config.init ? 0 : 1;
!config.init ? setStatus() : this.emit( 'input', { payload: false } );

this.on( 'input', msg => msg.payload ? start() : stop() );
this.on( 'close', stop );
Expand Down

0 comments on commit 01c1bcb

Please sign in to comment.