def start_mqtt(): from lib import utils import paho.mqtt.client as mqtt #import the client # --- leggo l'indirizzo del broker MQTT --- broker_address = utils.getConfig('serverMQTT', 'host') port = int(utils.getConfig('serverMQTT', 'port')) print("creating new instance") client = mqtt.Client() #create new instance client.connect(broker_address, port) #connect to broker return client
def get_message(topic="ST/VAL/ST02") : import time import paho.mqtt.client as mqtt #import the client from lib import utils #import utils global messaggi # --- leggo l'indirizzo del broker MQTT --- broker_address = utils.getConfig('serverMQTT','host') port = int(utils.getConfig('serverMQTT','port')) #print("creating new instance") client = mqtt.Client("prova") #create new instance #client.on_connect = on_connect client.on_message = on_message #print("connecting to broker") client.connect(broker_address,port) #connect to broker client.subscribe(topic) while True : # scorro la lista a rovescio per leggere il valore piu' recente #print("messaggi -----------------------") #print (messaggi) for messaggio in reversed(messaggi) : appo = messaggio.split("|") lista = appo[0].split("/") compo = lista[len(lista)-1] codice = appo[0][: appo[0].index(compo)-1] valore = appo[1] client.loop_stop() #client.disconnect() messaggi = [] return valore client.loop_start() time.sleep(.1)
#!/usr/bin/python3 # -*- coding: utf-8 -*- import paho.mqtt.client as mqtt #import the client import time from lib import utils import json import sqlite3 from websocket import create_connection conn = sqlite3.connect(utils.getConfig('Sqlitedb','dbfile'), timeout=30.0) messaggi = [] def on_message(client, userdata, message): print("message received " ,message.topic," ",str(message.payload.decode("utf-8"))) #ws = create_connection("ws://localhost:8000/ws/acs") #print("invio ws") #x = {"message": message.topic , "valore": str(message.payload.decode("utf-8"))} x = {"message": {"componente":message.topic , "valore": str(message.payload.decode("utf-8"))} } # convert into JSON: y = json.dumps(x) # the result is a JSON string: #print(y) #stringa = f'{ "message":"{message.topic}"}' #print(stringa) #ws.send(y) global messaggi messaggi.append(message.topic +"|"+ str(message.payload.decode("utf-8")) )
# python 3.7.X from lib.utils import getConfig, getLogger config = getConfig() print(config.sections()) print(config["log"]["path"]) logger = getLogger(config["log"]["path"]) logger.info("test")
fh.setLevel(logging.DEBUG) logger.addHandler(fh) from lib import Vm from lib import Gr from lib import utils import time import paho.mqtt.client as mqtt #import the client from multiprocessing import Process #import multiprocessing as mp import sqlite3 conn = sqlite3.connect(utils.getConfig('Sqlitedb', 'dbfile'), timeout=30.0) MAX_TIME = 60 # costante aggiornamento clock timestamp_old = 0 def main(): # ---leggo tutte le valvole presenti query = "SELECT codice FROM apparati WHERE tipo = 'VT' and active = 1" c = conn.cursor() c.execute(query) valvole = c.fetchall() c.close()