5分钟连续出现某现象+微信模板消息提醒 PHP详解编程语言

本文章主要介绍了5分钟连续出现某现象+微信模板消息提醒 PHP,具有不错的的参考价值,希望对您有所帮助,如解说有误或未考虑完全的地方,请您留言指出,谢谢!

需求场景:用电插座电流连续出现5次电流过高(大于 3A)后停止用电服务,前四次发送电流过高提醒,最后一次发送结束用电服务提醒

思路:

Redis  key 设为:插座编号+user户编号  value: 出现大于3A的次数 ;

心跳每分钟,心跳包数据含有 。判断电流:电流值 <3 清空对应key的次数  电流>=3  次数不为零小于5  次数加一 发送过高提醒,次数为5时发送结束服务提醒,清空对应key的次数;

先申请模板消息,获取模板ID(一搜一大把,此处不赘述了)

上代码:(Ci框架)

心跳包中拿到电流值 $i

if($i > 3){ 
       checkMeterCurrentUp($meter_id); 
                 
}else{ 
       //清空redis电流超限记录 
       deleteAbnormalCurrentCache($meter_id);
}

function checkMeterCurrentUp($meter_id)
             //加载redis 
        $up_count  = $this->redisclient->get('down_'.$user_id.$meter_id);

        
if($up_count < 5){//设备号 value加一 发送模板消息 $this->redisclient->save($user_id.$meter_id,$up_count+1,600);//给用户消息通知
            //获取access_token
 lm('config_model'); $access_token = $this->config_model->r(pw('datakey',CONFIG_KEY_GZHH_ACCESS_TOKEN,'platform_id',$order['platform_id'])); $access_token = element('datavalue',$access_token); if($access_token) { $url = sprintf("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s", $access_token); lm('users_model'); $user = $this->users_model->r(pw('id',$user_id)); lm('meters_model'); $meter = $this->meters_model->r(pw('id_open',$meter_id));
                 //获取微信模板消息ID lm(
'platform_config_model'); $platform_config = $this->platform_config_model->r(pw('platform_id',$order['platform_id'])); $template_id = $platform_config['wx_event_template_id'];
$postdata = json_encode(array( 'touser' => element('openid',$user), 'template_id' => $template_id, 'url' => site_url('detail?id='.$meter_id), 'data' => array( "keyword1" => array ( "value" => $meter_id, "color" => "#173177" ), "keyword2" => array ( //地址 "value" => element('name',$meter), "color" => "#173177" ), "keyword3" => array ( "value" => date('Y-m-d H:i:s'), "color" => "#173177" ), "first" => array ( "value" => '您的设备充电功率超限,连续五次超限后将停止充电服务', "color" => "#173177" ), "remark" => array ( "value" => '请及时查看', "color" => "#173177" ), ) ));
                 //发送 发送模板消息请求
$resp = http_post($url,$postdata); } }else{ //结束订单 //发送模板消息 $this->redisclient->delete($user_id.$meter_id); //给用户消息通知 lm('config_model'); $access_token = $this->config_model->r(pw('datakey',CONFIG_KEY_GZHH_ACCESS_TOKEN,'platform_id',$order['platform_id'])); $access_token = element('datavalue',$access_token); if($access_token) { $url = sprintf("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s", $access_token); lm('users_model'); $user = $this->users_model->r(pw('id',$user_id)); lm('meters_model'); $meter = $this->meters_model->r(pw('id_open',$meter_id));
                  lm(
'platform_config_model'); $platform_config = $this->platform_config_model->r(pw('platform_id',$order['platform_id'])); $template_id = $platform_config['wx_event_template_id']; $postdata = json_encode(array( 'touser' => element('openid',$user), 'template_id' => $template_id, 'url' => site_url('detail?id='.$meter_id), 'data' => array( "keyword1" => array ( "value" => $meter_id, "color" => "#173177" ), "keyword2" => array ( //地址 "value" => element('name',$meter), "color" => "#173177" ), "keyword3" => array ( "value" => date('Y-m-d H:i:s'), "color" => "#173177" ), "first" => array ( "value" => '您的设备充电功率连续五次超限,已停止充电服务', "color" => "#173177" ), "remark" => array ( "value" => '请知悉', "color" => "#173177" ), ) )); $resp = http_post($url,$postdata); } //停止订单

            //关闭设备 }
function deleteAbnormalCurrentCache($meter_id){ 
        $user_id = 99; 
      //加载Redis
    if($this->redisclient->is_supported() === TRUE){ $this->redisclient->delete($user_id.$meter_id); $this->redisclient->delete('down_'.$user_id.$meter_id); } }

原创文章,作者:506227337,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/228232.html

(0)
上一篇 2022年1月11日 15:28
下一篇 2022年1月11日 15:28

相关推荐

发表回复

登录后才能评论