Browse Source

道闸设备对接

qzyReal 2 years ago
parent
commit
40728db8cc

+ 78 - 0
business-system/venues-system/src/main/java/com/pj/biz/TbGateCommandLogController.java

@@ -0,0 +1,78 @@
+package com.pj.biz;
+
+import java.util.List;
+
+import com.pj.project.tb_gate_command_log.TbGateCommandLog;
+import com.pj.project.tb_gate_command_log.TbGateCommandLogService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.bind.annotation.*;
+
+import com.pj.utils.sg.*;
+import com.pj.utils.so.*;
+import com.pj.project4sp.SP;
+
+import com.pj.current.satoken.StpUserUtil;
+import cn.dev33.satoken.annotation.SaCheckPermission;
+
+
+/**
+ * Controller: tb_gate_command_log -- 车辆道闸命令日志
+ *
+ * @author qzyReal
+ */
+@RestController
+@RequestMapping("/TbGateCommandLog/")
+public class TbGateCommandLogController {
+
+    /**
+     * 底层 Service 对象
+     */
+    @Autowired
+    TbGateCommandLogService tbGateCommandLogService;
+
+
+    /**
+     * 删
+     */
+    @RequestMapping("delete")
+    @SaCheckPermission(TbGateCommandLog.PERMISSION_CODE_DEL)
+    public AjaxJson delete(Long id) {
+        tbGateCommandLogService.delete(id);
+        return AjaxJson.getSuccess();
+    }
+
+    /**
+     * 删 - 根据id列表
+     */
+    @RequestMapping("deleteByIds")
+    @SaCheckPermission(TbGateCommandLog.PERMISSION_CODE_DEL)
+    public AjaxJson deleteByIds() {
+        List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class);
+        int line = SP.publicMapper.deleteByIds(TbGateCommandLog.TABLE_NAME, ids);
+        return AjaxJson.getByLine(line);
+    }
+
+
+
+    /**
+     * 查 - 根据id
+     */
+    @RequestMapping("getById")
+    public AjaxJson getById(Long id) {
+        TbGateCommandLog t = tbGateCommandLogService.getById(id);
+        return AjaxJson.getSuccessData(t);
+    }
+
+    /**
+     * 查集合 - 根据条件(参数为空时代表忽略指定条件)
+     */
+    @RequestMapping("getList")
+    public AjaxJson getList() {
+        SoMap so = SoMap.getRequestSoMap();
+        List<TbGateCommandLog> list = tbGateCommandLogService.getList(so.startPage());
+        return AjaxJson.getPageData(so.getDataCount(), list);
+    }
+
+
+}

+ 109 - 0
business-system/venues-system/src/main/java/com/pj/biz/TbGateTerminalController.java

