Skip to content

Commit

Permalink
Added more functions to custom parser scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
crycode-de committed Nov 3, 2024
1 parent 46d4941 commit 81da060
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 10 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ In the scripts you are able to use the following features:
* Globals `undefined`, `NaN`, `isNaN`, `Infinity`, `isFinite`, `atob`, `btoa`,
`encodeURI`, `encodeURIComponent`, `decodeURI`, `decodeURIComponent`, `parseFloat`,
`parseInt`, `JSON`, `Number`, `String`, `Array`, `BigInt`, `Blob`, `Boolean`,
`Date`, `Map`, `Math`, `Object`, `RegExp`, `Set`, `Intl`, `Buffer`, `Promise`
`Date`, `Map`, `Math`, `Object`, `RegExp`, `Set`, `Intl`, `Buffer`, `Promise`,
`setTimeout`, `clearTimeout`
* `async`/`await`
* Adapter log functions `log.warn('something')`, `log.info('something')`, `log.debug('something')`
* `getStateAsync('id')` and `getObjectAsync('id')` where `id` is the partial ID of the state/object below the current adapter instance
* `getForeignStateAsync('id')` and `getForeignObjectAsync('id')` where `id` is the full ID of the state/object
* `getStateAsync('id')`, `getObjectAsync('id')`, `setStateAsync('id', 'value', ack)` where `id` is the partial ID of the state/object below the current adapter instance
* `getForeignStateAsync('id')`, `getForeignObjectAsync('id')` and `setForeignStateAsync('id', 'value', ack)` where `id` is the full ID of the state/object
* Function `wait(ms)` which returns a Promise which resolves after the given time
* An object `sharedData` which is shared between all custom scripts of an adapter instance

Errors in the scripts will be logged by the adapter.
Expand Down Expand Up @@ -164,6 +166,12 @@ By writing JSON data to the `raw.send` state you are able to send CAN messages c
Placeholder for the next version (at the beginning of the line):
### **WORK IN PROGRESS**
-->
### **WORK IN PROGRESS**

* (crycode-de) Allow `setStateAsync` and `setForeignStateAsync` in custom parser scripts
* (crycode-de) Allow `setTimeout` and `clearTimeout` in custom parser scripts (using the adapters setTimeout implementation)
* (crycode-de) Added `wait` function to custom parser scripts

### 2.0.0 (2024-11-02)

* (crycode-de) Node.js >= 18, Admin >= 6.17, js-contoller >= 5.0.19 are required
Expand Down
2 changes: 1 addition & 1 deletion admin/build/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions admin/build/index.js.map

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions build/parsers/custom.js

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

4 changes: 2 additions & 2 deletions build/parsers/custom.js.map

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

7 changes: 6 additions & 1 deletion src/parsers/custom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ const fakeAdapter = {
getForeignStateAsync: fakeGetStateAsync,
getObjectAsync: fakeGetObjectAsync,
getForeignObjectAsync: fakeGetObjectAsync,
setTimeout: setTimeout, // real adapter uses adapter internal function here but this is ok for the tests
clearTimeout: clearTimeout, // real adapter uses adapter internal function here but this is ok for the tests
} as unknown as CanBusAdapter;

describe('ParserCustom', () => {
Expand Down Expand Up @@ -234,12 +236,15 @@ describe('ParserCustom', () => {
typeof setState,
typeof Promise,
typeof sharedData,
typeof setTimeout,
typeof clearTimeout,
typeof wait,
].join(',');
`,
customScriptWrite: `/* ... */`,
});
const val = await parser.read(buf);
expect(val).to.equal('object,0,undefined,undefined,undefined,function,object');
expect(val).to.equal('object,0,undefined,undefined,undefined,function,object,function,function,function');
});

});
Loading

0 comments on commit 81da060

Please sign in to comment.