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

feat: add valueId, node and logger to parse functions #319

Merged
merged 11 commits into from
Jan 25, 2021
4 changes: 2 additions & 2 deletions docs/usage/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,5 +184,5 @@ The Gateway values table can be used with all gateway types to customize specifi
- **Device Class**: If the value is a multilevel sensor, a binary sensor or a meter you can set a custom `device_class` to use with home assistant discovery. Check [sensor](https://www.home-assistant.io/components/sensor/#device-class) and [binary sensor](https://www.home-assistant.io/components/binary_sensor/#device-class)
- **Topic**: The topic to use for this value. It is the topic added after topic prefix, node name and location. If gateway type is different than `Manual` this can be leave blank and the value topic will be the one based on the gateway configuration chosen
- **Post operation**: If you want to convert your value (eg. '/10' '/100' '*10' '*100')
- **Parse Send**: Enable this to allow users to specify a custom `function(value)` to parse the value sent to MQTT. The function must be sync
- **Parse receive**: Enable this to allow users to specify a custom `function(value)` to parse the value received via MQTT. The function must be sync
- **Parse send**: Enable this to allow users to specify a custom `function(value,valueId,node,logger)` to parse the value sent to MQTT. The function must be sync
- **Parse receive**: Enable this to allow users to specify a custom `function(value,valueId,node,logger)` to parse the value received via MQTT. The function must be sync
16 changes: 11 additions & 5 deletions lib/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,12 @@ function onValueChanged (valueId, node, changed) {
}

if (valueConf.parseSend) {
const parsedVal = evalFunction(valueConf.sendFunction, valueId, tmpVal)
const parsedVal = evalFunction(
valueConf.sendFunction,
valueId,
tmpVal,
node
)
if (parsedVal != null) {
tmpVal = parsedVal
}
Expand Down Expand Up @@ -504,13 +509,13 @@ function isValidOperation (op) {
* @param {*} value The actual value to parse
* @returns
*/
function evalFunction (code, valueId, value) {
function evalFunction (code, valueId, value, node) {
let result = null

try {
/* eslint-disable no-new-func */
const parseFunc = new Function('value', code)
result = parseFunc(value)
const parseFunc = new Function('value', 'valueId', 'node', 'logger', code)
result = parseFunc(value, valueId, node, logger)
} catch (error) {
logger.error(`Error eval function of value ${valueId.id} ${error.message}`)
}
Expand Down Expand Up @@ -807,7 +812,8 @@ Gateway.prototype.parsePayload = function (payload, valueId, valueConf) {
const parsedVal = evalFunction(
valueConf.receiveFunction,
valueId,
payload
payload,
{}
ahochsteger marked this conversation as resolved.
Show resolved Hide resolved
)
if (parsedVal != null) {
payload = parsedVal
Expand Down
14 changes: 9 additions & 5 deletions src/components/dialogs/DialogGatewayValue.vue
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@

<v-container v-if="editedValue.parseSend">
<p>
Write the function here. Args are <code>value</code>. The
function is sync and must return the parsed <code>value</code>
Write the function here. Args are: <code>value</code>,
<code>valueId</code>, <code>node</code>, <code>logger</code>.
The function is sync and must return the parsed
<code>value</code>.
</p>
<prism-editor
lineNumbers
Expand All @@ -150,8 +152,10 @@

<v-container v-if="editedValue.parseReceive">
<p>
Write the function here. Args are <code>value</code>. The
function is sync and must return the parsed <code>value</code>
Write the function here. Args are: <code>value</code>,
<code>valueId</code>, <code>node</code>, <code>logger</code>.
The function is sync and must return the parsed
<code>value</code>.
</p>
<prism-editor
lineNumbers
Expand All @@ -172,7 +176,7 @@
color="blue darken-1"
text
@click="$refs.form.validate() && $emit('save')"
>Save</v-btn
>Update</v-btn
ahochsteger marked this conversation as resolved.
Show resolved Hide resolved
>
</v-card-actions>
</v-card>
Expand Down