|
|
@@ -1,16 +1,21 @@
|
|
|
package com.inspur.device.service.impl;
|
|
|
|
|
|
+import cn.hutool.json.JSONArray;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.inspur.device.domain.SmsbDevicePowerSchedule;
|
|
|
import com.inspur.device.domain.bo.SmsbDevicePowerScheduleBo;
|
|
|
+import com.inspur.device.domain.constants.DeviceConstants;
|
|
|
import com.inspur.device.domain.vo.SmsbDevicePowerScheduleVo;
|
|
|
+import com.inspur.device.domain.vo.SmsbDeviceVo;
|
|
|
import com.inspur.device.mapper.SmsbDevicePowerScheduleMapper;
|
|
|
import com.inspur.device.service.ISmsbDevicePowerScheduleService;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import org.dromara.common.core.domain.R;
|
|
|
import org.dromara.common.core.utils.MapstructUtils;
|
|
|
+import org.dromara.common.core.utils.StringUtils;
|
|
|
import org.dromara.common.mybatis.core.page.PageQuery;
|
|
|
import org.dromara.common.mybatis.core.page.TableDataInfo;
|
|
|
import org.dromara.common.satoken.utils.LoginHelper;
|
|
|
@@ -66,6 +71,127 @@ public class SmsbDevicePowerScheduleServiceImpl implements ISmsbDevicePowerSched
|
|
|
return baseMapper.selectVoList(lqw);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public JSONObject createDeviceScheduleCron(SmsbDeviceVo smsbDeviceVo) {
|
|
|
+ // 查询当前设备 所有生效的配置
|
|
|
+ List<SmsbDevicePowerScheduleVo> powerScheduleVoList = baseMapper.selectInUseVoListByDeviceId(smsbDeviceVo.getId());
|
|
|
+ if (CollectionUtils.isEmpty(powerScheduleVoList)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ JSONObject result = new JSONObject();
|
|
|
+ // 1 sn
|
|
|
+ result.set("sn",smsbDeviceVo.getSerialNumber());
|
|
|
+ // 2 taskInfo
|
|
|
+ JSONObject taskInfo = new JSONObject();
|
|
|
+ // 2-1 type
|
|
|
+ taskInfo.set("type","FUNCTIONPOWER");
|
|
|
+ // 2-2 source 任务的发布来源
|
|
|
+ JSONObject source = new JSONObject();
|
|
|
+ // 2-2-1 type
|
|
|
+ source.set("type",1);
|
|
|
+ // 2-2-2 platform
|
|
|
+ source.set("platform",1);
|
|
|
+ taskInfo.set("source",source);
|
|
|
+ // 2-3 data
|
|
|
+ JSONArray data = new JSONArray();
|
|
|
+ JSONObject oneData = new JSONObject();
|
|
|
+ // 2-3-1 enable
|
|
|
+ oneData.set("enable",true);
|
|
|
+ // 2-3-2 portIndex
|
|
|
+ oneData.set("portIndex",1);
|
|
|
+ // 2-3-3 connectIndex
|
|
|
+ oneData.set("connectIndex",1);
|
|
|
+ // 2-3-4 commands
|
|
|
+ JSONArray commands = new JSONArray();
|
|
|
+ for (SmsbDevicePowerScheduleVo powerScheduleVo : powerScheduleVoList) {
|
|
|
+ // 一条记录生成两个command 开屏和息屏
|
|
|
+ JSONObject onCommands = createOneCommands(powerScheduleVo, DeviceConstants.DEVICE_POWER_ON);
|
|
|
+ JSONObject offCommands = createOneCommands(powerScheduleVo, DeviceConstants.DEVICE_POWER_OFF);
|
|
|
+ if (null != onCommands) {
|
|
|
+ commands.add(onCommands);
|
|
|
+ }
|
|
|
+ if (null != offCommands) {
|
|
|
+ commands.add(offCommands);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ oneData.set("commands",commands);
|
|
|
+ data.add(oneData);
|
|
|
+ taskInfo.set("data",data);
|
|
|
+ result.set("taskInfo",taskInfo);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private JSONObject createOneCommands(SmsbDevicePowerScheduleVo powerScheduleVo, String powerType) {
|
|
|
+ if (powerType.equals(DeviceConstants.DEVICE_POWER_ON) && StringUtils.isEmpty(powerScheduleVo.getPowerOnTime())) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (powerType.equals(DeviceConstants.DEVICE_POWER_OFF) && StringUtils.isEmpty(powerScheduleVo.getPowerOffTime())) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ JSONObject oneCommands = new JSONObject();
|
|
|
+ JSONArray conditions = new JSONArray();
|
|
|
+ for (int i = 0 ; i < 8 ; i++) {
|
|
|
+ JSONObject oneConditions = new JSONObject();
|
|
|
+ String cronStr = "";
|
|
|
+ Integer action = 0;
|
|
|
+ if (powerType.equals(DeviceConstants.DEVICE_POWER_ON)) {
|
|
|
+ action = 0;
|
|
|
+ cronStr = convertTimeToDailyCron(powerScheduleVo.getPowerOnTime());
|
|
|
+ }else {
|
|
|
+ action = 1;
|
|
|
+ cronStr = convertTimeToDailyCron(powerScheduleVo.getPowerOffTime());
|
|
|
+ }
|
|
|
+ JSONArray cron = new JSONArray();
|
|
|
+ cron.add(cronStr);
|
|
|
+ oneConditions.set("cron",cron);
|
|
|
+ oneConditions.set("action",action);
|
|
|
+ oneConditions.set("type","power");
|
|
|
+ oneConditions.set("startTime",powerScheduleVo.getStartDate());
|
|
|
+ oneConditions.set("endTime",powerScheduleVo.getEndDate());
|
|
|
+ oneConditions.set("enable",true);
|
|
|
+ oneConditions.set("powerIndex",i);
|
|
|
+ oneConditions.set("flag","abc");
|
|
|
+ conditions.add(oneConditions);
|
|
|
+ }
|
|
|
+ oneCommands.set("conditions",conditions);
|
|
|
+ return oneCommands;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将时间字符串转换为每天执行的 Cron 表达式
|
|
|
+ * @param timeStr 时间字符串,格式:HH:mm:ss
|
|
|
+ * @return Cron 表达式,如:"10 40 20 * * ?"
|
|
|
+ * @throws IllegalArgumentException 如果时间格式不正确
|
|
|
+ */
|
|
|
+ private String convertTimeToDailyCron(String timeStr) {
|
|
|
+ if (timeStr == null || timeStr.isEmpty()) {
|
|
|
+ throw new IllegalArgumentException("时间字符串不能为空");
|
|
|
+ }
|
|
|
+ // 分割时、分、秒
|
|
|
+ String[] parts = timeStr.split(":");
|
|
|
+ if (parts.length != 3) {
|
|
|
+ throw new IllegalArgumentException("时间格式必须为 HH:mm:ss");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ int hour = Integer.parseInt(parts[0]);
|
|
|
+ int minute = Integer.parseInt(parts[1]);
|
|
|
+ int second = Integer.parseInt(parts[2]);
|
|
|
+ // 验证时分秒范围
|
|
|
+ validateTimePart(hour, 0, 23, "小时");
|
|
|
+ validateTimePart(minute, 0, 59, "分钟");
|
|
|
+ validateTimePart(second, 0, 59, "秒");
|
|
|
+ // 构建 Cron 表达式:秒 分 时 * * ?
|
|
|
+ return String.format("%d %d %d * * ?", second, minute, hour);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ throw new IllegalArgumentException("时分秒必须为数字", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ private void validateTimePart(int value, int min, int max, String partName) {
|
|
|
+ if (value < min || value > max) {
|
|
|
+ throw new IllegalArgumentException(partName + "范围必须是 " + min + "-" + max);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 查询符合条件的多功能卡电源定时开关列表
|
|
|
*
|