Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java-demo
Overview
Overview
Details
Activity
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
open-source
java-demo
Commits
e86f1cc0
Commit
e86f1cc0
authored
May 28, 2019
by
杜祥龙
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix 接受消息
Signed-off-by: sdvdxl杜龙少 <sdvdxl@163.com>
parent
c7de935e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
9 deletions
+20
-9
src/main/java/me/hekr/demo/GatewayDevice.java
+15
-5
src/main/java/me/hekr/demo/TcpClient.java
+5
-4
No files found.
src/main/java/me/hekr/demo/GatewayDevice.java
View file @
e86f1cc0
...
...
@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject;
import
java.io.IOException
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.concurrent.CountDownLatch
;
import
java.util.concurrent.ExecutionException
;
import
java.util.concurrent.Executor
;
import
java.util.concurrent.Executors
;
...
...
@@ -12,7 +13,7 @@ import java.util.concurrent.TimeUnit;
import
java.util.concurrent.TimeoutException
;
public
class
GatewayDevice
{
private
static
final
Executor
EXECUTOR
=
Executors
.
new
SingleThreadExecutor
();
private
static
final
Executor
EXECUTOR
=
Executors
.
new
CachedThreadPool
();
// 设备id
private
static
final
String
DEV_TID
=
""
;
// 产品密钥
...
...
@@ -26,6 +27,7 @@ public class GatewayDevice {
public
static
void
main
(
String
[]
args
)
throws
IOException
,
InterruptedException
,
ExecutionException
,
TimeoutException
{
CountDownLatch
countDownLatch
=
new
CountDownLatch
(
1
);
TcpClient
tcpClient
=
new
TcpClient
(
"hub.hekr.me"
,
83
).
connect
();
String
randomKey
=
null
;
...
...
@@ -39,9 +41,11 @@ public class GatewayDevice {
message
.
put
(
"msgId"
,
tcpClient
.
getMsgId
());
params
=
new
HashMap
<>();
params
.
put
(
"devTid"
,
DEV_TID
);
params
.
put
(
"prodKey"
,
PROD_KEY
);
message
.
put
(
"params"
,
params
);
tcpClient
.
send
(
JSON
.
toJSONString
(
message
)
+
"\n"
);
String
respMsg
=
tcpClient
.
receive
(
MAX_WAIT_TIME_MILLS
);
System
.
out
.
println
(
respMsg
);
JSONObject
respMap
=
JSON
.
parseObject
(
respMsg
);
boolean
success
=
respMap
.
getIntValue
(
"code"
)
==
200
;
if
(!
success
)
{
...
...
@@ -50,7 +54,7 @@ public class GatewayDevice {
}
// 获取 randomKey
randomKey
=
(
String
)
respMap
.
get
(
"randomKey"
);
randomKey
=
(
String
)
respMap
.
get
JSONObject
(
"params"
).
get
(
"randomKey"
);
}
// 网关登录
...
...
@@ -58,6 +62,7 @@ public class GatewayDevice {
params
.
put
(
"devTid"
,
DEV_TID
);
// 如果启用两步认证,需要加上authKey
if
(
GATEWAY_DEVICE_TWO_FACTOR_AUTHENTICATION
)
{
System
.
out
.
println
(
"randomKey:"
+
randomKey
);
String
authKey
=
Util
.
MD5
(
randomKey
+
DEV_TID
+
PRIV_KEY
);
params
.
put
(
"authKey"
,
authKey
);
}
...
...
@@ -67,6 +72,7 @@ public class GatewayDevice {
message
.
put
(
"params"
,
params
);
tcpClient
.
send
(
JSON
.
toJSONString
(
message
)
+
"\n"
);
String
respMsg
=
tcpClient
.
receive
(
MAX_WAIT_TIME_MILLS
);
System
.
out
.
println
(
respMsg
);
JSONObject
resp
=
JSON
.
parseObject
(
respMsg
);
if
(
resp
.
getIntValue
(
"code"
)
!=
200
)
{
System
.
out
.
println
(
"gateway login failed,resp:"
+
respMsg
);
...
...
@@ -74,12 +80,14 @@ public class GatewayDevice {
}
// 登录成功,这里订阅消息
subcribeMessage
(
tcpClient
);
sub
s
cribeMessage
(
tcpClient
);
// TODO 发送业务数据
// 循环定时发送心跳
loopSendHeartbeat
(
tcpClient
);
countDownLatch
.
await
();
}
private
static
void
loopSendHeartbeat
(
TcpClient
tcpClient
)
{
...
...
@@ -91,7 +99,7 @@ public class GatewayDevice {
heartbeatMessage
.
put
(
"action"
,
"heartbeat"
);
heartbeatMessage
.
put
(
"msgId"
,
tcpClient
.
getMsgId
());
try
{
tcpClient
.
send
(
JSON
.
toJSONString
(
heartbeatMessage
));
tcpClient
.
send
(
JSON
.
toJSONString
(
heartbeatMessage
)
+
"\n"
);
}
catch
(
IOException
e
)
{
System
.
out
.
println
(
e
.
getMessage
());
}
...
...
@@ -100,10 +108,12 @@ public class GatewayDevice {
}
catch
(
InterruptedException
ignored
)
{
}
}
System
.
out
.
println
(
"tcp client closed"
);
});
}
private
static
void
subcribeMessage
(
TcpClient
tcpClient
)
{
private
static
void
sub
s
cribeMessage
(
TcpClient
tcpClient
)
{
EXECUTOR
.
execute
(
new
ReceiveMessageTask
(
tcpClient
,
...
...
src/main/java/me/hekr/demo/TcpClient.java
View file @
e86f1cc0
...
...
@@ -14,7 +14,6 @@ import java.util.concurrent.TimeoutException;
/** 描述 Created by du on 2017/8/6. */
public
class
TcpClient
{
private
static
final
int
TIMEOUT_MILL
=
20000
;
private
Socket
client
;
private
String
host
;
private
int
port
;
...
...
@@ -46,7 +45,7 @@ public class TcpClient {
public
TcpClient
connect
(
int
timeout
)
throws
IOException
{
client
.
setKeepAlive
(
true
);
client
.
setSoTimeout
(
TIMEOUT_MILL
);
client
.
setSoTimeout
(
0
);
client
.
connect
(
new
InetSocketAddress
(
host
,
port
),
timeout
);
outToServer
=
new
DataOutputStream
(
client
.
getOutputStream
());
...
...
@@ -62,6 +61,7 @@ public class TcpClient {
throw
new
RuntimeException
(
"output channel closed"
);
}
System
.
out
.
println
(
"SEND => "
+
message
);
outToServer
.
writeBytes
(
message
);
return
this
;
}
...
...
@@ -107,7 +107,8 @@ public class TcpClient {
*/
public
String
receive
(
int
timeoutMill
)
throws
InterruptedException
,
ExecutionException
,
TimeoutException
{
return
new
FutureTask
<
String
>(()
->
inFromServer
.
readLine
())
.
get
(
timeoutMill
,
TimeUnit
.
MICROSECONDS
);
FutureTask
<
String
>
futureTask
=
new
FutureTask
<>(()
->
inFromServer
.
readLine
());
futureTask
.
run
();
return
futureTask
.
get
(
timeoutMill
,
TimeUnit
.
MILLISECONDS
);
}
}
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