| package com.zy.asrs.task; | 
|   | 
| import com.alibaba.fastjson.JSON; | 
| import com.alibaba.fastjson.JSONObject; | 
| import com.baomidou.mybatisplus.mapper.EntityWrapper; | 
| import com.zy.common.utils.HttpHandler; | 
| import com.zy.core.cache.SlaveConnection; | 
| import com.zy.core.enums.SlaveType; | 
| import com.zy.core.model.ShuttleSlave; | 
| import com.zy.core.model.protocol.ShuttleProtocol; | 
| import com.zy.core.properties.SlaveProperties; | 
| import com.zy.core.thread.ShuttleThread; | 
| import com.zy.system.entity.Config; | 
| import com.zy.system.service.ConfigService; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.scheduling.annotation.Scheduled; | 
| import org.springframework.stereotype.Component; | 
|   | 
| import java.util.HashMap; | 
|   | 
| /** | 
|  * 小车电量预警检测 => 强制预警 | 
|  */ | 
| @Component | 
| @Slf4j | 
| public class ShuttlePowerEarlyWarning { | 
|   | 
|     @Autowired | 
|     private ConfigService configService; | 
|     @Autowired | 
|     private SlaveProperties slaveProperties; | 
|   | 
|     /** | 
|      * 小车电量预警检测 => 强制预警 | 
|      * 每30分钟扫描一次 | 
|      */ | 
|     @Scheduled(cron = "0 30 * * * ? ") | 
|     public synchronized void shuttlePowerEarlyWarning() { | 
|         Config config = configService.selectOne(new EntityWrapper<Config>().eq("code","dingdingReportUrl")); | 
|         if (config == null) { | 
|             return; | 
|         } | 
|   | 
|         if (config.getStatus() == 0) { | 
|             return;//通知禁用 | 
|         } | 
|   | 
|         //小车电量预警阈值 | 
|         int shuttlePowerEarlyValue = 20;//默认20 | 
|         Config shuttlePowerEarlyConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "shuttlePowerEarlyValue")); | 
|         if (shuttlePowerEarlyConfig != null) { | 
|             shuttlePowerEarlyValue = Integer.parseInt(shuttlePowerEarlyConfig.getValue()); | 
|         } | 
|   | 
|         StringBuffer buffer = new StringBuffer(); | 
|         buffer.append("【通知】四向库\n");//消息标题 | 
|   | 
|         boolean hasReport = false;//是否有需要报告的数据 | 
|         for (ShuttleSlave slave : slaveProperties.getShuttle()) { | 
|             ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, slave.getId()); | 
|             if (shuttleThread == null) { | 
|                 continue; | 
|             } | 
|   | 
|             ShuttleProtocol shuttleProtocol = shuttleThread.getStatus(); | 
|             if (shuttleProtocol == null) { | 
|                 continue; | 
|             } | 
|   | 
|             if (shuttleThread.isCharging()) { | 
|                 continue;//充电中,无需通知 | 
|             } | 
|   | 
|             if (Integer.parseInt(shuttleProtocol.getBatteryPower()) < shuttlePowerEarlyValue) { | 
|                 buffer.append(shuttleProtocol.getShuttleNo()).append("号小车,电量").append(shuttleProtocol.getBatteryPower()).append(",请注意。\n"); | 
|                 hasReport = true; | 
|             } | 
|         } | 
|   | 
|         if (hasReport) { | 
|             try { | 
|                 HashMap<String, Object> param = new HashMap<>(); | 
|                 HashMap<String, Object> data = new HashMap<>(); | 
|                 data.put("content", buffer.toString()); | 
|                 param.put("msgtype", "text"); | 
|                 param.put("text", data); | 
|                 String response = new HttpHandler.Builder() | 
|                         .setUri(config.getValue()) | 
|                         .setJson(JSON.toJSONString(param)) | 
|                         .setHttps(true) | 
|                         .build() | 
|                         .doPost(); | 
|                 JSONObject jsonObject = JSON.parseObject(response); | 
|                 if (jsonObject.get("errmsg").equals("ok")) { | 
|                     return;//发送成功 | 
|                 } | 
|             } catch (Exception e) { | 
|                 e.printStackTrace(); | 
|             } | 
|         } | 
|   | 
|     } | 
|   | 
| } |