Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
黄嘉阳
/
iot-mqtt-protocol
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
c5305438
authored
Nov 30, 2023
by
huangjy
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
mqtt协议包,功能调用toic
parent
13f928ae
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
107 additions
and
10 deletions
src/main/java/org/jetlinks/protocol/official/JetLinksMqttDeviceMessageCodec.java
src/main/java/org/jetlinks/protocol/official/TopicMessageCodec.java
src/main/java/org/jetlinks/protocol/official/entity/CustomFunctionInvokeMessage.java
src/main/java/org/jetlinks/protocol/official/entity/CustomFunctionMessage.java
src/main/java/org/jetlinks/protocol/official/JetLinksMqttDeviceMessageCodec.java
View file @
c5305438
...
...
@@ -14,6 +14,8 @@ import org.jetlinks.core.message.DisconnectDeviceMessage;
import
org.jetlinks.core.message.Message
;
import
org.jetlinks.core.message.codec.*
;
import
org.jetlinks.core.message.firmware.UpgradeFirmwareMessage
;
import
org.jetlinks.core.message.function.FunctionInvokeMessage
;
import
org.jetlinks.core.message.function.FunctionParameter
;
import
org.jetlinks.core.message.property.ReadPropertyMessage
;
import
org.jetlinks.core.message.property.WritePropertyMessage
;
import
org.jetlinks.protocol.official.entity.*
;
...
...
@@ -22,6 +24,7 @@ import reactor.core.publisher.Mono;
import
javax.annotation.Nonnull
;
import
java.io.UnsupportedEncodingException
;
import
java.nio.charset.StandardCharsets
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
...
...
@@ -176,6 +179,30 @@ public class JetLinksMqttDeviceMessageCodec implements DeviceMessageCodec {
customUpgradeFirmwareMessage
.
setMessageId
(
deviceMessage
.
getMessageId
());
customUpgradeFirmwareMessage
.
setDeviceId
(
deviceMessage
.
getDeviceId
());
convertResult
=
TopicMessageCodec
.
encode
(
mapper
,
customUpgradeFirmwareMessage
);
}
else
if
(
deviceMessage
instanceof
FunctionInvokeMessage
)
{
CustomFunctionInvokeMessage
functionInvokeMessage
=
new
CustomFunctionInvokeMessage
();
CustomFunctionInvokeMessage
.
Header
header
=
new
CustomFunctionInvokeMessage
.
Header
();
header
.
setTimestamp
(
deviceMessage
.
getTimestamp
());
header
.
setVersion
(
1
);
header
.
setUuid
(
deviceMessage
.
getMessageId
());
functionInvokeMessage
.
setHeader
(
header
);
CustomFunctionInvokeMessage
.
PayLoad
payLoad
=
new
CustomFunctionInvokeMessage
.
PayLoad
();
CustomFunctionInvokeMessage
.
Inputs
inputs
=
new
CustomFunctionInvokeMessage
.
Inputs
();
CustomFunctionInvokeMessage
.
Value
value
=
new
CustomFunctionInvokeMessage
.
Value
();
List
<
FunctionParameter
>
functionParameterList
=
((
FunctionInvokeMessage
)
deviceMessage
).
getInputs
();
if
(
functionParameterList
!=
null
&&
!
functionParameterList
.
isEmpty
())
{
FunctionParameter
functionParameter
=
functionParameterList
.
get
(
0
);
inputs
.
setName
(
functionParameter
.
getName
());
inputs
.
setValue
(
functionParameter
.
getValue
());
}
payLoad
.
setFunctionId
(((
FunctionInvokeMessage
)
deviceMessage
).
getFunctionId
());
payLoad
.
setInputs
(
inputs
);
functionInvokeMessage
.
setPayload
(
payLoad
);
functionInvokeMessage
.
setTimestamp
(
deviceMessage
.
getTimestamp
());
functionInvokeMessage
.
setMessageId
(
deviceMessage
.
getMessageId
());
functionInvokeMessage
.
setDeviceId
(((
DeviceMessage
)
message
).
getDeviceId
());
convertResult
=
TopicMessageCodec
.
encode
(
mapper
,
functionInvokeMessage
);
}
else
{
convertResult
=
TopicMessageCodec
.
encode
(
mapper
,
deviceMessage
);
}
...
...
@@ -232,7 +259,6 @@ public class JetLinksMqttDeviceMessageCodec implements DeviceMessageCodec {
}
else
{
propertiesMap
.
put
(
key
,
value
);
}
}
map
.
put
(
"properties"
,
propertiesMap
);
map
.
put
(
"success"
,
true
);
...
...
@@ -260,17 +286,20 @@ public class JetLinksMqttDeviceMessageCodec implements DeviceMessageCodec {
String
properties
=
customMessage
.
getPayload
().
getProperties
();
map
.
put
(
"properties"
,
JSON
.
parseObject
(
properties
));
result
=
JSON
.
toJSONString
(
map
);
}
else
if
(
message
.
getTopic
().
contains
(
"function/invoke"
))
{
CustomFunctionMessage
customFunctionMessage
=
JSON
.
parseObject
(
body
,
CustomFunctionMessage
.
class
);
CustomFunctionMessage
.
PayLoad
payload
=
customFunctionMessage
.
getPayload
();
map
.
put
(
"messageId"
,
customFunctionMessage
.
getHeader
().
getUuid
());
map
.
put
(
"timestamp"
,
System
.
currentTimeMillis
());
if
(
payload
.
getOutput
().
getRetCode
()
==
1
)
{
map
.
put
(
"output"
,
payload
.
getOutput
().
getValue
());
map
.
put
(
"success"
,
true
);
}
result
=
JSON
.
toJSONString
(
map
);
}
else
{
result
=
body
;
}
byte
[]
bytes
;
try
{
bytes
=
result
.
getBytes
(
"UTF-8"
);
}
catch
(
UnsupportedEncodingException
e
)
{
throw
new
RuntimeException
(
e
);
}
byte
[]
payload
=
bytes
;
byte
[]
payload
=
result
.
getBytes
(
StandardCharsets
.
UTF_8
);
return
TopicMessageCodec
.
decode
(
mapper
,
TopicMessageCodec
.
removeProductPath
(
message
.
getTopic
()),
payload
)
//如果不能直接解码,可能是其他设备功能
...
...
src/main/java/org/jetlinks/protocol/official/TopicMessageCodec.java
View file @
c5305438
...
...
@@ -13,6 +13,7 @@ import org.jetlinks.core.message.state.DeviceStateCheckMessage;
import
org.jetlinks.core.message.state.DeviceStateCheckMessageReply
;
import
org.jetlinks.core.route.MqttRoute
;
import
org.jetlinks.core.utils.TopicUtils
;
import
org.jetlinks.protocol.official.entity.CustomFunctionInvokeMessage
;
import
org.jetlinks.protocol.official.entity.CustomReadMessage
;
import
org.jetlinks.protocol.official.entity.CustomUpgradeFirmwareMessage
;
import
org.jetlinks.protocol.official.entity.CustomWriteMessage
;
...
...
@@ -106,7 +107,7 @@ public enum TopicMessageCodec {
//调用功能
functionInvoke
(
"/*/function/invoke"
,
FunctionInvokeMessage
.
class
,
Custom
FunctionInvokeMessage
.
class
,
route
->
route
.
upstream
(
false
)
.
downstream
(
true
)
...
...
src/main/java/org/jetlinks/protocol/official/entity/CustomFunctionInvokeMessage.java
0 → 100644
View file @
c5305438
package
org
.
jetlinks
.
protocol
.
official
.
entity
;
import
lombok.Data
;
import
org.jetlinks.core.message.function.FunctionInvokeMessage
;
@Data
public
class
CustomFunctionInvokeMessage
extends
FunctionInvokeMessage
{
private
CustomFunctionInvokeMessage
.
Header
header
;
private
CustomFunctionInvokeMessage
.
PayLoad
payload
;
@Data
public
static
class
Header
{
private
int
version
;
private
String
uuid
;
private
Long
timestamp
;
}
@Data
public
static
class
PayLoad
{
private
String
functionId
;
private
Inputs
inputs
;
}
@Data
public
static
class
Inputs
{
private
String
name
;
private
Object
value
;
}
@Data
public
static
class
Value
{
private
String
url
;
private
Long
timestamp
;
}
}
src/main/java/org/jetlinks/protocol/official/entity/CustomFunctionMessage.java
0 → 100644
View file @
c5305438
package
org
.
jetlinks
.
protocol
.
official
.
entity
;
import
lombok.Data
;
@Data
public
class
CustomFunctionMessage
{
private
Header
header
;
private
PayLoad
payload
;
@Data
public
static
class
Header
{
private
String
version
;
private
String
uuid
;
private
Long
timestamp
;
}
@Data
public
static
class
PayLoad
{
private
Output
output
;
}
@Data
public
static
class
Output
{
private
Integer
retCode
;
private
String
value
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
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 comment