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)

//加载redisif($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;

//加载Redisif($this->redisclient->is_supported() === TRUE){$this->redisclient->delete($user_id.$meter_id);$this->redisclient->delete(‘down_‘.$user_id.$meter_id);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值