Skip to content

Commit

Permalink
refactor(examples): update custom function examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Red-Asuka committed Jan 13, 2025
1 parent 7e35444 commit 62c5ace
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 41 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default antfu(
*/
markdown: 'prettier',
},
ignores: ['examples/**'],
},
{
rules: {
Expand Down
21 changes: 9 additions & 12 deletions examples/function/dynamicSwitch.js
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)
26 changes: 11 additions & 15 deletions examples/function/tempAndHum.js
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)
27 changes: 13 additions & 14 deletions examples/function/timestamp.js
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)

0 comments on commit 62c5ace

Please sign in to comment.