123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- package com.tidecloud.dataacceptance.web;
- import java.util.HashMap;
- import java.util.Map;
- import org.apache.commons.lang.ArrayUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
- import com.smartsanitation.common.response.ResultWrapper;
- import com.smartsanitation.common.util.StringUtil;
- import com.tidecloud.dataacceptance.common.CRCUtil;
- import com.tidecloud.dataacceptance.common.NumUtil;
- import com.tidecloud.dataacceptance.entity.ConnectMsg;
- import com.tidecloud.dataacceptance.service.impl.YiTongGpsServerHandler;
- import io.netty.channel.Channel;
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisPool;
- /**
- * @author: chudk
- * @date: 2017年10月30日 上午9:44:50
- */
- @RestController("setting")
- public class DeviceController {
-
- private static final Logger logger = LoggerFactory.getLogger(DeviceController.class);
-
- @Autowired
- private JedisPool jedisPool;
-
- @RequestMapping("cuttingOilAndElecticity")
- public ResultWrapper<?> deviceController(@RequestParam String deviceIds, @RequestParam String commandStr){
- Map<String, ConnectMsg> deviceIdSocketIdMap = getDeviceSocketId(deviceIds);
- if (deviceIdSocketIdMap.isEmpty()) {
- return ResultWrapper.ok();
- }
- try {
- Map<String, Channel> channelMap = YiTongGpsServerHandler.socketyChannelMap;
- if (channelMap.isEmpty()) {
- return ResultWrapper.ok();
- }
- for (Map.Entry<String, ConnectMsg> deviceIdSocketIdEntry : deviceIdSocketIdMap.entrySet()) {
- ConnectMsg connectMsg = deviceIdSocketIdEntry.getValue();
- // String deviceId = deviceIdSocketIdEntry.getKey();
- Channel channel = channelMap.get(connectMsg.getSocketId());
- if (channel != null && channel.isActive()) {
- logger.info("start write command to device the address [{}]", channel.remoteAddress());
- sendCommand(channel, commandStr, (byte)0x80);
- logger.info("end write command to device the address [{}]", channel.remoteAddress());
- }
- }
- } catch (Exception e) {
- logger.error(e.getLocalizedMessage());
- }
- return null;
- }
- private void sendCommand(Channel channel, String commandStr, byte protocalNum) {
- byte commandLength = (byte) (4 + commandStr.getBytes().length);
- byte dataPackageLength = (byte) ( 8 + commandLength);
-
- byte[] subCommandBytes = new byte[]{dataPackageLength, protocalNum,
- commandLength, 0x00, 0x00, 0x00, 0x00};
- byte[] commandBytes = ArrayUtils.addAll(subCommandBytes, commandStr.getBytes());
-
- byte[] commandEnding = new byte[]{0x00, 0x01};
- byte[] command = ArrayUtils.addAll(commandBytes, commandEnding);
- int doCrc = CRCUtil.do_crc(65535, command);
-
- byte[] intToByte = NumUtil.intToByte(doCrc, 2);
-
- byte[] startBytes = new byte[]{0x78, 0x78};
- byte[] endBytes = new byte[]{0x0d, 0x0a};
- byte[] startCommandBytes = ArrayUtils.addAll(startBytes, command);
- byte[] lastCommandBytes = ArrayUtils.addAll(startCommandBytes, intToByte);
- byte[] lastCommand = ArrayUtils.addAll(lastCommandBytes, endBytes);
-
- channel.writeAndFlush(lastCommand);
- }
- private Map<String, ConnectMsg> getDeviceSocketId(String deviceIds) {
- Map<String, ConnectMsg> deviceIdSocketIdMap = new HashMap<String, ConnectMsg>();
- String[] deviceIdsArray = deviceIds.split(",");
- try (Jedis jedis = jedisPool.getResource()) {
- jedis.select(YiTongGpsServerHandler.REDIS_INDEX_LINK);
- for (String deviceId : deviceIdsArray) {
- String selectKey = YiTongGpsServerHandler.PREFIX_DEVICE + deviceId;
- String result = jedis.get(selectKey);
- if (result != null) {
- ConnectMsg connectMsg = StringUtil.convert2Object(result, ConnectMsg.class);
- deviceIdSocketIdMap.put(deviceId, connectMsg);
- }
- }
- } catch (Exception e) {
- logger.error(e.getMessage(), e);
- } finally {
- jedisPool.close();
- }
- return deviceIdSocketIdMap;
- }
- }
|