DeviceController.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package com.tidecloud.dataacceptance.web;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import org.apache.commons.lang.ArrayUtils;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestParam;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import com.smartsanitation.common.response.ResultWrapper;
  12. import com.smartsanitation.common.util.StringUtil;
  13. import com.tidecloud.dataacceptance.common.CRCUtil;
  14. import com.tidecloud.dataacceptance.common.NumUtil;
  15. import com.tidecloud.dataacceptance.entity.ConnectMsg;
  16. import com.tidecloud.dataacceptance.service.impl.YiTongGpsServerHandler;
  17. import io.netty.channel.Channel;
  18. import redis.clients.jedis.Jedis;
  19. import redis.clients.jedis.JedisPool;
  20. /**
  21. * @author: chudk
  22. * @date: 2017年10月30日 上午9:44:50
  23. */
  24. @RestController("setting")
  25. public class DeviceController {
  26. private static final Logger logger = LoggerFactory.getLogger(DeviceController.class);
  27. @Autowired
  28. private JedisPool jedisPool;
  29. @RequestMapping("cuttingOilAndElecticity")
  30. public ResultWrapper<?> deviceController(@RequestParam String deviceIds, @RequestParam String commandStr){
  31. Map<String, ConnectMsg> deviceIdSocketIdMap = getDeviceSocketId(deviceIds);
  32. if (deviceIdSocketIdMap.isEmpty()) {
  33. return ResultWrapper.ok();
  34. }
  35. try {
  36. Map<String, Channel> channelMap = YiTongGpsServerHandler.socketyChannelMap;
  37. if (channelMap.isEmpty()) {
  38. return ResultWrapper.ok();
  39. }
  40. for (Map.Entry<String, ConnectMsg> deviceIdSocketIdEntry : deviceIdSocketIdMap.entrySet()) {
  41. ConnectMsg connectMsg = deviceIdSocketIdEntry.getValue();
  42. // String deviceId = deviceIdSocketIdEntry.getKey();
  43. Channel channel = channelMap.get(connectMsg.getSocketId());
  44. if (channel != null && channel.isActive()) {
  45. logger.info("start write command to device the address [{}]", channel.remoteAddress());
  46. sendCommand(channel, commandStr, (byte)0x80);
  47. logger.info("end write command to device the address [{}]", channel.remoteAddress());
  48. }
  49. }
  50. } catch (Exception e) {
  51. logger.error(e.getLocalizedMessage());
  52. }
  53. return null;
  54. }
  55. private void sendCommand(Channel channel, String commandStr, byte protocalNum) {
  56. byte commandLength = (byte) (4 + commandStr.getBytes().length);
  57. byte dataPackageLength = (byte) ( 8 + commandLength);
  58. byte[] subCommandBytes = new byte[]{dataPackageLength, protocalNum,
  59. commandLength, 0x00, 0x00, 0x00, 0x00};
  60. byte[] commandBytes = ArrayUtils.addAll(subCommandBytes, commandStr.getBytes());
  61. byte[] commandEnding = new byte[]{0x00, 0x01};
  62. byte[] command = ArrayUtils.addAll(commandBytes, commandEnding);
  63. int doCrc = CRCUtil.do_crc(65535, command);
  64. byte[] intToByte = NumUtil.intToByte(doCrc, 2);
  65. byte[] startBytes = new byte[]{0x78, 0x78};
  66. byte[] endBytes = new byte[]{0x0d, 0x0a};
  67. byte[] startCommandBytes = ArrayUtils.addAll(startBytes, command);
  68. byte[] lastCommandBytes = ArrayUtils.addAll(startCommandBytes, intToByte);
  69. byte[] lastCommand = ArrayUtils.addAll(lastCommandBytes, endBytes);
  70. channel.writeAndFlush(lastCommand);
  71. }
  72. private Map<String, ConnectMsg> getDeviceSocketId(String deviceIds) {
  73. Map<String, ConnectMsg> deviceIdSocketIdMap = new HashMap<String, ConnectMsg>();
  74. String[] deviceIdsArray = deviceIds.split(",");
  75. try (Jedis jedis = jedisPool.getResource()) {
  76. jedis.select(YiTongGpsServerHandler.REDIS_INDEX_LINK);
  77. for (String deviceId : deviceIdsArray) {
  78. String selectKey = YiTongGpsServerHandler.PREFIX_DEVICE + deviceId;
  79. String result = jedis.get(selectKey);
  80. if (result != null) {
  81. ConnectMsg connectMsg = StringUtil.convert2Object(result, ConnectMsg.class);
  82. deviceIdSocketIdMap.put(deviceId, connectMsg);
  83. }
  84. }
  85. } catch (Exception e) {
  86. logger.error(e.getMessage(), e);
  87. } finally {
  88. jedisPool.close();
  89. }
  90. return deviceIdSocketIdMap;
  91. }
  92. }