1. 下次浇水时间

This commit is contained in:
邱贞招 2024-04-09 19:50:53 +08:00
parent d6d6980820
commit de808f9c16
3 changed files with 65 additions and 3 deletions

View File

@ -103,13 +103,13 @@ spring:
enabled: false # 是否使能mqtt功能
redis:
# 地址
host: 117.50.189.165
host: localhost
# 端口默认为6379
port: 6379
# 数据库索引
database: 1
# 密码
password: YgUIGNudqOQaRxHZ
password:
# 连接超时时间
timeout: 10s
lettuce:

View File

@ -60,5 +60,7 @@ public class DatapointValue {
private int min;
private int sec;
private Boolean sw;//开关
private int week;//周转成二进制 bit0位开始为周一 0110 (week&1<<0) 1<<1 1<<2 00000110
private String date;//日期
}
}

View File

@ -10,6 +10,7 @@ import com.ruoyi.common.constant.IotConstants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.onenet.*;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
@ -34,6 +35,11 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.validation.Validator;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/**
@ -451,8 +457,11 @@ public class AsUserServiceImpl implements IAsUserService
DatapointValue.Tr tr = value.getTr();
asDevice1.setSoilMoistureClose(tr.getEnd_sd());
asDevice1.setSoilMoistureOpen(tr.getStart_sd());
asDevice1.setSoilMoisture(tr.getKaiguan().toString());
asDevice1.setPulseModeParam(JSON.toJSONString(value.getMc()));
asDevice1.setNextDs(JSON.toJSONString(value.getNext_ds()));
DatapointValue.nextDs next_ds = value.getNext_ds();
/**设置下次浇水时间日期*/
setNextDs(asDevice1, next_ds);
asDevice1.setCurrentSoilMoisture(value.getTuran_show());
List<AsWateringRecord> asWateringRecords = asWateringRecordService.selectAsWateringRecordList(AsWateringRecord.builder().deviceId(asDevice1.getDeviceId()).build());
// 根据asWateringRecords查询最后一次浇水时间
@ -465,4 +474,55 @@ public class AsUserServiceImpl implements IAsUserService
}
return asDevices;
}
private void setNextDs(AsDevice asDevice1, DatapointValue.nextDs next_ds) {
//将week将10进制转成2进制
String week = Integer.toBinaryString(next_ds.getWeek());
if(StringUtils.isNotEmpty(week)){
int requiredZeroes = 7 - week.length();
String zeroes = String.join("", Collections.nCopies(requiredZeroes, "0"));
week = zeroes + week;//0001110
//将week字符串反转
week = new StringBuilder(week).reverse().toString();//0111000 234
String date = DateUtils.getDate();//获取当前日期格式为yyyy-MM-dd
//获取今天是星期几
Calendar calendar = Calendar.getInstance();
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
// Calendar中周日对应的值为1需要将其转换为周一对应的值为1
int weekDay = (dayOfWeek == 1) ? 7 : dayOfWeek - 1;
System.out.println("今天是周" + weekDay);
//week从weekDay开始截取
week = week.substring(weekDay-1);//111000
//将week切割成字符
String[] weekArr = week.split("");
for (int i = 0; i < weekArr.length; i++) {
if(i==0 && "1".equals(weekArr[i])){
//今天的浇水时间是否已经过了
String wateringDate = date+" "+ next_ds.getHour()+":"+ next_ds.getMin()+":00";
//将wateringDate转成Date类型并与当前时间进行比较
long time = DateUtils.parseDate(wateringDate).getTime();
long time1 = new Date().getTime();
if(time >time1){
System.out.println("浇水时间大于当前时间,浇水时间未过");
next_ds.setDate(date);
asDevice1.setNextDs(JSON.toJSONString(next_ds));
}else{
System.out.println("浇水时间小于当前时间,浇水时间已过");
}
}
if(i>0 && "1".equals(weekArr[i])){
//当前日期加上i1天
LocalDate currentDate = LocalDate.now(); // 获取当前日期
LocalDate futureDate = currentDate.plusDays(i); // 当前日期加上i天
// 格式化日期以便输出可选根据实际需求选择合适的格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedFutureDate = futureDate.format(formatter);
System.out.println("当前日期加 " + i + " 天后是: " + formattedFutureDate);
next_ds.setDate(formattedFutureDate);
asDevice1.setNextDs(JSON.toJSONString(next_ds));
break;
}
}
}
}
}