def readTHWL(data): sprint(data) t = readT(data[0]) h = readH(data[1]) p = readP(data[2]) l = readL(data[3]) return t, h, l, p
def addMeteoData(connection, temp, hum, luz, pres): # timestamp = strftime('%d-%m-%Y %H:%M:%S', localtime()) statement = 'INSERT INTO MeteoData(fecha, temp, hum, luz, pres) VALUES(NOW(), {}, {}, {}, {});'.format( temp, hum, luz, pres) sprint(temp, hum, luz, pres) run(connection, statement) connection.commit()
def create_database(cnx, db): cursor = cnx.cursor() try: cursor.execute( "CREATE DATABASE {} DEFAULT CHARACTER SET 'utf8'".format(db)) except mysql.connector.Error as err: sprint("Failed creating database: {}".format(err)) exit(1)
def getTranslation(connection, txt_id, lang_code): available_lang_codes = ['EN', 'ES'] if lang_code in available_lang_codes: statement = 'SELECT {} FROM Lang WHERE ID = \'{}\''.format(lang_code, txt_id); resultado = run(connection, statement) primer = resultado.next() texto = primer[0] return texto else: sprint(lang_code, 'is not an available language code')
def changeDB(cnx, db): cursor = cnx.cursor() try: cnx.database = db except mysql.connector.Error as err: if err.errno == errorcode.ER_BAD_DB_ERROR: create_database(cursor, db) cnx.database = db else: sprint(err) exit(1)
def run(connection, comm): cursor = None sprint(comm) try: cursor = connection.cursor() except mysql.connector.errors.OperationalError: connection.reconnect() cursor = connection.cursor() try: cursor.execute(comm) return cursor except mysql.connector.Error as err: sprint("Error running statement: {}".format(err)) exit(1)
def readT(data): tok = "" temp = "" tempStart = False for char in data: sprint(char, tok) tok += char if tok == "TEMP = ": tempStart = True tok = "" elif tempStart == True: if char in list("0123456789."): temp += char elif char == "º" or char == "C": tempStart = False tok = "" return temp
def readH(data): tok = "" hum = "" humStart = False for char in data: sprint(char, tok) tok += char if tok == "HUM = ": humStart = True tok = "" elif humStart == True: if char in list("0123456789."): hum += char elif char == "%": humStart = False tok = "" return hum
def readL(data): tok = "" light = "" lightStart = False for char in data: sprint(char, tok) tok += char if tok == "LIGHT = ": lightStart = True tok = "" elif lightStart == True: if char in list("0123456789."): light += char elif char == "%": lightStart = False tok = "" sprint(light) return light
def getConnection(user, password, database): try: r = mysql.connector.connect(user=user, password=password, database=database, host = '127.0.0.1',port='3306') c = r.cursor() c.close() if r is None: sprint('Couldn\'t connect to the database') exit(1) return r except mysql.connector.Error as err: if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: sprint("Something is wrong with your user name or password") elif err.errno == errorcode.ER_BAD_DB_ERROR: sprint("Database does not exist") else: sprint(err) exit(1)
def main(): updater = Updater(token='500779322:AAHmBMF_nV48qNet4IMfgNmcOW5tuQ7ojdI') dispatcher = updater.dispatcher #Handlers start_handler = CommandHandler('start', opt) opt_handler = CommandHandler('options', opt) es_handler = CommandHandler('spanish', partial(changeLang, "ES")) en_handler = CommandHandler('english', partial(changeLang, "EN")) btn_handler = CallbackQueryHandler(button) #Register handlers dispatcher.add_handler(start_handler) dispatcher.add_handler(opt_handler) dispatcher.add_handler(es_handler) dispatcher.add_handler(en_handler) dispatcher.add_handler(btn_handler) #Start & run until Ctrl+C updater.start_polling() try: ultimaFecha = 0 while True: # sprint('looping:', ultimaFecha, time()) if time() >= (ultimaFecha) + 60: sprint('Read') data = parseData.readTHWL(parseData.readArduino([])) #sprint(type(data[0]), type(data[1]), type(data[2]), type(data[3])) if data[0] and data[1] and data[2] and data[3]: MySQL.addMeteoData(db_connection, data[0], data[1], data[2], data[3]) # The data will be *100 sprint('Data inserted into the database') else: sprint('Data was incomplete') sprint('Read done') ultimaFecha = time() except: #updater.idle() db_connection.close() exit(0)
def updatemenu(bot, update, query, msgId, kbdId): try: reply_markup = InlineKeyboardMarkup(getKbds()[kbdId]) localized_manu_name = "ERROR" if kbdId == 'Principal': localized_manu_name = MySQL.getTranslation(db_connection, 'main_menu', lang) elif kbdId == 'Temperatura': localized_manu_name = MySQL.getTranslation(db_connection, 'tmp_menu', lang) elif kbdId == 'Humedad': localized_manu_name = MySQL.getTranslation(db_connection, 'hum_menu', lang) elif kbdId == 'Presion': localized_manu_name = MySQL.getTranslation(db_connection, 'prs_menu', lang) elif kbdId == 'Luz': localized_manu_name = MySQL.getTranslation(db_connection, 'lig_menu', lang) bot.edit_message_text(text=localized_manu_name, chat_id=query.message.chat_id, message_id=query.message.message_id, reply_markup=reply_markup) except KeyError: if not kbdId == '@back': lastLecture = None if kbdId.endswith('Actual'): lastLecture = MySQL.getLastLecture(db_connection) elif kbdId.endswith('Media'): lastLecture = MySQL.getAvg(db_connection) elif kbdId.endswith('Max'): lastLecture = MySQL.getMax(db_connection) elif kbdId.endswith('Min'): lastLecture = MySQL.getMin(db_connection) temp = None hum = None lig = None prs = None #sprint(lastLecture.column_names) res = None try: res = lastLecture.fetchall()[0] except IndexError: res = None sprint(res) temp, hum, lig, prs = res temp, hum, lig, prs = str(temp / 100), str(hum / 100), str( lig / 100), str(prs / 100) # Data is stored *100 # if len(lastLecture) > 0: #for tempf, humf, ligf, prsf in lastLecture: # temp, hum, lig, prs = tempf, humf, ligf, prsf # sprint(tempf, humf, ligf, prsf) # break; #else: # (temp, hum, lig, prs) = 'NO DATA' if (kbdId == '@back'): updatemenu(bot, update, query, msgId, "Principal") return elif (kbdId == 'TActual'): bot.send_message( chat_id=query.message.chat_id, text=MySQL.getTranslation(db_connection, 'cur_tmp', lang) + ": " + temp) return elif (kbdId == 'HActual'): bot.send_message( chat_id=query.message.chat_id, text=MySQL.getTranslation(db_connection, 'cur_hum', lang) + ": " + hum) return elif (kbdId == 'LActual'): bot.send_message( chat_id=query.message.chat_id, text=MySQL.getTranslation(db_connection, 'cur_lig', lang) + ": " + lig) return elif (kbdId == "PActual"): bot.send_message( chat_id=query.message.chat_id, text=MySQL.getTranslation(db_connection, 'cur_prs', lang) + ": " + prs) return elif (kbdId == 'TMedia'): bot.send_message( chat_id=query.message.chat_id, text=MySQL.getTranslation(db_connection, 'avg_tmp', lang) + ": " + temp) return elif (kbdId == 'HMedia'): bot.send_message( chat_id=query.message.chat_id, text=MySQL.getTranslation(db_connection, 'avg_hum', lang) + ": " + hum) return elif (kbdId == 'LMedia'): bot.send_message( chat_id=query.message.chat_id, text=MySQL.getTranslation(db_connection, 'avg_lig', lang) + ": " + lig) return elif (kbdId == "PMedia"): bot.send_message( chat_id=query.message.chat_id, text=MySQL.getTranslation(db_connection, 'avg_prs', lang) + ": " + prs) return elif (kbdId == 'TMax'): bot.send_message( chat_id=query.message.chat_id, text=MySQL.getTranslation(db_connection, 'max_tmp', lang) + ": " + temp) return elif (kbdId == 'HMax'): bot.send_message( chat_id=query.message.chat_id, text=MySQL.getTranslation(db_connection, 'max_hum', lang) + ": " + hum) return elif (kbdId == 'LMax'): bot.send_message( chat_id=query.message.chat_id, text=MySQL.getTranslation(db_connection, 'max_lig', lang) + ": " + lig) return elif (kbdId == "PMax"): bot.send_message( chat_id=query.message.chat_id, text=MySQL.getTranslation(db_connection, 'max_prs', lang) + ": " + prs) return elif (kbdId == 'TMin'): bot.send_message( chat_id=query.message.chat_id, text=MySQL.getTranslation(db_connection, 'min_tmp', lang) + ": " + temp) return elif (kbdId == 'HMin'): bot.send_message( chat_id=query.message.chat_id, text=MySQL.getTranslation(db_connection, 'min_hum', lang) + ": " + hum) return elif (kbdId == 'LMin'): bot.send_message( chat_id=query.message.chat_id, text=MySQL.getTranslation(db_connection, 'min_lig', lang) + ": " + lig) return elif (kbdId == "PMin"): bot.send_message( chat_id=query.message.chat_id, text=MySQL.getTranslation(db_connection, 'min_prs', lang) + ": " + prs) return else: invalidMsg = bot.send_message(chat_id=query.message.chat_id, text=MySQL.getTranslation( db_connection, 'invalid_id', lang)) time.sleep(3) bot.delete_message(chat_id=invalidMsg.chat_id, message_id=invalidMsg.message_id) query.answer()
sprint(light) return light def readTHWL(data): sprint(data) t = readT(data[0]) h = readH(data[1]) p = readP(data[2]) l = readL(data[3]) return t, h, l, p i = 0 for port in serial.tools.list_ports.comports(): sprint(i, port.device) i = i + 1 p = input('Select the port number: ') arduino = serial.Serial(serial.tools.list_ports.comports()[int(p)].device, 9600) sprint('Serial port open') data = [] def readArduino(data=[]): rawString = arduino.readline() data = rawString.decode('unicode_escape') data = data[:-2] data = data.split(";") return data