做一次动作,但只在x次之后重复


Do action once but only repeat it after x time

基本上,我需要通知用户如果:

a)传感器断开连接并且在last $send_threshold

中没有发送告警

b)如果当天发送的告警大于$repeat_threshold.

变量

$send_threshold = 12 * 60; // 12 min
$repeat_threshold = 2 * 60 * 60 + 45 * 60; // 2 hr 45 min

我完全不知道该怎么做。传感器是无状态的,所以我没有办法检查传感器是否在线,除了检查传感器发送到api的接收数据的时间戳。

/* FUNCTIONS */
function handleDisconnectAlerts($sensor,$dataset,$users,$settings)
{
    end($dataset);
    $last_timestamp = $dataset[key($dataset)]['timestamp'];
    $now = time();
    if($now > $last_timestamp && $now - $last_timestamp > $settings['disconnect_alarm'] * 60)
    {
        $send_threshold = $settings['disconnect_alarm'] * 60;
        $repeat_threshold = $settings['disconnect_alarm_repeat_hours'] * 60 * 60 + $settings['disconnect_alarm_repeat_minutes'] * 60;
        //not really sure what to do from here.
    }
}

好吧,我想我自己弄明白了。初始检查覆盖60 * $settings['disconnect_alarm']的初始周期。从那里我只需要检查警报没有在$repeat_threshold时间发送。简直妙不可言

function handleDisconnectAlerts($sensor,$dataset,$users,$settings)
{
    end($dataset);
    $last_timestamp = $dataset[key($dataset)]['timestamp'];
    $now = time();
    if($now > $last_timestamp && $now - $last_timestamp > $settings['disconnect_alarm'] * 60)
    {
        $repeat_threshold = $settings['disconnect_alarm_repeat_hours'] * 60 * 60 + $settings['disconnect_alarm_repeat_minutes'] * 60;
        $n_sent = R::count('sensoralerts',' timestamp >=:time and field_name="disconnect" and sensor_id=:id ',
            [':time'=>$repeat_threshold,':id'=>$sensor['id']]
        );
        if($n_sent == 0){
            multiSendDisconnect($users,$sensor);
        }
    }
}