The received asynchronous message mqtt Python

    Since the C-terminal system to upload pictures sometimes does not receive the message, you need to do a synchronization. When the C-terminal loaded picture gallery but without requesting remote loading local images, the equivalent of doing a cache, greatly reducing the time to load the C-terminus of the picture, improving the user experience.

A functional role

   mqtt is a plug-in rabbitmq server, you can use it to publish and subscribe topic.

   This synchronization feature, in fact, is to use one of the rabbitmq application scenarios asynchronous processing.

Second, the process steps

  1, a unique ID is provided mqtt, because the client id can not be repeated, so that the current time is a unique ID is selected from

The time.strftime = client_id ( ' % D the Y% m%%% M% S H ' , time.localtime (the time.time ())) 
Client = mqtt.Client (client_id)   # the ClientId can not be reused, so the current time

  2, set rabbitmq server user name and password

client.username_pw_set("dev", "YTc4Mj")

  3, subscribe to topics

client.subscribe("sync")

  4, receives the message

recvmsg = msg.payload.decode("utf-8")

  5, the message processing

Three, demo source code

1, mqtt server connected rabbitmq

import paho.mqtt.client as mqtt
import time
import socketclient
import logger
import demjson
import common
import syncfile

log = logger.Logger("info")

HOST = "127.0.0.1"
PORT = 1883

def client_loop():
    client_id = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
    client = mqtt.Client(client_id)  # ClientId不能重复,所以使用当前时间
    client.username_pw_set(" Dev " , " YTc4Mj " )   # must be set, otherwise it will return "Connected with result code 4" 
    client.on_connect = on_connect 
    client.on_message = ON_MESSAGE 
    log.info ( ' start connecting the MQTT ' + HOST + ' : ' + str ( PORT)) 
    the client.connect (the HOST, PORT, 60 ) 
    log.info ( ' MQTT connection completion ' + the HOST + ' : ' + STR (PORT)) 
    client.loop_forever () 

DEF on_connect(client, userdata, flags, rc):
    log.info("Connected with result code " + str(rc))
    client.subscribe("projector-remote-control")
    log.info("订阅消息 projector-remote-control")
    client.subscribe("holo-file-sync")
    log.info("订阅消息 holo-file-sync")
    client.subscribe("sync")
    log.info("订阅消息 sync")
    client.subscribe("Work-SINGLE-Sync " ) 
    log.info ( " subscribe message Work-SINGLE-Sync " ) 

DEF ON_MESSAGE (Client, UserData, MSG): 
    recvmsg = msg.payload.decode ( " UTF-. 8 " ) 
    log.info ( " receive the message " + recvmsg + " , executes the command " )
     Print (msg.topic + " " + recvmsg) 

    IF (msg.topic == ' Sync-SINGLE-Work ' ): 
      common.insert_sql (recvmsg) 
    elif (MSG == .topic 'sync'):
      common.insert_sql(recvmsg)
    elif(msg.topic=='holo-file-sync'):
      common.insert_sql(recvmsg)
    elif(msg.topic=='projector-remote-control'):
      text = demjson.decode(msg)
      command = text['command']
      projectors = text['projectors']
      #socketclient.sendSocket(recvmsg)
      socketclient.handlerMsg(command,projectors)

if __name__ == '__main__':
    syncfile.scheduletask()
    syncfile.scheduleuncompletedtask()
    client_loop() 

2, the message processing

Import Logger
 from Threading Import the Timer
 Import OS
 Import Requests
 from IO Import BytesIO
 from the PIL Import Image 
 Import Common 

log = logger.Logger ( " info " ) 

DEF scheduletask (): 
    T = the Timer (10 , scheduletask) 
    t.start () 
    # Print ( 'timer task is turned on, waiting to receive parameters ...') 
    List common.select_sql = (. 3 )
     IF (List == None):
         return
    getimagesize(list[0],list[2])
