From c1507720a1d31402d91f568ea32750062e52a29f Mon Sep 17 00:00:00 2001 From: YuShifan <894402575bt@gmail.com> Date: Thu, 18 May 2023 16:02:06 +0800 Subject: [PATCH 1/2] feat(cli): add smart home scenario --- cli/src/scenarios/smart_home.ts | 100 ++++++++++++++++++ .../IoT-data-scenarios/smart_home.js | 99 +++++++++++++++++ 2 files changed, 199 insertions(+) create mode 100644 cli/src/scenarios/smart_home.ts create mode 100644 scripts-example/IoT-data-scenarios/smart_home.js diff --git a/cli/src/scenarios/smart_home.ts b/cli/src/scenarios/smart_home.ts new file mode 100644 index 000000000..bff7f2541 --- /dev/null +++ b/cli/src/scenarios/smart_home.ts @@ -0,0 +1,100 @@ +import { Faker } from '@faker-js/faker' + +const dataCache: Record = {} + +interface RoomData { + room_type: string + temperature: number + humidity: number + lights_on: boolean + window_open: boolean +} + +interface KitchenData extends RoomData { + fridge_temperature: number + oven_on: boolean +} + +interface BathroomData extends RoomData { + water_tap_running: boolean + bath_water_level: number +} + +interface BedroomData extends RoomData { + bed_occupancy: boolean +} + +const generateRoomData = (faker: Faker, roomType: string): RoomData | KitchenData | BathroomData | BedroomData => { + const currentHour = new Date().getHours() + const isDaytime = currentHour > 6 && currentHour < 20 + const isSleepingHours = currentHour > 22 || currentHour < 6 + const isKitchen = roomType === 'kitchen' + const isBathroom = roomType === 'bathroom' + const isBedroom = roomType === 'bedroom' + + const baseData: RoomData = { + room_type: roomType, + temperature: faker.datatype.number({ min: 18, max: 26 }), + humidity: faker.datatype.number({ min: 30, max: 50 }), + lights_on: isDaytime ? faker.datatype.boolean() : !isSleepingHours, + window_open: isDaytime ? faker.datatype.boolean() : false, + } + + if (isKitchen) { + const kitchenData: KitchenData = { + ...baseData, + fridge_temperature: faker.datatype.number({ min: 2, max: 8 }), + oven_on: faker.datatype.boolean(), + } + return kitchenData + } + + if (isBathroom) { + const bathroomData: BathroomData = { + ...baseData, + water_tap_running: faker.datatype.boolean(), + bath_water_level: faker.datatype.number({ min: 0, max: 100 }), + } + return bathroomData + } + + if (isBedroom) { + const bedroomData: BedroomData = { + ...baseData, + bed_occupancy: faker.datatype.boolean(), + } + return bedroomData + } + + return baseData +} + +const generator = function (faker: Faker, options: SimulatePubOptions) { + const { clientId } = options + if (!dataCache[clientId]) { + dataCache[clientId] = { + home_id: faker.datatype.uuid(), + owner_name: faker.name.fullName(), + address: faker.address.streetAddress(), + } + } + + const roomTypes = ['living room', 'bedroom', 'kitchen', 'bathroom'] + + const data = { + ...dataCache[clientId], + rooms: roomTypes.map((roomType) => generateRoomData(faker, roomType)), + timestamp: Date.now(), + } + return { + message: JSON.stringify(data), + } +} + +const name = 'smart_home' +const author = 'EMQX Team' +const dataFormat = 'JSON' +const version = '0.0.1' +const description = 'Simulation to generate Smart Home data' + +export { generator, name, author, dataFormat, version, description } diff --git a/scripts-example/IoT-data-scenarios/smart_home.js b/scripts-example/IoT-data-scenarios/smart_home.js new file mode 100644 index 000000000..0674a9070 --- /dev/null +++ b/scripts-example/IoT-data-scenarios/smart_home.js @@ -0,0 +1,99 @@ +/** + * This script is a data generator for simulating Smart Home data. + * It uses the faker library to generate randomized, but realistic data for a smart home. + * + * The script creates a home object with various properties such as its owner, address, and individual rooms. + * Each room has its own specific properties, such as temperature, humidity, and lights, with additional properties + * based on the room type (e.g. fridge temperature and oven state for the kitchen, water tap running and bath water + * level for the bathroom, and bed occupancy for the bedroom). + * + * The generated data is returned in JSON format. This script can be used as a module in a larger application, + * where the generated data might be used for testing, simulation or analytics purposes. + * + * This script is developed and maintained by the EMQX Team, and its current version is 0.0.1. + * + * @module smart_home + * @version 0.0.1 + * @author EMQX Team + */ + +const dataCache = {} + +const generateRoomData = (faker, roomType) => { + const currentHour = new Date().getHours() + const isDaytime = currentHour > 6 && currentHour < 20 + const isSleepingHours = currentHour > 22 || currentHour < 6 + const isKitchen = roomType === 'kitchen' + const isBathroom = roomType === 'bathroom' + const isBedroom = roomType === 'bedroom' + + const baseData = { + room_type: roomType, + temperature: faker.datatype.number({ min: 18, max: 26 }), + humidity: faker.datatype.number({ min: 30, max: 50 }), + lights_on: isDaytime ? faker.datatype.boolean() : !isSleepingHours, + window_open: isDaytime ? faker.datatype.boolean() : false, + } + + if (isKitchen) { + return { + ...baseData, + fridge_temperature: faker.datatype.number({ min: 2, max: 8 }), + oven_on: faker.datatype.boolean(), + } + } + + if (isBathroom) { + return { + ...baseData, + water_tap_running: faker.datatype.boolean(), + bath_water_level: faker.datatype.number({ min: 0, max: 100 }), + } + } + + if (isBedroom) { + return { + ...baseData, + bed_occupancy: faker.datatype.boolean(), + } + } + + return baseData +} + +const generator = function (faker, options) { + const { clientId } = options + if (!dataCache[clientId]) { + dataCache[clientId] = { + home_id: faker.datatype.uuid(), + owner_name: faker.name.fullName(), + address: faker.address.streetAddress(), + } + } + + const roomTypes = ['living room', 'bedroom', 'kitchen', 'bathroom'] + + const data = { + ...dataCache[clientId], + rooms: roomTypes.map((roomType) => generateRoomData(faker, roomType)), + timestamp: Date.now(), + } + return { + message: JSON.stringify(data), + } +} + +const name = 'smart_home' +const author = 'EMQX Team' +const dataFormat = 'JSON' +const version = '0.0.1' +const description = 'Simulation to generate Smart Home data' + +module.exports = { + generator, + name, + author, + dataFormat, + version, + description, +} From f8ecf98494780fdb98100985d095f40e8bebf655 Mon Sep 17 00:00:00 2001 From: YuShifan <894402575bt@gmail.com> Date: Thu, 18 May 2023 16:44:34 +0800 Subject: [PATCH 2/2] docs(cli): update quick start example --- cli/README-CN.md | 4 ++++ cli/README.md | 4 ++++ cli/src/scenarios/smart_home.ts | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/cli/README-CN.md b/cli/README-CN.md index d28fb3b23..cd7ea2304 100644 --- a/cli/README-CN.md +++ b/cli/README-CN.md @@ -119,7 +119,11 @@ mqttx sub -t 'hello' -h 'broker.emqx.io' -p 1883 Publish ```shell +# Publish a single message mqttx pub -t 'hello' -h 'broker.emqx.io' -p 1883 -m 'from MQTTX CLI' + +# Publish multiple messages (multiline) +mqttx pub -t 'hello' -h 'broker.emqx.io' -p 1883 -s -M ``` Benchmark diff --git a/cli/README.md b/cli/README.md index 96d19d368..d4a37e8fc 100644 --- a/cli/README.md +++ b/cli/README.md @@ -116,7 +116,11 @@ mqttx sub -t 'hello' -h 'broker.emqx.io' -p 1883 Publish ```shell +# Publish a single message mqttx pub -t 'hello' -h 'broker.emqx.io' -p 1883 -m 'from MQTTX CLI' + +# Publish multiple messages (multiline) +mqttx pub -t 'hello' -h 'broker.emqx.io' -p 1883 -s -M ``` Benchmark diff --git a/cli/src/scenarios/smart_home.ts b/cli/src/scenarios/smart_home.ts index bff7f2541..504d727ec 100644 --- a/cli/src/scenarios/smart_home.ts +++ b/cli/src/scenarios/smart_home.ts @@ -95,6 +95,6 @@ const name = 'smart_home' const author = 'EMQX Team' const dataFormat = 'JSON' const version = '0.0.1' -const description = 'Simulation to generate Smart Home data' +const description = 'Simulation to generate Smart Home data.' export { generator, name, author, dataFormat, version, description }