| | |
| | | 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 |
| | |
| | | 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 |
| | |
| | | .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 |
| | |
| | | if(response == null || !StringUtils.equals(response.getCode(), HKConstants.RESPONSE_SUCCEE)){ |
| | | 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){ |
| | |
| | | } |
| | | 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; |
| | | } |
| | | } |