Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Zefau authored Sep 30, 2020
1 parent 27d8025 commit 88c3c8f
Showing 1 changed file with 55 additions and 17 deletions.
72 changes: 55 additions & 17 deletions lib/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* @description Library of general functions as well as helping functions handling ioBroker
* @author Zefau <https://github.com/Zefau/>
* @license MIT License
* @version 0.28.0
* @date 2020-03-31
* @version 0.29.1
* @date 2020-09-30
*
*/
class Library
Expand All @@ -33,6 +33,7 @@ class Library
this.options.updatesExceptions = this.options.updatesExceptions || ['timestamp', 'datetime', 'UTC', 'localtime', 'last_use_date', 'lastSeen'];

this._STATES = {};
this.subscriptions = [];

this.set({ 'node': 'info', 'description': 'Adapter Information', 'role': 'channel' });
this.set(Library.CONNECTION, false);
Expand Down Expand Up @@ -109,6 +110,19 @@ class Library
return lowerCase ? string.toLowerCase() : string;
}

/**
* Waits for a specific time before invoking a callback.
*
* @param {number} time Time to wait before invoking the callback
* @param {function} callback Callback to be invoked
* @return void
*
*/
wait(time, callback)
{
setTimeout(() => callback, time);
}

/**
* Encode a string.
*
Expand Down Expand Up @@ -454,17 +468,28 @@ class Library
*/
set(node, value, options = {})
{
// catch error
if (!node || !node.node || (node.name === undefined && node.description === undefined))
this._adapter.log.error('Error: State not properly defined (' + JSON.stringify(node) + ')!');

// create node
if (this._STATES[node.node] === undefined)
this._createNode(node, () => this._setValue(node.node, value, options));

// set value
else
if (node && node.node && this._STATES[node.node] !== undefined) {

this._setValue(node.node, value, options);

if (options.subscribe === true && this.subscriptions.indexOf(node.node) === -1) {
this._adapter.subscribeStates(node.node);
this.subscriptions.push(node.node);
}
}
// create node
else {
// catch error
if (!node || !node.node || (node.name === undefined && node.description === undefined)) {
this._adapter.log.error('Error: State not properly defined (' + JSON.stringify(node) + ')!');
return false;
}

this._createNode(node, () => this._setValue(node.node, value, options), options);
}

return true;
}

/**
Expand All @@ -478,10 +503,11 @@ class Library
* @param {string} node.common.type Type of the node (in case it will be created)
* @param {object} node.native Native Details of the node (in case it will be created)
* @param {function} callback Callback function to be invoked
* @param {object} [options={}] Additional options
* @return void
*
*/
_createNode(node, callback)
_createNode(node, callback, options = {})
{
if (!this._adapter)
return Promise.reject('Adapter not defined!');
Expand All @@ -492,16 +518,18 @@ class Library
'name': node.name || node.description,
'role': node.common && node.common.role || node.role || 'state',
'type': node.common && node.common.type || node.type || 'string',
'read': true,
'write': false,
...node.common || {}
};

// special roles
if (common.role.indexOf('button') > -1)
if (common.role.indexOf('button') > -1 || common.role.indexOf('indicator') > -1) {
common = { ...common, 'type': 'boolean', 'read': false, 'write': true };

if (common.role == 'device' || common.role == 'channel')
}
else if (common.role == 'device' || common.role == 'channel') {
common = { ...common, 'type': undefined, 'role': undefined };
}

// create object
this._adapter.setObjectNotExists(
Expand All @@ -514,6 +542,12 @@ class Library
(err, obj) =>
{
this._STATES[node.node] = null;

if (options.subscribe === true && this.subscriptions.indexOf(node.node) === -1) {
this._adapter.subscribeStates(node.node);
this.subscriptions.push(node.node);
}

callback && callback();
}
);
Expand All @@ -526,6 +560,7 @@ class Library
* @param {string} value Value to be set
* @param {object} [options={}] Additional options
* @param {boolean} [options.force=false] Force to set value
* @param {boolean} [options.ack=true] Ack to use
* @return void
*
*/
Expand All @@ -535,8 +570,11 @@ class Library
{
if (value !== undefined && (options.force || this._STATES[state] === undefined || this._STATES[state] === null || this._STATES[state].val != value))
{
this.setDeviceState(state, value);
this._adapter.setStateAsync(state, { val: value, ts: Date.now(), ack: true });
if (options.ack !== false) {
this.setDeviceState(state, value);
}

this._adapter.setStateAsync(state, value, options.ack !== undefined ? options.ack : true, { ts: Date.now() }); // id, state, ack, options, callback
}
else
this.setDeviceProperties(state);
Expand Down

0 comments on commit 88c3c8f

Please sign in to comment.