package cn.besteam.modules.medical.biz; import cn.besteam.common.constant.ErrorCode; import cn.besteam.common.exception.BusinessException; import cn.besteam.common.util.RedisUtil; import cn.besteam.modules.medical.entity.MwMonitorDevice; import cn.besteam.modules.system.entity.SysDict; import cn.besteam.modules.system.entity.SysDictItem; import cn.besteam.modules.system.service.ISysDictItemService; import cn.besteam.modules.system.service.ISysDictService; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import java.util.List; @Service @Slf4j public class MonitorDeviceApiService { //登录 private static final String LOGIN_URL = "https://video-1.scjakj.com/StandardApiAction_login.action?account=%s&password=%s"; //添加设备 private static final String ADD_DEVICE_URL = "https://video-1.scjakj.com/StandardApiAction_addDevice.action?jsession=%s&devIdno=%s&devType=7&factoryType=0&companyName=%s&account=%s&channelNum=%s"; //绑定设备 private static final String BIND_DEVICE_URL = "https://video-1.scjakj.com/StandardApiAction_addVehicle.action?jsession=%s&vehiIdno=%s&devIdno=%s&devType=7&factoryType=0&companyName=%s&account=%s"; //删除设备 private static final String DELETE_DEVICE_URL = "https://video-1.scjakj.com/StandardApiAction_deleteVehicle.action?jsession=%s&vehiIdno=%s&delDevice=1"; //查询设备状态 private static final String SELState_DEVICE_URL = "https://video-1.scjakj.com/StandardApiAction_getDeviceOlStatus.action?jsession=%s&devIdno=%s"; //默认用户名、公司名 private static final String DEFAULT_ACCOUNT = "scjakj", DEFAULT_COMPANY = "scjakj", DEFAULT_PASSWORD = "123456"; @Autowired private RedisUtil redisUtil; @Autowired private ISysDictService sysDictService; @Autowired private ISysDictItemService sysDictItemService; /** * 登录 * * @return */ public String getJSession() { MonitorConfig monitorConfig = getMonitorConfig(); String key = "monitor:device:jsession"; String jsession = redisUtil.get(key); if (jsession == null) { RestTemplate restTemplate = new RestTemplate(); String completeUrl = String.format(LOGIN_URL, monitorConfig.getAccount(), monitorConfig.getPassword()); log.info("视频服务器:登录接口传参数据:{}", completeUrl); ApiResult result = restTemplate.getForObject(completeUrl, ApiResult.class); log.info("视频服务器:登录接口返回数据:{}", result); if (result == null) { throw new BusinessException(ErrorCode.ERR_500, "视频服务器连接失败"); } if (result.getResult().equals(0)) { jsession = result.getJsession(); } else { throw new BusinessException(ErrorCode.ERR_500, "视频服务器连接失败"); } } redisUtil.set(key, jsession, 300); return jsession; } /** * 绑定设备 * * @param mwMonitorDevice * @return */ public boolean bindDevice(MwMonitorDevice mwMonitorDevice) { MonitorConfig monitorConfig = getMonitorConfig(); RestTemplate restTemplate = new RestTemplate(); String completeUrl = String.format(BIND_DEVICE_URL, getJSession(), mwMonitorDevice.getDevIdno(), mwMonitorDevice.getDevIdno(), monitorConfig.getCompany(), monitorConfig.getAccount()); log.info("视频服务器:绑定设备接口传参数据:{}", completeUrl); ApiResult result = restTemplate.getForObject(completeUrl, ApiResult.class); log.info("视频服务器:绑定设备接口返回数据:{}", result); if (result == null) { throw new BusinessException(ErrorCode.ERR_500, "视频服务器连接失败"); } // if (result.getResult().equals(20)) { // throw new BusinessException(ErrorCode.ERR_500, "Current device does not match the company"); // } else if (result.getResult().equals(0) || result.getResult().equals(16)) { // return true; // } else { // return false; // } if (result.getResult().equals(0)) { return true; } else { if (StringUtils.isNotEmpty(result.getMessage()) && result.getMessage().contains("already exists")) { throw new BusinessException(ErrorCode.ERR_500, "监控设备已存在"); } throw new BusinessException(ErrorCode.ERR_500, result.getMessage()); } } /** * 添加设备 * * @param mwMonitorDevice * @return */ public boolean addDevice(MwMonitorDevice mwMonitorDevice) { MonitorConfig monitorConfig = getMonitorConfig(); RestTemplate restTemplate = new RestTemplate(); String completeUrl = String.format(ADD_DEVICE_URL, getJSession(), mwMonitorDevice.getDevIdno(), monitorConfig.getCompany(), monitorConfig.getAccount(), mwMonitorDevice.getChannelNum()); log.info("视频服务器:添加设备接口传参数据:{}", completeUrl); ApiResult result = restTemplate.getForObject(completeUrl, ApiResult.class); log.info("视频服务器:添加设备接口返回数据:{}", result); if (result == null) { throw new BusinessException(ErrorCode.ERR_500, "视频服务器连接失败"); } if (result.getResult().equals(0)) { return true; } else { if (StringUtils.isNotEmpty(result.getMessage()) && result.getMessage().contains("already exists")) { throw new BusinessException(ErrorCode.ERR_500, "监控设备已存在"); } throw new BusinessException(ErrorCode.ERR_500, result.getMessage()); } } /** * 删除设备 * * @param devIdno * @return */ public boolean deleteDevice(String devIdno) { RestTemplate restTemplate = new RestTemplate(); String completeUrl = String.format(DELETE_DEVICE_URL, getJSession(), devIdno); log.info("视频服务器:删除设备接口传参数据:{}", completeUrl); ApiResult result = restTemplate.getForObject(completeUrl, ApiResult.class); log.info("视频服务器:删除设备接口返回数据:{}", result); if (result == null) { throw new BusinessException(ErrorCode.ERR_500, "视频服务器连接失败"); } if (result.getResult().equals(0)) { return true; } else { throw new BusinessException(ErrorCode.ERR_500, result.getMessage()); } } /** * 获取设备状态: * 在线状态 * 1表示在线,否则不在线。 * * @param devIdno * @return */ public Integer getDeviceState(String devIdno) { Integer state=0; RestTemplate restTemplate = new RestTemplate(); String completeUrl = String.format(SELState_DEVICE_URL, getJSession(), devIdno); log.info("视频服务器:获取设备状态接口传参数据:{}", completeUrl); ApiStateResult result = restTemplate.getForObject(completeUrl, ApiStateResult.class); log.info("视频服务器:获取设备状态接口返回数据:{}", result); if (result == null) { throw new BusinessException(ErrorCode.ERR_500, "视频服务器连接失败"); } if (result.getResult().equals(0)) { state=0; if(result.getOnlines()!=null && result.getOnlines().size()>0){ state=result.getOnlines().get(0).getOnline(); } } return state; } @Data private static class ApiResult { private String message; private Integer result; private String jsession; } @Data private static class ApiStateResult { private Integer result; private List onlines; } @Data private static class ApiOnline{ private String did; private String vid; private Integer online; } @Data private static class MonitorConfig { private String account; private String company; private String password; } public MonitorConfig getMonitorConfig() { SysDict sysDict = sysDictService.getOne(new QueryWrapper().lambda().eq(SysDict::getDictCode, "MONITOR_DEVICE_CONFIG")); if (sysDict == null) { throw new BusinessException(ErrorCode.ERR_500, "请联系管理员进行参数初始化"); } List sysDictItemList = sysDictItemService.list(new QueryWrapper().lambda().eq(SysDictItem::getDictId, sysDict.getId())); if (CollectionUtils.isEmpty(sysDictItemList)) { throw new BusinessException(ErrorCode.ERR_500, "请联系管理员进行参数初始化"); } MonitorConfig config = new MonitorConfig(); for (SysDictItem sysDictItem : sysDictItemList) { if (sysDictItem.getItemText().equals("account")) { config.setAccount(sysDictItem.getItemValue()); } if (sysDictItem.getItemText().equals("company")) { config.setCompany(sysDictItem.getItemValue()); } if (sysDictItem.getItemText().equals("password")) { config.setPassword(sysDictItem.getItemValue()); } } return config; } }