Commit 52b8b82a authored by 王夏晖's avatar 王夏晖

完善命令发送方式

parent 0b088e72
This diff is collapsed.
...@@ -22,11 +22,15 @@ public interface UserDao extends BaseDao<User> { ...@@ -22,11 +22,15 @@ public interface UserDao extends BaseDao<User> {
@Select("SELECT * FROM tb_user WHERE temphone = #{phoneNumber}") @Select("SELECT * FROM tb_user WHERE temphone = #{phoneNumber}")
User getUserByPhoneNumber(@Param("phoneNumber") String phoneNumber); User getUserByPhoneNumber(@Param("phoneNumber") String phoneNumber);
List<Map<String,Object>> queryIpByUser(@Param("phone") String phone); List<Map<String,Object>> queryMacByPhone(@Param("phone") String phone);
@Update("UPDATE tb_user SET client_id = #{clientId} WHERE temphone = #{phoneNumber}") @Update("UPDATE tb_user SET client_id = #{clientId} WHERE temphone = #{phoneNumber}")
int updateClientIdByPhoneNumber(@Param("clientId")String clientId, @Param("phoneNumber")String phoneNumber); int updateClientIdByPhoneNumber(@Param("clientId")String clientId, @Param("phoneNumber")String phoneNumber);
List<Map<String,Object>> queryClientByIp(@Param("ip") String ip); List<Map<String,Object>> queryClientByIp(@Param("ip") String ip);
@Select("select way.mac_address from tb_user ur,tb_gateway way where ur.id = way.user_id\n" +
" and ur.temphone = #{phoneNumber}")
List<Map<String,Object>> getGateWayByPhoneNumber(@Param("phoneNumber") String phoneNumber);
} }
...@@ -45,7 +45,11 @@ public class UserService extends BaseService<User, String> { ...@@ -45,7 +45,11 @@ public class UserService extends BaseService<User, String> {
Boolean flag = false; Boolean flag = false;
User user = dao.getUserByPhoneNumber(phoneNumber); User user = dao.getUserByPhoneNumber(phoneNumber);
if(user != null) { if(user != null) {
flag = true; if(user.getPassword()!=null && !"".equals(user.getPassword())){
flag = true;
}else{
flag = false;
}
} }
return flag; return flag;
} }
...@@ -90,7 +94,7 @@ public class UserService extends BaseService<User, String> { ...@@ -90,7 +94,7 @@ public class UserService extends BaseService<User, String> {
List<String> encryptList = encryptList(plainPassword); List<String> encryptList = encryptList(plainPassword);
Asserts.notNull(encryptList, "encrypt list must not be null."); Asserts.notNull(encryptList, "encrypt list must not be null.");
String id = UUID.randomUUID(); String id = UUID.randomUUID();
User insertUser = new User(id, temphone, encryptList.get(0), encryptList.get(1)); User insertUser = new User(id, temphone, "", "");
logger.info("insertUser: {}", insertUser); logger.info("insertUser: {}", insertUser);
int result = dao.insertSelective(insertUser); int result = dao.insertSelective(insertUser);
if (result != 0) { if (result != 0) {
...@@ -99,14 +103,15 @@ public class UserService extends BaseService<User, String> { ...@@ -99,14 +103,15 @@ public class UserService extends BaseService<User, String> {
return false; return false;
} }
} else { } else {
User user = users.get(0); /*User user = users.get(0);
String decryptPassword = SecurityUtils.decrypt(user.getEncryptkey(), user.getPassword()); String decryptPassword = SecurityUtils.decrypt(user.getEncryptkey(), user.getPassword());
Asserts.notNull(plainPassword, "plainPassword must not be null."); Asserts.notNull(plainPassword, "plainPassword must not be null.");
if (plainPassword.equals(decryptPassword)) { if (plainPassword.equals(decryptPassword)) {
return true; return true;
} else { } else {
return false; return false;
} }*/
return true;
} }
} }
...@@ -125,8 +130,8 @@ public class UserService extends BaseService<User, String> { ...@@ -125,8 +130,8 @@ public class UserService extends BaseService<User, String> {
} }
public List<Map<String,Object>> queryIpByUser(String phone){ public List<Map<String,Object>> queryMacByPhone(String phone){
return dao.queryIpByUser(phone); return dao.getGateWayByPhoneNumber(phone);
} }
public void updateClientIdByPhoneNumber(String clientId, String phoneNumber) { public void updateClientIdByPhoneNumber(String clientId, String phoneNumber) {
......
...@@ -13,4 +13,6 @@ public class SystemValue { ...@@ -13,4 +13,6 @@ public class SystemValue {
public static Map<String, String> ipMacMap = new HashMap<>(); public static Map<String, String> ipMacMap = new HashMap<>();
public static Map<String,Object> channelMap = new HashMap<>();
} }
...@@ -35,11 +35,13 @@ public class NettyServer { ...@@ -35,11 +35,13 @@ public class NettyServer {
@Override @Override
protected void initChannel(SocketChannel ch) throws Exception { protected void initChannel(SocketChannel ch) throws Exception {
System.out.println("端口号:"+ch.remoteAddress().getPort());
System.out.println("connected...; Local:" + ch.localAddress().getHostString());
System.out.println("connected...; Client:" + ch.remoteAddress().getHostString()); System.out.println("connected...; Client:" + ch.remoteAddress().getHostString());
//發送請求指令 //發送請求指令
if( SystemValue.ipMacMap.get(ch.remoteAddress().getHostString()) == null){ /*if( SystemValue.ipMacMap.get(ch.remoteAddress().getHostString()) == null){
connectGateway(ch.remoteAddress().getHostString(),8899); connectGateway(ch.remoteAddress().getHostString(),8899);
} }*/
ch.pipeline().addLast(new ByteArrayEncoder()); ch.pipeline().addLast(new ByteArrayEncoder());
ch.pipeline().addLast(new ByteArrayDecoder()); ch.pipeline().addLast(new ByteArrayDecoder());
ch.pipeline().addLast(new ServerHandler()); // 客户端触发操作 ch.pipeline().addLast(new ServerHandler()); // 客户端触发操作
...@@ -48,7 +50,7 @@ public class NettyServer { ...@@ -48,7 +50,7 @@ public class NettyServer {
} }
}); });
ChannelFuture cf = sb.bind().sync(); // 服务器异步创建绑定 ChannelFuture cf = sb.bind(9999).sync(); // 服务器异步创建绑定
System.out.println(NettyServer.class + " started and listen on " + cf.channel().localAddress()); System.out.println(NettyServer.class + " started and listen on " + cf.channel().localAddress());
cf.channel().closeFuture().sync(); // 关闭服务器通道 cf.channel().closeFuture().sync(); // 关闭服务器通道
} finally { } finally {
......
...@@ -13,10 +13,12 @@ import io.netty.buffer.Unpooled; ...@@ -13,10 +13,12 @@ import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.group.ChannelGroup;
import oracle.jdbc.proxy.annotation.Post; import oracle.jdbc.proxy.annotation.Post;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import sun.java2d.pipe.SpanShapeRenderer; import sun.java2d.pipe.SpanShapeRenderer;
import sun.rmi.transport.Channel;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
...@@ -60,14 +62,15 @@ public class ServerHandler extends ChannelInboundHandlerAdapter { ...@@ -60,14 +62,15 @@ public class ServerHandler extends ChannelInboundHandlerAdapter {
// 1.上报MAC地址信息 6位mac地址+10位的(MAC-UPLOAD) // 1.上报MAC地址信息 6位mac地址+10位的(MAC-UPLOAD)
try{ try{
int len = SocketUtils.bytesToHex(result,0,result.length).split(" ").length; int len = SocketUtils.bytesToHex(result,0,result.length).split(" ").length;
if(len == 16){ if(String.valueOf(len).equals("16")){
String mac = SocketUtils.bytesToHex(result,0,6); String mac = SocketUtils.bytesToHex(result,0,6);
Gateway gateway = new Gateway(); Gateway gateway = new Gateway();
gateway.setIp(host); gateway.setIp(host);
gateway.setMacAddress(mac); gateway.setMacAddress(mac);
serverHandler.gatewayService.updateIpByMac(gateway); serverHandler.gatewayService.updateIpByMac(gateway);
SystemValue.ipMacMap.put(host, mac); SystemValue.ipMacMap.put(host, mac);
}else{ SystemValue.channelMap.put(mac,ctx.channel());
}else if(String.valueOf(len).equals("17") || String.valueOf(len).equals("14")){
String title = ""; String title = "";
String message = ""; String message = "";
String clientId = ""; String clientId = "";
...@@ -121,7 +124,25 @@ public class ServerHandler extends ChannelInboundHandlerAdapter { ...@@ -121,7 +124,25 @@ public class ServerHandler extends ChannelInboundHandlerAdapter {
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("server channelReadComplete.."); System.out.println("server channelReadComplete..");
// 第一种方法:写一个空的buf,并刷新写出区域。完成后关闭sock channel连接。 // 第一种方法:写一个空的buf,并刷新写出区域。完成后关闭sock channel连接。
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); //ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
@Override
public void channelActive(ChannelHandlerContext ctx)throws Exception{
System.out.println("channelActive"+ ctx.channel().remoteAddress());
byte[] request = new byte[23];
request[0] = 0x4d;
request[1] = 0x41;
request[2] = 0x43;
request[3] = 0x5F;
request[4] = 0x52;
request[5] = 0x45;
request[6] = 0x51;
request[7] = 0x55;
request[8] = 0x45;
request[9] = 0x53;
request[10] = 0x54;
ctx.channel().writeAndFlush(request);
} }
@Override @Override
......
...@@ -135,8 +135,8 @@ ...@@ -135,8 +135,8 @@
</if> </if>
</select> </select>
<select id="queryIpByUser" resultType="java.util.Map" parameterType="java.util.Map"> <select id="queryMacByPhone" resultType="java.util.Map" parameterType="java.util.Map">
select way.ip from tb_user ur,tb_gateway way where ur.id = way.user_id select way.mac_address from tb_user ur,tb_gateway way where ur.id = way.user_id
and ur.temphone = #{phone} and ur.temphone = #{phone}
</select> </select>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment