Skip to content

Commit

Permalink
Removed support for binary states (deprecated since v6.2.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
klein0r committed Apr 2, 2024
1 parent 835fa4f commit dfaf3b8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 142 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Since v5.5.0 of the JavaScript adapter the following locations (relative to the

NodeJS >= 18.x and js-controller >= 5 is required

* (klein0r) Breaking change: Removed support for binary states (deprecated since v6.2.0)
* (klein0r) Added missing functions to protectFS
* (klein0r) Fixed httpPost (missing data)
* (klein0r) Fixed hasAttribute blockly block
Expand Down
23 changes: 0 additions & 23 deletions docs/en/javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
- [compareTime](#comparetime)
- [setState](#setstate)
- [setStateAsync](#setstateasync)
- [setBinaryState](#setbinarystate)
- [setStateDelayed](#setstatedelayed)
- [clearStateDelayed](#clearstatedelayed)
- [getStateDelayed](#getstatedelayed)
Expand Down Expand Up @@ -773,17 +772,6 @@ await setStateAsync(id, state, ack);
```
Same as setState, but with `promise`.

### setBinaryState

**Attention: This method is deprecated!**

```js
setBinaryState(id, state, callback);
```
Same as setState, but for the binary states, like files, images, buffers.
The difference is that such a state has no ack, ts, lc, quality and so on flags und should be used only for binary things.
The object's `common.type` must be equal to `file`.

### setStateDelayed
```js
setStateDelayed(id, state, isAck, delay, clearRunning, callback);
Expand Down Expand Up @@ -890,17 +878,6 @@ const stateObject = await getStateAsync(id);
```
Same as getState, but with `promise`.

### getBinaryState
**Attention: This method is deprecated!**
```js
getBinaryState(id, (err, data) => {});
```

Same as getState, but for the binary states, like files, images, buffers.
The difference is that such a state has no ack, ts, lc, quality and so on flags und should be used only for binary "things".
The object's `common.type` must be equal to `file`.
This function must always be used with callback. "data" is a buffer.

### existsState
```js
existsState(id, (err, isExists) => {});
Expand Down
145 changes: 26 additions & 119 deletions lib/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,38 +341,24 @@ function sandBox(script, name, verbose, debug, context) {
}
}

function setStateHelper(sandbox, isCreate, isBinary, id, state, isAck, callback) {
let setStateFunc;
if (isBinary) {
sandbox.log(`setBinaryState(id=${id}) is deprecated. Please use Files instead!`, 'warn');
if (typeof adapter.setForeignBinaryState === 'function') {
setStateFunc = adapter.setForeignBinaryState.bind(adapter);
} else {
setStateFunc = adapter.setBinaryState.bind(adapter);
}
} else {
setStateFunc = adapter.setForeignState.bind(adapter);
}

function setStateHelper(sandbox, isCreate, id, state, isAck, callback) {
if (typeof isAck === 'function') {
callback = isAck;
isAck = undefined;
}

if (!isBinary) {
if (state === null) {
state = {val: null};
}
if (state === null) {
state = {val: null};
}

if (isAck === true || isAck === false || isAck === 'true' || isAck === 'false') {
if (isObject(state) && state.val !== undefined) {
// we assume that we were given a state object if
// state is an object that contains a `val` property
state.ack = isAck;
} else {
// otherwise, assume that the given state is the value to be set
state = {val: state, ack: isAck};
}
if (isAck === true || isAck === false || isAck === 'true' || isAck === 'false') {
if (isObject(state) && state.val !== undefined) {
// we assume that we were given a state object if
// state is an object that contains a `val` property
state.ack = isAck;
} else {
// otherwise, assume that the given state is the value to be set
state = {val: state, ack: isAck};
}
}

Expand Down Expand Up @@ -413,7 +399,7 @@ function sandBox(script, name, verbose, debug, context) {
+ `This warning might become an error in future versions.`
);
}
if (!isBinary && (actualCommonType === 'array' || actualCommonType === 'object')) {
if (actualCommonType === 'array' || actualCommonType === 'object') {
try {
if (isObject(state) && typeof state.val !== 'undefined') {
state.val = JSON.stringify(state.val);
Expand All @@ -433,28 +419,26 @@ function sandBox(script, name, verbose, debug, context) {
}
}
// Check min and max of value
if (!isBinary && isObject(state)) {
if (isObject(state)) {
if (common && typeof state.val === 'number') {
if (common.min !== undefined && state.val < common.min) state.val = common.min;
if (common.max !== undefined && state.val > common.max) state.val = common.max;
}
} else if (!isBinary && common && typeof state === 'number') {
} else if (common && typeof state === 'number') {
if (common.min !== undefined && state < common.min) state = common.min;
if (common.max !== undefined && state > common.max) state = common.max;
}

// modify state here, to make it available in callback
if (!isBinary && (!isObject(state) || state.val === undefined)) {
if (!isObject(state) || state.val === undefined) {
state = {val: state};
state.ack = isAck || false;
}

if (!isBinary) {
// we only need this when state cache is used
state = context.prepareStateObject(id, state, isAck);
// set as comment: from which script this state was set.
state.c = sandbox.name;
}
// we only need this when state cache is used
state = context.prepareStateObject(id, state, isAck);
// set as comment: from which script this state was set.
state.c = sandbox.name;

if (objects[id]) {
script.setStatePerMinuteCounter++;
Expand All @@ -478,7 +462,7 @@ function sandBox(script, name, verbose, debug, context) {
// risk that there will be an error on setState is very low
context.interimStateValues[id] = state;
}
setStateFunc(id, state, err => {
adapter.setForeignState(id, state, err => {
err && sandbox.log(`setForeignState: ${err}`, 'error');
// If adapter holds all states
if (err && !adapter.config.subscribe) {
Expand Down Expand Up @@ -647,9 +631,6 @@ function sandBox(script, name, verbose, debug, context) {
result.setState = function () {
return this;
};
result.setBinaryState = function () {
return this;
};
result.on = function () {
return this;
};
Expand Down Expand Up @@ -971,34 +952,6 @@ function sandBox(script, name, verbose, debug, context) {
return context.convertBackStringifiedValues(this[0], states[this[0]]);
}
};
result.getBinaryState = function (callback) {
sandbox.log(`$.getBinaryState(id=${this[0]}) is deprecated. Please use Files instead!`, 'warn');
if (adapter.config.subscribe) {
if (typeof callback !== 'function') {
sandbox.log('You cannot use this function synchronous', 'error');
} else {
if (typeof adapter.getForeignBinaryState === 'function') {
adapter.getForeignBinaryState(this[0], callback);
} else {
adapter.getBinaryState(this[0], callback);
}
}
} else {
return this[0] ? states[this[0]] : null;
}
};
result.getBinaryStateAsync = function() {
sandbox.log(`$.getBinaryStateAsync(id=${this[0]}) is deprecated. Please use Files instead!`, 'warn');
if (adapter.config.subscribe) {
if (typeof adapter.getForeignBinaryStateAsync === 'function') {
return adapter.getForeignBinaryStateAsync(this[0]);
} else {
return adapter.getBinaryStateAsync(this[0]);
}
} else {
return this[0] ? states[this[0]] : null;
}
};
result.setState = function (state, isAck, callback) {
if (typeof isAck === 'function') {
callback = isAck;
Expand Down Expand Up @@ -1040,35 +993,6 @@ function sandBox(script, name, verbose, debug, context) {
}
return this;
};
result.setBinaryState = function (state, isAck, callback) {
sandbox.log(`$.setBinaryState(id=${this[0]}) is deprecated. Please use Files instead!`, 'warn');
if (typeof isAck === 'function') {
callback = isAck;
isAck = undefined;
}
result.setBinaryStateAsync(state, isAck)
.then(() =>
typeof callback === 'function' && callback());
return this;
};
result.setBinaryStateAsync = async function (state, isAck) {
sandbox.log(`$.setBinaryStateAsync(id=${this[0]}) is deprecated. Please use Files instead!`, 'warn');
if (isAck === true || isAck === false || isAck === 'true' || isAck === 'false') {
if (isObject(state)) {
state.ack = isAck;
} else {
state = {val: state, ack: isAck};
}
}
const foreignMethod = typeof adapter.setForeignBinaryStateAsync === 'function';
for (let i = 0; i < this.length; i++) {
if (foreignMethod) {
await adapter.setForeignBinaryStateAsync(this[i], state);
} else {
await adapter.setBinaryStateAsync(this[i], state);
}
}
};
result.on = function (callbackOrId, value) {
for (let i = 0; i < this.length; i++) {
sandbox.subscribe(this[i], callbackOrId, value);
Expand Down Expand Up @@ -1966,10 +1890,7 @@ function sandBox(script, name, verbose, debug, context) {
return schedules;
},
setState: function (id, state, isAck, callback) {
return setStateHelper(sandbox, false, false, id, state, isAck, callback);
},
setBinaryState: function (id, state, callback) {
return setStateHelper(sandbox, false, true, id, state, callback);
return setStateHelper(sandbox, false, id, state, isAck, callback);
},
setStateDelayed: function (id, state, isAck, delay, clearRunning, callback) {
// find arguments
Expand Down Expand Up @@ -2153,7 +2074,7 @@ function sandBox(script, name, verbose, debug, context) {
},
setStateAsync: function (id, state, isAck) {
return new Promise((resolve, reject) =>
setStateHelper(sandbox, false, false, id, state, isAck, err => err ? reject(err) : resolve()));
setStateHelper(sandbox, false, id, state, isAck, err => err ? reject(err) : resolve()));
},
getState: function (id, callback) {
if (typeof callback === 'function') {
Expand Down Expand Up @@ -2188,18 +2109,6 @@ function sandBox(script, name, verbose, debug, context) {
}
}
},
getBinaryState: function (id, callback) {
sandbox.log(`getBinaryState(id=${id}) is deprecated. Please use Files instead!`, 'warn');
if (typeof callback === 'function') {
if (typeof adapter.getForeignBinaryState === 'function') {
adapter.getForeignBinaryState(id, callback);
} else {
adapter.getBinaryState(id, callback);
}
} else {
sandbox.log('The "getBinaryState" method cannot be used synchronously.', 'error');
}
},
existsState: function (id, callback) {
if (typeof callback === 'function') {
adapter.getForeignObject(id, (err, obj) => {
Expand Down Expand Up @@ -2816,12 +2725,12 @@ function sandBox(script, name, verbose, debug, context) {

if (!isAlias && initValue !== undefined) {
if (isObject(initValue) && initValue.ack !== undefined) {
setStateHelper(sandbox, true, false, id, initValue, callback);
setStateHelper(sandbox, true, id, initValue, callback);
} else {
setStateHelper(sandbox, true, false, id, initValue, true, callback);
setStateHelper(sandbox, true, id, initValue, true, callback);
}
} else if (!isAlias && !forceCreation) {
setStateHelper(sandbox, true, false, id, null, callback);
setStateHelper(sandbox, true, id, null, callback);
} else if (isAlias) {
try {
const state = await adapter.getForeignStateAsync(id);
Expand Down Expand Up @@ -4165,8 +4074,6 @@ function sandBox(script, name, verbose, debug, context) {
// promisify methods on the sandbox
/** @type {(keyof typeof sandbox)[]} */
const promisifiedMethods = [
'setBinaryState',
'getBinaryState',
'existsState',
'existsObject',
'getObject',
Expand Down

0 comments on commit dfaf3b8

Please sign in to comment.