Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
F
fake terminal
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
f.wang
fake terminal
Commits
93cb8905
提交
93cb8905
authored
10月 17, 2022
作者:
zxd
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
发送mqtt消息
上级
22bc57a0
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
130 行增加
和
17 行删除
+130
-17
emq_terminal.json
emq_terminal.json
+31
-0
fake_terminal.py
fake_terminal.py
+99
-17
没有找到文件。
emq_terminal.json
浏览文件 @
93cb8905
...
...
@@ -13,5 +13,35 @@
"port_address"
:
1
}
]
},
{
"emq_host"
:
"139.224.69.89"
,
"emq_port"
:
31101
,
"serial_no"
:
"VT00002"
,
"measurements"
:
[
{
"measurement"
:
"door"
,
"type"
:
0
,
"random_type"
:
1
,
"port"
:
"c"
,
"port_address"
:
1
}
]
},
{
"emq_host"
:
"139.224.69.89"
,
"emq_port"
:
31101
,
"serial_no"
:
"VT00003"
,
"measurements"
:
[
{
"measurement"
:
"door"
,
"type"
:
0
,
"random_type"
:
1
,
"port"
:
"c"
,
"port_address"
:
1
}
]
}
]
\ No newline at end of file
fake_terminal.py
浏览文件 @
93cb8905
# encoding=utf8
import
json
import
random
import
sys
from
paho.mqtt
import
client
as
mqtt_client
########################
# 虚拟终端上报数据脚本
########################
import
time
client_id
=
f
'python-mqtt-{random.randint(0, 1000)}'
topic
=
"wftest"
username
=
"admin"
password
=
"public"
cmdTemplate
=
"""
{{
"devSN": "{devSN}",
"data": {{
"offline": false,
"telemetry": {telemetry}
}},
"time": {time},
"mqttTopic": "/{topic}/{devSn1}/lalala",
"type": "data",
"deviceName": "105104012101126339",
"group": "saaspri",
"network": "wifi",
"middleware": "hub"
}}
"""
telemetryTemplate
=
"""
{{
"port": "{port}",
"portAddress": {protAddress},
"time": [
{time}
],
"value": {{
"{measurement}": [
{value}
]
}}
}}
"""
def
getTelemetry
(
param
):
t
=
time
.
time
()
l
=
len
(
param
)
tjsons
=
[]
for
i
in
range
(
l
):
dataT
=
telemetryTemplate
.
format
(
port
=
param
[
i
]
.
get
(
"port"
),
protAddress
=
param
[
i
]
.
get
(
"port_address"
),
time
=
str
(
t
),
measurement
=
param
[
i
]
.
get
(
"measurement"
),
value
=
str
(
random
.
randint
(
0
,
1000
)))
tJson
=
json
.
loads
(
dataT
)
tjsons
.
append
(
tJson
)
return
json
.
dumps
(
tjsons
)
def
connect_mqtt
(
ip
,
port
):
def
on_connect
(
client
,
userdata
,
flags
,
rc
):
if
rc
==
0
:
print
(
"Connected to MQTT Broker!"
)
else
:
print
(
"Failed to connect, return code
%
d
\n
"
,
rc
)
client
=
mqtt_client
.
Client
(
client_id
)
client
.
username_pw_set
(
username
,
password
)
client
.
on_connect
=
on_connect
client
.
connect
(
ip
,
port
)
return
client
def
publish
(
client
,
msg
):
result
=
client
.
publish
(
topic
,
msg
)
# result: [0, 1]
status
=
result
[
0
]
if
status
==
0
:
print
(
f
"Send `{msg}` to topic `{topic}`"
)
else
:
print
(
f
"Failed to send message to topic {topic}"
)
def
run
(
ip
,
port
,
msg
):
client
=
connect_mqtt
(
ip
=
ip
,
port
=
port
)
client
.
loop_start
()
publish
(
client
,
msg
)
terminal_confs
=
[
{
"emq_host"
:
""
,
"emq_port"
:
11883
,
"serial_no"
:
"v000000001"
,
"measurements"
:
[
{
"measurement"
:
"door"
,
"type"
:
0
,
"random_type"
:
1
,
"port"
:
"c"
,
"port_address"
:
1
}
]
},
]
if
__name__
==
'__main__'
:
jsonFile
=
"emq_terminal.json"
while
(
True
):
with
open
(
jsonFile
,
"r"
)
as
fp
:
datas
=
json
.
load
(
fp
)
dataLen
=
len
(
datas
)
for
key
in
range
(
dataLen
):
data
=
datas
[
key
]
tel
=
getTelemetry
(
data
.
get
(
"measurements"
))
cmd
=
cmdTemplate
.
format
(
devSN
=
data
.
get
(
"serial_no"
),
telemetry
=
tel
,
time
=
str
(
time
.
time
()),
devSn1
=
data
.
get
(
"serial_no"
),
topic
=
topic
)
print
(
cmd
)
run
(
data
.
get
(
"emq_host"
),
data
.
get
(
"emq_port"
),
cmd
)
time
.
sleep
(
10
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论