mqtt定时脚本

需求描述

给mqtt的topic发送信息

对应的topic接收请求后,执行发送短信指令

信息内容

SMS,10086,102

lua的mqtt里面,截取判断即可

–>可以实现

=====

定时任务,

每月开机一次
发短信?

或者使用开机通知?

定时消费方式,

定时执行

cron表达式,每隔2个月执行一次

cron定时表达式–每两月执行一次_corn 每月执行一次-CSDN博客
https://blog.csdn.net/weixin_44234800/article/details/129949900

0 0 2 1 /2 ?
0 0 1 1/2 * ?


# -*- coding: utf-8 -*-
import requests
import json
import time
import hmac
import hashlib
import base64
import urllib.parse
from datetime import datetime
# 

topic='led001'
# 用户私钥
uid='7801e4ba1bf7406593d47250797860fd'
# 定义中文日期时间格式
chinese_format = "%Y年%m月%d日 %H时%M分%S秒"
# 存放结果信息
msg_success =datetime.now().month+'月份消费情况\n生成时间:'+datetime.now().strftime(chinese_format)+'\n========\n'


msg_failed=''
phone_list=[
  13123456701,
  13123456702,
  13123456703,
  13123456704

]
# 根据手机号获取发送的短信内容
def getSmsContent(phone_num):
    # 获取当前日期和时间
    now = datetime.now()
    # 格式化日期和时间
    formatted_date = now.strftime("%Y-%m-%d %H:%M:%S")
    print("当前日期和时间:", formatted_date)
    sms_content= '{}\t{}\t执行消费'.format(formatted_date,phone_num)
    print("sms_content:",sms_content)
    return sms_content
# 请求mqtt
def post_mqtt(phone_num):
    content= getSmsContent(phone_num)
    msg= 'SMS,{},{}'.format(phone_num,content)
    print("msg:",msg)
    url ='https://apis.bemfa.com/va/postJsonMsg'
    # print("url:",url)
    json_text ={
    "uid": uid,
    "topic": topic,
    "type": 1,
    "msg": msg
    }
    headers = {'Content-Type': 'application/json;charset=utf-8'}
    response_str =  requests.post(url, json.dumps(json_text), headers=headers)
    print('response_str:',response_str)
    response_dict = json.loads(response_str) 
    if response_dict['code'] == 0 and response_dict['message'] == 'ok':
        print("请求成功")
        msg_success+ phone_num+ '\t发短信成功\t消费成功\n========\n'
    else:
        print("请求失败")
        msg_failed+phone_num+ '\t发短信失败\t消费失败\n========\n'



#  遍历list取出号码
def do_work():
  for index, item in enumerate(phone_list):
    print(f"Index: {index}, Item: {item}")
    post_mqtt(item)
    # 等待20s
    time.sleep(20)
    print("==============")


if __name__ == '__main__':
    print("111111")
    # 打印返回结果
    # print("result:\n",result)
    do_work()    
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用 Spring Integration 提供的 `IntegrationFlowContext` 和 `IntegrationFlowRegistration` 来创建定时发送 MQTT 消息的 Spring 集成流程,并将其注册到应用程序中。以下是一个示例: ```java @Configuration @EnableIntegration public class MqttConfiguration { @Value("${mqtt.broker.url}") private String brokerUrl; @Value("${mqtt.client.id}") private String clientId; @Value("${mqtt.topic}") private String topic; @Autowired private IntegrationFlowContext flowContext; @Bean public MqttPahoClientFactory mqttClientFactory() { DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory(); MqttConnectOptions options = new MqttConnectOptions(); options.setServerURIs(new String[]{brokerUrl}); factory.setConnectionOptions(options); return factory; } @Bean public IntegrationFlow mqttOutFlow() { return IntegrationFlows.from(() -> "Hello, MQTT!", e -> e.poller(Pollers.fixedRate(5000))) .handle(mqttOutbound()) .get(); } @Bean public MessageChannel mqttOutboundChannel() { return new DirectChannel(); } @Bean public MessageHandler mqttOutbound() { MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(clientId, mqttClientFactory()); messageHandler.setAsync(true); messageHandler.setDefaultTopic(topic); return messageHandler; } @Bean public IntegrationFlowRegistration mqttOutFlowRegistration() { IntegrationFlowRegistration registration = this.flowContext.registration(mqttOutFlow()) .id("mqttOutFlow") .register(); return registration; } } ``` 在上面的代码中,我们创建了一个 `IntegrationFlow` 对象,并使用 `Pollers.fixedRate()` 方法将其配置为每隔 5 秒发送一条 MQTT 消息到指定的主题。我们还创建了一个 `MqttPahoMessageHandler` 对象,并将其配置为使用指定的客户端 ID、MQTT 客户端工厂和主题。最后,我们使用 `IntegrationFlowContext` 和 `IntegrationFlowRegistration` 将 `mqttOutFlow` 集成流程注册到应用程序中。通过运行应用程序,您可以在指定的频率下定时发送 MQTT 消息到指定的主题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值