IOT设备

This commit is contained in:
76164 2024-08-09 17:11:19 +08:00
parent 70c87e41b1
commit 0c15f6917e
13 changed files with 534 additions and 69 deletions

View File

@ -226,4 +226,10 @@ public class MaMachine extends BaseEntity {
private String maTypeName;
@ApiModelProperty(value = "绑定IOT设备数量")
private Integer IotNum;
@ApiModelProperty(value = "Iot定位设备编号")
private String iotCode;
}

View File

@ -63,6 +63,7 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.5</version>
</dependency>
<!-- Alibaba Fastjson -->
@ -113,11 +114,15 @@
<artifactId>swagger-annotations</artifactId>
</dependency>
<!-- lombok-->
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>

View File

@ -91,4 +91,31 @@ public class HttpStatus
* 系统警告消息
*/
public static final int WARN = 601;
/**
*iot设备登录地址
*/
public static final String LOGIN_URL = "http://gps51.com/webapi?action=login";
/**
*iot设备定位地址
*/
public static final String LAST_POSITION_URL = "http://gps51.com/webapi?action=lastposition&token=";
/**
*iot查询行程地址
*/
public static final String QUERY_TRIPS_URL = "http://gps51.com/webapi?action=querytrips&token=";
/**
*iot查询停留点地址
*/
public static final String REPORT_PARK_DETAIL_BY_TIME_URL = "http://gps51.com/webapi?action=reportparkdetailbytime&token=";
/**
*iot报警地址
*/
public static final String REPORT_ALARM_URL = "http://gps51.com/webapi?action=reportalarm&token=";
}

View File

@ -22,4 +22,10 @@ public class TokenConstants
*/
public final static String SECRET = "abcdefghijklmnopqrstuvwxyz";
/**
* IOT设备登录token有效期
*/
public static final String TOKEN_LOCATION = "token_location";
}

View File

@ -92,4 +92,24 @@ public class UserConstants
* 手机1天限制
*/
public static final String MOBILE_PHONE_1D_LIMIT_DIR = "mobilePhoneDay:";
/**
*登录类型 :"USER""DEVICE"
*/
public static final String IOT_TYPE = "USER";
/**
*登录来源 :"ANDROID""IPHONE""WEB""WEIXIN"
*/
public static final String IOT_FROM = "WEB";
/**
*登录名
*/
public static final String IOT_USERNAME = "omni-demo";
/**
*密码,采用md5加密
*/
public static final String IOT_PASSWORD = "7d8609761efffc818e5ed11531ea2507";
}

View File

@ -3,6 +3,7 @@ package com.bonus.sgzb.common.core.utils;
import org.apache.commons.lang3.StringUtils;
import java.io.*;
import java.math.BigDecimal;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.List;
@ -226,4 +227,23 @@ public class StringHelper {
return !isEmpty(str);
}
/**
* 类型转换
* @param res
* @return
*/
public static Long conversionLong(String res) {
if (res == null) {
return null;
}
return Long.valueOf(res);
}
public static BigDecimal conversionBigDecimal(String res) {
if (res == null) {
return null;
}
return new BigDecimal(res);
}
}

View File

