Ejemplo n.º 1
0
 def exec(**kwargs):
     """
     kwargs  topic -- data
     data    topic  payload
     payload  api dn json data
     """
     logger.info('MQTT 接收解析器 测试参数获取{}'.format(kwargs))
     topic = kwargs.get('topic', bind_topic)
     msg = kwargs.get('data', dict())
     payload = msg.get('payload')
     payload_data = payload.get('data')
     if payload_data:
         opkg_url = payload_data.get('opkg_url')
         if opkg_url:
             opkg = requests.get(opkg_url)
             md5hash = hashlib.md5(opkg.content)
             md5 = md5hash.hexdigest()
             print(f'字节流{md5}')
     qos = payload.get('qos', dict())
     dn = payload.get('dn')
     redis_server.hmset(name='AntoUpdate:Subscribe:{}'.format(dn),
                        mapping=dict(subscribe='1',
                                     payload=json.dumps(kwargs)))
     logger.debug("Topic - [{}] - Msg - [{}] - Qos - [{}].".format(
         topic, msg, qos))
     redis_server.lpush(mqtt_topic_options.get('update_list.to_list'),
                        json.dumps(kwargs))
     logger.debug(
         "-------------------------- receive end ---------------------------------"
     )
Ejemplo n.º 2
0
 def mqtt_on_connect(self, client, user_data, flags, rc):
     """MQTT 链接事件"""
     topic_qos_list = [
         tuple(topic_qos.values()) for topic_qos in list(
             mqtt_topic_options.get('subscribe_topic').values())
     ]
     mqtt_server.subscribe(topic_qos_list)
     self.public_app.app.is_run = True
Ejemplo n.º 3
0
def update_dev(api, timestamp, cmd, ver, id, dn, data_file, data_path):
    """
    Upgrade the command line interface
        -- 更新交互
    """
    click.secho('API %s!' % api, bg='green', fg='red')
    click.secho('Timestamp %s!' % timestamp, bg='green', fg='red')
    with open(data_path, mode='r') as f:
        conext = f.read()
        name = mqtt_topic_options.get('update_list.from_list')
        redis_server.lpush(name, conext)
        click.secho(conext, fg='green')
Ejemplo n.º 4
0
    def __init__(self):
        self.parser = self.get_parser()
        self.app = RedisQueueListenParsePipeline()
        self.app.redis_server = redis_server
        self.app.redis_queue_key = mqtt_topic_options.get(
            'update_list.from_list')
        self.app.parse_callback = self.callback
        self.app.data_to_json = True
        self.app.parse_sleep = 0.3
        self.app.is_run = True

        while self.is_listen:
            self.app.listen()
            time.sleep(5)
Ejemplo n.º 5
0
 def exec(self, payload):
     logger.debug("推送测试测试数据 - {}".format(payload))
     data = payload.get('data')
     if data:
         opkg_url = data.get('opkg_url')
         if opkg_url:
             opkg = requests.get(opkg_url)
             md5hash = hashlib.md5(opkg.content)
             md5 = md5hash.hexdigest()
             print(f'字节流{md5}*********************************')
     dn = payload.get('dn')
     redis_server.hmset(name='AntoUpdate:Publish:{}'.format(dn),
                        mapping=dict(
                            publish_state='1',
                            payload=json.dumps(data),
                        ))
     return dict(topic=mqtt_topic_options.get('update_topic.public_topic'),
                 payload=payload)
Ejemplo n.º 6
0
# -*- coding: utf-8 -*-
# Author: WangChao
# Versions: 设备更新

import json
import logging
import os

import requests
import hashlib
from app.pipeline_listen import RedisQueueListenParsePipeline
from env.server import redis_server
from env.topic_config import mqtt_topic_options

logger = logging.getLogger('__name__')
bind_topic = mqtt_topic_options.get('alert_topic.public_topic')
mod_active = True


class Callback:
    def options(self):
        self.app = RedisQueueListenParsePipeline()
        self.app.redis_queue_key = 'Queue:list:second'
        self.app.is_parser_listen = True
        self.app.parse_callback = self.handle
        self.app.data_to_json = True
        self.app.parse_sleep = 0.3

    def handle(self, data):
        print(data, '*&*' * 20)
Ejemplo n.º 7
0
    def callback(self, data):
        topic = data.get('topic',
                         mqtt_topic_options.get('alert_topic.public_topic'))
        payload = data
        qos = data.get('qos', 1)
        mqtt_uuid = self.make_uuid()

        if topic is None:
            raise ExceptionError('topic not exist')
        else:
            logger.debug('topic: %s' % topic)

        if payload is None:
            raise ExceptionError('payload not exist')
        else:
            logger.debug('payload: %s' % payload)

        publish_list = list()
        # root_parser = self.parser.get('root').get('app.parser.public.' + topic)
        # sub_parser = self.parser.get('app.parser.public.' + topic)
        root_parser = self.parser.get('root').get('app.parser.public.' +
                                                  'update_public')
        sub_parser = self.parser.get('app.parser.public.' + 'update_public')
        if root_parser is None:
            publish_list.append(
                dict(topic=topic,
                     payload=payload,
                     qos=qos,
                     mqtt_uuid=mqtt_uuid))
        elif not sub_parser:
            logger.debug('-' * 50)
            logger.debug('exec parser: %s' % 'app.parser.public.' + topic)
            publish_list.append(
                self.get_publish(root_parser, payload, qos, mqtt_uuid))
        else:
            payload = payload if not hasattr(root_parser, 'exec') else getattr(
                root_parser, 'exec')(payload)
            for name, mod in self.parser.get('root').items():
                if name == 'app.parser.public.' + 'update_public':
                    # if name == 'app.parser.public.' + topic:  # todo 后期修改
                    logger.debug('-' * 50)
                    logger.debug('exec parser: %s' % name)
                    publish_list.append(
                        self.get_publish(mod, payload, qos, mqtt_uuid))
        for publish in publish_list:
            if publish is None:
                continue

            topic = publish.get('topic')
            payload = publish.get('payload')
            qos = publish.get('qos', qos)
            mqtt_uuid = publish.get('mqtt_uuid')

            if topic is None or payload is None:
                continue

            logger.debug('-' * 50)
            logger.debug('publish topic: %s' % topic)
            logger.debug('publish payload: %s' % payload)
            logger.debug('publish qos: %s' % qos)
            payload = payload if isinstance(payload,
                                            str) else json.dumps(payload)
            mqtt_server.publish(topic, payload, qos)
            logger.debug(
                "MQTT Public - Topic - [{}] - Mqtt_uid- [{}]- Payload - [{}] - Qos - [{}]."
                .format(topic, mqtt_uuid, payload, qos))
            logger.debug(
                "-------------------------- public end -------------------------------"
            )