| | |
| | | import io.netty.buffer.Unpooled; |
| | | import io.netty.channel.ChannelFuture; |
| | | import io.netty.channel.ChannelHandlerContext; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.data.redis.core.StringRedisTemplate; |
| | | import org.springframework.http.HttpEntity; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.util.LinkedMultiValueMap; |
| | | import org.springframework.util.MultiValueMap; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.StringUtils; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.concurrent.TimeUnit; |
| | |
| | | * @createDate 2016年6月3日 |
| | | * @version 1.0 |
| | | */ |
| | | @Component |
| | | public class NettyServerController { |
| | | |
| | | public static Hashtable<String, Hashtable<ChannelHandlerContext, String>> map = new Hashtable<String, Hashtable<ChannelHandlerContext,String>>(); |
| | | |
| | | public static Hashtable<String,String> table; |
| | | |
| | | private RedisTemplate<String, String> redisTemplate = SpringUtil.getObject(StringRedisTemplate.class); |
| | | |
| | | @Resource |
| | | private RedisTemplate<String, Object> redisTemplate; |
| | | |
| | | |
| | | private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); |
| | | |
| | |
| | | String device = jsonCon.getString("device"); |
| | | String version = jsonCon.getString("version"); |
| | | if(StringUtil.isNotEmpty(userId1)){ |
| | | String fluid_control = redisTemplate.opsForValue().get("fluid_control_" + userId1 + "_" + type); |
| | | String fluid_control = (String)redisTemplate.opsForValue().get("fluid_control_" + userId1 + "_" + type); |
| | | if(!StringUtils.hasLength(fluid_control)){ |
| | | redisTemplate.opsForValue().set("fluid_control_" + userId1 + "_" + type, System.currentTimeMillis() + ""); |
| | | }else{ |
| | |
| | | ctx.writeAndFlush(Unpooled.copiedBuffer((s).getBytes())); |
| | | } |
| | | }else{ |
| | | Map<String, Object> params = new HashMap<>(); |
| | | params.put("driverId", userId1); |
| | | HttpRequest post = HttpUtil.createPost(URLUtil.zuul + "/driver-server/base/driverOnline/addDriverOnline"); |
| | | post.contentType(MediaType.APPLICATION_FORM_URLENCODED.getType()); |
| | | post.form(params); |
| | | HttpResponse execute = post.execute(); |
| | | if(200 != execute.getStatus()){ |
| | | System.err.println("调用driver-server添加司机在线数据出错了"); |
| | | } |
| | | JSONObject jsonObject = JSON.parseObject(execute.body(), JSONObject.class); |
| | | if(jsonObject.getIntValue("code") != 200){ |
| | | System.err.println("调用driver-server添加司机在线数据出错了"); |
| | | } |
| | | |
| | | //TODO 存储最后一次上传的时间(用于保证车载端断电后1小时自动下班) |
| | | if(StringUtil.isNotEmpty(device) && device.equals("carDevice")){ |
| | | redisTemplate.opsForValue().set("DEVICE_" + userId1, String.valueOf(System.currentTimeMillis())); |
| | |
| | | System.out.println("id:" + driverId + "---lon" + lon + "---lat" + lat); |
| | | redisTemplate.opsForValue().set("DRIVER" + driverId, lon + "," + lat, 300, TimeUnit.SECONDS);//实时位置存入redis中 |
| | | }else{ |
| | | NettyServerController.sendMsgToClient(ctx, "__error__" + msg.toString()); |
| | | this.sendMsgToClient(ctx, "__error__" + msg.toString()); |
| | | } |
| | | }else{ |
| | | NettyServerController.sendMsgToClient(ctx, "__error__" + msg.toString()); |
| | | this.sendMsgToClient(ctx, "__error__" + msg.toString()); |
| | | } |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | if(isdebug) { |
| | | NettyServerController.sendMsgToClient(ctx, "__error__" + msg.toString()); |
| | | this.sendMsgToClient(ctx, "__error__" + msg.toString()); |
| | | } |
| | | e.printStackTrace(); |
| | | } |
| | |
| | | * @param msg |
| | | * @author TaoNingBo |
| | | */ |
| | | public static void sendMsgToClient(ChannelHandlerContext ctx, String msg) { |
| | | public void sendMsgToClient(ChannelHandlerContext ctx, String msg) { |
| | | if (ctx != null && ctx.channel().isActive()) { |
| | | ByteBuf buffer = Unpooled.copiedBuffer((msg).getBytes()); |
| | | ChannelFuture sync; |