package com.sandu.ximon.admin.newnova.led; import com.alibaba.fastjson.JSON; 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.common.util.SpringContextHolder; import com.sandu.ximon.admin.manager.iot.frame.inner.report.A5AtmosphereHeartbeatReportInnerFrame; import com.sandu.ximon.admin.newnova.conf.ProgramPathConfig; import com.sandu.ximon.admin.newnova.param.NewNovaLedParam; import com.sandu.ximon.admin.newnova.param.ProgramPrarm; import com.sandu.ximon.admin.newnova.program.NewNovaProgramService; import com.sandu.ximon.admin.newnova.utils.NovaAPIUtil; import com.sandu.ximon.admin.newnova.vo.ProWHVO; import com.sandu.ximon.admin.newnova.vo.StatusVO; import com.sandu.ximon.admin.newnova.vo.TrasfromStatusVO; import com.sandu.ximon.admin.security.SecurityUtils; import com.sandu.ximon.admin.service.AirDataService; import com.sandu.ximon.admin.service.PoleBindingService; import com.sandu.ximon.admin.service.PoleService; import com.sandu.ximon.dao.bo.NewNovaGroupListBo; import com.sandu.ximon.dao.domain.NewNovaLed; import com.sandu.ximon.dao.domain.Pole; import com.sandu.ximon.dao.domain.PoleBinding; import com.sandu.ximon.dao.mapper.NewNovaLedMapper; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import java.io.File; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * @author LiuHaoNan * @date 2022/11/9 */ @Service @AllArgsConstructor public class NewNovaLedService extends BaseServiceImpl { private final PoleBindingService poleBindingService; private ProgramPathConfig filePathConfig; /** * 屏幕注册 * * @param param * @return */ public boolean saveLed(NewNovaLedParam param) { NewNovaLed one = getOne(Wrappers.lambdaQuery(NewNovaLed.class).eq(NewNovaLed::getSn, param.getSn())); if (one != null) { throw new BusinessException("设备已存在,请勿重复注册!"); } NewNovaLed led = new NewNovaLed(); led.setSn(param.getSn()); led.setName(param.getName()); led.setOnlineSign(0); return save(led); } /** * 编辑屏幕名称 * * @param param * @return */ public boolean editLed(NewNovaLedParam param) { if (param.getId() == null) { throw new BusinessException("屏幕id不能为空"); } if (param.getName() == null || param.getName().isEmpty()) { throw new BusinessException("屏幕名称不能为空"); } NewNovaLed one = getById(param.getId()); if (one == null) { throw new BusinessException("设备不存在!"); } // one.setSn(param.getSn()); one.setName(param.getName()); return updateById(one); } /** * 删除LED * * @param ids * @return */ public boolean delLed(List ids) { if (ids.isEmpty()) { throw new BusinessException("id不能为空"); } List newNovaLeds = listByIds(ids); if (newNovaLeds.isEmpty()) { throw new BusinessException("设备不存在!"); } //获取删除设备的设备码 List snList = newNovaLeds.stream().map(NewNovaLed::getSn).collect(Collectors.toList()); //删除绑定关系 SpringContextHolder.getBean(PoleBindingService.class).remove(Wrappers.lambdaQuery(PoleBinding.class) .eq(PoleBinding::getDeviceType, 12).in(PoleBinding::getDeviceCode, snList)); //删除设备 return removeByIds(ids); } /** * LED列表 * * @param baseConditionVO * @param keyword * @param groupId * @param onlineStatus * @return */ public List listLed(BaseConditionVO baseConditionVO, String keyword, Long groupId, boolean onlineStatus) { NovaAPIUtil instanceUtil = NovaAPIUtil.getInstanceUtil(); //排序 PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize()); List newNovaListBos = baseMapper.listLed(keyword, groupId, SecurityUtils.getClientId()); //获取在线状态 List onLine = instanceUtil.getOnlineStatus(newNovaListBos, onlineStatus); //获取屏幕开关 instanceUtil.getScreenPowerState(onLine); //获取音量 instanceUtil.getVolumeState(onLine); //获取亮度 instanceUtil.getScreenBrightness(onLine); //获取同步状态 instanceUtil.getSync(onLine); //获取分辨率 instanceUtil.getDisplayInfoAsync(onLine); //获取时区 instanceUtil.getTimezone(onLine); //获取视频源 instanceUtil.getVideoInfoAsync(onLine); updateBatchById(onLine); return onLine; } public NewNovaLed getInfo(Long ledId) { NovaAPIUtil instanceUtil = NovaAPIUtil.getInstanceUtil(); NewNovaLed byId = getById(ledId); if (byId == null) { throw new BusinessException("设备不存在或id不正确 !"); } PoleBinding poleBinding = SpringContextHolder.getBean(PoleBindingService.class).getOne(Wrappers.lambdaQuery(PoleBinding.class) .eq(PoleBinding::getDeviceCode, byId.getSn()).eq(PoleBinding::getDeviceType, 12)); if (poleBinding != null) { Pole pole = SpringContextHolder.getBean(PoleService.class).getById(poleBinding.getPoleId()); byId.setPoleId(pole.getId()); byId.setPoleName(pole.getPoleName()); } List list = new ArrayList<>(); list.add(byId); //获取在线状态 instanceUtil.getOnlineStatus(list, false); //获取屏幕开关 instanceUtil.getScreenPowerState(list); //获取音量 instanceUtil.getVolumeState(list); //获取亮度 instanceUtil.getScreenBrightness(list); //获取同步状态 instanceUtil.getSync(list); //获取详情 instanceUtil.getInfo(byId); //获取分辨率 instanceUtil.getDisplayInfoAsync(list); //获取时区 instanceUtil.getTimezone(list); //获取视频源 instanceUtil.getVideoInfoAsync(list); return list.get(0); } /** * 获取整组的屏幕 * * @param groupId */ public List getListByGroupId(Long groupId) { List listByGroupId = baseMapper.getListByGroupId(groupId, SecurityUtils.getClientId()); return listByGroupId; } /** * 推送大气 * * @param id * @param duration * @param fontSize */ public Object pushAirData(Long id, Long duration, Long fontSize) throws InterruptedException { NovaAPIUtil apiUtil = NovaAPIUtil.getInstanceUtil(); NewNovaLed byId = getById(id); PoleBinding nova = poleBindingService.getOne(Wrappers.lambdaQuery(PoleBinding.class).eq(PoleBinding::getDeviceCode, byId.getSn()).eq(PoleBinding::getDeviceType, 12)); if (nova == null) { throw new BusinessException("未绑定诺瓦设备"); } PoleBinding air = poleBindingService.getOne(Wrappers.lambdaQuery(PoleBinding.class).eq(PoleBinding::getPoleId, nova.getPoleId()).eq(PoleBinding::getDeviceType, 3)); if (air == null) { throw new BusinessException("未绑定大气设备"); } //获取大气监测数据 A5AtmosphereHeartbeatReportInnerFrame.HeartBeatDataPackage data = SpringContextHolder.getBean(AirDataService.class).getDataByPoleid(nova.getPoleId()); //解析大气数据 Map pageInfo = parseData(data, duration, fontSize); ProgramPrarm page = new ProgramPrarm(); page.setPageInfo(pageInfo); page.setInsertPlay(true); ProWHVO whvo = new ProWHVO("大气监测", 128, 256); page.setProgramMsg(whvo); StatusVO createPro = apiUtil.createPro(whvo); Integer pid = Integer.valueOf(createPro.getStatusData()); page.setProgramID(pid); StatusVO editPro = apiUtil.editProgram(pid, pageInfo); if (editPro.getStatusCode() != 0) { throw new BusinessException(editPro.getStatusData()); } StatusVO genVO = apiUtil.genrateProgram(pid); if (genVO.getStatusCode() != 0) { throw new BusinessException(genVO.getStatusData()); } page.setProgramID(Integer.valueOf(createPro.getStatusData())); page.setStartPlayAfterTransferred(true); List sns = new ArrayList(); sns.add(byId.getSn()); page.setSnList(sns); List trasfromStatusVOS = apiUtil.trasfromProgram(page); File file = new File(filePathConfig.getOutPutPath() + "/program" + pid); SpringContextHolder.getBean(NewNovaProgramService.class).deleteFile(file); return trasfromStatusVOS; } /** * 处理大气数据 * * @param data * @param duration * @param fontSize * @return */ private Map parseData(A5AtmosphereHeartbeatReportInnerFrame.HeartBeatDataPackage data, Long duration, Long fontSize) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z+8:00'"); Date end = new Date(); end.setTime(end.getTime() + duration); String str = "{\n" + " \"name\": \"大气监测\",\n" + " \"widgetContainers\": [\n" + " {\n" + " \"contents\": {\n" + " \"widgets\": [\n" + " {\n" + " \"constraints\": [\n" + " {\n" + " \"cron\": [\n" + " \"\"\n" + " ],\n" + " \"endTime\": \"" + sdf.format(end) + "\",\n" + " \"startTime\": \"" + sdf.format(new Date()) + "\"\n" + " }\n" + " ],\n" + " \"duration\":" + duration + ",\n" + " \"metadata\": {\n" + " \"content\": {\n" + " \"autoPaging\": true,\n" + " \"backgroundMusic\": {\n" + " \"duration\": 0,\n" + " \"isTextSync\": false\n" + " },\n" + " \"displayStyle\": {\n" + " \"scrollAttributes\": {\n" + " \"effects\": {\n" + " \"animation\": \"MARQUEE_LEFT\",\n" + " \"speed\": 3\n" + " }\n" + " },\n" + " \"type\": \"STATIC\"\n" + " },\n" + " \"paragraphs\": [\n" + " {\n" + " \"backgroundColor\": \"#00000000\",\n" + " \"horizontalAlignment\": \"LEFT\",\n" + " \"letterSpacing\": 0,\n" + " \"lineSpacing\": 0,\n" + " \"lines\": [\n" + " {\n" + " \"segs\": [\n" + " {\n" + " \"content\": \"环境监测:\"\n" + " }\n" + " ]\n" + " },\n" + " {\n" + " \"segs\": [\n" + " {\n" + " \"content\": \"温度:" + (data == null || data.getTemperature() == null ? " - " : data.getTemperature()) + "°\"" + "\n" + " }\n" + " ]\n" + " },\n" + " {\n" + " \"segs\": [\n" + " {\n" + " \"content\": \"湿度:" + (data == null || data.getHumidity() == null ? " - " : data.getHumidity()) + "%\"" + "\n" + " }\n" + " ]\n" + " },\n" + " {\n" + " \"segs\": [\n" + " {\n" + " \"content\": \"pm25:" + (data == null || data.getPm25() == null ? " - " : data.getPm25()) + "μg/m³\"" + "\n" + " }\n" + " ]\n" + " },\n" + " {\n" + " \"segs\": [\n" + " {\n" + " \"content\": \"pm10:" + (data == null || data.getPm10() == null ? " - " : data.getPm10()) + "μg/m³\"" + "\n" + " }\n" + " ]\n" + " },\n" + " {\n" + " \"segs\": [\n" + " {\n" + " \"content\": \"甲醛:" + (data == null || data.getEch2o() == null ? " - " : data.getEch2o()) + "μg/m³\"" + "\n" + " }\n" + " ]\n" + " },\n" + " {\n" + " \"segs\": [\n" + " {\n" + " \"content\": \"CO2:" + (data == null || data.getCo2() == null ? " - " : data.getCo2()) + "ppm\"" + "\n" + " }\n" + " ]\n" + " }\n" + " ],\n" + " \"verticalAlignment\": \"TOP\"\n" + " }\n" + " ],\n" + " \"textAttributes\": [\n" + " {\n" + " \"backgroundColor\": \"#ff000000\",\n" + " \"attributes\": {\n" + " \"font\": {\n" + " \"family\": [\n" + " \"Helvetica\"\n" + " ],\n" + " \"isUnderline\": false,\n" + " \"size\": " + fontSize + ",\n" + " \"style\": \"NORMAL\"\n" + " },\n" + " \"letterSpacing\": 0,\n" + " \"textColor\": \"#ffff0000\"\n" + " }\n" + " }\n" + " ]\n" + " }\n" + " },\n" + " \"name\": \"环境监测\",\n" + " \"type\": \"ARCH_TEXT\"\n" + " }\n" + " ]\n" + " },\n" + " \"id\": 1,\n" + " \"name\": \"widgetContainers1\"\n" + " }\n" + " ]\n" + " }"; return JSON.parseObject(str, Map.class); } }