未验证 提交 8ec62c1d 编写于 作者: B Bo Ding 提交者: GitHub

docs: minor change for 09-emq-broker.md (#13352)

上级 97281b47
...@@ -21,17 +21,17 @@ Depending on the current operating system, users can download the installation p ...@@ -21,17 +21,17 @@ Depending on the current operating system, users can download the installation p
### Take the Docker installation of TDengine as an example ### Take the Docker installation of TDengine as an example
```bash ```bash
docker exec -it tdengine bash docker exec -it tdengine bash
taos taos
``` ```
### Create Database and Table ### Create Database and Table
```sql ```sql
CREATE DATABASE test; CREATE DATABASE test;
USE test; USE test;
CREATE TABLE sensor_data (ts timestamp, temperature float, humidity float, volume float, PM10 float, pm25 float, SO2 float, NO2 float, CO float, sensor_id NCHAR(255), area TINYINT, coll_time timestamp); CREATE TABLE sensor_data (ts timestamp, temperature float, humidity float, volume float, PM10 float, pm25 float, SO2 float, NO2 float, CO float, sensor_id NCHAR(255), area TINYINT, coll_time timestamp);
``` ```
Note: The table schema is based on the blog [(In Chinese) Data Transfer, Storage, Presentation, EMQX + TDengine Build MQTT IoT Data Visualization Platform](https://www.taosdata.com/blog/2020/08/04/1722.html) as an example. Subsequent operations are carried out with this blog scenario too. Please modify it according to your actual application scenario. Note: The table schema is based on the blog [(In Chinese) Data Transfer, Storage, Presentation, EMQX + TDengine Build MQTT IoT Data Visualization Platform](https://www.taosdata.com/blog/2020/08/04/1722.html) as an example. Subsequent operations are carried out with this blog scenario too. Please modify it according to your actual application scenario.
...@@ -81,84 +81,84 @@ Edit the resource configuration to add the key/value pairing for Authorization. ...@@ -81,84 +81,84 @@ Edit the resource configuration to add the key/value pairing for Authorization.
## Compose program to mock data ## Compose program to mock data
```javascript ```javascript
// mock.js // mock.js
const mqtt = require('mqtt') const mqtt = require('mqtt')
const Mock = require('mockjs') const Mock = require('mockjs')
const EMQX_SERVER = 'mqtt://localhost:1883' const EMQX_SERVER = 'mqtt://localhost:1883'
const CLIENT_NUM = 10 const CLIENT_NUM = 10
const STEP = 5000 // Data interval in ms const STEP = 5000 // Data interval in ms
const AWAIT = 5000 // Sleep time after data be written once to avoid data writing too fast const AWAIT = 5000 // Sleep time after data be written once to avoid data writing too fast
const CLIENT_POOL = [] const CLIENT_POOL = []
startMock() startMock()
function sleep(timer = 100) { function sleep(timer = 100) {
return new Promise(resolve => { return new Promise(resolve => {
setTimeout(resolve, timer) setTimeout(resolve, timer)
}) })
} }
async function startMock() { async function startMock() {
const now = Date.now() const now = Date.now()
for (let i = 0; i < CLIENT_NUM; i++) { for (let i = 0; i < CLIENT_NUM; i++) {
const client = await createClient(`mock_client_${i}`) const client = await createClient(`mock_client_${i}`)
CLIENT_POOL.push(client) CLIENT_POOL.push(client)
} }
// last 24h every 5s // last 24h every 5s
const last = 24 * 3600 * 1000 const last = 24 * 3600 * 1000
for (let ts = now - last; ts <= now; ts += STEP) { for (let ts = now - last; ts <= now; ts += STEP) {
for (const client of CLIENT_POOL) { for (const client of CLIENT_POOL) {
const mockData = generateMockData() const mockData = generateMockData()
const data = { const data = {
...mockData, ...mockData,
id: client.clientId, id: client.clientId,
area: 0, area: 0,
ts, ts,
}
client.publish('sensor/data', JSON.stringify(data))
}
const dateStr = new Date(ts).toLocaleTimeString()
console.log(`${dateStr} send success.`)
await sleep(AWAIT)
} }
console.log(`Done, use ${(Date.now() - now) / 1000}s`) client.publish('sensor/data', JSON.stringify(data))
}
/**
* Init a virtual mqtt client
* @param {string} clientId ClientID
*/
function createClient(clientId) {
return new Promise((resolve, reject) => {
const client = mqtt.connect(EMQX_SERVER, {
clientId,
})
client.on('connect', () => {
console.log(`client ${clientId} connected`)
resolve(client)
})
client.on('reconnect', () => {
console.log('reconnect')
})
client.on('error', (e) => {
console.error(e)
reject(e)
})
})
}
/**
* Generate mock data
*/
function generateMockData() {
return {
"temperature": parseFloat(Mock.Random.float(22, 100).toFixed(2)),
"humidity": parseFloat(Mock.Random.float(12, 86).toFixed(2)),
"volume": parseFloat(Mock.Random.float(20, 200).toFixed(2)),
"PM10": parseFloat(Mock.Random.float(0, 300).toFixed(2)),
"pm25": parseFloat(Mock.Random.float(0, 300).toFixed(2)),
"SO2": parseFloat(Mock.Random.float(0, 50).toFixed(2)),
"NO2": parseFloat(Mock.Random.float(0, 50).toFixed(2)),
"CO": parseFloat(Mock.Random.float(0, 50).toFixed(2)),
"area": Mock.Random.integer(0, 20),
"ts": 1596157444170,
}
} }
const dateStr = new Date(ts).toLocaleTimeString()
console.log(`${dateStr} send success.`)
await sleep(AWAIT)
}
console.log(`Done, use ${(Date.now() - now) / 1000}s`)
}
/**
* Init a virtual mqtt client
* @param {string} clientId ClientID
*/
function createClient(clientId) {
return new Promise((resolve, reject) => {
const client = mqtt.connect(EMQX_SERVER, {
clientId,
})
client.on('connect', () => {
console.log(`client ${clientId} connected`)
resolve(client)
})
client.on('reconnect', () => {
console.log('reconnect')
})
client.on('error', (e) => {
console.error(e)
reject(e)
})
})
}
/**
* Generate mock data
*/
function generateMockData() {
return {
"temperature": parseFloat(Mock.Random.float(22, 100).toFixed(2)),
"humidity": parseFloat(Mock.Random.float(12, 86).toFixed(2)),
"volume": parseFloat(Mock.Random.float(20, 200).toFixed(2)),
"PM10": parseFloat(Mock.Random.float(0, 300).toFixed(2)),
"pm25": parseFloat(Mock.Random.float(0, 300).toFixed(2)),
"SO2": parseFloat(Mock.Random.float(0, 50).toFixed(2)),
"NO2": parseFloat(Mock.Random.float(0, 50).toFixed(2)),
"CO": parseFloat(Mock.Random.float(0, 50).toFixed(2)),
"area": Mock.Random.integer(0, 20),
"ts": 1596157444170,
}
}
``` ```
Note: `CLIENT_NUM` in the code can be set to a smaller value at the beginning of the test to avoid hardware performance be not capable to handle a more significant number of concurrent clients. Note: `CLIENT_NUM` in the code can be set to a smaller value at the beginning of the test to avoid hardware performance be not capable to handle a more significant number of concurrent clients.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册