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

时间:2015-07-15 03:27:30

标签: php repeat

基本上,我需要在以下情况下通知用户:

a)传感器断开连接并且还没有在最后$ 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.

    }
}

1 个答案:

答案 0 :(得分:0)

嗯,我想我自己想通了。初始检查涵盖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);
        }
    }
}