def scheduleuncompletedtask():
    t = Timer(10,scheduleuncompletedtask)
    t.start()
    #print('定时任务已开启,等待接收参数中...')
    list = common.select_sql(2)
    if(list == None):
        return
    getimagesize(list[0],list[2])

def getimagesize(id,url):
    savepath = common.readJson()['holoImageUrl']
    try:
       response = requests.get(url)
    except:
       common.delete_sql(id)
       return
    tmpIm = BytesIO(response.content)
    im = Image.open(tmpIm)
    imgpath = url[url.index('.com/')+5:]
    dirpath = imgpath[:imgpath.rindex('/')].replace('/','\\')
    filename= imgpath[imgpath.rfind('/')+1:]
    targetpath = savepath+'\\'+dirpath+'\\'
    filename1 = filename[:filename.find('!')]
    if (os.path.exists(targetpath)):pass
    else:os.makedirs(targetpath)
    im.save(targetpath+filename1)
    if(os.path.exists(targetpath + filename)):
        os.remove(targetpath + filename)
    os.rename(targetpath + filename1, targetpath + filename)
    #localfilesize = os.path.getsize(targetpath+filename)
    #remotefilesize = dict(response.headers).get('Content-Length', 0)
    #IF (localfilesize == remotefilesize):  
    if(os.path.exists (TargetPath + filename) and os.path.getsize (TargetPath + filename) =! 0): 
        common.delete_sql (the above mentioned id) 
        log.info ( " incremental image to download complete ... " )
     the else : 
        common.update_sql (the above mentioned id) 
        log.info ( " incremental part of the picture to download ... " ) 

IF  __name__ == ' __main__ ' : 
    scheduletask ()

Third, the common module

import json
import sqlite3
import re
from threading import Timer
import logger

log = logger.Logger("info")

def readJson():
    with open('config.json', 'rt') as jsonFile:
        val = jsonFile.read()
        Config = json.loads(val)
        return Config

def create_table():
    conn = sqlite3.connect('imagesnyc.xs')
    curs=conn.cursor() # 获取游标
    try:
        create_tb_cmd='''
        CREATE TABLE IF NOT EXISTS image_sync(id INTEGER PRIMARY KEY AUTOINCREMENT,imgsize INTEGER,url TEXT,status INTEGER)
        '''
        #主要就是上面的语句
        curs.execute(create_tb_cmd)
        conn.commit()
    except:
        log.info('Create table failed')
        return False
    finally:
        return conn

def insert_sql(url):
    conn =the create_table () 
    CURS = conn.cursor () # Get cursor 
    imgurls re.split = ( ' , ' , URL)
     for ImageUrl in imgurls: 
       curs.execute ( " the INSERT the INTO image_sync (ImgSize, URL, Status) the VALUES ( '{} ',' {} ',' {} '); " .format (0, ImageUrl,. 3)) # add records 
    conn.commit () 
    log.info ( " insertion completion " ) 
             

DEF select_sql (Status): 
    Conn = the create_table () 
    CURS = conn.cursor () # get a cursor 
    curs.execute ( "* Status from image_sync WHERE SELECT = '% S' Order ID by the ASC. 1 the LIMIT " % Status) # query log 
    List = curs.fetchone () 
    conn.commit () 
    return List 

DEF update_sql (ID): 
    Conn = the create_table () 
    CURS conn.cursor = () # Get the cursor 
    the try : 
      curs.execute ( " update Status image_sync SET = 2 WHERE ID = '% S' " % ID) # update record 
      conn.commit ()
     the except : 
        log.info ( ' primary key update failure id ' )
     the finally:
         Print ( ' the Continue ' ) 

DEF delete_sql (the above mentioned id): 
    conn = create_table () 
    CURS = conn.cursor () # get a cursor 
    curs.execute ( " the Delete from the WHERE image_sync the above mentioned id = '% S' " % the above mentioned id) # delete records 
    conn.commit ()
     # log.info ( 'completed successfully deleted the downloaded records')

 

Guess you like

Origin www.cnblogs.com/772933011qq/p/11857998.html