@@ -0,0 +1,109 @@
+package com.pj.biz;
+
+import java.util.List;
+
+import com.pj.project.tb_gate_terminal.TbGateTerminal;
+import com.pj.project.tb_gate_terminal.TbGateTerminalService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.bind.annotation.*;
+
+import com.pj.utils.sg.*;
+import com.pj.utils.so.*;
+import com.pj.project4sp.SP;
+
+import com.pj.current.satoken.StpUserUtil;
+import cn.dev33.satoken.annotation.SaCheckPermission;
+
+
+/**
+ * Controller: tb_gate_terminal -- 车辆道闸
+ *
+ * @author qzyReal
+ */
+@RestController
+@RequestMapping("/TbGateTerminal/")
+public class TbGateTerminalController {
+
+    /**
+     * 底层 Service 对象
+     */
+    @Autowired
+    TbGateTerminalService tbGateTerminalService;
+
+    /**
+     * 增
+     */
+    @RequestMapping("add")
+    @SaCheckPermission(TbGateTerminal.PERMISSION_CODE_ADD)
+    public AjaxJson add(TbGateTerminal t) throws Exception{
+        tbGateTerminalService.add(t);
+        return AjaxJson.getSuccessData(t);
+    }
+
+    /**
+     * 删
+     */
+    @RequestMapping("delete")
+    @SaCheckPermission(TbGateTerminal.PERMISSION_CODE_DEL)
+    public AjaxJson delete(Long id) {
+        tbGateTerminalService.delete(id);
+        return AjaxJson.getSuccess();
+    }
+
+    /**
+     * 删 - 根据id列表
+     */
+    @RequestMapping("deleteByIds")
+    @SaCheckPermission(TbGateTerminal.PERMISSION_CODE_DEL)
+    public AjaxJson deleteByIds() {
+        List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class);
+        int line = SP.publicMapper.deleteByIds(TbGateTerminal.TABLE_NAME, ids);
+        return AjaxJson.getByLine(line);
+    }
+
+    /**
+     * 改
+     */
+    @RequestMapping("update")
+    @SaCheckPermission(TbGateTerminal.PERMISSION_CODE_EDIT)
+    public AjaxJson update(TbGateTerminal t) throws Exception{
+        tbGateTerminalService.update(t);
+        return AjaxJson.getSuccess();
+    }
+
+    @RequestMapping("check")
+    public AjaxJson check(TbGateTerminal t) throws Exception{
+        tbGateTerminalService.check(t);
+        return AjaxJson.getSuccess();
+    }
+    @RequestMapping("open")
+    @SaCheckPermission(TbGateTerminal.PERMISSION_CODE_OPEN)
+    public AjaxJson open(Long id) throws Exception{
+        tbGateTerminalService.open(id);
+        return AjaxJson.getSuccess();
+    }
+
+
+
+    /**
+     * 查 - 根据id
+     */
+    @RequestMapping("getById")
+    public AjaxJson getById(Long id) {
+        TbGateTerminal t = tbGateTerminalService.getById(id);
+        return AjaxJson.getSuccessData(t);
+    }
+
+    /**
+     * 查集合 - 根据条件(参数为空时代表忽略指定条件)
+     */
+    @RequestMapping("getList")
+    public AjaxJson getList() {
+        SoMap so = SoMap.getRequestSoMap();
+        List<TbGateTerminal> list = tbGateTerminalService.getList(so.startPage());
+        return AjaxJson.getPageData(so.getDataCount(), list);
+    }
+
+
+}

+ 1 - 0
sp-core/src/main/java/com/pj/current/config/MyConfig.java

@@ -57,5 +57,6 @@ public class MyConfig {
 	private int commandPort;
 	private String commandPrefix;
 	private String heartPrefix;
+	private int gatePort;
 
 }

+ 4 - 0
sp-core/src/main/java/com/pj/current/global/GlobalExceptionHandler.java

@@ -1,5 +1,6 @@
 package com.pj.current.global;
 
+import java.net.SocketTimeoutException;
 import java.sql.SQLException;
 
 import org.springframework.data.redis.RedisConnectionFailureException;
