用于在Sqlite中存储收到的有效负载的Python 3 MQTT客户端-一次打开数据库,存储多次,最后关闭数据库?

时间:2018-07-31 22:45:14

标签: python-3.x sqlite mqtt paho

我有一个Python 3.6代码,该代码连接到MQTT并订阅一个主题。每次触发回调函数“ on_message”时,它都会实例化一个类,该类具有执行以下操作的单个方法:打开db文件,保存接收到的数据,关闭db文件。

上述Python脚本几乎可以正常工作。每秒大约接收7条MQTT消息,因此对于每条消息,它需要[Open_DB - Save_Data - Close_DB]。有一些消息正在获取PUBACK但未保存,这可能是由于一些不必要的操作所致,所以我想改进:

我花了很多时间(不是专家)试图创建一个类,该类将一次打开数据库,向数据库写入数千次,并且仅在完成后关闭数据库文件。创建一个具有三个方法的类:

1. MyDbClass.open_db_file()
2. MyDbClass.save_data()
3. MyDbClass.close_db_file()

您可能会想到的问题是,即使对象已放置在全局变量上,也无法从“ on_message”回调中调用MyDbClass.save_data()。这是带有建议的想法的无效代码,我对其进行了清理以方便阅读:

# -----------------------------

此代码已经过清理,可以更快地读取

import paho.mqtt.client as mqtt
import time
import json
import sqlite3

全局变量

db_object = ""

class MyDbClass():


def __init__(self):
    pass

def open_db_file(self, dbfile):
   self.db_conn = sqlite3.connect(db_file)
   return self.db_conn

def save_data(self, json_data):
    self.time_stamp = time.strftime('%Y%m%d%H%M%S')
    self.data = json.loads(json_data)
    self.sql = '''INSERT INTO trans_reqs (received, field_a, field_b, field_c) \
                  VALUES (?, ?, ?, ?)'''
    self.fields_values = ( self.time_stamp, self.data['one'], self.data['two'], self.data['three']] )
    self.cur = self.db_conn.cursor()
    self.cur.execute(self.sql, self.fields_values)
    self.db_conn.commit()            

def close_db_file(self):    
    self.cur.close()
    self.db_conn.close()


def on_mqtt_message(client, userdata, msg):

global db_object

m_decode = msg.payload.decode("utf-8","ignore")

db_object.save_data(m_decode)



def main():  



global db_object

要使用的数据库-尝试创建一个对象来管理数据库任务(来自MyDbClass)

db_file =  "my_filename.sqlite"
db_object = MyDbClass.open_db_file(db_file)

# MQTT -- Set varibles
broker_address= "..."
port = 1883
client_id = "..."
sub_topic = "..."
sub_qos = 1
# MQTT -- Instanciate the MQTT Client class and set callbacks
client = mqtt.Client(client_id)
client.on_connect = on_mqtt_connect
client.on_disconnect = on_mqtt_disconnect
client.on_message = on_mqtt_message
client.on_log = on_mqtt_log
client.clean_session = True
#client.username_pw_set(usr, password=pwd)          #set username and password

print('Will connect to broker ', broker_address)
client.connect(broker_address, port=port, keepalive=45 )
client.loop_start()
client.subscribe(sub_topic, sub_qos)
try:
    while True:
        time.sleep(.1)

except KeyboardInterrupt:
    # Disconnects MQTT
    client.disconnect()
    client.loop_stop()
    print("....................................")
    print("........ User Interrupted ..........")
    print("....................................")

db_object.close_db_file()

client.loop_stop()
client.disconnect()

if __name__ == "__main__":
    main()

在此方面的任何帮助将不胜感激!

0 个答案:

没有答案