|
@@ -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);
|
|
|
+ }
|
|
|
+}
|