-
Notifications
You must be signed in to change notification settings - Fork 457
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(examples): update custom function examples
- Loading branch information
Showing
4 changed files
with
34 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ export default antfu( | |
*/ | ||
markdown: 'prettier', | ||
}, | ||
ignores: ['examples/**'], | ||
}, | ||
{ | ||
rules: { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,18 @@ | ||
/** | ||
* @description: Simulated dynamic switch command | ||
* @param {string | object} value - Payload | ||
* @param {string} msgType - Message type, value is 'received' or 'publish' | ||
* @param {string} message - Message payload | ||
* @param {string} messageType - Message type, value is 'received' or 'publish' | ||
* @param {number} index - Index of the message, valid only when script is used in the publish message and timed message is enabled | ||
* @return {object} - Return two commands alternately, { "command": "on" } or { "command": "off" } | ||
* @return {string} - Return two commands alternately, { "command": "on" } or { "command": "off" } | ||
*/ | ||
function handlePayload(value, msgType, index) { | ||
let _value = value | ||
if (typeof value === 'string') { | ||
_value = JSON.parse(value) | ||
} | ||
function handlePayload(message, messageType, index) { | ||
let _message = JSON.parse(message || '{}') | ||
if (index % 2 === 0) { | ||
_value.command = 'on' | ||
_message.command = 'on' | ||
} else { | ||
_value.command = 'off' | ||
_message.command = 'off' | ||
} | ||
return JSON.stringify(_value, null, 2) | ||
return JSON.stringify(_message, null, 2) | ||
} | ||
|
||
execute(handlePayload) | ||
return execute(handlePayload) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,17 @@ | ||
/** | ||
* Simulated temperature and humidity reporting | ||
* @return Return a simulated temperature and humidity JSON data - { "temperature": 23, "humidity": 40 } | ||
* @param value, MQTT Payload - {} | ||
*/ | ||
|
||
function random(min, max) { | ||
return Math.round(Math.random() * (max - min)) + min | ||
} | ||
|
||
function handlePayload(value) { | ||
let _value = value | ||
if (typeof value === 'string') { | ||
_value = JSON.parse(value) | ||
} | ||
_value.temperature = random(10, 30) | ||
_value.humidity = random(20, 40) | ||
return JSON.stringify(_value, null, 2) | ||
/** | ||
* @description: Simulated dynamic switch command | ||
* @param {string} message - Message payload | ||
* @return {string} - Return a simulated temperature and humidity data - { "temperature": 23, "humidity": 40 } | ||
*/ | ||
function handlePayload(message) { | ||
let _message = JSON.parse(message || '{}') | ||
_message.temperature = random(10, 30) | ||
_message.humidity = random(20, 40) | ||
return JSON.stringify(_message, null, 2) | ||
} | ||
|
||
execute(handlePayload) | ||
return execute(handlePayload) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,17 @@ | ||
/** | ||
* Convert timestamp to normal time. | ||
* @return Return the UTC time - { "time": "2020-12-17 14:18:07" } | ||
* @param value, MQTT Payload - { "time": 1608185887 } | ||
* Convert timestamp to normal time | ||
* @param {string} message - Message payload - { "time": 1608185887 } | ||
* @return {string} - Return the message with the time converted to normal time - { "time": "17/12/2020, 06:18:07" } | ||
*/ | ||
|
||
function handleTimestamp(value) { | ||
let _value = value | ||
if (typeof value === 'string') { | ||
_value = JSON.parse(value) | ||
} | ||
// East Eight District needs an additional 8 hours | ||
const date = new Date(_value.time * 1000 + 8 * 3600 * 1000) | ||
_value.time = date.toJSON().substr(0, 19).replace('T', ' ') | ||
return JSON.stringify(_value, null, 2) | ||
function handlePayload(message) { | ||
let _message = JSON.parse(message) | ||
const date = new Date(_message.time * 1000) | ||
_message.time = new Intl.DateTimeFormat('en-GB', { | ||
timeZone: 'UTC', | ||
dateStyle: 'short', | ||
timeStyle: 'medium' | ||
}).format(date) | ||
return JSON.stringify(_message, null, 2) | ||
} | ||
|
||
execute(handleTimestamp) | ||
return execute(handlePayload) |