| | |
| | | import com.doumee.core.haikang.model.param.request.CustomBroadcastRequest; |
| | | import com.doumee.core.haikang.model.param.request.TransparentChannelSingleRequest; |
| | | import com.doumee.core.haikang.service.HKService; |
| | | import com.doumee.core.utils.HttpsUtil; |
| | | import com.doumee.core.utils.*; |
| | | import com.doumee.dao.business.*; |
| | | import com.doumee.dao.business.model.*; |
| | | import com.doumee.mqtt.config.MqttConfig; |
| | | import com.doumee.mqtt.service.MqttBizService; |
| | | import com.doumee.service.business.impl.mqtt.MqttClientCache; |
| | | import com.doumee.service.business.third.model.LoginUserInfo; |
| | | import com.doumee.service.business.third.model.PageData; |
| | | import com.doumee.service.business.third.model.PageWrap; |
| | | import com.doumee.core.utils.Constants; |
| | | import com.doumee.core.utils.Utils; |
| | | import com.doumee.service.business.DeviceService; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | |
| | | import com.github.yulichang.wrapper.MPJLambdaWrapper; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.eclipse.paho.client.mqttv3.MqttClient; |
| | | import org.eclipse.paho.client.mqttv3.MqttMessage; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import java.util.*; |
| | | import java.util.Date; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | public class DeviceServiceImpl implements DeviceService { |
| | | |
| | | @Autowired |
| | | private MqttBizService mqttBizService; |
| | | @Autowired |
| | | private DeviceMapper deviceMapper; |
| | | @Autowired |
| | | private DeviceDataMapper deviceDataMapper; |
| | | @Autowired |
| | | private PlatformMapper platformMapper; |
| | | @Autowired |
| | |
| | | private SystemDictDataBiz systemDictDataBiz; |
| | | @Autowired |
| | | private PlatformBroadcastLogMapper platformBroadcastLogMapper; |
| | | @Autowired |
| | | private InterfaceLogMapper interfaceLogMapper; |
| | | |
| | | |
| | | @Override |
| | |
| | | model.setIsdeleted(Constants.ZERO); |
| | | model.setEditDate(new Date()); |
| | | model.setCreateDate(model.getEditDate()); |
| | | if(model.getDoorNameObj()!=null && Constants.equalsInteger(model.getType(),Constants.DEVICE_TYPE.duanluqi)){ |
| | | model.setDoorName(JSONObject.toJSONString(model.getDoorNameObj())); |
| | | if(StringUtils.isNotBlank(model.getLevel())){ |
| | | if(getNumberByStr(model.getLevel()) <300){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,控制时长必须大于等于300秒"); |
| | | } |
| | | } |
| | | } |
| | | deviceMapper.insert(model); |
| | | return model.getId(); |
| | | } |
| | |
| | | public void updateById(Device device) { |
| | | device.setEdirot(device.getLoginUserInfo().getId()+""); |
| | | device.setEditDate(new Date()); |
| | | if(device.getDoorNameObj()!=null && Constants.equalsInteger(device.getType(),Constants.DEVICE_TYPE.duanluqi)){ |
| | | device.setDoorName(JSONObject.toJSONString(device.getDoorNameObj())); |
| | | } |
| | | deviceMapper.updateById(device); |
| | | } |
| | | |
| | |
| | | |
| | | @Override |
| | | public Device findById(Integer id) { |
| | | return deviceMapper.selectById(id); |
| | | Device d = deviceMapper.selectById(id); |
| | | if(StringUtils.isNotBlank(d.getDoorName()) &&Constants.equalsInteger(d.getType(),Constants.DEVICE_TYPE.duanluqi)){ |
| | | try { |
| | | //断路器设备参数 |
| | | d.setDoorNameObj(JSONObject.parseObject(d.getDoorName())); |
| | | }catch (Exception e){ |
| | | |
| | | } |
| | | } |
| | | return d; |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | @Override |
| | | public List<Device> findList(Device device) { |
| | | device.setIsdeleted(Constants.ZERO); |
| | | QueryWrapper<Device> wrapper = new QueryWrapper<>(device); |
| | | return deviceMapper.selectList(wrapper); |
| | | } |
| | |
| | | .eq(null !=param.getIsdeleted(),Device::getIsdeleted,param.getIsdeleted()) |
| | | .eq(Objects.isNull(param.getIsdeleted()),Device::getIsdeleted,Constants.ZERO) |
| | | .eq(null != param.getHkStatus(),Device::getHkStatus,param.getHkStatus()); |
| | | return deviceMapper.selectList(wrapper); |
| | | List<Device> list = deviceMapper.selectList(wrapper); |
| | | if(list!=null){ |
| | | for(Device d : list){ |
| | | if(StringUtils.isNotBlank(d.getDoorName()) &&Constants.equalsInteger(d.getType(),Constants.DEVICE_TYPE.duanluqi)){ |
| | | try { |
| | | //断路器设备参数 |
| | | d.setDoorNameObj(JSONObject.parseObject(d.getDoorName())); |
| | | }catch (Exception e){ |
| | | |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | @Override |
| | |
| | | queryWrapper.orderByAsc(sortData.getProperty()); |
| | | } |
| | | } |
| | | return PageData.from(deviceMapper.selectPage(page, queryWrapper)); |
| | | IPage<Device> result = deviceMapper.selectPage(page, queryWrapper); |
| | | if(result!=null){ |
| | | for(Device d : result.getRecords()){ |
| | | if(StringUtils.isNotBlank(d.getDoorName()) &&Constants.equalsInteger(d.getType(),Constants.DEVICE_TYPE.duanluqi)){ |
| | | try { |
| | | //断路器设备参数 |
| | | d.setDoorNameObj(JSONObject.parseObject(d.getDoorName())); |
| | | }catch (Exception e){ |
| | | |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return PageData.from(result); |
| | | } |
| | | |
| | | @Override |
| | |
| | | throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"发送失败:"+ JSONObject.toJSONString(response)); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void updateUsedById(Device param){ |
| | | Device model = deviceMapper.selectById(param.getId()); |
| | | if(model ==null && Constants.equalsInteger(param.getType(),Constants.DEVICE_TYPE.duanluqi)){ |
| | | throw new BusinessException(ResponseStatus.DATA_EMPTY); |
| | | } |
| | | |
| | | MqttConfig config = getMqttConfigByParam(model,"device_"); |
| | | MqttClient mqttClient = MqttClientCache.clientMapCache.get("device"+param.getId()); |
| | | String[] topics =new String[]{Constants.MqttTopic.mts_status, |
| | | Constants.MqttTopic.mts_attr, |
| | | Constants.MqttTopic.mts_resp}; |
| | | if(Constants.equalsInteger(param.getIsUsed(),Constants.ONE)){ |
| | | //如果系统不用了,端口mqtt连接 |
| | | try { |
| | | mqttClient = mqttBizService.unsubscribe(mqttClient,config ,topics); |
| | | if(mqttClient ==null){ |
| | | throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"建立连接失败!"); |
| | | } |
| | | if(mqttClient.isConnected()){ |
| | | mqttClient.disconnect(); |
| | | } |
| | | }catch (Exception e){ |
| | | log.error("==============端口mqtt链接失败:"+model.getName()+e.getMessage()); |
| | | } |
| | | }else{ |
| | | //如果开始使用,开始订阅 |
| | | mqttClient = mqttBizService.subscribe(mqttClient,config ,topics); |
| | | if(mqttClient ==null){ |
| | | throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"建立连接失败!"); |
| | | } |
| | | MqttClientCache.clientMapCache.put("device"+param.getId(),mqttClient); |
| | | } |
| | | |
| | | this.updateById(param); |
| | | } |
| | | @Override |
| | | public void duanluqiCmd(Device param){ |
| | | Device model = deviceMapper.selectById(param.getId()); |
| | | if(model ==null && Constants.equalsInteger(param.getType(),Constants.DEVICE_TYPE.duanluqi)){ |
| | | throw new BusinessException(ResponseStatus.DATA_EMPTY); |
| | | } |
| | | dealDuanluqiCmd(model,param,"device_"); |
| | | |
| | | } |
| | | |
| | | private void dealDuanluqiCmd(Device model, Device param,String clientIndex) { |
| | | MqttConfig config = getMqttConfigByParam(model,clientIndex); |
| | | MqttClient mqttClient = MqttClientCache.clientMapCache.get("device"+param.getId()); |
| | | String cmdTopic = Constants.MqttTopic.mts_cmd.replace("$CATE",model.getNo()).replace("$DEVID",model.getDoorNo()); |
| | | if(Constants.equalsInteger(param.getStatus(),Constants.ONE)){ |
| | | //如果是开闸,先解锁 |
| | | String cmdParamAction = CmdContants.T30A.cmdParam.replace("$DEVID", model.getDoorNo()) |
| | | .replace("$CMD", CmdContants.T30ACmd.do_err_clear)//异常解锁 |
| | | .replace("$BUSADDR",model.getChannelNo()); |
| | | mqttClient = mqttBizService.publish(mqttClient,config,cmdTopic, cmdParamAction);//解锁命令 |
| | | if(mqttClient ==null){ |
| | | throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"指令发送失败!"); |
| | | } |
| | | } |
| | | String cmdParamAction =CmdContants.T30A.cmdParam.replace("$DEVID", model.getDoorNo()) |
| | | .replace("$CMD",(Constants.equalsInteger(param.getStatus(),Constants.ONE)?CmdContants.T30ACmd.do_turn_on:CmdContants.T30ACmd.do_turn_off)) |
| | | .replace("$BUSADDR", param.getChannelNo()); |
| | | mqttClient = mqttBizService.publish(mqttClient,config,cmdTopic, cmdParamAction);//开关闸命令 |
| | | if(mqttClient ==null){ |
| | | throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"指令发送失败!"); |
| | | } |
| | | MqttClientCache.clientMapCache.put("device"+param.getId(),mqttClient); |
| | | String curremak = "【"+param.getLoginUserInfo().getRealname() |
| | | +"】于"+ DateUtil.getPlusTime2(new Date()) +"进行了"+(Constants.equalsInteger(param.getStatus(),Constants.ONE)?"【开闸】":"【关闸】")+"操作,开关【"+param.getChannelNo()+"】;"; |
| | | deviceMapper.update(null,new UpdateWrapper<Device>().lambda() |
| | | // .setSql("remark = concat(ifnull(remark,''),'"+curremak+"','\n')") |
| | | .set(Device::getRemark,curremak) |
| | | .set(Device::getEditDate,new Date()) |
| | | .set(Device::getEdirot,param.getLoginUserInfo().getId()) |
| | | .eq(Device::getId,param.getId())); |
| | | } |
| | | |
| | | private MqttConfig getMqttConfigByParam(Device model,String index) { |
| | | MqttConfig config = new MqttConfig(); |
| | | JSONObject mqtt = new JSONObject(); |
| | | if(StringUtils.isBlank(model.getNo()) |
| | | ||StringUtils.isBlank(model.getDoorNo()) ){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,设备标识符和设备号未配置!"); |
| | | } |
| | | if(StringUtils.isNotBlank(model.getDoorName())){ |
| | | try { |
| | | //断路器设备参数 |
| | | mqtt = (JSONObject.parseObject(model.getDoorName())); |
| | | }catch (Exception e){ |
| | | } |
| | | } |
| | | if(mqtt == null |
| | | ||StringUtils.isBlank(mqtt.getString("mqttUsername")) |
| | | ||StringUtils.isBlank(mqtt.getString("mqttIp")) |
| | | ||StringUtils.isBlank(mqtt.getString("mqttPort")) |
| | | ||StringUtils.isBlank(mqtt.getString("mqttPassword"))){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,设备mqtt服务参数配置不正确!"); |
| | | } |
| | | /** |
| | | * MqttConfig config = (new MqttConfig()); |
| | | * config.setHost("tcp://192.168.0.7:1883"); |
| | | * config.setClientid("doumee1"); |
| | | * config.setPassword("doumee@168"); |
| | | * config.setUsername("doumee"); |
| | | * config.setVersion("003"); |
| | | */ |
| | | config.setVersion("003"); |
| | | config.setUsername( mqtt.getString("mqttUsername")); |
| | | config.setPassword( mqtt.getString("mqttPassword")); |
| | | config.setHost("tcp://"+ mqtt.getString("mqttIp")+":"+mqtt.getString("mqttPort")); |
| | | config.setClientid(index+model.getId()); |
| | | return config; |
| | | } |
| | | public void mqttCallbackService(String topic, MqttMessage message){ |
| | | log.error("mqtt收到消息=====topic:",topic); |
| | | log.error("mqtt收到消息=====message:",JSONObject.toJSONString(message)); |
| | | } |
| | | @Override |
| | | public String setBroadcaseBobaoHttp(Device param){ |
| | | /* Device model = deviceMapper.selectById(param.getId()); |
| | | Device model = deviceMapper.selectById(param.getId()); |
| | | if(model ==null && Constants.equalsInteger(param.getType(),Constants.FOUR)){ |
| | | throw new BusinessException(ResponseStatus.DATA_EMPTY); |
| | | } |
| | | String input = ""; |
| | | JSONObject objext = new JSONObject(); |
| | | |
| | | model.setSendInfo(param.getSendInfo()); |
| | | return sendBroadcaseBobaoHttpBiz(model,interfaceLogMapper); |
| | | } |
| | | public static String sendBroadcaseBobaoHttpBiz(Device model,InterfaceLogMapper interfaceLogMapper){ |
| | | //重新下发计划 |
| | | JSONObject jsonObject = new JSONObject(); |
| | | //第一层 |
| | | JSONArray firstJsonArray = new JSONArray(); |
| | | JSONObject downJson = new JSONObject(); |
| | | downJson.put("terminalID",1); |
| | | //不填默认对所有通道下发 |
| | | JSONArray audioJsonArray = new JSONArray(); |
| | | audioJsonArray.add(1); |
| | | downJson.put("audioOutID",audioJsonArray); |
| | | firstJsonArray.add(downJson); |
| | | jsonObject.put("TerminalInfoList",firstJsonArray); |
| | | //第一层数组 |
| | | JSONArray allJsonArray = new JSONArray(); |
| | | //数组中对象 |
| | | for (WaterTimingBroadcastDO waterTimingBroadcastDO : searchList) { |
| | | JSONObject downArrayData = new JSONObject(); |
| | | downArrayData.put("audioOutID",audioJsonArray); |
| | | downArrayData.put("enabled",true); |
| | | downArrayData.put("planSchemeID",waterTimingBroadcastDO.getName()); |
| | | JSONObject executeJson = new JSONObject(); |
| | | executeJson.put("startTime", getDateStringByZones(waterTimingBroadcastDO.getStartDate())); |
| | | executeJson.put("stopTime",getDateStringByZones(waterTimingBroadcastDO.getEndDate())); |
| | | if (Constant.ONE.equals(waterTimingBroadcastDO.getPeriodType())){ |
| | | //日计划 |
| | | JSONArray executeJsonArray = new JSONArray(); |
| | | JSONObject executeJsonDataItem = new JSONObject(); |
| | | executeJsonDataItem.put("beginTime",waterTimingBroadcastDO.getStartTime()+"+08:00"); |
| | | executeJsonDataItem.put("endTime",waterTimingBroadcastDO.getEndTime()+"+08:00"); |
| | | executeJsonDataItem.put("playMode","loop"); |
| | | executeJsonDataItem.put("playNowTime",""); |
| | | executeJsonDataItem.put("planSchemeExecID",0); |
| | | JSONObject secondDownOperationJson = new JSONObject(); |
| | | if (Constant.ONE.equals(waterTimingBroadcastDO.getContentType())){ |
| | | //文本 |
| | | secondDownOperationJson.put("audioSource","speechSynthesis"); |
| | | secondDownOperationJson.put("speechSynthesisContent",waterTimingBroadcastDO.getContent()); |
| | | }else if (Constant.TWO.equals(waterTimingBroadcastDO.getContentType())){ |
| | | //音频 |
| | | secondDownOperationJson.put("audioSource","customAudio"); |
| | | String json = waterTimingBroadcastDO.getJson(); |
| | | JSONObject jsonData = JSONObject.parseObject(json); |
| | | List<Integer> hkAudioIds = new ArrayList<>(); |
| | | hkAudioIds.add(jsonData.getIntValue(waterDeviceDO.getIp())); |
| | | secondDownOperationJson.put("customAudioID",hkAudioIds); |
| | | } |
| | | secondDownOperationJson.put("audioLevel",5); |
| | | //语言类型 |
| | | secondDownOperationJson.put("TTSLanguageType","chinese"); |
| | | secondDownOperationJson.put("voiceType","female"); |
| | | secondDownOperationJson.put("audioVolume",100); |
| | | executeJsonDataItem.put("Operation",secondDownOperationJson); |
| | | executeJsonArray.add(executeJsonDataItem); |
| | | executeJson.put("dailyScheduleList",executeJsonArray); |
| | | }else { |
| | | //周计划 |
| | | JSONArray executeJsonArray = new JSONArray(); |
| | | List<String> executeTime = new ArrayList<>(); |
| | | if (Constant.ONE.equals(waterTimingBroadcastDO.getPeriodType())){ |
| | | //日计划 循环一次 |
| | | executeTime.add("1"); |
| | | }else if (Constant.TWO.equals(waterTimingBroadcastDO.getPeriodType())){ |
| | | //周计划多一个周几 |
| | | executeTime = Arrays.asList(waterTimingBroadcastDO.getExecuteTime().split(",")); |
| | | } |
| | | for (String s : executeTime) { |
| | | JSONObject executeJsonData = new JSONObject(); |
| | | if (Constant.TWO.equals(waterTimingBroadcastDO.getPeriodType())){ |
| | | //周计划多一个周几 |
| | | executeJsonData.put("dayOfWeek",Integer.parseInt(s)); |
| | | } |
| | | JSONArray executeJsonDataArray = new JSONArray(); |
| | | JSONObject executeJsonDataItem = new JSONObject(); |
| | | executeJsonDataItem.put("beginTime",waterTimingBroadcastDO.getStartTime()+"+08:00"); |
| | | executeJsonDataItem.put("endTime",waterTimingBroadcastDO.getEndTime()+"+08:00"); |
| | | executeJsonDataItem.put("playMode","loop"); |
| | | executeJsonDataItem.put("playNowTime",""); |
| | | executeJsonDataItem.put("planSchemeExecID",0); |
| | | JSONObject secondDownOperationJson = new JSONObject(); |
| | | if (Constant.ONE.equals(waterTimingBroadcastDO.getContentType())){ |
| | | //文本 |
| | | secondDownOperationJson.put("audioSource","speechSynthesis"); |
| | | secondDownOperationJson.put("speechSynthesisContent",waterTimingBroadcastDO.getContent()); |
| | | }else if (Constant.TWO.equals(waterTimingBroadcastDO.getContentType())){ |
| | | //音频 |
| | | secondDownOperationJson.put("audioSource","customAudio"); |
| | | String json = waterTimingBroadcastDO.getJson(); |
| | | JSONObject jsonData = JSONObject.parseObject(json); |
| | | List<Integer> hkAudioIds = new ArrayList<>(); |
| | | hkAudioIds.add(jsonData.getIntValue(waterDeviceDO.getIp())); |
| | | secondDownOperationJson.put("customAudioID",hkAudioIds); |
| | | } |
| | | secondDownOperationJson.put("audioLevel",5); |
| | | //语言类型 |
| | | secondDownOperationJson.put("TTSLanguageType","chinese"); |
| | | secondDownOperationJson.put("voiceType","female"); |
| | | secondDownOperationJson.put("audioVolume",10); |
| | | executeJsonDataItem.put("Operation",secondDownOperationJson); |
| | | executeJsonDataArray.add(executeJsonDataItem); |
| | | executeJsonData.put("scheduleList",executeJsonDataArray); |
| | | executeJsonArray.add(executeJsonData); |
| | | jsonObject.put("command","start"); |
| | | jsonObject.put("TTSContent",model.getSendInfo()); |
| | | jsonObject.put("audioLevel",6); |
| | | jsonObject.put("audioVolume",100); |
| | | jsonObject.put("TTSLanguageType","chinese"); |
| | | jsonObject.put("voiceType","female"); |
| | | jsonObject.put("pace",50); |
| | | JSONArray a = new JSONArray(); |
| | | a.add(1); |
| | | jsonObject.put("audioOutID",a); |
| | | String params =jsonObject.toJSONString(); |
| | | log.error("========海康广播播放入参内容 : " + params); |
| | | String result = HttpsUtil.doPutHk(model.getIp(),Integer.parseInt(StringUtils.defaultString(model.getPort(),"80")),model.getDoorId(), model.getDoorName() |
| | | ,"/ISAPI/AccessControl/EventCardLinkageCfg/TTSAudio?format=json",params); |
| | | log.error("========海康广播播放返回内容 : " + result); |
| | | |
| | | } |
| | | executeJson.put("weeklyScheduleList",executeJsonArray); |
| | | } |
| | | |
| | | if (Constant.ONE.equals(waterTimingBroadcastDO.getPeriodType())){ |
| | | //日计划 |
| | | downArrayData.put("dailyScheduleInfo",executeJson); |
| | | }else { |
| | | //周计划 |
| | | downArrayData.put("weklyScheduleInfo",executeJson); |
| | | } |
| | | allJsonArray.add(downArrayData); |
| | | JSONObject resultJson = JSONObject.parseObject(result); |
| | | Boolean success = false; |
| | | if(Constants.equalsInteger(resultJson.getInteger("statusCode"),Constants.ONE)){ |
| | | success = true; |
| | | } |
| | | jsonObject.put("broadcastPlanSchemeList",allJsonArray); |
| | | log.info("海康播放计划下发入参内容 : " + jsonObject); |
| | | InterfaceLog log = new InterfaceLog(); |
| | | log.setCreateDate(new Date()); |
| | | log.setUrl("/ISAPI/AccessControl/EventCardLinkageCfg/TTSAudio?format=json"); |
| | | log.setEditDate(log.getCreateDate()); |
| | | log.setPlat(Constants.ZERO); |
| | | log.setName("海康广播播放"); |
| | | log.setIsdeleted(Constants.ZERO); |
| | | log.setRequest(params); |
| | | log.setType(Constants.ZERO); |
| | | log.setSuccess(success?Constants.ZERO:Constants.ONE); |
| | | log.setRepose(result); |
| | | interfaceLogMapper.insert(log); |
| | | |
| | | String result = HttpsUtil.doPostHk(param.getIp(),Integer.parseInt(StringUtils.defaultString(param.getPort(),"80")) |
| | | ,"/ISAPI/VideoIntercom/broadcast/AddPlanScheme?format=json",param.getDoorId(), param.getDoorName(),jsonObject.toJSONString()); |
| | | return result;*/ |
| | | return null; |
| | | |
| | | return result; |
| | | } |
| | | @Override |
| | | public void setLedContent(TransparentChannelSingleRequest model) { |
| | |
| | | }catch (Exception e){ |
| | | log.error( "对不起,屏幕内容设置异常"+ device.getName()); |
| | | } |
| | | |
| | | } |
| | | log.error( "屏幕内容设置=======================结束========" ); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 服务开启订阅任务 |
| | | */ |
| | | @Override |
| | | public void startCheckDuanluqiSubjob() { |
| | | List<Device> devices = deviceMapper.selectList(new QueryWrapper<Device>().lambda() |
| | | .eq(Device::getType,Constants.DEVICE_TYPE.duanluqi)//断路器 |
| | | .eq(Device::getIsUsed,Constants.ZERO) |
| | | .eq(Device::getIsdeleted,Constants.ZERO) |
| | | ); |
| | | if(devices!=null && devices.size()>0){ |
| | | for (Device model : devices){ |
| | | try { |
| | | MqttConfig config = getMqttConfigByParam(model,"device_"); |
| | | MqttClient mqttClient = MqttClientCache.clientMapCache.get("device"+model.getId()); |
| | | String[] topics =new String[]{Constants.MqttTopic.mts_status, |
| | | Constants.MqttTopic.mts_attr, |
| | | Constants.MqttTopic.mts_resp}; |
| | | //如果开始使用,开始订阅 |
| | | mqttClient = mqttBizService.subscribe(mqttClient,config ,topics); |
| | | if(mqttClient ==null){ |
| | | throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"建立连接失败!"); |
| | | } |
| | | MqttClientCache.clientMapCache.put("device"+model.getId(),mqttClient); |
| | | log.error("=======开始自动定于mqtt任务完成:"+model.getName() ); |
| | | }catch (Exception e){ |
| | | log.error("=======开始自动定于mqtt任务失败:"+model.getName()+e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | /** |
| | | * 断路器判断是否需要远程分闸定人任务 |
| | | */ |
| | | @Override |
| | | public void autoCloseCmdTimer(){ |
| | | if(Constants.DEALING_DUANLUQI_CLOSE){ |
| | | return; |
| | | } |
| | | log.error("开启定时远程控制断路器开始========="); |
| | | Constants.DEALING_DUANLUQI_CLOSE = true; |
| | | try { |
| | | List<Device> devices = deviceMapper.selectList(new QueryWrapper<Device>().lambda() |
| | | .eq(Device::getType,Constants.DEVICE_TYPE.duanluqi)//断路器 |
| | | .isNotNull(Device::getLevel)//配置了空闲时长限制 |
| | | .isNotNull(Device::getDoorName)//配置了MQTT参数 |
| | | .isNotNull(Device::getChannelInfo)//配置了控制开关序号 |
| | | .eq(Device::getIsUsed,Constants.ZERO) |
| | | .eq(Device::getIsdeleted,Constants.ZERO) |
| | | ); |
| | | if(devices!=null && devices.size()>0){ |
| | | for(Device device : devices){ |
| | | try { |
| | | long time = (long)getNumberByStr(device.getLevel());//控制时长 |
| | | if(time < 300 ){ |
| | | log.error("开启定时远程控制断路器分闸失败=========空闲时长未正确配置"+time); |
| | | continue; |
| | | } |
| | | double closeDianliu = getCloseDianliuByParam(device); |
| | | if(closeDianliu < 0 ){ |
| | | log.error("开启定时远程控制断路器分闸失败=========空闲电流阈值设置错误"+closeDianliu); |
| | | continue; |
| | | } |
| | | List<String> closeBtn = new ArrayList<>(); |
| | | String[] btns = device.getChannelInfo().split(","); |
| | | Date startDate = new Date((System.currentTimeMillis()-time*1000));//查询开始时间 |
| | | List<DeviceData> dataList = deviceDataMapper.selectList(new QueryWrapper<DeviceData>().lambda() |
| | | .eq(DeviceData::getDeviceId,device.getId()) |
| | | .in(DeviceData::getVal1,btns) |
| | | .ge(DeviceData::getHappenTime,DateUtil.getPlusTime2(startDate)) |
| | | .orderByDesc(DeviceData::getHappenTime)); |
| | | if(dataList != null && dataList.size()>0 ){ |
| | | for(String b : btns){ |
| | | DeviceData last = getLastDataByVal1(b,dataList,closeDianliu); |
| | | if(last !=null && StringUtils.equals(last.getVal7(),"1")){ |
| | | closeBtn.add(b);//需要分闸 |
| | | } |
| | | } |
| | | if(StringUtils.equals( dataList.get(0).getVal7(),"0")){ |
| | | log.error("开启定时远程控制断路器分闸结束======分闸状态无需操作==="+device.getName()); |
| | | continue; |
| | | } |
| | | } |
| | | if(closeBtn.size()>0){ |
| | | Device param = new Device(); |
| | | param.setLoginUserInfo(new LoginUserInfo()); |
| | | param.getLoginUserInfo().setRealname("系统自动控制"); |
| | | param.setChannelNo(String.join(",",closeBtn)); |
| | | param.setId(device.getId()); |
| | | param.setStatus(Constants.ZERO); |
| | | dealDuanluqiCmd(device,param,"deviceTimer_"); |
| | | } |
| | | }catch (Exception e){ |
| | | log.error("开启定时远程控制断路器分闸失败========="+device.getName()+e.getMessage()); |
| | | } |
| | | } |
| | | } |
| | | log.error("开启定时远程控制断路器分闸数量========="+devices.size()); |
| | | }catch (Exception e){ |
| | | log.error("开启定时远程控制断路器分闸异常========="+e.getMessage()); |
| | | }finally { |
| | | Constants.DEALING_DUANLUQI_CLOSE = false; |
| | | } |
| | | log.error("开启定时远程控制断路器分闸结束========="); |
| | | } |
| | | |
| | | private double getCloseDianliuByParam(Device device) { |
| | | try { |
| | | JSONObject json = JSONObject.parseObject(device.getDoorName() ); |
| | | return json.getDouble("max"); |
| | | }catch (Exception e){ |
| | | |
| | | } |
| | | return 0; |
| | | } |
| | | |
| | | private DeviceData getLastDataByVal1(String b, List<DeviceData> dataList,double limit) { |
| | | List<DeviceData> list = new ArrayList<>(); |
| | | for(DeviceData d :dataList){ |
| | | if(StringUtils.equals(d.getVal1(),b)){ |
| | | if(limit <= getNumberByStr(d.getVal2())){ |
| | | //如果有实时电流值大于空闲阈值,则表示工作中,不做处理 |
| | | return null; |
| | | } |
| | | list.add(d); |
| | | } |
| | | } |
| | | return list.size()>0?list.get(0):null; |
| | | } |
| | | |
| | | private double getNumberByStr(String level) { |
| | | try { |
| | | return Double.parseDouble(level); |
| | | }catch (Exception e){ |
| | | |
| | | } |
| | | return 0; |
| | | } |
| | | } |