【浇花条件(非雨天)】
1.夏季/秋季:
第一次: 07:30 必浇
第二次: 13:00 仅在温度>33℃时浇
第三次: 18:00 仅在温度>30℃时浇
2.冬季/春季:
第一次: 08:00 必浇
第二次: 13:00 仅在湿度<50%时浇
3.浇花时长:
温度>25℃时浇水25秒,每天浇花
温度>15℃时浇水20秒,星期二、四、六、日浇花
温度>0℃时浇水15秒,星期一、三、五浇花
// ====================== [配置常量] ======================
const plantGuides = global.get('plantGuides');
// 温度规则配置(阈值单位:℃;延迟单位:ms)
const WATER_RULES = [
{ threshold: 25, delay: 25000, wateringDays: [0,1,2,3,4,5,6] }, // 温度>25℃时浇水25秒,每天浇花
{ threshold: 15, delay: 20000, wateringDays: [0, 2, 4, 6] }, // 温度>15℃时浇水20秒,隔天浇花(星期0、2、4、6)
{ threshold: 0, delay: 15000, wateringDays: [1, 3, 5] } // 温度>0℃时浇水15秒,星期1、3、5浇花
];
// ====================== [核心逻辑] ======================
const {
weather: weather_en,
weather_data: {
attributes: {
temperature,
humidity,
condition_cn: weather_cn
}
}
} = msg || {};
// >>>>> 时间调度逻辑 <<<<<
const now = new Date();
const currentDate = now.toISOString().split('T')[0]; // 获取当前日期(YYYY-MM-DD)
const currentHour = now.getHours(); // 当前小时(0-23)
const currentMinute = now.getMinutes(); // 当前分钟(0-59)
const currentMonth = now.getMonth() + 1; // 当前月份(1-12)
const currentWeek = now.getDay(); // 当前星期(0-6)
const WEEK_CN_MAP = {
0: "星期日",
1: "星期一",
2: "星期二",
3: "星期三",
4: "星期四",
5: "星期五",
6: "星期六",
};
// >>>>> 使用统一温度规则判断浇水和延迟 <<<<<
const matchedTemp = WATER_RULES.find(rule => temperature > rule.threshold);
const isWateringDay = matchedTemp ? matchedTemp.wateringDays.includes(currentWeek) : false;
const waterDelay = matchedTemp ? matchedTemp.delay : 0;
// 判断是否处于预设浇水时段
let isWateringTime = false;
if ([4, 5, 6, 7, 8, 9, 10].includes(currentMonth)) {
// 夏季/秋季:
isWateringTime = (currentHour === 7 && currentMinute === 30) ||
(currentHour === 13 && currentMinute === 0 && temperature > 33) ||
(currentHour === 18 && currentMinute === 0 && temperature > 30);
} else {
// 春季/冬季:
isWateringTime = (currentHour === 8 && currentMinute === 0) ||
(currentHour === 16 && currentMinute === 30 && humidity < 50);
}
// >>>>> 环境条件判断 <<<<<
const isRainyDay = ['rainy', 'stormy', 'thunderstorm'].includes(weather_en) || weather_cn.includes('雨');
const isHighHumidity = humidity > 90;
const shouldWater = !isRainyDay && !isHighHumidity && isWateringDay;
// >>>>> 最终决策逻辑 <<<<<
const waterSwitch = (shouldWater && matchedTemp) ? 1 : 0;
// ====================== [输出控制] ======================
if (!isWateringTime) {
return [null, null];
}
const outputMsg = {
topic: msg.topic,
delay: waterDelay,
weather_cn: weather_cn ?? '未知',
weather_en: weather_en ?? "未知",
water_switch: waterSwitch,
week_cn: WEEK_CN_MAP[currentWeek],
temperature,
humidity,
...plantGuides
};
// 使用数组索引直接返回
return waterSwitch === 1 ? [outputMsg, null] : [null, outputMsg];
流下载: