package com.sandu.ximon.admin.service;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.google.gson.Gson;
|
import com.sandu.common.execption.BusinessException;
|
import com.sandu.common.service.impl.BaseServiceImpl;
|
import com.sandu.common.util.SpringContextHolder;
|
import com.sandu.ximon.admin.config.RealtimeServerBean;
|
import com.sandu.ximon.admin.entity.*;
|
import com.sandu.ximon.admin.param.PoleBindingParam;
|
import com.sandu.ximon.admin.security.SecurityUtils;
|
import com.sandu.ximon.admin.utils.FileUtil;
|
import com.sandu.ximon.admin.utils.HtmlTemplateUtils;
|
import com.sandu.ximon.admin.utils.LightemitUtils;
|
import com.sandu.ximon.admin.vo.EquipmentInfomation;
|
import com.sandu.ximon.dao.domain.Pole;
|
import com.sandu.ximon.dao.domain.PoleLightemitEntity;
|
import com.sandu.ximon.dao.enums.PoleBindingEnums;
|
import com.sandu.ximon.dao.mapper.PoleLightemitEntityMapper;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.cache.annotation.EnableCaching;
|
import org.springframework.context.ApplicationContext;
|
import org.springframework.scheduling.annotation.Async;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.PostConstruct;
|
import java.io.File;
|
import java.io.IOException;
|
import java.util.*;
|
|
/**
|
* 熙讯设备
|
*/
|
@Service
|
@EnableCaching
|
public class PoleLightemitService extends BaseServiceImpl<PoleLightemitEntityMapper, PoleLightemitEntity> {
|
|
@Autowired
|
private PoleBindingService poleBindingService;
|
|
LightemitUtils lightemitUtils;
|
@Autowired
|
private RealtimeServerBean realtimeServerBean;
|
@Value("${server.port}")
|
private String port;
|
@Autowired
|
ApplicationContext applicationContext;
|
|
@Autowired
|
PoleLightemitEntityMapper poleLightemitDao;
|
|
@PostConstruct
|
public void init() {
|
//重新获取单例
|
lightemitUtils = applicationContext.getBean(LightemitUtils.class);
|
}
|
|
/**
|
* 熙讯list
|
*
|
* @param keyword
|
* @param isOnLine
|
* @return
|
*/
|
public List<PoleLightemitEntity> listLed(String keyword, boolean isOnLine) {
|
List<PoleLightemitEntity> poleLightemitEntityList;
|
//超管
|
if (SecurityUtils.getClientId() == null) {
|
poleLightemitEntityList = poleLightemitDao.listLed(keyword, null);
|
} else {
|
poleLightemitEntityList = poleLightemitDao.listLed(keyword, SecurityUtils.getUserId());
|
}
|
if (isOnLine) {
|
return ledOnline(poleLightemitEntityList);
|
} else {
|
ledOnlineCheck(poleLightemitEntityList);
|
return poleLightemitEntityList;
|
}
|
}
|
|
/**
|
* 取出在线的熙讯
|
*
|
* @param list
|
* @return
|
*/
|
public List<PoleLightemitEntity> ledOnline(List<PoleLightemitEntity> list) {
|
List<PoleLightemitEntity> onLineList = new ArrayList<>();
|
for (PoleLightemitEntity poleLightemitEntity : list) {
|
// if(lightemitUtils.getIsScreenOpen(poleLightemitEntity.getLightemitControlCode()).contains("true")){
|
// poleLightemitEntity.setIsOpen(lightemitUtils.getIsScreenOpen(poleLightemitEntity.getLightemitControlCode()).contains("true"));
|
// }
|
poleLightemitEntity.setIsOpen(lightemitUtils.getIsScreenOpen(poleLightemitEntity.getLightemitControlCode()));
|
boolean ledOnLine = lightemitUtils.getLedOnLine(poleLightemitEntity.getLightemitControlCode());
|
if (ledOnLine) {
|
poleLightemitEntity.setOnLine(true);
|
onLineList.add(poleLightemitEntity);
|
} else {
|
poleLightemitEntity.setOnLine(false);
|
}
|
}
|
return onLineList;
|
}
|
|
|
/**
|
* 设置在线状态
|
*
|
* @param list
|
*/
|
public void ledOnlineCheck(List<PoleLightemitEntity> list) {
|
for (PoleLightemitEntity poleLightemitEntity : list) {
|
poleLightemitEntity.setIsOpen(lightemitUtils.getIsScreenOpen(poleLightemitEntity.getLightemitControlCode()));
|
poleLightemitEntity.setOnLine(lightemitUtils.getLedOnLine(poleLightemitEntity.getLightemitControlCode()));
|
}
|
}
|
|
|
/**
|
* 查询单个LED
|
*
|
* @param lightControlCode
|
* @return
|
*/
|
public PoleLightemitEntity getLedByLightControlCode(String lightControlCode) {
|
PoleLightemitEntity xiXun = getOne(Wrappers.lambdaQuery(PoleLightemitEntity.class).eq(PoleLightemitEntity::getLightemitControlCode, lightControlCode));
|
Pole getpole = poleLightemitDao.getpole(lightControlCode);
|
if (xiXun == null) {
|
return null;
|
}
|
boolean isScreenOpen = lightemitUtils.getLedOnLine(xiXun.getLightemitControlCode());
|
|
xiXun.setIsOpen(lightemitUtils.getIsScreenOpen(lightControlCode));
|
xiXun.setOnLine(isScreenOpen);
|
xiXun.setStreetlightName(getpole.getPoleName());
|
xiXun.setStreetlightId(getpole.getId());
|
|
return xiXun;
|
}
|
|
/**
|
* 首页灯杆绑定信息
|
*/
|
public EquipmentInfomation getLedByLightControlCodeInfo(String lightControlCode) {
|
EquipmentInfomation equipmentInfo = new EquipmentInfomation();
|
equipmentInfo.setEquipmentType("熙讯LED");
|
if (lightControlCode == null || lightControlCode.trim().length() == 0) {
|
return equipmentInfo;
|
}
|
|
PoleLightemitEntity xiXun = getOne(Wrappers.lambdaQuery(PoleLightemitEntity.class).eq(PoleLightemitEntity::getLightemitControlCode, lightControlCode));
|
if (xiXun != null) {
|
equipmentInfo.setEquipmentCreateTime(xiXun.getCreateTime());
|
equipmentInfo.setEquipmentMac(xiXun.getLightemitControlCode());
|
equipmentInfo.setEquipmentName(xiXun.getLightemitName());
|
}
|
boolean isScreenOpen = lightemitUtils.getLedOnLine(xiXun.getLightemitControlCode());
|
if (isScreenOpen) {
|
equipmentInfo.setEquipmentState("在线");
|
} else {
|
equipmentInfo.setEquipmentState("离线");
|
}
|
|
return equipmentInfo;
|
}
|
|
|
public void savePoleLightemit(PoleLightemitEntity poleLightemit) {
|
boolean save = this.save(poleLightemit);
|
// 绑定灯杆
|
|
if (save && poleLightemit.getPoleId() != null) {
|
PoleBindingParam poleBindingParam = new PoleBindingParam();
|
poleBindingParam.setDeviceCode(poleLightemit.getLightemitControlCode());
|
poleBindingParam.setDeviceType(1);
|
poleBindingParam.setDeviceName(poleLightemit.getLightemitName());
|
poleBindingService.bindPole(poleLightemit.getPoleId(), poleBindingParam);
|
}
|
// poleStreetlightLightemitService.saveStreetlightLightemit(poleLightemit.getLightemitId(),poleLightemit.getStreetlightId());
|
}
|
|
|
public void updatePoleLightemit(Long ledId, PoleLightemitEntity poleLightemit) {
|
PoleLightemitEntity byId = getById(ledId);
|
if (byId == null) {
|
throw new BusinessException("未找到LED屏");
|
}
|
|
boolean belong = SpringContextHolder.getBean(PoleBindingService.class).isBelong(byId.getLightemitControlCode(), PoleBindingEnums.XIXUN);
|
if (!belong) {
|
throw new RuntimeException("该设备不属于您,不能修改设备信息");
|
}
|
poleLightemit.setLightemitId(ledId);
|
boolean b = this.updateById(poleLightemit);
|
// 绑定灯杆
|
|
if (b && poleLightemit.getPoleId() != null) {
|
//先删除绑定关系
|
poleBindingService.unBindPole(null, poleLightemit.getLightemitControlCode());
|
//再绑定
|
PoleBindingParam poleBindingParam = new PoleBindingParam();
|
poleBindingParam.setDeviceCode(poleLightemit.getLightemitControlCode());
|
poleBindingParam.setDeviceType(10);
|
poleBindingParam.setDeviceName(poleLightemit.getLightemitName());
|
poleBindingService.bindPole(poleLightemit.getPoleId(), poleBindingParam);
|
}
|
// poleStreetlightLightemitService.saveStreetlightLightemit(poleLightemit.getLightemitId(),poleLightemit.getStreetlightId());
|
}
|
|
public boolean deletePoleLightemit(List<Long> ledIds) {
|
|
List<PoleLightemitEntity> poleLightemitEntities = listByIds(ledIds);
|
|
// 删除设备绑定
|
if (poleLightemitEntities != null && poleLightemitEntities.size() != 0) {
|
for (PoleLightemitEntity poleLightemitEntitie : poleLightemitEntities) {
|
if (poleLightemitEntitie.getLightemitControlCode() != null) {
|
//删除绑定关系//先删除绑定关系
|
poleBindingService.unBindPole(poleLightemitEntitie.getLightemitControlCode());
|
}
|
}
|
}
|
//删除设备
|
boolean b = removeByIds(ledIds);
|
return b;
|
// poleStreetlightLightemitService.saveStreetlightLightemit(poleLightemit.getLightemitId(),poleLightemit.getStreetlightId());
|
}
|
|
/**
|
* led上传图片
|
*
|
* @param lightemitControlCode
|
* @param path
|
* @param filenameList
|
* @param userName
|
* @throws IOException
|
*/
|
@Async("taskExecutor")
|
public void photoUpload(String lightemitControlCode, String path, List<String> filenameList, String userName) throws IOException {
|
//获取屏幕宽
|
String screenWidth = lightemitUtils.getScreenWidth(lightemitControlCode);
|
String screenHeight = lightemitUtils.getScreenHeight(lightemitControlCode);
|
|
//html地址
|
String filenameTemp = path + userName + "_" + lightemitControlCode + ".html";
|
File htmlFile = new File(filenameTemp);
|
if (!htmlFile.exists() || htmlFile.delete()) {
|
htmlFile.createNewFile();
|
}
|
|
//获取body的html
|
Map<String, Object> params = new HashMap<>();
|
params.put("imgList", filenameList);
|
params.put("screenWidth", screenWidth);
|
params.put("screenHeight", screenHeight);
|
|
String body = new HtmlTemplateUtils().renderPath("classpath:templates/lightemitTemplates/photoTemplates.html", params);
|
|
boolean flag = new FileUtil().writeToFile(body, filenameTemp);
|
|
if (flag) {
|
//清屏操作
|
lightemitUtils.clear(lightemitControlCode);
|
//清除播放列表
|
lightemitUtils.clearVideoPlay(lightemitControlCode);
|
//清除节目列表
|
lightemitUtils.clearPlayerTask(lightemitControlCode);
|
//推送图片
|
lightemitUtils.postHtml(lightemitControlCode, userName);
|
}
|
}
|
|
/**
|
* led上传视频
|
*
|
* @param lightemitControlCode
|
* @param filename
|
* @param path
|
* @param userName
|
* @throws IOException
|
*/
|
@Async("taskExecutor")
|
public void videoUpload(String lightemitControlCode, String filename, String path, String userName) throws IOException {
|
//获取屏幕宽
|
String screenWidth = lightemitUtils.getScreenWidth(lightemitControlCode);
|
String screenHeight = lightemitUtils.getScreenHeight(lightemitControlCode);
|
|
//发送视频至开发板并播放
|
lightemitUtils.postVideo(screenWidth, screenHeight, filename, lightemitControlCode);
|
|
//清屏
|
lightemitUtils.clear(lightemitControlCode);
|
|
}
|
|
|
@Async("taskExecutor")
|
public void videoXixunPlayer(String lightemitControlCode, String ip, String filename, Long filesize, Integer videoTime) {
|
//获取屏幕宽
|
Integer screenWidth = Integer.valueOf(lightemitUtils.getScreenWidth(lightemitControlCode));
|
Integer screenHeight = Integer.valueOf(lightemitUtils.getScreenHeight(lightemitControlCode));
|
Source source = new Source();
|
Layer layer = new Layer();
|
Program pro = new Program();
|
TaskItem items = new TaskItem();
|
ProgramsTask task = new ProgramsTask();
|
Command command = new Command();
|
XixunPlayer xixun = new XixunPlayer();
|
//所有_id都可用UUID随机生成
|
source.set_id(UUID.randomUUID().toString());
|
//请求头拼接该ID为该资源的完整下载地址
|
source.setId(filename);
|
//设置资源类型,其他资源类型请参考xixunplayer节目json说明文档
|
source.set_type("Video");
|
//资源后缀名
|
source.setFileExt(".mp4");
|
//资源高度
|
source.setHeight(screenHeight);
|
//资源宽度
|
source.setWidth(screenWidth);
|
//距左
|
source.setLeft(0);
|
source.setMd5("dd135d5d2d44d619a542db773ab529a4");
|
source.setMime("video/mp4");
|
source.setName(filename);
|
source.setPlayTime(0); //播放起始时间
|
source.setSize(filesize); //资源字节数,要精准
|
source.setTimeSpan(videoTime); //该资源播放时长
|
source.setTop(0); //距顶
|
List<Source> list = new ArrayList<Source>();
|
list.add(source);
|
layer.setSources(list);
|
pro.set_id(UUID.randomUUID().toString());
|
pro.setHeight(screenHeight); //节目高度
|
List<Layer> list1 = new ArrayList<Layer>();
|
list1.add(layer);
|
pro.setLayers(list1);
|
pro.setName("demo"); //节目名称
|
pro.setOthers(true);//如果为true,则该节目来自第三方接口,不是来自我们自己的web,二次开发请务必赋值为true
|
pro.setTotalSize(filesize); //所有资源总字节数
|
pro.setVersion(0); //高级节目=0,简易节目=2
|
pro.setWidth(screenWidth); //节目宽度
|
items.set_id(UUID.randomUUID().toString());
|
items.set_program(pro);
|
items.setRepeatTimes(1);
|
items.setSchedules(null);//定时段,不做定时可为null
|
task.set_id(UUID.randomUUID().toString());
|
task.setName("这是一个示例");
|
List<TaskItem> list2 = new ArrayList<TaskItem>();
|
list2.add(items);
|
task.setItems(list2);
|
command.setId(UUID.randomUUID().toString());
|
//这里是下方的post回调地址,需要修改IP地址
|
command.setNotificationURL("http://" + ip + ":" + port + "/machine-fast/serv/download/getJSON");
|
//资源下载链接的请求头
|
command.setPreDownloadURL("http://" + ip + ":" + port + "/machine-fast/serv/download/downloadFile/");
|
command.setTask(task);
|
xixun.set_id(UUID.randomUUID().toString());
|
xixun.setCommand(command);
|
xixun.setType("commandXixunPlayer"); //命令固定类型,不可更改
|
String jsondata = new Gson().toJson(xixun);
|
lightemitUtils.clear(lightemitControlCode);
|
poleLightemitDao.updateRequestBody(lightemitControlCode, jsondata);
|
lightemitUtils.post(realtimeServerBean.getCommand() + lightemitControlCode, jsondata);
|
}
|
|
/**
|
* 更新请求数据
|
*
|
* @param ledCode
|
* @param postBody
|
*/
|
public void updateRequestBody(String ledCode, String postBody) {
|
baseMapper.updateRequestBody(ledCode, postBody);
|
}
|
|
// /**
|
// * 根据led屏编码获取灯杆所有信息
|
// *
|
// * @param ledCode
|
// * @return
|
// */
|
// public PoleStreetlightEntity getStreetlightByLedCode(String ledCode) {
|
// return baseMapper.getStreetlightByLedCode(ledCode);
|
// }
|
|
|
/**
|
* 根据led屏编码设置音量
|
*
|
* @param lightemitControlCode
|
* @param volume
|
*/
|
@Async("taskExecutor")
|
public void setVolume(String lightemitControlCode, Integer volume) {
|
lightemitUtils.setVoiume(lightemitControlCode, volume);
|
}
|
|
/**
|
* 根据led屏编码设置亮度
|
*
|
* @param lightemitControlCode
|
* @param brightness
|
*/
|
@Async("taskExecutor")
|
public void setBrightness(String lightemitControlCode, Integer brightness) {
|
lightemitUtils.setBrightness(lightemitControlCode, brightness);
|
}
|
|
}
|