| | |
| | | package com.sandu.ximon.admin.service; |
| | | |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.http.HttpUtil; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.sandu.common.execption.BusinessException; |
| | | import com.sandu.common.object.BaseConditionVO; |
| | | import com.sandu.common.service.impl.BaseServiceImpl; |
| | | import com.sandu.ximon.admin.param.BannerParam; |
| | | import com.sandu.ximon.admin.param.MonitorParam; |
| | | import com.sandu.ximon.dao.domain.Banner; |
| | | import com.sandu.ximon.admin.security.SecurityUtils; |
| | | import com.sandu.ximon.dao.bo.AirEquipmentBo; |
| | | import com.sandu.ximon.dao.bo.MonitorBo; |
| | | import com.sandu.ximon.dao.domain.Monitor; |
| | | import com.sandu.ximon.admin.dto.YSY_AccessTokenDto; |
| | | import com.sandu.ximon.admin.dto.YSY_MonitorDto; |
| | | import com.sandu.ximon.dao.mapper.MonitorMapper; |
| | | import lombok.AllArgsConstructor; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | @Service |
| | | @AllArgsConstructor |
| | | public class MonitorService extends BaseServiceImpl<MonitorMapper, Monitor> { |
| | | private final MonitorMapper monitorMapper; |
| | | //萤石云AppKey |
| | | private static final String AppKey = "51a534ebadf54c31a0848dc575dfa206"; |
| | | //萤石云 AppKey Secret |
| | | private static final String Secret = "8c32c67a73c87b9e461b2e3bdf58967a"; |
| | | |
| | | /** |
| | | * 查询全部摄像头 |
| | | */ |
| | | public List<Monitor> listMonitor() { |
| | | LambdaQueryWrapper<Monitor> wrapper = Wrappers.lambdaQuery(Monitor.class); |
| | | List<Monitor> list = list(wrapper); |
| | | //萤石云地址 |
| | | private static final String YSY_URL = "https://open.ys7.com/api/lapp"; |
| | | //获取萤石云accessToken地址 |
| | | private static final String ACCESSTOKEN_URL = "/token/get"; |
| | | //添加萤石云摄像头 |
| | | private static final String ADD_DEVICE_URL = "/device/add"; |
| | | //删除萤石云摄像头 |
| | | private static final String DELETE_DEVICE_URL = "/device/delete"; |
| | | //获取萤石云单个设备信息 |
| | | private static final String GET_DEVICE_URL = "/device/info"; |
| | | //获取萤石云摄像头播放地址URL |
| | | private static final String GET_VEDIO_URL = "/v2/live/address/get"; |
| | | |
| | | return list; |
| | | } |
| | | |
| | | private final PoleBindingService poleBindingService; |
| | | |
| | | /** |
| | | * 模糊查询 |
| | | */ |
| | | public List<Monitor> listMonitorByKeyword(MonitorParam monitorParam) { |
| | | //设备状态:在线/离线/全部 |
| | | String equipmentState1; |
| | | String equipmentState2; |
| | | if ("全部".equals(monitorParam.getEquipmentState())) { |
| | | equipmentState1 = "在线"; |
| | | equipmentState2 = "离线"; |
| | | public List<MonitorBo> listMonitorByKeyword(BaseConditionVO baseConditionVO, MonitorParam monitorParam) { |
| | | List<String> list; |
| | | if (SecurityUtils.getClientId() == null) { |
| | | list = monitorMapper.listMonitorDeviceSerial(null, monitorParam.getKeyword()); |
| | | } else { |
| | | equipmentState1 = monitorParam.getEquipmentState(); |
| | | equipmentState2 = monitorParam.getEquipmentState(); |
| | | list = monitorMapper.listMonitorDeviceSerial(SecurityUtils.getUserId(), monitorParam.getKeyword()); |
| | | } |
| | | |
| | | //绑定状态:已绑定/未绑定/全部 |
| | | String bindingState1; |
| | | String bindingState2; |
| | | if ("全部".equals(monitorParam.getBindingState())) { |
| | | bindingState1 = "已绑定"; |
| | | bindingState2 = "未绑定"; |
| | | } else { |
| | | bindingState1 = monitorParam.getBindingState(); |
| | | bindingState2 = monitorParam.getBindingState(); |
| | | //没有数据时候直接返回空数组 |
| | | if (list.size() == 0 || list == null) { |
| | | List<MonitorBo> nullList = new ArrayList<>(); |
| | | return nullList; |
| | | } |
| | | return monitorMapper.listMonitorByKeyword(monitorParam.getKeyword(), equipmentState1, equipmentState2, bindingState1, bindingState2); |
| | | |
| | | MonitorParam monitorParam1; |
| | | for (String devices_serial : list) { |
| | | monitorParam1 = new MonitorParam(); |
| | | monitorParam1.setAccessToken(getAccessToken()); |
| | | monitorParam1.setDeviceSerial(devices_serial); |
| | | getYSYMonitorInfoAndaddMonitorToTable(monitorParam1); |
| | | } |
| | | |
| | | if (baseConditionVO != null) { |
| | | PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize()); |
| | | } |
| | | List<MonitorBo> monitorList; |
| | | //绑定状态:已绑定/未绑定/全部 |
| | | if (monitorParam.getBindingState() == null) { |
| | | //全部 |
| | | monitorParam.setBindingState(2); |
| | | } |
| | | if (monitorParam.getEquipmentState() == null) { |
| | | //全部 |
| | | monitorParam.setEquipmentState(2); |
| | | } |
| | | |
| | | monitorList = monitorMapper.listMonitorByIds(list, monitorParam.getBindingState(), monitorParam.getEquipmentState()); |
| | | |
| | | return monitorList; |
| | | } |
| | | |
| | | public Monitor getYSYMonitorInfoAndaddMonitorToTable(MonitorParam monitorParam) { |
| | | YSY_MonitorDto getMonitorDto = getYSYMonitorInfo(getAccessToken(), monitorParam.getDeviceSerial()); |
| | | |
| | | if ("200".equals(getMonitorDto.getCode())) { |
| | | Monitor monitor = getOne(Wrappers.lambdaQuery(Monitor.class).eq(Monitor::getDeviceSerial, monitorParam.getDeviceSerial())); |
| | | if (monitor != null) { |
| | | monitor.setDeviceSerial(getMonitorDto.getData().getDeviceSerial()); |
| | | if (monitorParam.getValidateCode() != null) { |
| | | monitor.setDevicesCode(monitorParam.getValidateCode()); |
| | | } |
| | | monitor.setDeviceName(getMonitorDto.getData().getDeviceName()); |
| | | monitor.setDeviceType(getMonitorDto.getData().getModel()); |
| | | monitor.setEquipmentState(getMonitorDto.getData().getStatus()); |
| | | monitor.setIsEncrypt(getMonitorDto.getData().getIsEncrypt()); |
| | | monitor.setAddTime(getMonitorDto.getData().getUpdateTime()); |
| | | monitor.setNetAddress(getMonitorDto.getData().getNetAddress()); |
| | | //更新摄像头状态 |
| | | updateById(monitor); |
| | | } else { |
| | | monitor = new Monitor(); |
| | | monitor.setDeviceSerial(getMonitorDto.getData().getDeviceSerial()); |
| | | if (monitorParam.getValidateCode() != null) { |
| | | monitor.setDevicesCode(monitorParam.getValidateCode()); |
| | | } |
| | | monitor.setDeviceName(getMonitorDto.getData().getDeviceName()); |
| | | monitor.setDeviceType(getMonitorDto.getData().getModel()); |
| | | monitor.setEquipmentState(getMonitorDto.getData().getStatus()); |
| | | monitor.setIsEncrypt(getMonitorDto.getData().getIsEncrypt()); |
| | | monitor.setAddTime(getMonitorDto.getData().getUpdateTime()); |
| | | monitor.setNetAddress(getMonitorDto.getData().getNetAddress()); |
| | | //更新摄像头状态 |
| | | save(monitor); |
| | | } |
| | | |
| | | return monitor; |
| | | } else { |
| | | throw new BusinessException(getMonitorDto.getMsg()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | if (monitor == null) { |
| | | throw new BusinessException("找不到摄像头"); |
| | | } |
| | | |
| | | //删除摄像头设备前需要先解绑 |
| | | poleBindingService.unBindPole(null, monitor.getDeviceSerial()); |
| | | return removeById(Id); |
| | | } |
| | | |
| | | /** |
| | | * 编辑摄像头 |
| | | * @param Id |
| | | * @param monitorParam |
| | | * 获取萤石云accessToken |
| | | */ |
| | | |
| | | public String getAccessToken() { |
| | | Map<String, Object> paramMap = new HashMap<>(); |
| | | paramMap.put("appKey", AppKey); |
| | | paramMap.put("appSecret", Secret); |
| | | String s = HttpUtil.post(YSY_URL + ACCESSTOKEN_URL, paramMap); |
| | | YSY_AccessTokenDto accessTokenDto = JSON.parseObject(s, YSY_AccessTokenDto.class); |
| | | return accessTokenDto.getData().getAccessToken(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取萤石云摄像头信息 无需验证accessToken |
| | | * |
| | | * @return |
| | | */ |
| | | public boolean updateBanner(Long Id, MonitorParam monitorParam) { |
| | | Monitor monitor = getById(Id); |
| | | if (monitor == null) { |
| | | throw new BusinessException("找不到摄像头"); |
| | | } |
| | | Monitor update = new Monitor(); |
| | | update.setId(Id); |
| | | update.setSerialNumber(monitorParam.()); |
| | | update.setBannerUrl(bannerParam.getUrl()); |
| | | update.setId(bannerId); |
| | | return updateById(update); |
| | | public YSY_MonitorDto.MonitorDto getMonitorInfo(String deviceSerial) { |
| | | String token = getAccessToken(); |
| | | Map<String, Object> paramMap = new HashMap<>(); |
| | | paramMap.put("accessToken", token); |
| | | paramMap.put("deviceSerial", deviceSerial); |
| | | String s = HttpUtil.post(YSY_URL + GET_DEVICE_URL, paramMap); |
| | | YSY_MonitorDto monitorDto = JSON.parseObject(s, YSY_MonitorDto.class); |
| | | return monitorDto.getData(); |
| | | } |
| | | |
| | | /** |
| | | * 添加萤石云摄像头 |
| | | * |
| | | * @param accessToken 授权过程获取的access_token |
| | | * @param deviceSerial 设备序列号,存在英文字母的设备序列号,字母需为大写 |
| | | * // * @param validateCode 设备验证码,设备机身上的六位大写字母 |
| | | */ |
| | | public YSY_MonitorDto addYSYMonitor(String accessToken, String deviceSerial, String validateCode) { |
| | | // public YSY_MonitorDto addYSYMonitor(String accessToken, String deviceSerial) { |
| | | |
| | | Map<String, Object> paramMap = new HashMap<>(); |
| | | paramMap.put("accessToken", accessToken); |
| | | paramMap.put("deviceSerial", deviceSerial); |
| | | if (validateCode != null && validateCode.trim().length() != 0) { |
| | | paramMap.put("validateCode", validateCode); |
| | | } |
| | | String s = HttpUtil.post(YSY_URL + ADD_DEVICE_URL, paramMap); |
| | | System.out.println(s); |
| | | YSY_MonitorDto monitorDto = JSON.parseObject(s, YSY_MonitorDto.class); |
| | | return monitorDto; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 删除萤石云摄像头 |
| | | * |
| | | * @param accessToken 授权过程获取的access_token |
| | | * @param deviceSerial 设备序列号,存在英文字母的设备序列号,字母需为大写 |
| | | */ |
| | | public YSY_MonitorDto deleteYSYMonitor(String accessToken, String deviceSerial) { |
| | | |
| | | Map<String, Object> paramMap = new HashMap<>(); |
| | | paramMap.put("accessToken", accessToken); |
| | | paramMap.put("deviceSerial", deviceSerial); |
| | | String s = HttpUtil.post(YSY_URL + DELETE_DEVICE_URL, paramMap); |
| | | System.out.println(s); |
| | | YSY_MonitorDto monitorDto = JSON.parseObject(s, YSY_MonitorDto.class); |
| | | return monitorDto; |
| | | } |
| | | |
| | | /** |
| | | * 获取萤石云摄像头单个设备信息 |
| | | * |
| | | * @param accessToken 授权过程获取的access_token |
| | | * @param deviceSerial 设备序列号,存在英文字母的设备序列号,字母需为大写 |
| | | */ |
| | | public YSY_MonitorDto getYSYMonitorInfo(String accessToken, String deviceSerial) { |
| | | |
| | | Map<String, Object> paramMap = new HashMap<>(); |
| | | paramMap.put("accessToken", accessToken); |
| | | paramMap.put("deviceSerial", deviceSerial); |
| | | String s = HttpUtil.post(YSY_URL + GET_DEVICE_URL, paramMap); |
| | | System.out.println(s); |
| | | YSY_MonitorDto monitor = JSON.parseObject(s, YSY_MonitorDto.class); |
| | | return monitor; |
| | | } |
| | | |
| | | /** |
| | | * 将摄像头设备相关信息存储到数据库 |
| | | * |
| | | * @param monitor 摄像头对象 |
| | | * @return |
| | | */ |
| | | public boolean addMonitorInfoToTable(Monitor monitor) { |
| | | Monitor one = getOne(Wrappers.lambdaQuery(Monitor.class).eq(Monitor::getDeviceSerial, monitor.getDeviceSerial())); |
| | | if (one == null) { |
| | | return save(monitor); |
| | | } else { |
| | | monitor.setId(one.getId()); |
| | | return updateById(monitor); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将摄像头设备相关信息更新到数据库 |
| | | * |
| | | * @param monitor 摄像头对象 |
| | | * @return |
| | | */ |
| | | public boolean upDateMonitorInfo(Monitor monitor) { |
| | | if (monitor.getId() == null) { |
| | | throw new BusinessException("缺少摄像头相关ID参数"); |
| | | } |
| | | |
| | | Monitor info = getById(monitor.getId()); |
| | | |
| | | if (info == null) { |
| | | throw new BusinessException("找不到相关摄像头"); |
| | | } |
| | | if (!monitor.getDevicesCode().equals(info.getDevicesCode()) || !monitor.getDeviceSerial().equals(info.getDeviceSerial())) { |
| | | throw new BusinessException("监控序列号或监控验证码不正确"); |
| | | } |
| | | |
| | | return updateById(monitor); |
| | | } |
| | | |
| | | /** |
| | | * 返回摄像头实时画面播放地址 |
| | | * |
| | | * @param accessToken |
| | | * @param deviceSerial |
| | | * @return |
| | | */ |
| | | public String getMonitorVideoUrl(String accessToken, String deviceSerial, int protocol, int expireTime) { |
| | | if (expireTime < 30 || expireTime > 62208000) { |
| | | throw new BusinessException("设置的URL播放地址过期时长数据错误"); |
| | | } |
| | | Map<String, Object> paramMap = new HashMap<>(); |
| | | paramMap.put("accessToken", accessToken); |
| | | paramMap.put("deviceSerial", deviceSerial); |
| | | paramMap.put("protocol", protocol); |
| | | paramMap.put("expireTime", expireTime); |
| | | String s = HttpUtil.post(YSY_URL + GET_VEDIO_URL, paramMap); |
| | | System.out.println(s); |
| | | YSY_MonitorDto monitor = JSON.parseObject(s, YSY_MonitorDto.class); |
| | | if ("200".equals(monitor.getCode())) { |
| | | return monitor.getData().getUrl(); |
| | | } else { |
| | | throw new BusinessException(monitor.getMsg()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 修改设备绑定状态 |
| | | * |
| | | * @param flag true为绑定 false 为解绑 |
| | | */ |
| | | public void updateBingdingState(boolean flag, String deviceCode) { |
| | | if (flag) {//绑定 |
| | | Monitor monitor = getOne(Wrappers.lambdaQuery(Monitor.class).eq(Monitor::getDeviceSerial, deviceCode)); |
| | | if (monitor == null) { |
| | | return; |
| | | } |
| | | monitor.setBindingState(1); |
| | | updateById(monitor); |
| | | } else {//解绑 |
| | | Monitor monitor = getOne(Wrappers.lambdaQuery(Monitor.class).eq(Monitor::getDeviceSerial, deviceCode)); |
| | | if (monitor == null) { |
| | | return; |
| | | } |
| | | monitor.setBindingState(0); |
| | | updateById(monitor); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 修改note |
| | | * |
| | | * @param deviceSerial |
| | | * @param notes |
| | | * @return |
| | | */ |
| | | public boolean updateNotes(String deviceSerial, String notes) { |
| | | if (deviceSerial == null || deviceSerial.trim().length() == 0 || notes == null) { |
| | | throw new BusinessException("修改备注参数有误!"); |
| | | } |
| | | Monitor one = getOne(Wrappers.lambdaQuery(Monitor.class).eq(Monitor::getDeviceSerial, deviceSerial)); |
| | | if (one != null) { |
| | | one.setNote(notes); |
| | | return updateById(one); |
| | | } else { |
| | | throw new BusinessException("摄像头设备不存在!"); |
| | | } |
| | | } |
| | | |
| | | |
| | | } |