@@ -67,6 +68,9 @@ public class GlobalExceptionHandler {
 		else if (e instanceof BusinessException){
 			aj = AjaxJson.getError(e.getMessage());
 			SpApilogUtil.endRequest(aj);
+		}else if (e instanceof SocketTimeoutException){
+			aj = AjaxJson.getError("连接超时");
+			aj.set("reqId", SpApilogUtil.getCurrReqId());
 		}
 		// 普通异常输出:500 + 异常信息 
 		else {

+ 93 - 0
sp-core/src/main/java/com/pj/project/tb_gate_command_log/TbGateCommandLog.java

@@ -0,0 +1,93 @@
+package com.pj.project.tb_gate_command_log;
+
+import java.io.Serializable;
+import java.util.Date;
+
+import com.baomidou.mybatisplus.annotation.*;
+import com.baomidou.mybatisplus.extension.activerecord.Model;
+import lombok.EqualsAndHashCode;
+
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+/**
+ * Model: tb_gate_command_log -- 车辆道闸命令日志
+ * @author qzyReal 
+ */
+@Data
+@Accessors(chain = true)
+@TableName(TbGateCommandLog.TABLE_NAME)
+@EqualsAndHashCode(callSuper = false)
+public class TbGateCommandLog extends Model<TbGateCommandLog> implements Serializable {
+
+	// ---------- 模块常量 ----------
+	/**
+	 * 序列化版本id 
+	 */
+	private static final long serialVersionUID = 1L;	
+	/**
+	 * 此模块对应的表名 
+	 */
+	public static final String TABLE_NAME = "tb_gate_command_log";	
+	/**
+	 * 此模块对应的权限码 
+	 */
+	public static final String PERMISSION_CODE = "tb-gate-command-log";	
+	public static final String PERMISSION_CODE_DEL = "tb-gate-command-log-del";
+
+
+	// ---------- 表中字段 ----------
+	/**
+	 * 主键 
+	 */
+	@TableId(type = IdType.AUTO)
+	private Long id;	
+
+	/**
+	 * 部门ID 
+	 */
+	private Long deptId;	
+
+	/**
+	 * 命令 
+	 */
+	private String command;	
+
+	/**
+	 * 设备SN 
+	 */
+	private String sn;	
+
+	/**
+	 * 设备名 
+	 */
+	private String terminalName;	
+
+	/**
+	 * 通道 
+	 */
+	private String venuesName;	
+
+	/**
+	 * 下发时间 
+	 */
+	private Date createTime;
+
+	/**
+	 * 回复内容 
+	 */
+	private String responseContent;	
+
+	/**
+	 * 创建人 
+	 */
+	private String createBy;	
+
+
+
+
+
+	
+
+
+}

+ 28 - 0
sp-core/src/main/java/com/pj/project/tb_gate_command_log/TbGateCommandLogMapper.java

@@ -0,0 +1,28 @@
+package com.pj.project.tb_gate_command_log;
+
+import java.util.List;
+
+import org.apache.ibatis.annotations.Mapper;
+
+import com.pj.utils.so.*;
+	import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.springframework.stereotype.Repository;
+
+/**
+* Mapper: tb_gate_command_log -- 车辆道闸命令日志
+* @author qzyReal
+*/
+
+@Mapper
+@Repository
+public interface TbGateCommandLogMapper extends BaseMapper <TbGateCommandLog> {
+
+/**
+* 查集合 - 根据条件(参数为空时代表忽略指定条件)
+* @param so 参数集合
+* @return 数据列表
+*/
+List<TbGateCommandLog> getList(SoMap so);
+
+
+}

+ 68 - 0
sp-core/src/main/java/com/pj/project/tb_gate_command_log/TbGateCommandLogMapper.xml

@@ -0,0 +1,68 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.pj.project.tb_gate_command_log.TbGateCommandLogMapper">
+
+
+	<!-- ================================== 查询相关 ================================== -->
+			<!-- select id, dept_id, command, sn, terminal_name, venues_name, create_time, response_content, create_by from tb_gate_command_log  -->
+
+		<!-- 通用映射:手动模式 -->
+		<resultMap id="model" type="com.pj.project.tb_gate_command_log.TbGateCommandLog">
+				<result property="id" column="id" />
+				<result property="deptId" column="dept_id" />
+				<result property="command" column="command" />
+				<result property="sn" column="sn" />
+				<result property="terminalName" column="terminal_name" />
+				<result property="venuesName" column="venues_name" />
+				<result property="createTime" column="create_time" />
+				<result property="responseContent" column="response_content" />
+				<result property="createBy" column="create_by" />
+		</resultMap>
+
+	<!-- 公共查询sql片段 -->
+	<sql id="select_sql">
+		select *
+		from tb_gate_command_log
+	</sql>
+
+
+
+	<!-- 查集合 - 根据条件(参数为空时代表忽略指定条件) [G] -->
+	<select id="getList" resultMap="model">
+		<include refid="select_sql"></include>
+		<where>
+						<if test=' this.has("id") '> and id = #{id} </if>
+			<if test=' this.has("deptId") '> and dept_id = #{deptId} </if>
+			<if test=' this.has("command") '> and command = #{command} </if>
+			<if test=' this.has("sn") '> and sn = #{sn} </if>
+			<if test=' this.has("terminalName") '> and terminal_name = #{terminalName} </if>
+			<if test=' this.has("venuesName") '> and venues_name = #{venuesName} </if>
+			<if test=' this.has("createTime") '> and create_time = #{createTime} </if>
+			<if test=' this.has("responseContent") '> and response_content = #{responseContent} </if>
+			<if test=' this.has("createBy") '> and create_by = #{createBy} </if>
+		</where>
+		order by
+		<choose>
+						<when test='sortType == 1'> id desc </when>
+			<when test='sortType == 2'> dept_id desc </when>
+			<when test='sortType == 3'> command desc </when>
+			<when test='sortType == 4'> sn desc </when>
+			<when test='sortType == 5'> terminal_name desc </when>
+			<when test='sortType == 6'> venues_name desc </when>
+			<when test='sortType == 7'> create_time desc </when>
+			<when test='sortType == 8'> response_content desc </when>
+			<when test='sortType == 9'> create_by desc </when>
+			<otherwise> id desc </otherwise>
+		</choose>
+	</select>
+
+
+
+
+
+
+
+
+
+
+</mapper>

+ 63 - 0
sp-core/src/main/java/com/pj/project/tb_gate_command_log/TbGateCommandLogService.java

@@ -0,0 +1,63 @@
+package com.pj.project.tb_gate_command_log;
+
+import java.util.List;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import com.pj.utils.so.*;
+
+/**
+ * Service: tb_gate_command_log -- 车辆道闸命令日志
+ *
+ * @author qzyReal
+ */
+@Service
+public class TbGateCommandLogService extends ServiceImpl<TbGateCommandLogMapper, TbGateCommandLog> implements IService<TbGateCommandLog> {
+
+    /**
+     * 底层 Mapper 对象
+     */
+    @Autowired
+    TbGateCommandLogMapper tbGateCommandLogMapper;
+
+
+    /**
+     * 增
+     */
+    public void add(TbGateCommandLog t) {
+        this.save(t);
+    }
+
+    /**
+     * 删
+     */
+    public void delete(Long id) {
+        this.removeById(id);
+    }
+
+    /**
+     * 改
+     */
+    public void update(TbGateCommandLog t) {
+        this.updateById(t);
+    }
+
+    /**
+     * 查
+     */
+    public TbGateCommandLog getById(Long id) {
+        return super.getById(id);
+    }
+
+    /**
+     * 查集合 - 根据条件(参数为空时代表忽略指定条件)
+     */
+    public List<TbGateCommandLog> getList(SoMap so) {
+        return tbGateCommandLogMapper.getList(so);
+    }
+
+
+}

+ 109 - 0
sp-core/src/main/java/com/pj/project/tb_gate_terminal/TbGateTerminal.java

@@ -0,0 +1,109 @@
+package com.pj.project.tb_gate_terminal;
+
+import java.io.Serializable;
+import java.util.Date;
+
+import com.baomidou.mybatisplus.annotation.*;
+import com.baomidou.mybatisplus.extension.activerecord.Model;
+import lombok.EqualsAndHashCode;
+
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+/**
+ * Model: tb_gate_terminal -- 车辆道闸
+ * @author qzyReal 
+ */
+@Data
+@Accessors(chain = true)
+@TableName(TbGateTerminal.TABLE_NAME)
+@EqualsAndHashCode(callSuper = false)
+public class TbGateTerminal extends Model<TbGateTerminal> implements Serializable {
+
+	// ---------- 模块常量 ----------
+	/**
+	 * 序列化版本id 
+	 */
+	private static final long serialVersionUID = 1L;	
+	/**
+	 * 此模块对应的表名 
+	 */
+	public static final String TABLE_NAME = "tb_gate_terminal";	
+	/**
+	 * 此模块对应的权限码 
+	 */
+	public static final String PERMISSION_CODE = "tb-gate-terminal";	
+	public static final String PERMISSION_CODE_ADD = "tb-gate-terminal-add";
+	public static final String PERMISSION_CODE_EDIT = "tb-gate-terminal-edit";
+	public static final String PERMISSION_CODE_DEL = "tb-gate-terminal-del";
+	public static final String PERMISSION_CODE_OPEN = "tb-gate-terminal-open";
+
+
+	// ---------- 表中字段 ----------
+	/**
+	 * 主键 
+	 */
+	@TableId(type = IdType.AUTO)
+	private Long id;	
+
+	/**
+	 * 名称 
+	 */
+	private String name;	
+
+	/**
+	 * 部门id 
+	 */
+	private Long deptId;
+	/**
+	 * 场地id
+	 */
+	private Long venuesId;
+	/**
+	 * 进出 1 进,2出
+	 */
+	private Integer direction;
+
+	/**
+	 * 序列号 
+	 */
+	private String sn;	
+
+	/**
+	 * IP地址 
+	 */
+	private String ip;	
+
+	/**
+	 * 状态 
+	 */
+	private String state;	
+
+	/**
+	 * 创建时间 
+	 */
+	private Date createTime;
+	/**
+	 * 创建人
+	 */
+	private String createBy;
+	private String updateBy;
+
+	/**
+	 * 更新时间 
+	 */
+	private Date updateTime;
+
+	@TableField(exist = false)
+	private String deptName;
+	@TableField(exist = false)
+	private String venuesName;
+
+
+
+
+
+	
+
+
+}

+ 30 - 0
sp-core/src/main/java/com/pj/project/tb_gate_terminal/TbGateTerminalMapper.java

@@ -0,0 +1,30 @@
+package com.pj.project.tb_gate_terminal;
+
+import java.util.List;
+
+import org.apache.ibatis.annotations.Mapper;
+
+import com.pj.utils.so.*;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.springframework.stereotype.Repository;
+
+/**
+ * Mapper: tb_gate_terminal -- 车辆道闸
+ *
+ * @author qzyReal
+ */
+
+@Mapper
+@Repository
+public interface TbGateTerminalMapper extends BaseMapper<TbGateTerminal> {
+
+    /**
+     * 查集合 - 根据条件(参数为空时代表忽略指定条件)
+     *
+     * @param so 参数集合
+     * @return 数据列表
+     */
+    List<TbGateTerminal> getList(SoMap so);
+
+
+}

+ 67 - 0
sp-core/src/main/java/com/pj/project/tb_gate_terminal/TbGateTerminalMapper.xml

@@ -0,0 +1,67 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.pj.project.tb_gate_terminal.TbGateTerminalMapper">
+
+
+	<!-- ================================== 查询相关 ================================== -->
+			<!-- select id, name, dept_id, sn, ip, state, create_time, update_time from tb_gate_terminal  -->
+
+		<!-- 通用映射:手动模式 -->
+		<resultMap id="model" type="com.pj.project.tb_gate_terminal.TbGateTerminal">
+				<result property="id" column="id" />
+				<result property="name" column="name" />
+				<result property="deptId" column="dept_id" />
+				<result property="sn" column="sn" />
+				<result property="ip" column="ip" />
+				<result property="state" column="state" />
+				<result property="createTime" column="create_time" />
+				<result property="updateTime" column="update_time" />
+		</resultMap>
+
+	<!-- 公共查询sql片段 -->
+	<sql id="select_sql">
+		select *,
+		(select name from tb_dept where id=tb_gate_terminal.dept_id) as deptName,
+		(select name from tb_venues where id=tb_gate_terminal.venues_id) as venuesName
+		from tb_gate_terminal
+	</sql>
+
+
+
+	<!-- 查集合 - 根据条件(参数为空时代表忽略指定条件) [G] -->
+	<select id="getList" resultMap="model">
+		<include refid="select_sql"></include>
+		<where>
+						<if test=' this.has("id") '> and id = #{id} </if>
+			<if test=' this.has("name") '> and name like concat('%',#{name},'%') </if>
+			<if test=' this.has("deptId") '> and dept_id = #{deptId} </if>
+			<if test=' this.has("sn") '> and sn = #{sn} </if>
+			<if test=' this.has("ip") '> and ip like concat('%', #{ip},'%') </if>
+			<if test=' this.has("state") '> and state = #{state} </if>
+			<if test=' this.has("createTime") '> and create_time = #{createTime} </if>
+			<if test=' this.has("updateTime") '> and update_time = #{updateTime} </if>
+		</where>
+		order by
+		<choose>
+						<when test='sortType == 1'> id desc </when>
+			<when test='sortType == 2'> name desc </when>
+			<when test='sortType == 3'> dept_id desc </when>
+			<when test='sortType == 4'> sn desc </when>
+			<when test='sortType == 5'> ip desc </when>
+			<when test='sortType == 6'> state desc </when>
+			<when test='sortType == 7'> create_time desc </when>
+			<when test='sortType == 8'> update_time desc </when>
+			<otherwise> id desc </otherwise>
+		</choose>
+	</select>
+
+
+
+
+
+
+
+
+
+
+</mapper>

+ 169 - 0
sp-core/src/main/java/com/pj/project/tb_gate_terminal/TbGateTerminalService.java

@@ -0,0 +1,169 @@
+package com.pj.project.tb_gate_terminal;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.util.Date;
+import java.util.List;
+
+import cn.dev33.satoken.stp.StpUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.json.JSONUtil;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.pj.current.config.MyConfig;
+import com.pj.current.global.BusinessException;
+import com.pj.current.satoken.StpUserUtil;
+import com.pj.project.tb_gate_command_log.TbGateCommandLog;
+import com.pj.project.tb_gate_command_log.TbGateCommandLogService;
+import com.pj.project.tb_venues.TbVenues;
+import com.pj.project.tb_venues.TbVenuesService;
+import com.pj.utils.zkt.GateCommandUtils;
+import com.pj.utils.zkt.ZKTecoUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import com.pj.utils.so.*;
+import sun.net.util.IPAddressUtil;
+
+import javax.annotation.Resource;
+
+/**
+ * Service: tb_gate_terminal -- 车辆道闸
+ *
+ * @author qzyReal
+ */
+@Service
+@Transactional
+public class TbGateTerminalService extends ServiceImpl<TbGateTerminalMapper, TbGateTerminal> implements IService<TbGateTerminal> {
+
+
+    @Resource
+    private MyConfig myConfig;
+
+    /**
+     * 底层 Mapper 对象
+     */
+    @Autowired
+    TbGateTerminalMapper tbGateTerminalMapper;
+
+    @Resource
+    private TbVenuesService tbVenuesService;
+    @Resource
+    private TbGateCommandLogService tbGateCommandLogService;
+
+
+    /**
+     * 增
+     */
+    public void add(TbGateTerminal t) throws Exception {
+        String ip = t.getIp();
+        if (!IPAddressUtil.isIPv4LiteralAddress(ip)) {
+            throw new BusinessException("IP地址不正确");
+        }
+        TbGateTerminal tbGateTerminal = findByIp(ip);
+        if (tbGateTerminal != null) {
+            throw new BusinessException("IP地址已被占用");
+        }
+        String sn = getSn(ip);
+        Date now = new Date();
+        String loginId = StpUtil.getLoginIdAsString();
+        t.setSn(sn).setCreateTime(now).setUpdateTime(now).setCreateBy(loginId).setUpdateBy(loginId);
+        TbVenues tbVenues = tbVenuesService.getById(t.getVenuesId());
+        t.setDeptId(tbVenues.getDeptId());
+        this.save(t);
+    }
+
+    /**
+     * 获取sn
+     *
+     * @param ip
+     * @return
+     * @throws Exception
+     */
+    private String getSn(String ip) throws Exception {
+        Socket socket = new Socket();
+        socket.connect(new InetSocketAddress(ip, myConfig.getGatePort()), 4000);
+        String result = ZKTecoUtils.sendAndGetResult(socket, GateCommandUtils.getSn());
+        return JSONUtil.parseObj(result).getStr("value");
+    }
+
+    private TbGateTerminal findByIp(String ip) {
+        QueryWrapper<TbGateTerminal> ew = new QueryWrapper<>();
+        ew.eq("ip", ip);
+        return getOne(ew);
+    }
+
+    /**
+     * 删
+     */
+    public void delete(Long id) {
+        this.removeById(id);
+    }
+
+    /**
+     * 改
+     */
+    public void update(TbGateTerminal t) throws Exception {
+        TbGateTerminal ipTerminal = findByIp(t.getIp());
+        if (!t.getId().equals(ipTerminal.getId())) {
+            throw new BusinessException("IP地址已被占用");
+        }
+        TbGateTerminal db = this.getById(t.getId());
+        if (!StrUtil.equals(t.getIp(), db.getIp())) {
+            String sn = getSn(t.getIp());
+            t.setSn(sn);
+        }
+        t.setUpdateBy(StpUtil.getLoginIdAsString()).setUpdateTime(new Date());
+        this.updateById(t);
+    }
+
+    /**
+     * 查
+     */
+    public TbGateTerminal getById(Long id) {
+        return super.getById(id);
+    }
+
+    /**
+     * 查集合 - 根据条件(参数为空时代表忽略指定条件)
+     */
+    public List<TbGateTerminal> getList(SoMap so) {
+
+        return tbGateTerminalMapper.getList(so);
+    }
+
+    /**
+     * 检测通讯
+     *
+     * @param t
+     * @throws Exception
+     */
+    public void check(TbGateTerminal t) throws Exception {
+        String ip = t.getIp();
+        getSn(ip);
+    }
+
+    /**
+     * 远程开门
+     *
+     * @param id
+     */
+    public void open(Long id) throws Exception {
+        TbGateTerminal tbGateTerminal = this.getById(id);
+        TbVenues tbVenues = tbVenuesService.getById(tbGateTerminal.getVenuesId());
+        Socket socket = new Socket();
+        socket.connect(new InetSocketAddress(tbGateTerminal.getIp(), myConfig.getGatePort()), 4000);
+        String result = ZKTecoUtils.sendAndGetResult(socket, GateCommandUtils.openCommand());
+        Integer code = JSONUtil.parseObj(result).getInt("state_code");
+        if (code == null || code != 200) {
+            throw new BusinessException("开闸失败");
+        }
+        TbGateCommandLog log = new TbGateCommandLog();
+        log.setTerminalName(tbGateTerminal.getName()).setVenuesName(tbVenues.getName()).setDeptId(tbGateTerminal.getDeptId())
+                .setCommand(GateCommandUtils.openCommand()).setResponseContent(result).setSn(tbGateTerminal.getSn())
+                .setCreateBy(StpUserUtil.getAdminName()).setCreateTime(new Date());
+        tbGateCommandLogService.save(log);
+    }
+}

+ 1 - 0
sp-core/src/main/java/com/pj/project/tb_terminal/TbTerminal.java

@@ -80,6 +80,7 @@ public class TbTerminal extends Model<TbTerminal> implements Serializable {
 	 * 场所ID 
 	 */
 	private Long venuesId;	
+	private Integer direction;
 
 	/**
 	 * 场所名称 

+ 24 - 0
sp-core/src/main/java/com/pj/utils/zkt/GateCommandUtils.java

@@ -0,0 +1,24 @@
+package com.pj.utils.zkt;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+
+public class GateCommandUtils {
+    /**
+     * sn命令
+     * @return
+     */
+    public static String getSn(){
+        return "{\"cmd\" :\"getsn\"}";
+    }
+
+    /**
+     * 开闸命令
+     * @return
+     */
+    public static String openCommand(){
+        return "{\"cmd\":\"ioctl_resp\",\"io\" :0,\"value\":2,\"delay\":500}";
+    }
+
+}

+ 99 - 0
sp-core/src/main/java/com/pj/utils/zkt/ZKTecoUtils.java

@@ -0,0 +1,99 @@
+package com.pj.utils.zkt;
+
+import cn.hutool.log.StaticLog;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.Socket;
+
+public class ZKTecoUtils {
+
+    public static boolean sendCmd(Socket socket, String cmd) {
+        try {
+            int len = cmd.getBytes().length;
+            byte[] header = {'V', 'Z', 0, 0, 0, 0, 0, 0};
+            header[4] += (byte) ((len >> 24) & 0xFF);
+            header[5] += (byte) ((len >> 16) & 0xFF);
+            header[6] += (byte) ((len >> 8) & 0xFF);
+            header[7] += (byte) (len & 0xFF);
+            OutputStream out = socket.getOutputStream();
+            out.write(header);
+            out.write(cmd.getBytes());
+        } catch (Exception e) {
+            StaticLog.error("下发道闸命令异常:{},{}", cmd, e.getMessage());
+            return false;
+        }
+        return true;
+    }
+
+    public static String sendAndGetResult(Socket socket, String cmd) {
+        boolean success = sendCmd(socket, cmd);
+        if (!success) {
+            return "";
+        }
+        int sn_len = recvPacketSize(socket);
+        if (sn_len > 0) {
+            //接收实际数据
+            byte[] data = new byte[sn_len];
+            int recvLen = ZKTecoUtils.recvBlock(socket, data, sn_len);
+            String result = new String(data, 0, recvLen);
+            StaticLog.info("下发道闸返回:{},{}", cmd, result);
+            return result;
+        }
+        return "";
+    }
+
+    public static int recvPacketSize(Socket socket) {
+        byte[] header = new byte[8];
+        int recvLen = recvBlock(socket, header, 8);
+        if (recvLen <= 0) {
+            return -1;
+        }
+
+        if (header[0] != 'V' || header[1] != 'Z') {
+            //格式不对
+            return -1;
+        }
+
+        if (header[2] == 1) {
+            //心跳包
+            return 0;
+        }
+
+        return convBytesToInt(header, 4);
+    }
+
+    private static int convBytesToInt(byte[] buff, int offset) {
+        //4bytes 转为int,要考虑机器的大小端问题
+        int len, byteValue;
+        len = 0;
+        byteValue = (0x000000FF & ((int) buff[offset]));
+        len += byteValue << 24;
+        byteValue = (0x000000FF & ((int) buff[offset + 1]));
+        len += byteValue << 16;
+        byteValue = (0x000000FF & ((int) buff[offset + 2]));
+        len += byteValue << 8;
+        byteValue = (0x000000FF & ((int) buff[offset + 3]));
+        len += byteValue;
+        return len;
+    }
+
+    //接收指定长度的数据,收完为止
+    public static int recvBlock(Socket socket, byte[] buff, int len) {
+        try {
+            InputStream in = socket.getInputStream();
+            int totleRecvLen = 0;
+            int recvLen;
+            while (totleRecvLen < len) {
+                recvLen = in.read(buff, totleRecvLen, len - totleRecvLen);
+                totleRecvLen += recvLen;
+            }
+            return len;
+        } catch (Exception e) {
+            System.out.println("recvBlock timeout!");
+            // System.out.println("Error:"+e);
+            return -1;
+        }
+    }
+
+}

+ 2 - 1
sp-start/src/main/resources/application-dev.yml

@@ -50,4 +50,5 @@ spring:
         heart-port: 9999
         command-port: 8888
         command-prefix: "command:"
-        heart-prefix: "heart:"
+        heart-prefix: "heart:"
+        gate-port: 8131