You need to enable JavaScript to run this app. VOLTTRON Csv Driver Agent如何实现每日固定时间触发
优惠活动
大模型
产品
解决方案
定价
更多

VOLTTRON Csv Driver Agent如何实现每日固定时间触发

VOLTTRON Agent 每日固定时间触发实现方案

完全可以实现每日凌晨4点固定执行的业务需求,无需依赖原有固定间隔轮询的写法,可按照以下逻辑改造现有Agent代码:

核心实现思路

原示例使用的self.core.periodic(interval, callback)接口本身支持自定义首次等待时长,可基于此实现定点触发,无需额外开发独立的定时组件:

  • 移除原代码中固定600秒间隔执行actuate_point的注册逻辑,改为计算当前系统时间距离下一个凌晨4点的时间差
  • 将计算得到的时间差作为首次触发的等待值,后续触发间隔固定设置为86400秒(24小时),保证每次触发点精准落在每日凌晨4点
  • 在触发的回调方法中依次加入冬季判断、天气条件校验、预冷执行的分支逻辑,不满足条件时直接返回不执行操作

参考改造代码

import datetime
from volttron.platform.agent import Core

# 配置冬季月份集合,可根据项目实际地理位置调整
WINTER_MONTH_CONFIG = {12, 1, 2}

@Core.receiver("onstart")
def onstart(self, sender, **kwargs):
    # 计算下一次凌晨4点的触发时间
    current_time = datetime.datetime.now()
    today_target_time = current_time.replace(hour=4, minute=0, second=0, microsecond=0)
    # 如果当日4点已过,目标触发时间顺延到次日
    if current_time > today_target_time:
        next_trigger_time = today_target_time + datetime.timedelta(days=1)
    else:
        next_trigger_time = today_target_time
    # 计算首次触发需要等待的秒数
    initial_wait_seconds = (next_trigger_time - current_time).total_seconds()
    # 注册定时任务:首次等待到目标时间后触发,后续每24小时重复执行
    self.core.periodic(
        interval=86400,
        callback=self.daily_precool_flow,
        wait=initial_wait_seconds
    )

def daily_precool_flow(self):
    """每日凌晨4点执行的预冷全流程"""
    # 冬季判断:属于冬季月份直接跳过
    current_month = datetime.datetime.now().month
    if current_month in WINTER_MONTH_CONFIG:
        return
    # 调用封装的OpenWeatherMap接口获取当日逐小时预报
    hourly_forecast = self._get_openweather_hourly_data()
    # 校验凌晨4点到下午时段的天气条件是否满足预冷要求
    if not self._is_forecast_suitable(hourly_forecast):
        return
    # 满足条件则触发原有设备点位执行逻辑
    self.actuate_point()

def actuate_point(self):
    """请求Actuator为CSV设备设置点位的原有逻辑保持不变"""
    pass

落地注意事项

  • 主机时间同步:需保证Agent运行节点的系统时间开启NTP同步,避免时间漂移导致长期运行后触发时间偏移
  • 接口超时控制:调用OpenWeatherMap API时需配置合理的请求超时阈值,防止网络阻塞卡住VOLTTRON核心事件循环
  • 任务隔离:如果原Agent中需要保留每600秒执行的常规巡检逻辑,可单独注册对应的定时任务,和每日预冷任务独立运行,互不干扰

内容的提问来源于stack exchange,提问作者bbartling

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.23 13:36:03