workman异步监听消息发送腾讯云短信

1 篇文章 0 订阅
    /**
     * 获取告警信息,存入库中,并返回入库结果
     * @param $topic  *主题
     * @param $content * 返回消息
     * @return bool|int|string
     * @throws \think\db\exception\DbException
     */
    public function warningSignal($topic,$content)
    {
        // 从主题中获取设备id
        $equipment = $this->cut_str($topic,'/',2);

        // 判断主题 - 断电主题
        $powerOff = $this->cut_str($topic,'/',-1);

        $content = json_decode($content,true);

        //设备电源情况
        $offType = $content['data']['type'];
        if ($powerOff === 'powerOff') $this->powerOffOrder($equipment,$offType);

        //当主题消息和返回消息 正常情况下,才进行逻辑操作,
        if(!empty($content['data']['name']) && !empty($equipment)){
            // 调用发短信
            $handle = 1; // handel 处理参数 1未处理
            $sendCode = (new TencentNote())->acceptEquipment($equipment);
            if($sendCode == 1) $handle = 2; // 短信  发送成功后  状态变2 处理中

            // 设备报警属性名
            $monitorName =  $powerOff === 'powerOff' ? "powerOff" : $content['data']['name'];

            $equipmentMonitorId =  Db::table('bj_monitor')->where('equipment',$equipment)->where('property',$monitorName)->value('id');

            // 存在 及修改之前状态
            if(!empty($equipmentMonitorId)) return Db::name('monitor')->where('id',$equipmentMonitorId)->update([
                'type'=>$content['data']['type'],
                'status'=> $content['data']['type'] == 1 ? 3: $handle, // $handel 告警设备状态 处理状态参数
                'create_time'=>time(),
            ]);


            // 不存在ID 即新增
            if(empty($equipmentMonitorId)) {
                $info = [
                    'property'=>$monitorName,
                    'type'=>$content['data']['type'],
                    'equipment'  => $equipment,
                    'create_time'=> time(),
                ];

                // 新增设备状态。
                return Db::name('monitor')->insert($info);
            }

        }


        return true;
    }



    /**
     * 断电处理订单
     * @param $equipment //设备编号
     * @return bool
     * @throws \think\db\exception\DbException
     */
    public function powerOffOrder($equipment,$offType)
    {
        //查询房间id
        $rooms_id = Db::table('bj_rooms')
            ->alias('r')
            ->join('bj_equipment e','e.id = r.equipment_id')
            ->where('e.equipment_number',$equipment)
            ->value('r.id');

        //查询订单
        $order_id =  Db::table('bj_order')->where('rooms_id',$rooms_id)->where('pay_state',0)->value('id');

        // 查询该设备红外状态
        $unqiueId =$this->equRandVar();
        // 检测红外
        $properties = ['humanSensor2'];
        $emptyPerson=(new MqttService($equipment, $unqiueId))->pub(2,'','',$properties);
        $unqiueId =$this->equRandVar();
        $status = (new MqttService($equipment, $unqiueId))->sub(2,2);
        $status = json_decode($status['data'], true);

        if($status=== '1')  return $this->returnData(714,'链接设备失败');

        if($status != 1) {
             // 检测订单无人 直接结束订单
            if ($status['properties']['humanSensor2'] == 0){
                Db::table('bj_order')->where('id',$order_id)->update(['pay_state'=>2]);
//                $a = Db::getLastsql();var_dump($a);
            }

        }


       return true;
    }
<?php
namespace app\service\alone\Note;
require_once __DIR__ . '/../../../../vendor/autoload.php';
// 导入对应产品模块的client
use TencentCloud\Sms\V20210111\SmsClient;
// 导入要请求接口对应的Request类
use TencentCloud\Sms\V20210111\Models\SendSmsRequest;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Credential;
// 导入可选配置类
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use think\facade\Db;


class TencentNote
{

    //接收设备编号
    public function acceptEquipment($equipment_number)
    {
        //查询设备维护人员
        $info = Db::table('bj_rooms')
            ->alias('r')
            ->join('bj_equipment e','e.id = r.equipment_id')
            ->join('bj_service_area s','s.id = r.service_area_id')
            ->where('e.equipment_number',$equipment_number)
            ->field('r.operation_phone,s.name,r.name rname')
            ->select()->toArray();

        $messageCode = $this->sendMessage($info);

        // 短信发送成功 修改告警列表中处理状态
        // 1 表示成功  2表示发送失败
        if($messageCode['SendStatusSet'][0]['Code'] == 'Ok')  return  1 ;
        if($messageCode['SendStatusSet'][0]['Code'] !== 'Ok')  return 2 ;

    }




