Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation
This project
Loading...
Sign in
李苗
/
Vion-DevOps
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 66424764
authored
Nov 01, 2024
by
姚冰
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
[add] 设备离线添加处理入库
1 parent
cdf7edc0
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
66 additions
and
7 deletions
src/main/java/vion/constant/EventStatusEnum.java
src/main/java/vion/constant/MqttMessageType.java
src/main/java/vion/dto/OfflineDevice.java
src/main/java/vion/event/mqtt/MqttClientMessageListener.java
src/main/java/vion/constant/EventStatusEnum.java
0 → 100644
View file @
6642476
package
vion
.
constant
;
public
enum
EventStatusEnum
{
UNKNOWN
((
short
)
-
1
,
"未知"
),
SUCCESS
((
short
)
1
,
"成功"
),
FAIL
((
short
)
0
,
"失败"
),
;
private
final
Short
code
;
private
final
String
desc
;
EventStatusEnum
(
Short
code
,
String
desc
)
{
this
.
code
=
code
;
this
.
desc
=
desc
;
}
public
Short
getCode
()
{
return
code
;
}
public
String
getDesc
()
{
return
desc
;
}
}
src/main/java/vion/constant/MqttMessageType.java
View file @
6642476
...
...
@@ -19,7 +19,7 @@ public enum MqttMessageType {
public
static
MqttMessageType
getEnumByType
(
String
type
)
{
return
Arrays
.
stream
(
values
())
.
filter
(
e
->
e
.
type
==
type
)
.
filter
(
e
->
e
.
type
.
equals
(
type
)
)
.
findFirst
()
.
orElse
(
null
);
}
...
...
src/main/java/vion/dto/OfflineDevice.java
0 → 100644
View file @
6642476
package
vion
.
dto
;
import
lombok.Data
;
@Data
public
class
OfflineDevice
{
private
String
mallUid
;
private
String
devices
;
}
src/main/java/vion/event/mqtt/MqttClientMessageListener.java
View file @
6642476
package
vion
.
event
.
mqtt
;
import
com.baomidou.mybatisplus.core.toolkit.StringUtils
;
import
com.fasterxml.jackson.databind.JsonNode
;
import
net.dreamlu.iot.mqtt.codec.MqttPublishMessage
;
import
net.dreamlu.iot.mqtt.core.client.IMqttClientMessageListener
;
import
net.dreamlu.iot.mqtt.spring.client.MqttClientSubscribe
;
import
org.dromara.hutool.json.JSON
;
import
org.json.JSONArray
;
import
org.json.JSONObject
;
import
org.dromara.hutool.core.bean.BeanUtil
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.tio.core.ChannelContext
;
import
vion.constant.EventStatusEnum
;
import
vion.constant.MqttMessageType
;
import
vion.dto.OfflineDevice
;
import
vion.model.monitor.EventRecord
;
import
vion.service.monitor.IEventRecordService
;
import
vion.utils.JsonUtil
;
import
java.nio.charset.StandardCharsets
;
import
java.util.List
;
import
static
vion
.
constant
.
MqttMessageType
.
WHOLEDAYA_ANALYZE
;
...
...
@@ -28,21 +33,26 @@ import static vion.constant.MqttMessageType.WHOLEDAYA_ANALYZE;
public
class
MqttClientMessageListener
implements
IMqttClientMessageListener
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
MqttClientMessageListener
.
class
);
@Autowired
private
IEventRecordService
recordService
;
@Override
public
void
onMessage
(
ChannelContext
context
,
String
topic
,
MqttPublishMessage
message
,
byte
[]
payload
)
{
String
payloadStr
=
new
String
(
payload
,
StandardCharsets
.
UTF_8
);
logger
.
info
(
"topic:{} payload:{}"
,
topic
,
payloadStr
);
try
{
J
SONObject
object
=
new
JSONObject
(
payloadStr
);
String
type
=
object
.
get
String
(
"type"
);
J
sonNode
object
=
JsonUtil
.
parseTree
(
payloadStr
);
String
type
=
object
.
get
(
"eventType"
).
asText
(
);
switch
(
MqttMessageType
.
getEnumByType
(
type
))
{
case
MqttMessageType
.
DEVICE_OFFLINE
:
//设备离线
handleDeviceOffline
(
payloadStr
);
break
;
case
MqttMessageType
.
WHOLEDAYA_ANALYZE
:
//全天分析异常
handleWholeDayAnalyze
(
payloadStr
);
break
;
default
:
logger
.
info
(
"未定义的消息类型:{}, payload:{}"
,
type
,
payloadStr
);
...
...
@@ -56,7 +66,23 @@ public class MqttClientMessageListener implements IMqttClientMessageListener {
//设备离线上报
private
void
handleDeviceOffline
(
String
payloadStr
)
{
EventRecord
eventRecord
=
JsonUtil
.
parseObject
(
payloadStr
,
EventRecord
.
class
);
// JSONArray arrary = (JSONArray) eventRecord.getData();
String
list
=
(
String
)
eventRecord
.
getData
();
List
<
OfflineDevice
>
deviceList
=
JsonUtil
.
parseArray
(
list
,
OfflineDevice
.
class
);
for
(
int
i
=
0
;
i
<
deviceList
.
size
();
i
++)
{
OfflineDevice
device
=
deviceList
.
get
(
i
);
EventRecord
eventRecord1
=
new
EventRecord
();
BeanUtil
.
copyProperties
(
eventRecord
,
eventRecord1
);
eventRecord1
.
setEventUid
(
eventRecord
.
getEventUid
());
eventRecord1
.
setData
(
JsonUtil
.
toJsonString
(
device
));
eventRecord1
.
setMallUid
(
deviceList
.
get
(
i
).
getMallUid
());
if
(
StringUtils
.
isEmpty
(
device
.
getDevices
()))
{
eventRecord1
.
setStatus
(
EventStatusEnum
.
SUCCESS
.
getCode
());
}
else
{
eventRecord1
.
setStatus
(
EventStatusEnum
.
FAIL
.
getCode
());
}
//入库
recordService
.
save
(
eventRecord1
);
}
// logger.info("设备离线:{}", JSON.toJSONString(eventRecord));
}
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment