Preface
Prior to using RABBITMQ to deploy a simple MQTT server, no user isolation, but also deployed the Influxdb sequential database, but can not directly through the configuration of the MQTT server data to the Time-series database, so I decided to write my own script implementation. Ready to
Open shell Use
Pip Install influxdb install inluxdb required modules
Pip Install Paho-mqtt install RABBMQ required module source code
# coding=utf-8 Import JSON import random import threading import OS import paho.mqtt.client as Mqtt import time from Influ
Xdb Import influxdbclient from My_lib.code_handle.code_handle import Auto_code class mqtt_handle:topic_sub= ' $DP ' topic_pub= ' $info ' counts = 0 ClientID = ' For I in range (0, 2): ClientID = Clientid.join (str (random . Uniform (0, 1)) Mqtt_client = Mqtt. Client (clientID) db_client = Influxdbclient (Self._host, 8086, ', ', ', ' MyDB ') # Initialize Def __init__ (self, host, por T): Self._host = host Self._port = Port self.mqtt_client.on_connect = self._on_connect # settings connect to the server callback function self.mqtt_client.on_message = self._on_message # setting receives Server message callback function def connect (self, username=none, passwo Rd=none): Self.mqtt_client.username_pw_set (username, password) self.mqtt_client.connect (Self._host, Self._ Port, 60) # Connect to the server, ports 1883, maintain a heartbeat of 60 seconds def publish (self, Data): Self.mqtt_client.publish (self.toPic_pub, data) def loop (self, timeout=none): Thread = Threading. Thread (Target=self._loop, args= (timeout,)) Thread.Start () def _loop (self, timeout=none): If not Timeo Ut:self.mqtt_client.loop_forever () Else:self.mqtt_client.loop (timeout) def _on_conn ECT (self, client, UserData, flags, rc): Local_time = Time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime (time.time) ) with open ('./run.log ', ' A + ') as F:f.write (' @Run ' + local_time + ' Connected with result code: ' +
STR (RC)) Client.subscribe (self.topic_sub) def _on_message (self, client, UserData, msg): # Callback This function after receiving a message from the server Data_json = Auto_code (str (msg.payload)) if Self._is_json (Data_json): Data_list = [Json.loads (dat A_json)] #如果符合InfluxDB格式就转存至数据库 if ' measurement ' in data_list[0] and ' tags ' in data_list[0] and ' F Ields ' in data_list[0]: Try:db_cLient.write_points (data_list) self.counts + = 1 local_time = time.strftime ('%y-%m-% D%h:%m:%s ', Time.localtime (Time.time ()) #//**********//records an upload log with open ('./UPL
Oad.log ', ' A + ') as F:f.write (' success,counts: ' + str (self.counts) + ' time: ' + local_time + ' \ n ') Except Exception as E:with open ('./upload.log ', ' A + ') as f:f.
Write (E.message) f.write (' \ntopic: ' + auto_code (str (msg.topic)) + "msg:" + Data_json + ' \ n ') #//**********//#如果接受到停止指令就停止程序并记录一个停止日志 elif data_list[0].has_key (' cmd ') and DA ta_list[0][' cmd '] = = ' exit ': print ' \n@mqtt_handle exit\n ' with open ('./run.log ', ' a ') as F : Local_time = Time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime (Time.time ())) F.W
Rite ' @Stop ' + local_time + ' Topic: ' + auto_code (str (msg.topic) + "msg:" + Data_json + ' \ n ') os._exit
(0) #停止程序 #解析JSON前先判断数据 is JSON format to avoid program crashes Def _is_json (self, data): Try:json.loads (data) Except Valueerror:return False return True if __name__ = = ' __main__ ': local_host = ' 127.0.0.1
' Db_client = influxdbclient (Local_host, 8086, ', ', ' MyDB ') # Initialize mqtt_client = Mqtt_handle (Local_host, 1883)
Mqtt_client.connect (' influxdb ', ' influxdb ') Mqtt_client.loop ()
Write a simple shell to run the script:
#!/bin/bash
nohup python/home/ubuntu/app/py/mqttdb/mqtt_handle.py &