diff --git a/bonus-common/bonus-common-core/pom.xml b/bonus-common/bonus-common-core/pom.xml index 9dd91bf..e6fa943 100644 --- a/bonus-common/bonus-common-core/pom.xml +++ b/bonus-common/bonus-common-core/pom.xml @@ -156,6 +156,11 @@ + + dom4j + dom4j + 1.6.1 + diff --git a/bonus-common/bonus-common-core/src/main/java/com/bonus/common/core/utils/DateTimeHelper.java b/bonus-common/bonus-common-core/src/main/java/com/bonus/common/core/utils/DateTimeHelper.java index c94f4f2..c45dfce 100644 --- a/bonus-common/bonus-common-core/src/main/java/com/bonus/common/core/utils/DateTimeHelper.java +++ b/bonus-common/bonus-common-core/src/main/java/com/bonus/common/core/utils/DateTimeHelper.java @@ -916,4 +916,16 @@ public class DateTimeHelper { } return true; } + + /** + * 将时间戳转换为时间 + */ + public static String stampToDate(String stap) { + String time; + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + long lt = new Long(stap); + Date date = new Date(lt * 1000); + time = simpleDateFormat.format(date); + return time; + } } diff --git a/bonus-common/bonus-common-entity/src/main/java/com/bonus/common/entity/tcp/DeviceUseVo.java b/bonus-common/bonus-common-entity/src/main/java/com/bonus/common/entity/tcp/DeviceUseVo.java new file mode 100644 index 0000000..df3d0aa --- /dev/null +++ b/bonus-common/bonus-common-entity/src/main/java/com/bonus/common/entity/tcp/DeviceUseVo.java @@ -0,0 +1,42 @@ +package com.bonus.common.entity.tcp; + +import lombok.Data; + +import java.util.Date; + +/** + * @className:DeviceUseVo + * @author:cwchen + * @date:2024-08-27-15:36 + * @version:1.0 + * @description:设备领用-vo + */ +@Data +public class DeviceUseVo { + private Long id; + /**设备ID*/ + private Long devId; + /**工程ID*/ + private Long proId; + /**班组ID*/ + private Long teamId; + /**杆塔ID*/ + private Long gtId; + /**人员身份证号*/ + private String idCard; + /**人员ID*/ + private Long userId; + /**告警内容*/ + private String warnContent; + /**告警时间*/ + private String warnTime; + /**告警类型 1 设备告警 2 违章识别 3 带电体告警*/ + private String warnType = "2"; + private Date createTime = new Date(); + /**删除状态*/ + private Integer delFlag = 0; + /**设备关联类型 0手环 1设备*/ + private Integer devType = 1; + /**人员类型 0默认 1 临时人员*/ + private Integer peopleType = 0; +} diff --git a/bonus-common/bonus-common-entity/src/main/java/com/bonus/common/entity/tcp/PeopleVioVo.java b/bonus-common/bonus-common-entity/src/main/java/com/bonus/common/entity/tcp/PeopleVioVo.java new file mode 100644 index 0000000..6ebe44f --- /dev/null +++ b/bonus-common/bonus-common-entity/src/main/java/com/bonus/common/entity/tcp/PeopleVioVo.java @@ -0,0 +1,21 @@ +package com.bonus.common.entity.tcp; + +import lombok.Data; + +/** + * @className:PeopleVioVo + * @author:cwchen + * @date:2024-08-27-16:06 + * @version:1.0 + * @description:人员违章-vo + */ +@Data +public class PeopleVioVo { + + /**人员ID*/ + private Long id; + /**姓名*/ + private String name; + /**人员类别 0-班组成员 1-临时人员*/ + private Integer peopleType; +} diff --git a/bonus-modules/bonus-tcp/src/main/java/com/bonus/tcp/smartIdentify/controller/SmartIdentifyController.java b/bonus-modules/bonus-tcp/src/main/java/com/bonus/tcp/smartIdentify/controller/SmartIdentifyController.java new file mode 100644 index 0000000..29bd96f --- /dev/null +++ b/bonus-modules/bonus-tcp/src/main/java/com/bonus/tcp/smartIdentify/controller/SmartIdentifyController.java @@ -0,0 +1,217 @@ +package com.bonus.tcp.smartIdentify.controller; + +import com.bonus.common.core.utils.BASE64DecodedMultipartFile; +import com.bonus.common.core.utils.DateTimeHelper; +import com.bonus.common.entity.tcp.DeviceUseVo; +import com.bonus.common.redis.service.RedisService; +import com.bonus.tcp.smartIdentify.service.SmartIdentifyService; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.DocumentHelper; +import org.dom4j.Element; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReentrantLock; + +/** + * @className:SmartIdentifyController + * @author:cwchen + * @date:2024-08-27-14:26 + * @version:1.0 + * @description:球机智能识别 + */ +@Slf4j +@RestController +@RequestMapping("/ball") +public class SmartIdentifyController { + + @Autowired + private RedisService redisService; + + @Resource(name = "SmartIdentifyService") + private SmartIdentifyService service; + + public static final ReentrantLock BED_LOCK = new ReentrantLock(); + + /** + * 事件id集合 + * E_IVS_HelmetNotWear :未带安全帽 + * E_IVS_Smoking :抽烟报警 + * E_IVS_NotWear3ColorVest :未穿三色马甲报警 + * E_IVS_FaceRecognition :人脸识别 + */ + public static List eventIds = new ArrayList<>(); + + { + eventIds.add("E_IVS_HelmetNotWear"); + eventIds.add("E_IVS_Smoking"); + eventIds.add("E_IVS_NotWear3ColorVest"); + eventIds.add("E_IVS_FaceRecognition"); + } + + /** + * 清新互联平台事件推送接口 + * + * @param xml + * @return + */ + @PostMapping("xmlAnalysis") + public String xmlAnalysis(@RequestBody String xml) { + try { + // 设置对象锁 + boolean flag = BED_LOCK.tryLock(10L * 1000L, TimeUnit.MILLISECONDS); + if (flag) { + boolean existence = false; + //解析xml字符串为Document + Document doc = null; + //事件ID + String evenID = null; + //身份证号 + String idCard = null; + //事件时间 + String eventTime = DateTimeHelper.getNowTime(); + //解析xml内容 + doc = DocumentHelper.parseText(xml); + //获取根节点 + Element root = doc.getRootElement(); + //获取根节点下的所有M子节点 + Iterator it = root.elementIterator("E"); + //获取单个事件 + if (it != null) { + Element element = (Element) it.next(); + String id = element.attributeValue("ID"); + evenID = id; + //获取事件发生时间 + if (StringUtils.isNotBlank(element.attributeValue("Time"))) { + eventTime = DateTimeHelper.stampToDate(element.attributeValue("Time")); + } + Iterator desc2Interator = element.elementIterator("Desc2"); + if(desc2Interator != null){ + Element idCardEle = (Element) desc2Interator.next(); + idCard = idCardEle.attributeValue("IDCard"); + } + Iterator srcInterator = element.elementIterator("Src"); + String ballName = null; + if (srcInterator != null) { + Element headEle = (Element) srcInterator.next(); + String puid = headEle.attributeValue("ID"); + // 十分钟内存在该事件,直接返回 + Object cacheObject = redisService.getCacheObject(evenID + "_" + puid); + if (Objects.nonNull(cacheObject)) { + return "error"; + } + Iterator resInterator = headEle.elementIterator("Res"); + if (resInterator != null) { + Element nameEle = (Element) resInterator.next(); + ballName = nameEle.attributeValue("Name"); + } + boolean sf = eventIds.contains(evenID); + List devices = null; + if (sf) { + devices = service.getDevices(puid); + existence = CollectionUtils.isNotEmpty(devices) ? true : false; + } + //存在该设备 + if (existence) { + switchEvent(evenID, element, eventTime, puid, ballName,idCard, devices); + // 事件推送成功后,设置缓存 + String key = evenID + "_" + puid; + redisService.setCacheObject(key, puid, 1L, TimeUnit.MILLISECONDS); + } else { + return "error"; + } + } else { + return "error"; + } + } else { + return "error"; + } + } else { + log.info("系统繁忙,请稍后重试"); + return "error"; + } + } catch (InterruptedException e) { + log.error("对象锁", e); + } catch (DocumentException e) { + throw new RuntimeException(e); + } finally { + // 确保锁被释放 + BED_LOCK.unlock(); + } + return "ok"; + } + + private void switchEvent(String eventId, Element element, String eventTime, String puid, String ballName,String idCard, List devices) { + List files = null; + DeviceUseVo deviceUseVo = devices.get(0); + deviceUseVo.setWarnTime(eventTime); + deviceUseVo.setIdCard(idCard); + switch (eventId) { + case "E_IVS_FaceRecognition": + log.info(puid + ":违章事件:人脸识别"); + deviceUseVo.setWarnContent("未戴安全帽"); + files = deviceVolEvenHandler(puid, eventTime, element); + service.addEventData(deviceUseVo, files); + break; + case "E_IVS_HelmetNotWear": + log.info(puid + ":违章事件:未戴安全帽"); + deviceUseVo.setWarnContent("未戴安全帽"); + files = deviceVolEvenHandler(puid, eventTime, element); +// service.addEventData(deviceUseVo, files); + break; + case "E_IVS_Smoking": + log.info(puid + ":违章事件:吸烟行为"); + deviceUseVo.setWarnContent("吸烟行为"); + files = deviceVolEvenHandler(puid, eventTime, element); +// service.addEventData(deviceUseVo, files); + break; + case "E_IVS_NotWear3ColorVest": + log.info(puid + ":违章事件:未穿马甲"); + deviceUseVo.setWarnContent("未穿马甲"); + files = deviceVolEvenHandler(puid, eventTime, element); +// service.addEventData(deviceUseVo, files); + break; + default: + break; + } + } + + private List deviceVolEvenHandler(String puid, String volTime, Element element) { + List multipartFiles = new ArrayList<>(); + Iterator silcesInterator = element.elementIterator("Slices"); + if (silcesInterator.hasNext()) { + Element silcesEle = (Element) silcesInterator.next(); + if (silcesEle != null) { + Iterator silcInterator = silcesEle.elementIterator("Slice"); + if (silcInterator.hasNext()) { + Element snapshotEle = (Element) silcInterator.next(); + if (snapshotEle != null) { + Iterator snapshotInterator = snapshotEle.elementIterator("Snapshot"); + if (snapshotInterator.hasNext()) { + Element TestEle = (Element) snapshotInterator.next(); + String photoBase64 = TestEle.getText(); + log.info("base64地址:{}", photoBase64); +// MultipartFile multipartFile = BASE64DecodedMultipartFile.base64ToMultipart(photoBase64); +// multipartFiles.add(multipartFile); + } + } + } + } + } + return multipartFiles; + } +} diff --git a/bonus-modules/bonus-tcp/src/main/java/com/bonus/tcp/smartIdentify/mapper/SmartIdentifyMapper.java b/bonus-modules/bonus-tcp/src/main/java/com/bonus/tcp/smartIdentify/mapper/SmartIdentifyMapper.java new file mode 100644 index 0000000..fbab032 --- /dev/null +++ b/bonus-modules/bonus-tcp/src/main/java/com/bonus/tcp/smartIdentify/mapper/SmartIdentifyMapper.java @@ -0,0 +1,38 @@ +package com.bonus.tcp.smartIdentify.mapper; + +import com.bonus.common.entity.tcp.DeviceUseVo; +import com.bonus.common.entity.tcp.PeopleVioVo; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Map; + +/** + * @className:SmartIdentifyMapper + * @author:cwchen + * @date:2024-08-27-15:16 + * @version:1.0 + * @description:智能识别 + */ +@Repository(value = "SmartIdentifyMapper") +public interface SmartIdentifyMapper { + + /** + * 判断球机是否被班组领用 + * @param puid + * @return boolean + * @date 2024/8/27 15:22 + */ + List getDevices(String puid); + + /** + * 根据身份证号获取班组成员/临时人员 + * @param idCard + * @return List + * @author cwchen + * @date 2024/8/27 16:08 + */ + List getUserByIdCard(String idCard); + + void addWarnInfo(DeviceUseVo deviceUseVo); +} diff --git a/bonus-modules/bonus-tcp/src/main/java/com/bonus/tcp/smartIdentify/service/SmartIdentifyService.java b/bonus-modules/bonus-tcp/src/main/java/com/bonus/tcp/smartIdentify/service/SmartIdentifyService.java new file mode 100644 index 0000000..de4316e --- /dev/null +++ b/bonus-modules/bonus-tcp/src/main/java/com/bonus/tcp/smartIdentify/service/SmartIdentifyService.java @@ -0,0 +1,33 @@ +package com.bonus.tcp.smartIdentify.service; + +import com.bonus.common.entity.tcp.DeviceUseVo; +import org.springframework.web.multipart.MultipartFile; + +import java.util.List; + +/** + * @className:SmartIdentifyService + * @author:cwchen + * @date:2024-08-27-15:15 + * @version:1.0 + * @description:智能识别 + */ +public interface SmartIdentifyService { + /** + * 判断球机是否被班组领用 + * + * @param puid + * @return boolean + * @date 2024/8/27 15:22 + */ + List getDevices(String puid); + + /** + * 添加智能识别告警信息 + * @param deviceUseVo + * @param files + * @return void + * @date 2024/8/27 16:00 + */ + void addEventData(DeviceUseVo deviceUseVo, List files); +} diff --git a/bonus-modules/bonus-tcp/src/main/java/com/bonus/tcp/smartIdentify/service/impl/SmartIdentifyServiceImpl.java b/bonus-modules/bonus-tcp/src/main/java/com/bonus/tcp/smartIdentify/service/impl/SmartIdentifyServiceImpl.java new file mode 100644 index 0000000..b40da96 --- /dev/null +++ b/bonus-modules/bonus-tcp/src/main/java/com/bonus/tcp/smartIdentify/service/impl/SmartIdentifyServiceImpl.java @@ -0,0 +1,129 @@ +package com.bonus.tcp.smartIdentify.service.impl; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONObject; +import com.bonus.common.core.constant.HttpStatus; +import com.bonus.common.core.constant.SecurityConstants; +import com.bonus.common.core.domain.R; +import com.bonus.common.core.utils.encryption.Sm4Utils; +import com.bonus.common.entity.tcp.DeviceUseVo; +import com.bonus.common.entity.tcp.PeopleVioVo; +import com.bonus.system.api.RemoteFileService; +import com.bonus.system.api.RemoteSourceService; +import com.bonus.system.api.domain.SysFileSource; +import com.bonus.tcp.smartIdentify.mapper.SmartIdentifyMapper; +import com.bonus.tcp.smartIdentify.service.SmartIdentifyService; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.interceptor.TransactionAspectSupport; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; +import java.util.List; + +/** + * @className:SmartIdentifyServiceImpl + * @author:cwchen + * @date:2024-08-27-15:15 + * @version:1.0 + * @description:智能识别 + */ +@Service(value = "SmartIdentifyService") +@Slf4j +public class SmartIdentifyServiceImpl implements SmartIdentifyService { + + @Resource(name = "SmartIdentifyMapper") + private SmartIdentifyMapper mapper; + + @Resource + private RemoteFileService remoteFileService; + + @Resource + private RemoteSourceService remoteSourceService; + + @Override + public List getDevices(String puid) { + List list = null; + try { + list = mapper.getDevices(puid); + } catch (Exception e) { + log.error(e.toString(), e); + } + return list; + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void addEventData(DeviceUseVo deviceUseVo, List files) { + // 文件ID + String delFileId = null; + try { + if (StringUtils.isNotBlank(deviceUseVo.getIdCard())) { + List userIds = mapper.getUserByIdCard(Sm4Utils.encode(deviceUseVo.getIdCard())); + if (CollectionUtils.isNotEmpty(userIds)) { + deviceUseVo.setUserId(userIds.get(0).getId()); + deviceUseVo.setPeopleType(userIds.get(0).getPeopleType()); + deviceUseVo.setWarnContent(userIds.get(0).getName() + deviceUseVo.getWarnContent()); + } + } + // 添加告警信息 + mapper.addWarnInfo(deviceUseVo); + // 上传智能识别图片 + if (CollectionUtils.isNotEmpty(files)) { + delFileId = uploadFile(files.get(0), deviceUseVo.getId()); + } + } catch (Exception e) { + log.error(e.toString(), e); + TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); + // 添加失败-删除文件 + if (delFileId != null) { + remoteFileService.delFile(delFileId, SecurityConstants.INNER); + } + } + } + + public String uploadFile(MultipartFile file, Long warnId) { + if (file == null) { + return null; + } + R result = remoteFileService.singleUploadFile(file, SecurityConstants.INNER); + if (result != null && result.getCode() == HttpStatus.ERROR) { + log.error("违章照片上传失败"); + return null; + } else if (result != null && result.getCode() == HttpStatus.SUCCESS && result.getData() != null) { + String jsonString = JSON.toJSONString(result.getData()); + JSONObject item = JSON.parseObject(jsonString); + if (item != null) { + SysFileSource fileVo = setResourceFileData(item, warnId); + R r = null; + try { + r = remoteSourceService.addFileSource(fileVo, SecurityConstants.INNER); + if (!r.getData()) { + // 资源文件保存失败,删除文件 + remoteFileService.delFile(item.getString("fileId"), SecurityConstants.INNER); + return null; + } + } catch (Exception e) { + log.error(e.toString(), e); + } + } + return item.getString("fileId"); + } + return null; + } + + public SysFileSource setResourceFileData(JSONObject item, Long warnId) { + SysFileSource fileVo = new SysFileSource(); + fileVo.setFileType(1); + fileVo.setFilePath(item.getString("fileId")); + fileVo.setFileSuffix(item.getString("suffix")); + fileVo.setFileName(item.getString("fileName")); + fileVo.setSourceId(warnId + ""); + fileVo.setSourceType("8"); + return fileVo; + } + +} diff --git a/bonus-modules/bonus-tcp/src/main/resources/mapper/tcp/ConsControlMapper.xml b/bonus-modules/bonus-tcp/src/main/resources/mapper/tcp/ConsControlMapper.xml new file mode 100644 index 0000000..9e585b3 --- /dev/null +++ b/bonus-modules/bonus-tcp/src/main/resources/mapper/tcp/ConsControlMapper.xml @@ -0,0 +1,53 @@ + + + + + + INSERT INTO tb_warn + + pro_id, + team_id, + dev_id, + user_id, + warn_content, + warn_time, + dev_type, + people_type, + create_time, + del_flag, + id + + + #{proId}, + #{teamId}, + #{devId}, + #{userId}, + #{warnContent}, + #{warnTime}, + #{devType}, + #{peopleType}, + #{createTime} + #{delFlag}, + null + + + + + + + + \ No newline at end of file