新版模拟器
This commit is contained in:
parent
a0465f79b3
commit
af364756d7
Binary file not shown.
Binary file not shown.
|
|
@ -0,0 +1 @@
|
||||||
|
reload --script mqtt/benchmark.js
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
1669884337156:benchmark mqtt --size 100
|
||||||
|
|
@ -0,0 +1,116 @@
|
||||||
|
/**
|
||||||
|
* JetLinks mqtt 官方协议模拟器
|
||||||
|
* benchmark mqtt --host=127.0.0.1 --port=8801 --script=mqtt/benchmark.js report=true reportLimit=100 interval=1000
|
||||||
|
*/
|
||||||
|
|
||||||
|
//绑定内置参数,否则匿名函数无法使用。
|
||||||
|
var $benchmark = benchmark;
|
||||||
|
|
||||||
|
//在jetlinks平台的产品ID
|
||||||
|
var productId = args.getOrDefault("productId", "simulator");
|
||||||
|
var deviceIdPrefix = args.getOrDefault("deviceIdPrefix", "mqtt-test-");
|
||||||
|
|
||||||
|
var $enableReport = "true" === args.getOrDefault("report", "true");
|
||||||
|
var $reportLimit = parseInt(args.getOrDefault("reportLimit", "100"));
|
||||||
|
var $reportInterval = parseInt(args.getOrDefault("interval", "1000"));
|
||||||
|
|
||||||
|
|
||||||
|
//创建连接之前动态生成用户名密码
|
||||||
|
function beforeConnect(index, options) {
|
||||||
|
var clientId = deviceIdPrefix + index;
|
||||||
|
|
||||||
|
var secureId = "test";
|
||||||
|
var secureKey = "test";
|
||||||
|
|
||||||
|
var username = secureId + "|" + now();
|
||||||
|
var password = md5(username + "|" + secureKey);
|
||||||
|
|
||||||
|
options.setUsername(username);
|
||||||
|
options.setPassword(password);
|
||||||
|
options.setClientId(clientId);
|
||||||
|
}
|
||||||
|
|
||||||
|
//全部连接完成后执行
|
||||||
|
function onComplete() {
|
||||||
|
if (!$enableReport) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//定时执行1s
|
||||||
|
$benchmark
|
||||||
|
.interval(function () {
|
||||||
|
//随机获取1000个连接然后上报属性数据
|
||||||
|
$benchmark.print("批量上报属性..");
|
||||||
|
return $benchmark
|
||||||
|
.randomConnectionAsync($reportLimit, reportProperties);
|
||||||
|
}, $reportInterval)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function reportProperties(client) {
|
||||||
|
//创建随机数据
|
||||||
|
var data = {};
|
||||||
|
// $benchmark.print("上报[" + client.getId() + "]属性");
|
||||||
|
for (let i = 0; i < 10; i++) {
|
||||||
|
data["temp" + i] = randomFloat(10, 30);
|
||||||
|
}
|
||||||
|
var msg = {
|
||||||
|
"properties": data
|
||||||
|
}
|
||||||
|
|
||||||
|
//推送mqtt
|
||||||
|
return client.publishAsync(createTopic(client, "/properties/read/report"), 0, $benchmark.toJson(msg));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//单个连接创建成功时执行
|
||||||
|
function onConnected(client) {
|
||||||
|
|
||||||
|
//订阅读取属性
|
||||||
|
client
|
||||||
|
.subscribe(createTopic(client, "/properties/read"),
|
||||||
|
0,
|
||||||
|
function (msg) {
|
||||||
|
handleReadProperty(client, msg.payload().toJsonObject())
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//根据jetlinks官方协议topic规则创建topic
|
||||||
|
function createTopic(client, topic) {
|
||||||
|
return "/" + productId + "/" + client.getId() + topic;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function handleReadProperty(client, msg) {
|
||||||
|
var messageId = msg.getString("messageId");
|
||||||
|
var properties = msg.getJsonArray("properties");
|
||||||
|
|
||||||
|
$benchmark.print("读取[" + client.getId() + "]属性:" + msg);
|
||||||
|
|
||||||
|
//创建随机数据
|
||||||
|
var data = {};
|
||||||
|
properties.forEach(function (property) {
|
||||||
|
//随机数据
|
||||||
|
data[property] = randomFloat(10, 30);
|
||||||
|
});
|
||||||
|
|
||||||
|
//构造回复数据
|
||||||
|
var reply = {
|
||||||
|
"messageId": messageId,
|
||||||
|
"properties": data
|
||||||
|
}
|
||||||
|
//推送mqtt
|
||||||
|
doPublish(client, "/properties/read/reply", reply)
|
||||||
|
}
|
||||||
|
|
||||||
|
function doPublish(client, topic, payload) {
|
||||||
|
//推送mqtt
|
||||||
|
client.publish(createTopic(client, topic), 0, $benchmark.toJson(payload));
|
||||||
|
}
|
||||||
|
|
||||||
|
//重点! 绑定函数到benchmark
|
||||||
|
benchmark
|
||||||
|
.beforeConnect(beforeConnect)
|
||||||
|
.onConnected(onConnected)
|
||||||
|
.onComplete(onComplete);
|
||||||
|
|
@ -1,77 +0,0 @@
|
||||||
/**
|
|
||||||
* 子设备消息模拟,请在启动脚本start.sh中自行修改scriptFile选项
|
|
||||||
*
|
|
||||||
* 在平台中创建对应的设备实例:
|
|
||||||
*
|
|
||||||
* 父设备ID使用: gateway-1
|
|
||||||
* 子设备ID使用: child-device-1
|
|
||||||
* 型号使用演示型号.
|
|
||||||
* 协议使用最新的demo-protocol-1.0.jar,源代码地址: https://github.com/jetlinks/demo-protocol
|
|
||||||
*/
|
|
||||||
var _logger = logger;
|
|
||||||
|
|
||||||
//事件类型
|
|
||||||
var events = {
|
|
||||||
reportProperty: function (index, session) {
|
|
||||||
var deviceId = "child-device-1";
|
|
||||||
var topic = "/children/report-property";
|
|
||||||
var json = JSON.stringify({
|
|
||||||
"deviceId": deviceId,
|
|
||||||
"success": true,
|
|
||||||
"timestamp": new Date().getTime(),
|
|
||||||
properties: {"temperature": java.util.concurrent.ThreadLocalRandom.current().nextDouble(20, 40)},
|
|
||||||
});
|
|
||||||
session.sendMessage(topic, json)
|
|
||||||
},
|
|
||||||
fireAlarm: function (index, session) {
|
|
||||||
var deviceId = "child-device-1";
|
|
||||||
var topic = "/children/fire_alarm";
|
|
||||||
var json = JSON.stringify({
|
|
||||||
"deviceId": deviceId, // 设备编号 "pid": "TBS-110", // 设备编号
|
|
||||||
"a_name": "商务大厦", // 区域名称 "bid": 2, // 建筑 ID
|
|
||||||
"b_name": "C2 栋", // 建筑名称
|
|
||||||
"l_name": "12-05-201", // 位置名称
|
|
||||||
"timestamp": new Date().getTime() // 消息时间
|
|
||||||
});
|
|
||||||
|
|
||||||
session.sendMessage(topic, json)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
//事件上报
|
|
||||||
simulator.onEvent(function (index, session) {
|
|
||||||
//上报属性
|
|
||||||
events.reportProperty(index, session);
|
|
||||||
|
|
||||||
//上报火警
|
|
||||||
events.fireAlarm(index, session);
|
|
||||||
});
|
|
||||||
|
|
||||||
simulator.bindHandler("/children/read-property", function (message, session) {
|
|
||||||
_logger.info("读取子设备属性:[{}]", message);
|
|
||||||
session.sendMessage("/read-property-reply", JSON.stringify({
|
|
||||||
messageId: message.messageId,
|
|
||||||
deviceId: message.deviceId,
|
|
||||||
timestamp: new Date().getTime(),
|
|
||||||
properties: {"temperature": java.util.concurrent.ThreadLocalRandom.current().nextDouble(20, 40)},
|
|
||||||
success: true
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
simulator.onConnect(function (session) {
|
|
||||||
//模拟子设备上线
|
|
||||||
session.sendMessage("/children/device_online_status", JSON.stringify({
|
|
||||||
deviceId: "child-device-1",
|
|
||||||
timestamp: new Date().getTime(),
|
|
||||||
status: "1",
|
|
||||||
success: true
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
|
|
||||||
simulator.onAuth(function (index, auth) {
|
|
||||||
//使用网关设备id 连接平台
|
|
||||||
auth.setClientId("gateway-1" );
|
|
||||||
auth.setUsername("admin");
|
|
||||||
auth.setPassword("admin");
|
|
||||||
});
|
|
||||||
|
|
@ -1,119 +0,0 @@
|
||||||
/**
|
|
||||||
* 烟感设备模拟器
|
|
||||||
*/
|
|
||||||
var _logger = logger;
|
|
||||||
//设备实例id前缀
|
|
||||||
var devicePrefix = "demo-";
|
|
||||||
|
|
||||||
var eventId = Math.ceil(Math.random() * 1000);
|
|
||||||
//事件类型
|
|
||||||
var events = {
|
|
||||||
reportProperty: function (index, session) {
|
|
||||||
var deviceId = session.auth.clientId;
|
|
||||||
var topic = "/report-property";
|
|
||||||
var json = JSON.stringify({
|
|
||||||
"deviceId": deviceId,
|
|
||||||
"success": true,
|
|
||||||
"timestamp": new Date().getTime(),
|
|
||||||
properties: {"temperature": java.util.concurrent.ThreadLocalRandom.current().nextInt(20, 30)},
|
|
||||||
});
|
|
||||||
session.sendMessage(topic, json)
|
|
||||||
},
|
|
||||||
fireAlarm: function (index, session) {
|
|
||||||
var deviceId = session.auth.clientId;
|
|
||||||
var topic = "/fire_alarm/department/1/area/1/dev/" + deviceId;
|
|
||||||
var json = JSON.stringify({
|
|
||||||
"deviceId": deviceId, // 设备编号 "pid": "TBS-110", // 设备编号
|
|
||||||
"a_name": "商务大厦", // 区域名称 "bid": 2, // 建筑 ID
|
|
||||||
"b_name": "C2 栋", // 建筑名称
|
|
||||||
"l_name": "12-05-201", // 位置名称
|
|
||||||
"timestamp": new Date().getTime() // 消息时间
|
|
||||||
});
|
|
||||||
|
|
||||||
session.sendMessage(topic, json)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
//事件上报
|
|
||||||
simulator.onEvent(function (index, session) {
|
|
||||||
//上报属性
|
|
||||||
events.reportProperty(index, session);
|
|
||||||
|
|
||||||
//上报火警
|
|
||||||
events.fireAlarm(index, session);
|
|
||||||
});
|
|
||||||
|
|
||||||
//读取属性
|
|
||||||
simulator.bindHandler("/read-property", function (message, session) {
|
|
||||||
_logger.info("读取属性:[{}]", message);
|
|
||||||
session.sendMessage("/read-property-reply", JSON.stringify({
|
|
||||||
messageId: message.messageId,
|
|
||||||
deviceId: message.deviceId,
|
|
||||||
timestamp: new Date().getTime(),
|
|
||||||
properties: {"temperature": java.util.concurrent.ThreadLocalRandom.current().nextInt(20, 30)},
|
|
||||||
success: true
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
|
|
||||||
//读取子设备属性
|
|
||||||
simulator.bindHandler("/children/read-property", function (message, session) {
|
|
||||||
_logger.info("读取子设备属性:[{}]", message);
|
|
||||||
session.sendMessage("/children/read-property-reply", JSON.stringify({
|
|
||||||
messageId: message.messageId,
|
|
||||||
deviceId: message.deviceId,
|
|
||||||
timestamp: new Date().getTime(),
|
|
||||||
properties: {"temperature": java.util.concurrent.ThreadLocalRandom.current().nextInt(20, 30)},
|
|
||||||
success: true
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
|
|
||||||
//调用功能
|
|
||||||
simulator.bindHandler("/invoke-function", function (message, session) {
|
|
||||||
_logger.info("调用功能:[{}]", message);
|
|
||||||
session.sendMessage("/invoke-function", JSON.stringify({
|
|
||||||
messageId: message.messageId,
|
|
||||||
deviceId: message.deviceId,
|
|
||||||
timestamp: new Date().getTime(),
|
|
||||||
output: "ok", //返回结果
|
|
||||||
success: true
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
|
|
||||||
//修改属性
|
|
||||||
simulator.bindHandler("/write-property", function (message, session) {
|
|
||||||
var reply = com.alibaba.fastjson.JSON.toJSONString({
|
|
||||||
messageId: message.messageId,
|
|
||||||
deviceId: message.deviceId,
|
|
||||||
timestamp: new Date().getTime(),
|
|
||||||
properties: new java.util.HashMap(message.properties),
|
|
||||||
success: true
|
|
||||||
});
|
|
||||||
_logger.info("修改属性:{}\n{}", message,reply);
|
|
||||||
|
|
||||||
session.sendMessage("/write-property",reply);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
simulator.onConnect(function (session) {
|
|
||||||
//自动绑定下级设备
|
|
||||||
// session.sendMessage("/children/register", JSON.stringify({
|
|
||||||
// deviceId: "test202278", //子设备ID
|
|
||||||
// timestamp: new Date().getTime(),
|
|
||||||
// success: true
|
|
||||||
// }));
|
|
||||||
//注销子设备
|
|
||||||
// simulator.runDelay(function () {
|
|
||||||
// session.sendMessage("/children/unregister", JSON.stringify({
|
|
||||||
// deviceId: "test202278",
|
|
||||||
// timestamp: new Date().getTime(),
|
|
||||||
// success: true
|
|
||||||
// }));
|
|
||||||
// },2000)
|
|
||||||
});
|
|
||||||
|
|
||||||
simulator.onAuth(function (index, auth) {
|
|
||||||
|
|
||||||
auth.setClientId(devicePrefix + index);
|
|
||||||
auth.setUsername("admin");
|
|
||||||
auth.setPassword("admin");
|
|
||||||
});
|
|
||||||
|
|
@ -1,13 +1,5 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
java -jar -Dfile.encoding=UTF-8 device-simulator.jar \
|
java -jar -Dfile.encoding=UTF-8 device-simulator.jar
|
||||||
mqtt.limit=1 \
|
|
||||||
mqtt.start=0 \
|
|
||||||
mqtt.enableEvent=true \
|
|
||||||
mqtt.eventLimit=1 \
|
|
||||||
mqtt.eventRate=1000 \
|
|
||||||
mqtt.scriptFile=./scripts/demo-device.js \
|
|
||||||
mqtt.address=127.0.0.1 \
|
|
||||||
mqtt.port=1883
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
{"id":"demo-device","name":"演示设备","describe":"","messageProtocol":"demo-v1","metadata":"{\"properties\":[{\"id\":\"temperature\",\"name\":\"温度\",\"dataType\":\"float\",\"valueType\":{\"min\":\"0\",\"max\":\"100\",\"step\":\"0.1\",\"unit\":\"celsiusDegrees\",\"type\":\"float\"},\"expands\":{\"readOnly\":\"true\",\"report\":\"true\"},\"description\":\"\"}],\"functions\":[],\"events\":[{\"id\":\"fire_alarm\",\"name\":\"火警报警\",\"dataType\":\"object\",\"valueType\":{\"properties\":[{\"id\":\"a_name\",\"name\":\"区域名称\",\"dataType\":\"string\",\"valueType\":{\"expands\":{\"maxLength\":\"2048\"},\"type\":\"string\"},\"description\":\"\"},{\"id\":\"b_name\",\"name\":\"建筑名称\",\"dataType\":\"string\",\"valueType\":{\"expands\":{\"maxLength\":\"2048\"},\"type\":\"string\"},\"description\":\"\"},{\"id\":\"l_name\",\"name\":\"位置名称\",\"dataType\":\"string\",\"valueType\":{\"expands\":{\"maxLength\":\"2048\"},\"type\":\"string\"},\"description\":\"\"}],\"type\":\"object\"},\"expands\":{\"level\":\"urgent\",\"eventType\":\"reportEvent\"},\"description\":\"\"}]}","transportProtocol":"MQTT","deviceType":{"text":"设备","value":"device"},"configuration":{"username":"admin","password":"admin"},"state":0,"creatorId":"admin","createTime":1579265291769,"orgId":"","_id":3,"_uid":3}
|
|
||||||
Loading…
Reference in New Issue