| | |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.doumee.service.business.impl.hksync.HkSyncPushServiceImpl; |
| | | import com.doumee.tcp.WaterElectricityUtil; |
| | | import com.github.yulichang.wrapper.MPJLambdaWrapper; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | |
| | | import org.eclipse.paho.client.mqttv3.MqttMessage; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import java.util.*; |
| | |
| | | model.setIsdeleted(Constants.ZERO); |
| | | model.setEditDate(new Date()); |
| | | model.setCreateDate(model.getEditDate()); |
| | | if(model.getDoorNameObj()!=null && Constants.equalsInteger(model.getType(),Constants.DEVICE_TYPE.duanluqi)){ |
| | | if(model.getDoorNameObj()!=null && |
| | | (Constants.equalsInteger(model.getType(),Constants.DEVICE_TYPE.duanluqi) |
| | | ||Constants.equalsInteger(model.getType(),Constants.DEVICE_TYPE.dianbiao))){ |
| | | model.setDoorName(JSONObject.toJSONString(model.getDoorNameObj())); |
| | | if(StringUtils.isNotBlank(model.getLevel())){ |
| | | if(getNumberByStr(model.getLevel()) <300){ |
| | |
| | | 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)){ |
| | | if(device.getDoorNameObj()!=null && |
| | | (Constants.equalsInteger(device.getType(),Constants.DEVICE_TYPE.duanluqi) |
| | | ||Constants.equalsInteger(device.getType(),Constants.DEVICE_TYPE.dianbiao))){ |
| | | device.setDoorName(JSONObject.toJSONString(device.getDoorNameObj())); |
| | | } |
| | | Device model = deviceMapper.selectById(device.getId()); |
| | | if(model ==null){ |
| | | throw new BusinessException(ResponseStatus.DATA_EMPTY); |
| | | } |
| | | if(Constants.equalsInteger(model.getType(),Constants.DEVICE_TYPE.duanluqi)){ |
| | | //如果是斷路器 |
| | | if( Constants.equalsInteger(model.getIsUsed(),Constants.ZERO) |
| | | && StringUtils.equals(device.getDoorName(),model.getDoorName())){ |
| | | //如果配置参数发生了变化,设置状态未启用 |
| | | device.setIsUsed(Constants.ONE); |
| | | try { |
| | | MqttClient mqttClient = MqttClientCache.clientMapCache.get("device"+device.getId()); |
| | | if(mqttClient !=null){ |
| | | if(mqttClient.isConnected()){ |
| | | mqttClient.disconnect(); |
| | | } |
| | | mqttClient.close(); |
| | | MqttClientCache.clientMapCache.remove("device"+device.getId()); |
| | | } |
| | | }catch (Exception e){ |
| | | log.error("==============端口mqtt链接失败:"+model.getName()+e.getMessage()); |
| | | } |
| | | } |
| | | } |
| | | deviceMapper.updateById(device); |
| | | |
| | | } |
| | | |
| | | @Override |
| | |
| | | if (pageWrap.getModel().getName() != null) { |
| | | queryWrapper.lambda().like(Device::getName, pageWrap.getModel().getName()); |
| | | } |
| | | if (pageWrap.getModel().getDoorNo() != null) { |
| | | queryWrapper.lambda().like(Device::getDoorNo, pageWrap.getModel().getDoorNo()); |
| | | } |
| | | if (pageWrap.getModel().getNo() != null) { |
| | | queryWrapper.lambda().like(Device::getNo, pageWrap.getModel().getNo()); |
| | | } |
| | | if (pageWrap.getModel().getDoorName() != null) { |
| | | queryWrapper.lambda().like(Device::getDoorName, pageWrap.getModel().getDoorName()); |
| | | } |
| | |
| | | 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)){ |
| | | if(StringUtils.isNotBlank(d.getDoorName()) |
| | | && (Constants.equalsInteger(d.getType(),Constants.DEVICE_TYPE.duanluqi)|| |
| | | Constants.equalsInteger(d.getType(),Constants.DEVICE_TYPE.dianbiao))){ |
| | | try { |
| | | //断路器设备参数 |
| | | d.setDoorNameObj(JSONObject.parseObject(d.getDoorName())); |
| | |
| | | //如果系统不用了,端口mqtt连接 |
| | | try { |
| | | mqttClient = mqttBizService.unsubscribe(mqttClient,config ,topics); |
| | | if(mqttClient ==null){ |
| | | throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"建立连接失败!"); |
| | | } |
| | | if(mqttClient.isConnected()){ |
| | | mqttClient.disconnect(); |
| | | if(mqttClient !=null){ |
| | | if(mqttClient.isConnected()){ |
| | | mqttClient.disconnect(); |
| | | } |
| | | mqttClient.close(); |
| | | MqttClientCache.clientMapCache.remove("device"+param.getId()); |
| | | } |
| | | }catch (Exception e){ |
| | | log.error("==============端口mqtt链接失败:"+model.getName()+e.getMessage()); |
| | |
| | | } |
| | | dealDuanluqiCmd(model,param,"device_"); |
| | | |
| | | } |
| | | @Override |
| | | public void dianbaoCmd(Device param){ |
| | | Device model = deviceMapper.selectById(param.getId()); |
| | | if(model ==null && Constants.equalsInteger(param.getType(),Constants.DEVICE_TYPE.dianbiao)){ |
| | | throw new BusinessException(ResponseStatus.DATA_EMPTY); |
| | | } |
| | | if (param.getStatus() == null || param.getCmdDate() == null |
| | | ||param.getCmdDate().getTime() <= System.currentTimeMillis()) { |
| | | //如果是开闸 |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST); |
| | | } |
| | | |
| | | boolean r ; |
| | | String date = DateUtil.formatDate(new Date(),"yyyyMMddHHmmss"); |
| | | if(Constants.equalsInteger(param.getStatus(),Constants.ONE)) { |
| | | //如果是开闸 |
| | | r = WaterElectricityUtil.electricityAct(param.getIp(),Integer.parseInt(param.getPort()),param.getNo(),0,date); |
| | | }else { |
| | | //如果是合闸 |
| | | r = WaterElectricityUtil.electricityAct(param.getIp(),Integer.parseInt(param.getPort()),param.getNo(),1,date); |
| | | } |
| | | if(!r){ |
| | | throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"远程控制电表失败,请稍后重试!"); |
| | | } |
| | | String curremak = "【"+param.getLoginUserInfo().getRealname() |
| | | +"】于"+ DateUtil.getPlusTime2(new Date()) +"进行了"+(Constants.equalsInteger(param.getStatus(),Constants.ONE)?"【合闸】":"【分闸】")+"操作,开关【"+param.getChannelNo()+"】;"; |
| | | deviceMapper.update(null,new UpdateWrapper<Device>().lambda() |
| | | .set(Device::getRemark,curremak) |
| | | .set(Device::getEditDate,new Date()) |
| | | .set(Device::getEdirot,param.getLoginUserInfo().getId()) |
| | | .eq(Device::getId,param.getId())); |
| | | DeviceData data = new DeviceData(); |
| | | data.setCreateDate(new Date()); |
| | | data.setEditDate(new Date()); |
| | | data.setCreator(param.getLoginUserInfo().getId()); |
| | | data.setEditor(param.getLoginUserInfo().getId()); |
| | | data.setDeviceId(param.getId()+""); |
| | | data.setDataType(Constants.ONE);// |
| | | data.setVal1("远程控制"); |
| | | data.setVal2(curremak); |
| | | data.setHappenTime(DateUtil.getPlusTime2(data.getCreateDate())); |
| | | data.setVal3((Constants.equalsInteger(param.getStatus(),Constants.ONE)?"【合闸】":"【分闸】")); |
| | | data.setVal4(param.getLoginUserInfo().getRealname()); |
| | | data.setVal5(param.getChannelNo()); |
| | | deviceDataMapper.insert(data); |
| | | |
| | | } |
| | | @Override |
| | | @Transactional |
| | | public void dianbiaoData(Device param){ |
| | | Device model = deviceMapper.selectById(param.getId()); |
| | | if(model ==null && Constants.equalsInteger(param.getType(),Constants.DEVICE_TYPE.dianbiao)){ |
| | | throw new BusinessException(ResponseStatus.DATA_EMPTY); |
| | | } |
| | | try { |
| | | Map<String, Object> readData= WaterElectricityUtil.electricityData(model.getIp(),Integer.parseInt(model.getPort()),model.getNo()); |
| | | if(readData!=null){ |
| | | String curremak = "【"+param.getLoginUserInfo().getRealname() |
| | | +"】于"+ DateUtil.getPlusTime2(new Date()) +"进行了数据读取操作"; |
| | | Date time =(Date) readData.get("time"); |
| | | String total = (Double) readData.get("total")+""; |
| | | String status = (String) readData.get("status"); |
| | | model.setHkDate(new Date());//最近同步时间 |
| | | model.setOnline(Constants.ONE);//标识设备在线 |
| | | model.setRemark(curremak); |
| | | |
| | | DeviceData data = new DeviceData(); |
| | | data.setCreateDate(new Date()); |
| | | data.setEditDate(new Date()); |
| | | data.setCreator(param.getLoginUserInfo().getId()); |
| | | data.setEditor(param.getLoginUserInfo().getId()); |
| | | data.setDeviceId(param.getId()+""); |
| | | data.setDataType(Constants.ZERO);// |
| | | data.setVal1(total); |
| | | data.setVal2(status); |
| | | data.setHappenTime(DateUtil.getPlusTime2(data.getCreateDate())); |
| | | data.setVal3(DateUtil.getPlusTime2(time)); |
| | | data.setVal4(param.getLoginUserInfo().getRealname()); |
| | | data.setVal5(param.getNo());//地址域 |
| | | deviceDataMapper.insert(data); |
| | | deviceMapper.updateById(model); |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | log.error("电表数据读取失败,"+e.getMessage()); |
| | | throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"读取电表数据失败!"); |
| | | } |
| | | } |
| | | |
| | | private void dealDuanluqiCmd(Device model, Device param,String clientIndex) { |
| | |
| | | } |
| | | MqttClientCache.clientMapCache.put("device"+param.getId(),mqttClient); |
| | | String curremak = "【"+param.getLoginUserInfo().getRealname() |
| | | +"】于"+ DateUtil.getPlusTime2(new Date()) +"进行了"+(Constants.equalsInteger(param.getStatus(),Constants.ONE)?"【开闸】":"【关闸】")+"操作,开关【"+param.getChannelNo()+"】;"; |
| | | +"】于"+ 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) |
| | |
| | | data.setVal1("远程控制"); |
| | | data.setVal2(curremak); |
| | | data.setHappenTime(DateUtil.getPlusTime2(data.getCreateDate())); |
| | | data.setVal3((Constants.equalsInteger(param.getStatus(),Constants.ONE)?"【开闸】":"【关闸】")); |
| | | data.setVal3((Constants.equalsInteger(param.getStatus(),Constants.ONE)?"【合闸】":"【分闸】")); |
| | | data.setVal4(param.getLoginUserInfo().getRealname()); |
| | | data.setVal5(param.getChannelNo()); |
| | | deviceDataMapper.insert(data); |