Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
I
ioc_sixiang_license
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zengtianlai3
ioc_sixiang_license
Commits
b5ed11ec
Commit
b5ed11ec
authored
Jun 07, 2022
by
zengtianlai3
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
协议的data和end字段单独解析出来
parent
789b2919
Changes
13
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
447 additions
and
17 deletions
+447
-17
Consts.java
license/src/main/java/iot/sixiang/license/consts/Consts.java
+1
-1
DeviceDecoder.java
...c/main/java/iot/sixiang/license/device/DeviceDecoder.java
+11
-4
DeviceEncoder.java
...c/main/java/iot/sixiang/license/device/DeviceEncoder.java
+1
-0
DeviceProtocol.java
.../main/java/iot/sixiang/license/device/DeviceProtocol.java
+3
-1
DeviceServerHandler.java
.../java/iot/sixiang/license/device/DeviceServerHandler.java
+10
-2
DeviceClientLicenseEvent.java
...a/iot/sixiang/license/event/DeviceClientLicenseEvent.java
+10
-0
DeviceClientLicenseEventHandler.java
...ixiang/license/event/DeviceClientLicenseEventHandler.java
+43
-0
ForwardMessageResponseEventHandler.java
...ang/license/event/ForwardMessageResponseEventHandler.java
+9
-5
ForwardClientHandler.java
...ava/iot/sixiang/license/forward/ForwardClientHandler.java
+3
-1
ForwardConnectionListener.java
...ot/sixiang/license/forward/ForwardConnectionListener.java
+26
-0
ForwardDecoder.java
...main/java/iot/sixiang/license/forward/ForwardDecoder.java
+11
-3
ForwardEncoder.java
...main/java/iot/sixiang/license/forward/ForwardEncoder.java
+1
-0
Util.java
license/src/main/java/iot/sixiang/license/util/Util.java
+318
-0
No files found.
license/src/main/java/iot/sixiang/license/consts/Consts.java
View file @
b5ed11ec
...
@@ -3,7 +3,7 @@ package iot.sixiang.license.consts;
...
@@ -3,7 +3,7 @@ package iot.sixiang.license.consts;
public
class
Consts
{
public
class
Consts
{
public
static
final
int
CMD_LICENSE
=
1
6
;
//授权消息的cmd,十进制
public
static
final
int
CMD_LICENSE
=
1
;
//授权消息的cmd,十进制
public
static
final
int
EXECUTOR_THREAD_NUM
=
30
;
public
static
final
int
EXECUTOR_THREAD_NUM
=
30
;
public
static
final
Integer
DEVICE_STATE_ONLINE
=
1
;
// 设备在线
public
static
final
Integer
DEVICE_STATE_ONLINE
=
1
;
// 设备在线
public
static
final
Integer
DEVICE_STATE_OFFLINE
=
0
;
// 设备离线
public
static
final
Integer
DEVICE_STATE_OFFLINE
=
0
;
// 设备离线
...
...
license/src/main/java/iot/sixiang/license/device/DeviceDecoder.java
View file @
b5ed11ec
...
@@ -3,6 +3,7 @@ package iot.sixiang.license.device;
...
@@ -3,6 +3,7 @@ package iot.sixiang.license.device;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.handler.codec.ByteToMessageDecoder
;
import
io.netty.handler.codec.ByteToMessageDecoder
;
import
iot.sixiang.license.util.Util
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
java.util.List
;
import
java.util.List
;
...
@@ -13,10 +14,14 @@ import java.util.List;
...
@@ -13,10 +14,14 @@ import java.util.List;
*/
*/
@Slf4j
@Slf4j
public
class
DeviceDecoder
extends
ByteToMessageDecoder
{
public
class
DeviceDecoder
extends
ByteToMessageDecoder
{
public
final
int
BASE_LENGTH
=
2
+
2
+
1
+
1
;
public
final
int
BASE_LENGTH
=
2
+
2
+
1
+
1
+
1
;
@Override
@Override
protected
void
decode
(
ChannelHandlerContext
ctx
,
ByteBuf
buffer
,
List
<
Object
>
out
)
{
protected
void
decode
(
ChannelHandlerContext
ctx
,
ByteBuf
buffer
,
List
<
Object
>
out
)
{
byte
[]
packet
=
new
byte
[
buffer
.
readableBytes
()];
buffer
.
readBytes
(
packet
);
buffer
.
resetReaderIndex
();
Util
.
DEBUG_HEX
(
"SERVER -> IN"
,
packet
,
packet
.
length
);
try
{
try
{
if
(
buffer
.
readableBytes
()
<
BASE_LENGTH
)
{
if
(
buffer
.
readableBytes
()
<
BASE_LENGTH
)
{
return
;
return
;
...
@@ -54,10 +59,10 @@ public class DeviceDecoder extends ByteToMessageDecoder {
...
@@ -54,10 +59,10 @@ public class DeviceDecoder extends ByteToMessageDecoder {
log
.
debug
(
"stx:"
+
stx
+
",len:"
+
len
+
",cmd:"
+
cmd
+
",ack:"
+
ack
);
//stx:21930,len:52,cmd:-112,ack:0
log
.
debug
(
"stx:"
+
stx
+
",len:"
+
len
+
",cmd:"
+
cmd
+
",ack:"
+
ack
);
//stx:21930,len:52,cmd:-112,ack:0
int
real_len
=
len
+
1
;
int
real_len
=
len
;
int
cmd_ack_len
=
2
;
int
cmd_ack_len
=
2
;
if
(
buffer
.
readableBytes
()
<
real_len
-
cmd_ack_len
)
{
if
(
buffer
.
readableBytes
()
<
real_len
-
cmd_ack_len
+
1
)
{
buffer
.
resetReaderIndex
();
buffer
.
resetReaderIndex
();
return
;
return
;
}
}
...
@@ -67,7 +72,9 @@ public class DeviceDecoder extends ByteToMessageDecoder {
...
@@ -67,7 +72,9 @@ public class DeviceDecoder extends ByteToMessageDecoder {
byte
[]
content
=
new
byte
[
real_len
-
cmd_ack_len
];
byte
[]
content
=
new
byte
[
real_len
-
cmd_ack_len
];
buffer
.
readBytes
(
content
);
buffer
.
readBytes
(
content
);
DeviceProtocol
protocol
=
new
DeviceProtocol
(
stx
,
len
,
cmd
,
ack
,
content
);
byte
end
=
buffer
.
readByte
();
DeviceProtocol
protocol
=
new
DeviceProtocol
(
stx
,
len
,
cmd
,
ack
,
content
,
end
);
out
.
add
(
protocol
);
out
.
add
(
protocol
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
...
...
license/src/main/java/iot/sixiang/license/device/DeviceEncoder.java
View file @
b5ed11ec
...
@@ -27,6 +27,7 @@ public class DeviceEncoder extends MessageToByteEncoder<DeviceProtocol> {
...
@@ -27,6 +27,7 @@ public class DeviceEncoder extends MessageToByteEncoder<DeviceProtocol> {
}
else
{
}
else
{
out
.
writeBytes
(
msg
.
getContent
());
out
.
writeBytes
(
msg
.
getContent
());
}
}
out
.
writeByte
(
msg
.
getEnd
());
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
// TODO Auto-generated catch block
// TODO Auto-generated catch block
e
.
printStackTrace
();
e
.
printStackTrace
();
...
...
license/src/main/java/iot/sixiang/license/device/DeviceProtocol.java
View file @
b5ed11ec
...
@@ -13,14 +13,16 @@ public class DeviceProtocol {
...
@@ -13,14 +13,16 @@ public class DeviceProtocol {
private
byte
ack
;
private
byte
ack
;
private
byte
[]
content
;
// 数据
private
byte
[]
content
;
// 数据
private
byte
end
;
public
DeviceProtocol
(
short
stx
,
int
len
,
byte
cmd
,
byte
ack
,
byte
[]
content
)
{
public
DeviceProtocol
(
short
stx
,
int
len
,
byte
cmd
,
byte
ack
,
byte
[]
content
,
byte
end
)
{
super
();
super
();
this
.
stx
=
stx
;
this
.
stx
=
stx
;
this
.
len
=
len
;
this
.
len
=
len
;
this
.
cmd
=
cmd
;
this
.
cmd
=
cmd
;
this
.
ack
=
ack
;
this
.
ack
=
ack
;
this
.
content
=
content
;
this
.
content
=
content
;
this
.
end
=
end
;
}
}
@Override
@Override
...
...
license/src/main/java/iot/sixiang/license/device/DeviceServerHandler.java
View file @
b5ed11ec
...
@@ -10,6 +10,7 @@ import iot.sixiang.license.event.DeviceClientInactiveEvent;
...
@@ -10,6 +10,7 @@ import iot.sixiang.license.event.DeviceClientInactiveEvent;
import
iot.sixiang.license.event.EventPublisher
;
import
iot.sixiang.license.event.EventPublisher
;
import
iot.sixiang.license.event.ForwardClientRequestEvent
;
import
iot.sixiang.license.event.ForwardClientRequestEvent
;
import
iot.sixiang.license.model.SessionContext
;
import
iot.sixiang.license.model.SessionContext
;
import
iot.sixiang.license.util.HexUtil
;
import
iot.sixiang.license.util.SpringUtil
;
import
iot.sixiang.license.util.SpringUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
@@ -51,6 +52,7 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
...
@@ -51,6 +52,7 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
byte
cmd
=
protocol
.
getCmd
();
byte
cmd
=
protocol
.
getCmd
();
int
cmdInt
=
cmd
&
0xFF
;
int
cmdInt
=
cmd
&
0xFF
;
log
.
info
(
"real cmd:"
+
cmdInt
);
log
.
info
(
"real cmd:"
+
cmdInt
);
log
.
info
(
"收到的消息:"
+
HexUtil
.
bytes2hex
(
protocol
.
getContent
()));
boolean
license
=
false
;
boolean
license
=
false
;
// cmdInt = Consts.CMD_LICENSE;
// cmdInt = Consts.CMD_LICENSE;
...
@@ -90,7 +92,7 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
...
@@ -90,7 +92,7 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
// TODO 以下为模拟的测试代码
// TODO 以下为模拟的测试代码
String
appId
=
"123456"
;
/*
String appId = "123456";
String appKey = "123456";
String appKey = "123456";
String token = "123456";
String token = "123456";
...
@@ -114,7 +116,7 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
...
@@ -114,7 +116,7 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
CreateForwarClientEvent event = new CreateForwarClientEvent();
CreateForwarClientEvent event = new CreateForwarClientEvent();
event.setAppId(appId);
event.setAppId(appId);
eventPublisher.publishEvent(event);
eventPublisher.publishEvent(event);
}
}
*/
}
}
@Override
@Override
...
@@ -178,6 +180,12 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
...
@@ -178,6 +180,12 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
eventPublisher
.
publishEvent
(
event
);
eventPublisher
.
publishEvent
(
event
);
}
}
// byte ack = 0x0;
// byte ack2 = 0x01;
// protocol.setAck(ack);
// channel.writeAndFlush(protocol);
return
license
;
return
license
;
}
}
...
...
license/src/main/java/iot/sixiang/license/event/DeviceClientLicenseEvent.java
0 → 100644
View file @
b5ed11ec
package
iot
.
sixiang
.
license
.
event
;
import
iot.sixiang.license.device.DeviceProtocol
;
import
lombok.Data
;
@Data
public
class
DeviceClientLicenseEvent
extends
BaseEvent
{
private
String
appId
;
private
DeviceProtocol
protocol
;
}
license/src/main/java/iot/sixiang/license/event/DeviceClientLicenseEventHandler.java
0 → 100644
View file @
b5ed11ec
package
iot
.
sixiang
.
license
.
event
;
import
iot.sixiang.license.device.DeviceManager
;
import
iot.sixiang.license.device.DeviceProtocol
;
import
iot.sixiang.license.model.SessionContext
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.event.EventListener
;
import
org.springframework.stereotype.Component
;
@Component
@Slf4j
public
class
DeviceClientLicenseEventHandler
{
@Autowired
DeviceManager
deviceManager
;
@Autowired
EventPublisher
eventPublisher
;
public
DeviceClientLicenseEventHandler
()
{
}
// @Async("asyncExecutor")
@EventListener
public
void
handlerEvent
(
DeviceClientLicenseEvent
event
)
{
String
appId
=
event
.
getAppId
();
DeviceProtocol
protocol
=
event
.
getProtocol
();
SessionContext
session
=
deviceManager
.
getSessionContextByAppId
(
appId
);
if
(
session
==
null
)
{
log
.
debug
(
"device client license undo ..."
);
return
;
}
else
{
session
.
getClientChannel
().
writeAndFlush
(
protocol
);
log
.
debug
(
"device client license success ..."
);
}
}
}
license/src/main/java/iot/sixiang/license/event/ForwardMessageResponseEventHandler.java
View file @
b5ed11ec
...
@@ -35,13 +35,17 @@ public class ForwardMessageResponseEventHandler {
...
@@ -35,13 +35,17 @@ public class ForwardMessageResponseEventHandler {
SessionContext
forwardSessionContext
=
forwardManager
.
getSessionByChannelId
(
channelId
);
SessionContext
forwardSessionContext
=
forwardManager
.
getSessionByChannelId
(
channelId
);
String
appId
=
forwardSessionContext
.
getAppId
();
String
appId
=
forwardSessionContext
.
getAppId
();
log
.
info
(
"forward client response..."
+
appId
+
",forward session:"
+
forwardSessionContext
);
SessionContext
deviceSessionContext
=
deviceManager
.
getSessionContextByAppId
(
appId
);
SessionContext
deviceSessionContext
=
deviceManager
.
getSessionContextByAppId
(
appId
);
SocketChannel
deviceClientChannel
=
deviceSessionContext
.
getClientChannel
();
if
(
deviceSessionContext
!=
null
){
SocketChannel
deviceClientChannel
=
deviceSessionContext
.
getClientChannel
();
log
.
info
(
"forward client response..."
+
appId
+
",forward session:"
+
deviceSessionContext
);
deviceClientChannel
.
writeAndFlush
(
protocol
);
}
else
{
log
.
info
(
"forward client response undo ..."
+
appId
);
}
log
.
info
(
"forward client response..."
+
appId
+
",forward session:"
+
deviceSessionContext
);
deviceClientChannel
.
writeAndFlush
(
protocol
);
}
}
...
...
license/src/main/java/iot/sixiang/license/forward/ForwardClientHandler.java
View file @
b5ed11ec
...
@@ -11,6 +11,7 @@ import iot.sixiang.license.event.EventPublisher;
...
@@ -11,6 +11,7 @@ import iot.sixiang.license.event.EventPublisher;
import
iot.sixiang.license.event.ForwardClientInactiveEvent
;
import
iot.sixiang.license.event.ForwardClientInactiveEvent
;
import
iot.sixiang.license.event.ForwardMessageResponseEvent
;
import
iot.sixiang.license.event.ForwardMessageResponseEvent
;
import
iot.sixiang.license.model.SessionContext
;
import
iot.sixiang.license.model.SessionContext
;
import
iot.sixiang.license.util.HexUtil
;
import
iot.sixiang.license.util.SpringUtil
;
import
iot.sixiang.license.util.SpringUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
@@ -45,7 +46,8 @@ public class ForwardClientHandler extends SimpleChannelInboundHandler<Object> {
...
@@ -45,7 +46,8 @@ public class ForwardClientHandler extends SimpleChannelInboundHandler<Object> {
int
serverPort
=
socketAddr
.
getPort
();
int
serverPort
=
socketAddr
.
getPort
();
log
.
debug
(
"channelRead0..."
);
log
.
debug
(
"channelRead0..."
);
DeviceProtocol
protocol
=
(
DeviceProtocol
)
msg
;
DeviceProtocol
protocol
=
(
DeviceProtocol
)
msg
;
log
.
info
(
"准备发回给设备:"
+
protocol
.
toString
());
log
.
info
(
"桥接服务器响应1"
+
protocol
.
toString
());
log
.
info
(
"桥接服务器响应2:"
+
HexUtil
.
bytes2hex
(
protocol
.
getContent
()));
String
channelId
=
channel
.
id
().
asLongText
();
String
channelId
=
channel
.
id
().
asLongText
();
...
...
license/src/main/java/iot/sixiang/license/forward/ForwardConnectionListener.java
View file @
b5ed11ec
...
@@ -3,7 +3,9 @@ package iot.sixiang.license.forward;
...
@@ -3,7 +3,9 @@ package iot.sixiang.license.forward;
import
io.netty.channel.ChannelFuture
;
import
io.netty.channel.ChannelFuture
;
import
io.netty.channel.EventLoop
;
import
io.netty.channel.EventLoop
;
import
io.netty.channel.socket.SocketChannel
;
import
io.netty.channel.socket.SocketChannel
;
import
iot.sixiang.license.device.DeviceProtocol
;
import
iot.sixiang.license.event.DeviceClientBeForcedOfflineEvent
;
import
iot.sixiang.license.event.DeviceClientBeForcedOfflineEvent
;
import
iot.sixiang.license.event.DeviceClientLicenseEvent
;
import
iot.sixiang.license.event.EventPublisher
;
import
iot.sixiang.license.event.EventPublisher
;
import
iot.sixiang.license.event.ForwardClientConnectEvent
;
import
iot.sixiang.license.event.ForwardClientConnectEvent
;
import
iot.sixiang.license.net.BaseConnectionListener
;
import
iot.sixiang.license.net.BaseConnectionListener
;
...
@@ -54,6 +56,30 @@ public class ForwardConnectionListener extends BaseConnectionListener {
...
@@ -54,6 +56,30 @@ public class ForwardConnectionListener extends BaseConnectionListener {
log
.
debug
(
"Successful connection to the server"
);
log
.
debug
(
"Successful connection to the server"
);
log
.
info
(
"forward client connect success ..."
+
appId
);
log
.
info
(
"forward client connect success ..."
+
appId
);
// byte ack2 = 0x01;
short
stx
=
21930
;
byte
ack
=
0x0
;
int
len
=
3
;
byte
cmd
=
0x1
;
byte
[]
content
=
new
byte
[
1
];
content
[
0
]
=
0x7e
;
byte
end
=
0x1
;
// stx:21930,len:76,cmd:1,ack:0
DeviceProtocol
protocol
=
new
DeviceProtocol
(
stx
,
len
,
cmd
,
ack
,
content
,
end
);
DeviceClientLicenseEvent
deviceClientLicenseEvent
=
new
DeviceClientLicenseEvent
();
deviceClientLicenseEvent
.
setAppId
(
appId
);
deviceClientLicenseEvent
.
setProtocol
(
protocol
);
eventPublisher
.
publishEvent
(
deviceClientLicenseEvent
);
}
}
}
}
...
...
license/src/main/java/iot/sixiang/license/forward/ForwardDecoder.java
View file @
b5ed11ec
...
@@ -4,6 +4,7 @@ import io.netty.buffer.ByteBuf;
...
@@ -4,6 +4,7 @@ import io.netty.buffer.ByteBuf;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.handler.codec.ByteToMessageDecoder
;
import
io.netty.handler.codec.ByteToMessageDecoder
;
import
iot.sixiang.license.device.DeviceProtocol
;
import
iot.sixiang.license.device.DeviceProtocol
;
import
iot.sixiang.license.util.Util
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
java.util.List
;
import
java.util.List
;
...
@@ -17,6 +18,11 @@ public class ForwardDecoder extends ByteToMessageDecoder {
...
@@ -17,6 +18,11 @@ public class ForwardDecoder extends ByteToMessageDecoder {
protected
void
decode
(
ChannelHandlerContext
ctx
,
ByteBuf
buffer
,
List
<
Object
>
out
)
{
protected
void
decode
(
ChannelHandlerContext
ctx
,
ByteBuf
buffer
,
List
<
Object
>
out
)
{
// 可读长度必须大于等于基本长度
// 可读长度必须大于等于基本长度
try
{
try
{
byte
[]
packet
=
new
byte
[
buffer
.
readableBytes
()];
buffer
.
readBytes
(
packet
);
buffer
.
resetReaderIndex
();
Util
.
DEBUG_HEX
(
"SERVER -> IN"
,
packet
,
packet
.
length
);
if
(
buffer
.
readableBytes
()
<
BASE_LENGTH
)
{
if
(
buffer
.
readableBytes
()
<
BASE_LENGTH
)
{
return
;
return
;
}
}
...
@@ -33,10 +39,10 @@ public class ForwardDecoder extends ByteToMessageDecoder {
...
@@ -33,10 +39,10 @@ public class ForwardDecoder extends ByteToMessageDecoder {
log
.
debug
(
"stx:"
+
stx
+
",len:"
+
len
);
log
.
debug
(
"stx:"
+
stx
+
",len:"
+
len
);
int
real_len
=
len
+
1
;
//注意,透传前已经去掉了END一个字符
int
real_len
=
len
;
//注意,透传前已经去掉了END一个字符
int
cmd_ack_len
=
2
;
int
cmd_ack_len
=
2
;
if
(
buffer
.
readableBytes
()
<
real_len
-
cmd_ack_len
)
{
if
(
buffer
.
readableBytes
()
<
real_len
-
cmd_ack_len
+
1
)
{
buffer
.
resetReaderIndex
();
buffer
.
resetReaderIndex
();
return
;
return
;
}
}
...
@@ -47,9 +53,11 @@ public class ForwardDecoder extends ByteToMessageDecoder {
...
@@ -47,9 +53,11 @@ public class ForwardDecoder extends ByteToMessageDecoder {
byte
[]
content
=
new
byte
[
real_len
-
cmd_ack_len
];
byte
[]
content
=
new
byte
[
real_len
-
cmd_ack_len
];
buffer
.
readBytes
(
content
);
buffer
.
readBytes
(
content
);
byte
end
=
buffer
.
readByte
();
log
.
debug
(
"ForwardDecoder.msg.getLen()...."
+
real_len
);
log
.
debug
(
"ForwardDecoder.msg.getLen()...."
+
real_len
);
DeviceProtocol
protocol
=
new
DeviceProtocol
(
stx
,
real_len
,
cmd
,
ack
,
content
);
DeviceProtocol
protocol
=
new
DeviceProtocol
(
stx
,
real_len
,
cmd
,
ack
,
content
,
end
);
out
.
add
(
protocol
);
out
.
add
(
protocol
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
...
...
license/src/main/java/iot/sixiang/license/forward/ForwardEncoder.java
View file @
b5ed11ec
...
@@ -29,6 +29,7 @@ public class ForwardEncoder extends MessageToByteEncoder<DeviceProtocol> {
...
@@ -29,6 +29,7 @@ public class ForwardEncoder extends MessageToByteEncoder<DeviceProtocol> {
}
else
{
}
else
{
out
.
writeBytes
(
msg
.
getContent
());
out
.
writeBytes
(
msg
.
getContent
());
}
}
out
.
writeByte
(
msg
.
getEnd
());
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
// TODO Auto-generated catch block
// TODO Auto-generated catch block
e
.
printStackTrace
();
e
.
printStackTrace
();
...
...
license/src/main/java/iot/sixiang/license/util/Util.java
0 → 100644
View file @
b5ed11ec
package
iot
.
sixiang
.
license
.
util
;
import
lombok.extern.slf4j.Slf4j
;
import
java.io.File
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.io.UnsupportedEncodingException
;
import
java.net.Inet4Address
;
import
java.net.InetAddress
;
import
java.net.NetworkInterface
;
import
java.net.SocketException
;
import
java.security.MessageDigest
;
import
java.security.NoSuchAlgorithmException
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.Enumeration
;
import
java.util.List
;
@Slf4j
public
class
Util
{
public
static
boolean
Debug
=
false
;
public
static
void
logInit
(
String
path
){
}
public
static
byte
asciiToByte
(
byte
ascii
)
{
byte
ret
;
if
(
ascii
>=
'A'
){
ret
=
(
byte
)((
ascii
-
'A'
)+
0X0A
);
ret
&=
0x0f
;
return
ret
;
}
else
{
ret
=
(
byte
)(
ascii
-(
byte
)
0x30
);
ret
=
(
byte
)(
ret
&
0x0f
);
return
ret
;
}
}
public
static
byte
byteToAscii
(
byte
data
){
byte
ret
=
0x0f
;
if
(
data
>=
0X0A
){
ret
=(
byte
)((
data
-
0X0A
)+
'A'
);
}
else
{
ret
=(
byte
)(
data
+
'0'
);
}
return
ret
;
}
public
static
int
AsciiToHexByte
(
byte
[]
dst
,
byte
[]
src
){
if
(
src
.
length
==
0
){
return
0
;
}
if
((
src
.
length
%
2
)!=
0
){
return
0
;
}
for
(
int
i
=
0
;
i
<
src
.
length
;){
dst
[
i
/
2
]=(
byte
)(
asciiToByte
(
src
[
i
])<<
4
);
dst
[
i
/
2
]+=
asciiToByte
(
src
[
i
+
1
]);
i
+=
2
;
}
return
src
.
length
/
2
;
}
public
static
int
AsciiToHexByte
(
byte
[]
dst
,
int
dstOffset
,
byte
[]
src
,
int
srcOffset
,
int
len
){
if
(
len
==
0
){
return
0
;
}
if
((
len
%
2
)!=
0
){
return
0
;
}
for
(
int
i
=
0
;
i
<
len
;){
dst
[(
i
/
2
)+
dstOffset
]=(
byte
)(
asciiToByte
(
src
[
i
+
srcOffset
])<<
4
);
dst
[(
i
/
2
)+
dstOffset
]+=
asciiToByte
(
src
[
i
+
1
+
srcOffset
]);
i
+=
2
;
}
return
len
/
2
;
}
public
static
int
HexToAsciiByte
(
byte
[]
dst
,
byte
[]
src
){
for
(
int
i
=
0
;
i
<
src
.
length
;
i
++){
dst
[
i
*
2
]=
byteToAscii
((
byte
)((
src
[
i
]>>
4
)&
0x0f
));
dst
[(
i
*
2
)+
1
]=
byteToAscii
((
byte
)(
src
[
i
]&
0x0f
));
}
return
src
.
length
*
2
;
}
public
static
int
IndexOfByte
(
byte
[]
src
,
int
offer
,
int
len
,
byte
data
){
for
(
int
i
=
offer
;
i
<
len
;
i
++){
if
(
data
==
src
[
i
]){
return
i
;
}
}
return
len
;
}
public
static
void
DEBUG_HEX
(
String
tag
,
byte
[]
data
,
int
datalen
){
String
hexStr
=
""
;
int
i
=
0
;
for
(
i
=
0
;
i
<
datalen
;
i
++){
if
((
i
%
16
)==
0
){
log
.
info
(
hexStr
);
hexStr
=
""
;
}
if
(
Integer
.
toHexString
(
data
[
i
]&
0xff
).
length
()==
1
){
hexStr
+=
"0"
;
}
hexStr
+=
Integer
.
toHexString
(
data
[
i
]&
0xff
).
toUpperCase
()+
" "
;
if
((
i
%
16
)==
15
){
for
(
int
j
=(
i
-
15
);
j
<=
i
;
j
++){
if
(
data
[
j
]>
0x20
&&
data
[
j
]<=
0x7e
){
hexStr
+=
String
.
valueOf
((
char
)(
data
[
j
]&
0xff
));
}
else
{
hexStr
+=
'.'
;
}
}
}
}
if
((
i
%
16
)!=
0
){
for
(
int
j
=
0
;
j
<
16
;
j
++){
if
(
j
<(
16
-(
i
%
16
))){
hexStr
+=
"-- "
;
}
}
for
(
int
j
=(
i
-(
i
%
16
));
j
<
i
;
j
++){
if
(
data
[
j
]>
0x20
&&
data
[
j
]<=
0x7e
){
hexStr
+=
String
.
valueOf
((
char
)(
data
[
j
]&
0xff
));
}
else
{
hexStr
+=
'.'
;
}
}
}
log
.
info
(
hexStr
);
}
public
static
void
INFO
(
String
tag
,
String
content
){
if
(!
Debug
){
return
;
}
SimpleDateFormat
df
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
System
.
out
.
println
(
"I-["
+
df
.
format
(
new
Date
())+
"]["
+
tag
+
"]:"
+
content
);
}
public
static
void
DEBUG
(
String
tag
,
String
content
){
if
(!
Debug
){
return
;
}
SimpleDateFormat
df
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
System
.
out
.
println
(
"D-["
+
df
.
format
(
new
Date
())+
"]["
+
tag
+
"]:"
+
content
);
}
public
static
void
ERROR
(
String
tag
,
Object
content
){
SimpleDateFormat
df
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
System
.
out
.
println
(
"E-["
+
df
.
format
(
new
Date
())+
"]["
+
tag
+
"]:"
+
content
);
if
(
content
instanceof
Exception
)
{
StackTraceElement
stack
[]
=
((
Exception
)
content
).
getStackTrace
();
for
(
int
i
=
0
;
i
<
stack
.
length
;
i
++)
{
StackTraceElement
s
=
stack
[
i
];
System
.
out
.
format
(
"Class:%d\t%s\n"
,
i
,
s
.
getClass
());
//������
System
.
out
.
format
(
"ClassName:%d\t%s\n"
,
i
,
s
.
getClassName
());
//����
System
.
out
.
format
(
"MethodName:%d\t%s\n"
,
i
,
s
.
getMethodName
());
//������
System
.
out
.
format
(
"FileName:%d\t%s\n"
,
i
,
s
.
getFileName
());
//�ļ���
System
.
out
.
format
(
"LineNumber:%d\t%s\n"
,
i
,
s
.
getLineNumber
());
//����
System
.
out
.
println
(
"-------------------------------------------"
);
//����
}
}
}
public
static
String
HexToStr
(
byte
[]
data
,
int
offset
,
int
length
,
boolean
space
){
String
hexStr
=
""
;
for
(
int
i
=
offset
;
i
<(
length
+
offset
);
i
++){
if
(
Integer
.
toHexString
(
data
[
i
]&
0xff
).
length
()==
1
){
hexStr
+=
"0"
;
}
hexStr
+=
Integer
.
toHexString
(
data
[
i
]&
0xff
).
toUpperCase
();
if
(
space
){
hexStr
+=
" "
;
}
}
return
hexStr
;
}
public
static
List
<
String
>
getLocalIPList
()
{
List
<
String
>
ipList
=
new
ArrayList
<
String
>();
try
{
Enumeration
<
NetworkInterface
>
networkInterfaces
=
NetworkInterface
.
getNetworkInterfaces
();
NetworkInterface
networkInterface
;
Enumeration
<
InetAddress
>
inetAddresses
;
InetAddress
inetAddress
;
String
ip
;
while
(
networkInterfaces
.
hasMoreElements
())
{
networkInterface
=
networkInterfaces
.
nextElement
();
inetAddresses
=
networkInterface
.
getInetAddresses
();
while
(
inetAddresses
.
hasMoreElements
())
{
inetAddress
=
inetAddresses
.
nextElement
();
if
(
inetAddress
!=
null
&&
inetAddress
instanceof
Inet4Address
)
{
// IPV4
ip
=
inetAddress
.
getHostAddress
();
ipList
.
add
(
ip
);
}
}
}
}
catch
(
SocketException
e
)
{
e
.
printStackTrace
();
}
return
ipList
;
}
public
static
int
CRC16
(
byte
[]
p
,
int
offer
,
int
len
)
{
//Ԥ�� 1 �� 16 λ�ļĴ���Ϊʮ������FFFF, �ƴ˼Ĵ���Ϊ CRC�Ĵ�����
int
crc
=
0xFFFF
;
int
i
,
j
;
for
(
i
=
0
;
i
<
len
;
i
++)
{
//�ѵ�һ�� 8 λ���������� �� 16 λ�� CRC�Ĵ����ĵ� 8 λ�����, �ѽ������ CRC�Ĵ���
crc
=
((
crc
&
0xFF00
)
|
(
crc
&
0x00FF
)
^
(
p
[
i
+
offer
]
&
0xFF
));
for
(
j
=
0
;
j
<
8
;
j
++)
{
//�� CRC �Ĵ�������������һλ( ����λ)�� 0 ����λ, ��������ƺ���Ƴ�λ
if
((
crc
&
0x0001
)
>
0
)
{
//����Ƴ�λΪ 1, CRC�Ĵ��������ʽA001�������
crc
=
crc
>>
1
;
crc
=
crc
^
0xA001
;
}
else
//����Ƴ�λΪ 0,�ٴ�����һλ
crc
=
crc
>>
1
;
}
}
return
crc
;
}
public
static
String
UnicodeToUTF8
(
String
content
){
try
{
return
new
String
(
content
.
getBytes
(
"UTF-8"
));
}
catch
(
UnsupportedEncodingException
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
}
return
null
;
}
public
static
String
splitLineString
(
String
content
,
int
lineLength
)
{
String
tmp
=
""
;
int
offset
=
0
;
int
length
=
content
.
length
();
while
(
length
>
0
)
{
if
(
length
<
lineLength
)
{
tmp
+=
content
.
substring
(
offset
)+
"\n"
;
length
=
0
;
}
else
{
tmp
+=
content
.
substring
(
offset
,
lineLength
+
offset
)+
"\n"
;
offset
+=
lineLength
;
length
-=
lineLength
;
}
}
return
tmp
;
}
public
static
byte
[]
getSHA256
(
byte
[]
content
)
{
MessageDigest
messageDigest
;
try
{
messageDigest
=
MessageDigest
.
getInstance
(
"SHA-256"
);
messageDigest
.
update
(
content
);
return
messageDigest
.
digest
();
}
catch
(
NoSuchAlgorithmException
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
}
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