@ -6,10 +6,8 @@ import com.bonus.sgzb.base.api.domain.MaLabelBind;
import com.bonus.sgzb.base.api.domain.SltAgreementApply;
import com.bonus.sgzb.base.domain.BmProjectLot;
import com.bonus.sgzb.base.domain.MaPropSet;
import com.bonus.sgzb.base.mapper.MaLabelBindMapper;
import com.bonus.sgzb.base.mapper.MaMachineMapper;
import com.bonus.sgzb.base.mapper.MaPropInfoMapper;
import com.bonus.sgzb.base.mapper.MaTypeMapper;
import com.bonus.sgzb.base.domain.vo.IotRecordVo;
import com.bonus.sgzb.base.mapper.*;
import com.bonus.sgzb.base.service.MaMachineService;
import com.bonus.sgzb.common.core.constant.Constants;
import com.bonus.sgzb.common.core.exception.ServiceException;
@ -18,6 +16,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@ -36,6 +35,8 @@ public class MaMachineServiceImpl implements MaMachineService {
@Resource
private MaPropInfoMapper maPropInfoMapper;
@Resource
private IotMachineMapper iotMachineMapper;
@Override
public List<MaMachine> getMaMachine(MaMachine maMachine) {
@ -45,7 +46,28 @@ public class MaMachineServiceImpl implements MaMachineService {
if (maMachine.getMaType() != null && ("2").equals(maMachine.getMaType())) {
maMachine.setMaTypeName(Constants.TS);
}
return maMachineMapper.getMaMachine(maMachine);
List<MaMachine> listMaMachine = new ArrayList<>();
listMaMachine =maMachineMapper.getMaMachine(maMachine);
if (listMaMachine.size() > 0){
for (MaMachine ma : listMaMachine) {
//根据机具编码查询iot设备
List<IotRecordVo> iotRecordVoList = iotMachineMapper.selectList(ma.getMaCode());
ma.setIotNum(iotRecordVoList.size());
//判断是否绑定了iot设备
if (iotRecordVoList.size()>0){
for (int i = 0; i < iotRecordVoList.size(); i++) {
//保存定位iot设备编码
if ("125".equals(iotRecordVoList.get(i).getIotType())){
ma.setIotCode(iotRecordVoList.get(i).getIotCode());
}
}
}
}
}
return listMaMachine;
}
/**

View File

@ -20,8 +20,10 @@ public enum ExceptionEnum {
DELETE_TO_DATABASE(500, "删除失败,请联系管理员!!!"),
BIND_TO_DATABASE(500, "绑定失败,请联系管理员!!!"),
UN_BIND_TO_DATABASE(500, "解绑失败,请联系管理员!!!"),
UPDATE_TO_DATABASE(500, "修改失败,请联系管理员!!!");
UPDATE_TO_DATABASE(500, "修改失败,请联系管理员!!!"),
RETURN_DATA_IS_EMPTY(501, "返回数据为空!!"),
IOT_ENCODING_ERROR(502, "输入的IOT编码有误请输入正确的编码");
private Integer code;
private String msg;

View File

@ -4,6 +4,7 @@ import com.bonus.sgzb.common.core.web.controller.BaseController;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.common.core.web.page.TableDataInfo;
import com.bonus.sgzb.material.domain.IotDto;
import com.bonus.sgzb.material.domain.IotLocationVo;
import com.bonus.sgzb.material.domain.IotRecordVo;
import com.bonus.sgzb.material.domain.IotVo;
import com.bonus.sgzb.material.service.IotMachineService;
@ -17,8 +18,8 @@ import javax.annotation.Resource;
import java.util.List;
/**
* @Author
* @create 2024/8/5
* @Author ma_sh
* @create 2024/7/4 16:23
* iot设备管理控制层
*/
@Api(tags = " iot设备管理控制层")
@ -82,6 +83,18 @@ public class IotMachineController extends BaseController {
return iotMachineService.bind(iotDto);
}
/**
* 绑定设备APP
* @param iotDto
* @return
*/
@ApiOperation("App绑定设备")
@PostMapping("/bindApp")
public AjaxResult bindApp(@Validated @RequestBody IotDto iotDto) {
log.info("绑定设备传参:{}", iotDto);
return iotMachineService.bindApp(iotDto);
}
/**
* 解绑设备
* @param iotDto
@ -146,4 +159,32 @@ public class IotMachineController extends BaseController {
return AjaxResult.success(list);
}
@ApiOperation("获取定位")
@PostMapping("/getLocation")
public AjaxResult getLocation(@Validated @RequestBody IotDto iotDto) {
log.info("获取定位接口:{}", iotDto);
return iotMachineService.getLocation(iotDto);
}
@ApiOperation("查询行程")
@PostMapping("/searchItinerary")
public AjaxResult searchItinerary(@Validated @RequestBody IotLocationVo iotLocationVo) {
log.info("查询行程接口:{}", iotLocationVo);
return iotMachineService.searchItinerary(iotLocationVo);
}
@ApiOperation("查询停留点")
@PostMapping("/reportParkDetailByTime")
public AjaxResult reportParkDetailByTime(@Validated @RequestBody IotLocationVo iotLocationVo) {
log.info("查询停留点接口:{}", iotLocationVo);
return iotMachineService.reportParkDetailByTime(iotLocationVo);
}
@ApiOperation("报警记录")
@PostMapping("/reportAlarm")
public AjaxResult reportAlarm(@Validated @RequestBody IotLocationVo iotLocationVo) {
log.info("报警记录接口:{}", iotLocationVo);
return iotMachineService.reportAlarm(iotLocationVo);
}
}

View File

@ -2,6 +2,7 @@ package com.bonus.sgzb.material.service;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.material.domain.IotDto;
import com.bonus.sgzb.material.domain.IotLocationVo;
import com.bonus.sgzb.material.domain.IotRecordVo;
import com.bonus.sgzb.material.domain.IotVo;
@ -76,4 +77,39 @@ public interface IotMachineService {
* @return
*/
List<IotRecordVo> getRecordList(IotDto iotDto);
/**
* 获取设备定位
* @param iotDto
* @return
*/
AjaxResult getLocation(IotDto iotDto);
/**
* 查询行程
* @param iotLocationVo
* @return
*/
AjaxResult searchItinerary(IotLocationVo iotLocationVo);
/**
* 查询停留点
* @param iotLocationVo
* @return
*/
AjaxResult reportParkDetailByTime(IotLocationVo iotLocationVo);
/**
* App绑定设备
* @param iotDto
* @return
*/
AjaxResult bindApp(IotDto iotDto);
/**
* 报警记录
* @param iotLocationVo
* @return
*/
AjaxResult reportAlarm(IotLocationVo iotLocationVo);
}

View File

@ -1,6 +1,14 @@
package com.bonus.sgzb.material.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bonus.sgzb.common.core.constant.HttpStatus;
import com.bonus.sgzb.common.core.constant.TokenConstants;
import com.bonus.sgzb.common.core.utils.HttpHelper;
import com.bonus.sgzb.common.core.utils.StringHelper;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.common.redis.service.RedisService;
import com.bonus.sgzb.common.security.utils.SecurityUtils;
import com.bonus.sgzb.material.config.ExceptionEnum;
import com.bonus.sgzb.material.config.FieldGenerator;
@ -13,7 +21,11 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* iot设备管理实现层
@ -26,6 +38,9 @@ public class IotMachineServiceImpl implements IotMachineService {
@Resource
private IotMachineMapper iotMachineMapper;
@Resource
private RedisService redisService;
/**
* 添加设备-保存
*
@ -265,4 +280,265 @@ public class IotMachineServiceImpl implements IotMachineService {
return iotRecordList;
}
/**
* 获取定位
* @param iotDto
* @return
*/
@Override
public AjaxResult getLocation(IotDto iotDto) {
log.info("getLocation:{}", iotDto);
if (iotDto == null || iotDto.getIotId() == null) {
throw new RuntimeException(ExceptionEnum.PARAM_NULL.getMsg());
}
List<String> list=new ArrayList<>();
Map<String,Object> map=new HashMap<>();
//从redis中获取token
String redisCode = redisService.getCacheObject(TokenConstants.TOKEN_LOCATION);
//如果token为空先去调登录接口获取token
if (redisCode == null){
redisCode = HttpHelper.getIotToken();
redisService.setCacheObject(TokenConstants.TOKEN_LOCATION,redisCode,23L, TimeUnit.HOURS);
}
map.clear();
list.add(iotDto.getIotId().toString());
map.put("deviceids",list);
String param = JSON.toJSONString(map);
String res = HttpHelper.doPost(HttpStatus.LAST_POSITION_URL+redisCode,param);
//对返回的结果进行解析
IotLocationVo iotLocationVo = resultDataHandler(res);
log.info("resultDataHandler:{}", iotLocationVo);
return AjaxResult.success(iotLocationVo);
}
/**
* 查询行程
* @param iotLocationVo
* @return
*/
@Override
public AjaxResult searchItinerary(IotLocationVo iotLocationVo) {
log.info("searchItinerary:{}", iotLocationVo);
if (iotLocationVo == null || iotLocationVo.getBeginTime() == null || iotLocationVo.getEndTime() == null) {
throw new RuntimeException(ExceptionEnum.PARAM_NULL.getMsg());
}
Map<String,Object> map=new HashMap<>();
//从redis中获取token
String redisCode = redisService.getCacheObject(TokenConstants.TOKEN_LOCATION);
//如果token为空先去调登录接口获取token
if (redisCode == null){
redisCode = HttpHelper.getIotToken();
redisService.setCacheObject(TokenConstants.TOKEN_LOCATION,redisCode,23L, TimeUnit.HOURS);
}
map.clear();
map.put("deviceid",iotLocationVo.getIotId());
map.put("begintime",iotLocationVo.getBeginTime());
map.put("endtime",iotLocationVo.getEndTime());
String param = JSON.toJSONString(map);
String res = HttpHelper.doPost(HttpStatus.QUERY_TRIPS_URL+redisCode,param);
return AjaxResult.success(res);
}
/**
* 查询停留点
* @param iotLocationVo
* @return
*/
@Override
public AjaxResult reportParkDetailByTime(IotLocationVo iotLocationVo) {
log.info("reportParkDetailByTime:{}", iotLocationVo);
if (iotLocationVo == null || iotLocationVo.getBeginTime() == null || iotLocationVo.getEndTime() == null) {
throw new RuntimeException(ExceptionEnum.PARAM_NULL.getMsg());
}
Map<String,Object> map=new HashMap<>();
//从redis中获取token
String redisCode = redisService.getCacheObject(TokenConstants.TOKEN_LOCATION);
//如果token为空先去调登录接口获取token
if (redisCode == null){
redisCode = HttpHelper.getIotToken();
redisService.setCacheObject(TokenConstants.TOKEN_LOCATION,redisCode,23L, TimeUnit.HOURS);
}
map.clear();
map.put("deviceid",iotLocationVo.getIotId());
map.put("begintime",iotLocationVo.getBeginTime());
map.put("endtime",iotLocationVo.getEndTime());
String param = JSON.toJSONString(map);
String res = HttpHelper.doPost(HttpStatus.REPORT_PARK_DETAIL_BY_TIME_URL+redisCode,param);
//对返回的结果进行解析
List<IotLocationVo> iotLocationVoList = resultReportParkDetailByTime(res);
log.info("resultDataHandler:{}", iotLocationVo);
return AjaxResult.success(iotLocationVoList);
}
/**
* 解析停留点返回报文
* @param res
* @return
*/
private List<IotLocationVo> resultReportParkDetailByTime(String res) {
if (res == null) {
throw new RuntimeException(ExceptionEnum.RETURN_DATA_IS_EMPTY.getMsg());
}
List<IotLocationVo> iotLocationVoList = new ArrayList<>();
JSONObject object = JSONObject.parseObject(res);
String cause = object.getString("cause");
String status = object.getString("status");
if ("OK".equals(cause) && "0".equals(status)) {
String records = object.getString("records");
//判断返回结果是否为空
if (records != null){
JSONArray jsonArray = JSON.parseArray(records);
for (int i = 0; i < jsonArray.size(); i++) {
IotLocationVo iotLocationVo = new IotLocationVo();
JSONObject jsonObject = JSONObject.from(jsonArray.getJSONObject(i));
iotLocationVo.setBeginTime(jsonObject.getString("starttime"));
iotLocationVo.setEndTime(jsonObject.getString("endtime"));
iotLocationVo.setAddress(jsonObject.getString("address"));
iotLocationVoList.add(iotLocationVo);
}
}
}
return iotLocationVoList;
}
/**
* 绑定设备APP
* @param iotDto
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public AjaxResult bindApp(IotDto iotDto) {
if (iotDto == null || iotDto.getIotCode() == null || iotDto.getMaCode() == null) {
return AjaxResult.error(ExceptionEnum.PARAM_NULL.getCode(), ExceptionEnum.PARAM_NULL.getMsg());
}
iotDto.setBinder(SecurityUtils.getLoginUser().getUserid().toString());
//根据输入的iot编码查询iot设备
IotVo iotVo = iotMachineMapper.selectIotCode(iotDto);
if (iotVo == null) {
return AjaxResult.error(ExceptionEnum.IOT_ENCODING_ERROR.getCode(), ExceptionEnum.IOT_ENCODING_ERROR.getMsg());
}
iotDto.setIotId(iotVo.getIotId());
int res;
try {
//绑定设备
res = iotMachineMapper.bind(iotDto);
if (res == 0) {
throw new RuntimeException(ExceptionEnum.BIND_TO_DATABASE.getMsg());
}
//更新绑定状态
res = iotMachineMapper.updateBindStatus(iotDto);
if (res == 0) {
throw new RuntimeException(ExceptionEnum.UPDATE_TO_DATABASE.getMsg());
}
} catch (Exception e) {
log.error("设备绑定异常:{}", e.getMessage());
// 添加事务回滚逻辑保证全部成功或者全部失败
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return AjaxResult.error(ExceptionEnum.UPDATE_TO_DATABASE.getCode(), ExceptionEnum.UPDATE_TO_DATABASE.getMsg());
}
return AjaxResult.success(ExceptionEnum.SUCCESS.getMsg(), res);
}
/**
* 报警记录
* @param iotLocationVo
* @return
*/
@Override
public AjaxResult reportAlarm(IotLocationVo iotLocationVo) {
log.info("reportAlarm:{}", iotLocationVo);
if (iotLocationVo == null || iotLocationVo.getIotId() == null || iotLocationVo.getBeginTime() == null || iotLocationVo.getEndTime() == null) {
throw new RuntimeException(ExceptionEnum.PARAM_NULL.getMsg());
}
Map<String,Object> map=new HashMap<>();
List<String> list=new ArrayList<>();
//从redis中获取token
String redisCode = redisService.getCacheObject(TokenConstants.TOKEN_LOCATION);
//如果token为空先去调登录接口获取token
if (redisCode == null){
redisCode = HttpHelper.getIotToken();
redisService.setCacheObject(TokenConstants.TOKEN_LOCATION,redisCode,23L, TimeUnit.HOURS);
}
map.clear();
list.add(iotLocationVo.getIotId().toString());
map.put("devices",list);
map.put("startday",iotLocationVo.getBeginTime());
map.put("endday",iotLocationVo.getEndTime());
//报警类型
map.put("needalarm","");
String param = JSON.toJSONString(map);
String res = HttpHelper.doPost(HttpStatus.REPORT_ALARM_URL+redisCode,param);
//对返回的结果进行解析
List<IotLocationVo> iotLocationVoList = resultReportAlarm(res);
log.info("resultDataHandler:{}", iotLocationVo);
return AjaxResult.success(iotLocationVoList);
}
/**
* 解析报警返回报文
* @param res
* @return
*/
private List<IotLocationVo> resultReportAlarm(String res) {
if (res == null) {
throw new RuntimeException(ExceptionEnum.RETURN_DATA_IS_EMPTY.getMsg());
}
List<IotLocationVo> iotLocationVoList = new ArrayList<>();
JSONObject object = JSONObject.parseObject(res);
String cause = object.getString("cause");
String status = object.getString("status");
if ("OK".equals(cause) && "0".equals(status)) {
String records = object.getString("alarmrecords");
//判断返回结果是否为空
if (records != null){
JSONArray jsonArray = JSON.parseArray(records);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = JSONObject.from(jsonArray.getJSONObject(i));
IotLocationVo iotLocationVo = new IotLocationVo();
iotLocationVo.setStartAlarmTime(jsonObject.getString("startalarmtime"));
iotLocationVo.setStartAlarm(jsonObject.getString("stralarm"));
iotLocationVoList.add(iotLocationVo);
}
}
}
return iotLocationVoList;
}
/**
* 解析定位返回报文
* @param res
* @return
*/
private IotLocationVo resultDataHandler(String res) {
if (res == null) {
throw new RuntimeException(ExceptionEnum.RETURN_DATA_IS_EMPTY.getMsg());
}
IotLocationVo iotLocationVo = new IotLocationVo();
JSONObject object = JSONObject.parseObject(res);
String status = object.getString("status");
String cause = object.getString("cause");
if ("0".equals(status) && "OK".equals(cause)) {
String records = object.getString("records");
//判断返回结果是否为空
if (records != null){
JSONArray jsonArray = JSON.parseArray(records);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = JSONObject.from(jsonArray.getJSONObject(i));
iotLocationVo.setIotId(StringHelper.conversionLong(jsonObject.getString("deviceid")));
iotLocationVo.setCallat(StringHelper.conversionBigDecimal(jsonObject.getString("callat")));
iotLocationVo.setCallon(StringHelper.conversionBigDecimal(jsonObject.getString("callon")));
}
}
}
return iotLocationVo;
}
}

View File

@ -1,7 +1,7 @@
<?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">
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bonus.sgzb.material.mapper.IotMachineMapper">
<insert id="add">
@ -91,8 +91,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
iot_machine
WHERE
del_flag = '0'
<if test="iotType != null and iotType != ''" >
AND iot_type = #{iotType}
</if>
<if test="iotCode != null and iotCode != ''" >
AND iot_code = #{iotCode}
</if>
</select>
<select id="getIotList" resultType="com.bonus.sgzb.material.domain.IotVo">
SELECT