package com.sandu.ximon.admin.service; import com.sandu.common.execption.BusinessException; import com.sandu.ximon.admin.config.VnnoxConstant; import com.sandu.ximon.admin.utils.RedisUtils; import com.sandu.ximon.admin.utils.VnnoxAPIUtil; import com.sandu.ximon.admin.utils.request.VnnoxScreenStatusType; import com.sandu.ximon.admin.utils.response.VnnoxPlayerListResponse; import com.sandu.ximon.admin.utils.response.VnnoxPlayerResponse; import com.sandu.ximon.admin.utils.response.VnnoxResult; import com.sandu.ximon.dao.domain.LedPlayerEntity; import com.sandu.ximon.dao.domain.LedV2RegisterResultEntity; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.net.URISyntaxException; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * @Author liuhaonan * @Date 2021/12/22 14:18 * @Version 1.0 */ @Service @AllArgsConstructor public class VnnoxService { // @Autowired private VnnoxAPIUtil vnnoxAPIUtil; // @Autowired private RedisUtils redisUtils; // @Autowired private LedPlayerEntityService ledPlayerEntityService; /** * 设备校验注册 * * @param sn * @return */ @Transactional(rollbackFor = Exception.class) public LedV2RegisterResultEntity validateSN(String name,String sn) throws URISyntaxException { LedV2RegisterResultEntity ledV2RegisterResultEntity = new LedV2RegisterResultEntity(); // 判断数据库是否存在,若不存在,判断是否已经注册到Vnnox服务器 //LedPlayerEntity ledPlayerEntity = vnnoxDao.getBySN(sn); // LambdaQueryWrapper eq = Wrappers.lambdaQuery(LedPlayerEntity.class).eq(LedPlayerEntity::getSn, sn); LedPlayerEntity ledPlayerEntity = ledPlayerEntityService.getBySn(sn); if (null != ledPlayerEntity) { ledV2RegisterResultEntity.setRegister(1); ledV2RegisterResultEntity.setMsg("设备已存在!"); return ledV2RegisterResultEntity; } Integer page = 0; Integer limit = 100; List playerList; // 获取Vnnox服务器列表 VnnoxPlayerListResponse response = vnnoxAPIUtil.getPlayerList(limit, page,name); playerList = response.getRows(); Integer total = response.getTotal() - limit; while (total > 0) { page = page + 1; total = total - limit; response = vnnoxAPIUtil.getPlayerList(limit, page,name); playerList.addAll(response.getRows()); } ledPlayerEntity = new LedPlayerEntity(); for (VnnoxPlayerResponse res : playerList) { if (res.getSn().equals(sn)) { ledPlayerEntity.setSn(sn); ledPlayerEntity.setPlayerId(res.getPlayerId()); ledPlayerEntity.setPlayerName(res.getName()); ledPlayerEntity.setCreateTimestamp(new Date().getTime()); ledPlayerEntityService.saveLed(ledPlayerEntity); ledV2RegisterResultEntity.setRegister(1); ledV2RegisterResultEntity.setMsg("设备校验通过,设备注册成功!"); return ledV2RegisterResultEntity; } } ledV2RegisterResultEntity.setRegister(0); ledV2RegisterResultEntity.setMsg("设备未注册到平台!"); return ledV2RegisterResultEntity; } /** * 屏幕状态调整-POST-JSON * * @param playerList * @param screenStatus * @return */ public VnnoxResult screenStatusChange(List playerList, Integer screenStatus) { VnnoxScreenStatusType type; if (screenStatus.equals(0)) { type = VnnoxScreenStatusType.CLOSE; } else { type = VnnoxScreenStatusType.OPEN; } VnnoxResult vnnoxResult = vnnoxAPIUtil.screenStatus( playerList.stream().map(item -> item.getPlayerId()).collect(Collectors.toList()), type ); // 根据屏幕状态调整REDIS标识位 for (String playerId : vnnoxResult.getSuccess()) { redisUtils.set(VnnoxConstant.REDIS_SCREEN_STATUS + playerId, screenStatus, VnnoxConstant.REDIS_MAX_SAVE_TIME); } return vnnoxResult; } public VnnoxResult volChange(List playerList, Integer vol) { VnnoxResult vnnoxResult = vnnoxAPIUtil.volChange( playerList.stream().map(item -> item.getPlayerId()).collect(Collectors.toList()), vol ); // 根据音量调整REDIS标识位 for (String playerId : vnnoxResult.getSuccess()) { redisUtils.set(VnnoxConstant.REDIS_VOL + playerId, vol, VnnoxConstant.REDIS_MAX_SAVE_TIME); } return vnnoxResult; } public VnnoxResult brightnessChange(List playerList, Integer brightness) { VnnoxResult vnnoxResult = vnnoxAPIUtil.brightnessChange( playerList.stream().map(item -> item.getPlayerId()).collect(Collectors.toList()), brightness ); // 根据亮度调整REDIS标识位 for (String playerId : vnnoxResult.getSuccess()) { redisUtils.set(VnnoxConstant.REDIS_BRIGHTNESS + playerId, brightness, VnnoxConstant.REDIS_MAX_SAVE_TIME); } return vnnoxResult; } public Map getScreenShotUrl(Integer id) { Map map = new HashMap(); LedPlayerEntity playerEntity = ledPlayerEntityService.getById(id); try { redisUtils.delete(VnnoxConstant.REDIS_SCREEN_SHOT + playerEntity.getPlayerId()); } catch (Exception e) { } VnnoxResult vnnoxResult = vnnoxAPIUtil.screenShot(playerEntity.getPlayerId()); if (null == vnnoxResult) { map.put("code", "500"); map.put("msg", "设备已下线"); return map; } if (vnnoxResult.getSuccess().size() == 0) { map.put("code", "500"); map.put("msg", "获取缩略图失败!"); return map; } String url = null; Integer checkCount = 0; while (checkCount < 10) { url = redisUtils.get(VnnoxConstant.REDIS_SCREEN_SHOT + playerEntity.getPlayerId()); if (null != url) { break; } try { Thread.sleep(2000); checkCount = checkCount + 1; } catch (InterruptedException e) { e.printStackTrace(); } } map.put("url", url); map.put("code", "200"); return map; } public VnnoxResult reboot(List playerList) { return vnnoxAPIUtil.reboot( playerList.stream().map( item -> item.getPlayerId() ).collect(Collectors.toList()) ); } public boolean updateDataName(Long id, String name){ LedPlayerEntity byId = ledPlayerEntityService.getById(id); if(byId==null){ throw new BusinessException("未找到该设备"); } LedPlayerEntity led=new LedPlayerEntity(); led.setId(id); led.setName(name); return ledPlayerEntityService.updateById(led); } }