-
Notifications
You must be signed in to change notification settings - Fork 444
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(cli): add smart home scenario #1299
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { Faker } from '@faker-js/faker' | ||
|
||
const dataCache: Record<string, any> = {} | ||
|
||
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 } | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with the code review. First, the code is well-structured and easy to read. It is well-commented and provides a good description of the purpose of the script. The code uses consistent indentation and naming conventions, making it easier to read. It also uses good practices for variable names, such as using descriptive names that indicate their purpose. The code is also well-tested and has good error handling. It checks for valid input and handles errors gracefully. In addition, the data generated is randomized but realistic, which is important for testing purposes. Overall, the code looks good and suitable for its purpose. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the code review: