def RegisterReceiveCallback(self,aReceiveCallback): self.ReceiveCallback = aReceiveCallback try: _thread.start_new_thread(self.SerialReadlineThread, ()) except: s=sys.exc_info()[0] print("Error starting Read thread: ", sys.exc_info()[0]) logger_warning.warning('RegisterReceiveCallback : {}'.format(s))
def __del__(self): try: if self.serialport.is_open(): self.serialport.close() except: s=sys.exc_info()[0] print("Destructor error closing COM port: ", s) logger_warning.warning('Destructor : {}'.format(s))
def cCelToFah(celsius) : try: fahrenheit= 9./5.*celsius+32 except: s=sys.exc_info()[0] print("Error converting Celsius to Fahrenheit : ", s ) logger_warning.warning("Error converting Celsius to Fahrenheit : {}".format(s) ) else: return fahrenheit
def cFahToCel(fahrenheit) : try: celsius= 5./9.*(fahrenheit-32) except: s=sys.exc_info()[0] print("Error converting Fahrenheit to Celsius : ", s) logger_warning.warning("Error converting Fahrenheit to Celsius : {}".format(s) ) else: return celsius
def Close(self): if self.isopen: try: self.serialport.close() self.isopen = False except: s=sys.exc_info()[0] print("Close error closing COM port: ", s) logger_warning.warning('Close : {}'.format(s))
def SerialReadlineThread(self): while 1: try: if self.isopen: self.receivedMessage = self.serialport.readline() if self.receivedMessage != "": self.ReceiveCallback(self.receivedMessage) except: s=sys.exc_info()[0] print("Error reading COM port: ",s) logger_warning.warning('SerialReadlineThread : {}'.format(s)) sys.exit(0) # evite une écriture infini dans le fichier log
def insert_db(query, values): try: print("Execute") cur.execute(query, values) print("Commit") db.commit() print("Insert end") #print(cur.rowcount, "record inserted") # Gestion position des capteurs except: s = sys.exc_info()[0] print("Error database insertion : ", s) logger_warning.warning('insert_db : {}'.format(s))
def fermer_prog(signal, frame): try: serialPort.Close() db.close() sys.stdout.flush() #print("Exit") except: s = sys.exc_info()[0] print("Error closing gateway.py: ", s) logger_warning.warning('fermer_prog : {}'.format(s)) finally: logger_info.info("Gateway STOP") logger_warning.warning("Gateway STOP") sys.exit(0)
def Send(self,message): if self.isopen: try: # Ensure that the end of the message has both \r and \n, not just one or the other newmessage = message.strip() newmessage += '\r\n' self.serialport.write(newmessage.encode('utf-8')) except: s=sys.exc_info()[0] print("Error sending message: ", s ) logger_warning.warning('Send : {}'.format(s)) else: return True else: return False
def Open(self,portname,baudrate,bytesize,parity,stopbits): if not self.isopen: # serialPort = 'portname', baudrate, bytesize = 8, parity = 'N', stopbits = 1, timeout = None, xonxoff = 0, rtscts = 0) self.serialport.port = portname self.serialport.baudrate = baudrate self.serialport.bytesize = int(bytesize) self.serialport.parity = parity self.serialport.stopbits = int(stopbits) try: self.serialport.open() self.isopen = True except: s=sys.exc_info()[0] print("Error opening COM port: ", s) logger_warning.warning('Open : {}'.format(s))
def heat_index(temperature, rh): try: T = cCelToFah(temperature) rh2 = rh * rh T2 = T * T # Calculation based on https://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml and http://www.atmos.albany.edu/student/msmith/misc/vtemp.py # Calculate the Heat Index -- constants converted for RH in [0, 100] # Steadman equation hi = (-42.379 + 2.04901523 *T + 10.14333127 * rh - 0.22475541 * T * rh - 6.83783e-3 * T2 - 5.481717e-2 * rh2 + 1.22874e-3 * T2 * rh + 8.5282e-4 * T * rh2 - 1.99e-6 * T2 * rh2) if (rh < 13.00 and T >= 80.00 and T<=112.00): delta = ((13.-rh)/4.)*sqrt((17.-abs(T-95.))/17.) res = cFahToCel(hi-delta) elif (rh >= 85.00 and T >= 80.00 and T<=87.00): delta = ((rh-85.)/10.)*(87.-T)/5. res = cFahToCel(hi+delta) elif (rh >= 40.00 and T >= 80.00): # 80°F = 26.67°C voir aussi https://en.wikipedia.org/wiki/Heat_index res = cFahToCel(hi) else : res = cFahToCel(0.5 * (T + 61.0 + (T-68.0)*1.2 + rh*0.094)) except: s=sys.exc_info()[0] print("Error converting temperature to heat index : ", s ) logger_warning.warning("Error converting temperature to heat index : {}".format(s) ) else: return round(res, 2)
# -*- coding: utf-8 -* import signal import time import sys import serial_rx_tx # Serial port from log_files import logger_info, logger_warning from config import port, baudrate, bytesize, parity, stopbits from capteurs import * logger_info.info("Gateway START") logger_warning.warning("Gateway START") ACK_OPTION = True # RASPBERRY_1 renvoie acknoledge #ACK_OPTION= False # RASPBERRY_2 pas de renvoie acknoledge # Lors de la fermeture du programme def fermer_prog(signal, frame): try: serialPort.Close() db.close() sys.stdout.flush() #print("Exit") except: s = sys.exc_info()[0] print("Error closing gateway.py: ", s) logger_warning.warning('fermer_prog : {}'.format(s))