    /**
     * 发送告警短信
     * @param $operation_phone  // 发送手机号
     * @param $equipment_number // 短信模板中内容 设备编号
     */
    public function sendMessage($info)
    {

        try {
            /* 必要步骤:
             * 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。
             * 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。
             * 你也可以直接在代码中写死密钥对,但是小心不要将代码复制、上传或者分享给他人,
             * 以免泄露密钥对危及你的财产安全。
             * CAM密匙查询: https://console.cloud.tencent.com/cam/capi*/
            $cred = new Credential("xxx", "XXX");
            //$cred = new Credential(getenv("TENCENTCLOUD_SECRET_ID"), getenv("TENCENTCLOUD_SECRET_KEY"));
            // 实例化一个http选项,可选的,没有特殊需求可以跳过
            $httpProfile = new HttpProfile();
            // 配置代理
            // $httpProfile->setProxy("https://ip:port");
            $httpProfile->setReqMethod("GET");  // post请求(默认为post请求)
            $httpProfile->setReqTimeout(30);    // 请求超时时间,单位为秒(默认60秒)
            $httpProfile->setEndpoint("sms.tencentcloudapi.com");  // 指定接入地域域名(默认就近接入)
            // 实例化一个client选项,可选的,没有特殊需求可以跳过
            $clientProfile = new ClientProfile();
            $clientProfile->setSignMethod("TC3-HMAC-SHA256");  // 指定签名算法(默认为HmacSHA256)
            $clientProfile->setHttpProfile($httpProfile);
            // 实例化要请求产品(以sms为例)的client对象,clientProfile是可选的
            // 第二个参数是地域信息,可以直接填写字符串 ap-guangzhou,或者引用预设的常量
            $client = new SmsClient($cred, "ap-guangzhou", $clientProfile);
            // 实例化一个 sms 发送短信请求对象,每个接口都会对应一个request对象。
            $req = new SendSmsRequest();
            /* 填充请求参数,这里request对象的成员变量即对应接口的入参
            * 你可以通过官网接口文档或跳转到request对象的定义处查看请求参数的定义
            * 基本类型的设置:
            * 帮助链接:
            * 短信控制台: https://console.cloud.tencent.com/smsv2
            * sms helper: https://cloud.tencent.com/document/product/382/3773 */
            /* 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId,示例如1400006666 */
            $req->SmsSdkAppId = "xxx";
            /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,签名信息可登录 [短信控制台] 查看 */
            $req->SignName = "xxx";
            /* 短信码号扩展号: 默认未开通,如需开通请联系 [sms helper] */
            $req->ExtendCode = "";
            /* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
             * 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/
            $req->PhoneNumberSet = array("+86xxx"); // 手机号
            /* 国际/港澳台短信 SenderId: 国内短信填空,默认未开通,如需开通请联系 [sms helper] */
            $req->SenderId = "";
            /* 用户的 session 内容: 可以携带用户侧 ID 等上下文信息,server 会原样返回 */
            $req->SessionContext = "xxx";
            /* 模板 ID: 必须填写已审核通过的模板 ID。模板ID可登录 [短信控制台] 查看 */
            $req->TemplateId = "xxx"; //模板id
            /* 模板参数: 若无模板参数,则设置为空*/
            $req->TemplateParamSet = array("{$info[0]['name']}","{$info[0]['rname']}",'1'); //模板参数
            // 通过client对象调用SendSms方法发起请求。注意请求方法名与请求对象是对应的
            // 返回的resp是一个SendSmsResponse类的实例,与请求对象对应
            $resp = $client->SendSms($req);
            // 输出json格式的字符串回包
           $result = $resp->toJsonString();
           $result = json_decode($result,'true');
           return $result;

            // 也可以取出单个值。
            // 您可以通过官网接口文档或跳转到response对象的定义处查看返回字段的定义
//            print_r($resp->TotalCount);
        }
        catch(TencentCloudSDKException $e) {
            echo $e;
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值