Exemple #1
0
 def select_config_file(filename):
     configuration = parse_config.ConfPacket()
     global configs
     configs = configuration.load_config('FILES, AUDIO_PARAM', filename)
     global temp_folder
     temp_folder = configs['FILES']['temp_folder']
     global log_folder
     log_folder = configs['FILES']['log_folder']
     global definitive_folder
     definitive_folder = os.path.join(
         configs['FILES']['saved_files_folder'])
     global INPUT_BLOCK_TIME
     INPUT_BLOCK_TIME = int(configs['AUDIO_PARAM']['input_block_time'])
     global NAME
     NAME = (configs['FILES']['name'])
import paho.mqtt.client as mqtt
import parse_config as parse
import json

configuration = parse.ConfPacket()
configs = configuration.load_config('MQTT, MQTT_TOPICS')

on_msg = None


class MQTTReceiver:
    def __init__(self):
        pass

    @staticmethod
    def on_connect(client, userdata, flags, rc):
        """ The callback for when the client receives a CONNACK response from the server."""
        print('Connected with result code ' + str(rc))
        for topic in configs['MQTT_TOPICS']:
            client.subscribe(configs['MQTT_TOPICS'][topic])

    @staticmethod
    def on_message(client, userdata, msg):
        """The callback for when a PUBLISH message is received from the server."""
        #print(msg.topic + ' ' + str(msg.payload))
        msg_decoded = str(msg.payload.decode("utf-8", "ignore"))
        #print("Msg decoded: ", msg_decoded)
        try:
            msg_dict = json.loads(msg_decoded)
            msg_dict['topic'] = msg.topic
            #print("Dict: ", msg_dict)
from datetime import date, datetime, timedelta
import parse_config
from time import sleep
import os
configuration = parse_config.ConfPacket()
configs = configuration.load_config('FILES')
definitive_folder = os.path.join(configs['FILES']['saved_files_folder'])
log_folder = os.path.join(definitive_folder, "logs")
if (not os.path.exists(log_folder)):
    os.path.mkdir(log_folder)
try:

    def adiciona_linha_log(texto, time_offset=0):
        dataFormatada = (
            datetime.now() +
            timedelta(seconds=time_offset)).strftime('%d/%m/%Y %H:%M:%S')
        mes_ano = (datetime.now() +
                   timedelta(seconds=time_offset)).strftime('_%Y%m')
        try:
            filename = 'log' + mes_ano + '.txt'
            logfilepath = os.path.join(log_folder, filename)
            f = open(logfilepath, 'a')
            f.write(dataFormatada + " " + str(texto) + "\n")
            f.close()
        except Exception as err:
            print(dataFormatada, "ERRO ao adicionar linha log: ", err)
            adiciona_linha_log(str(err))
except Exception as ERR:
    print(ERR)
    sleep(50)