def update_buffer(self, values, appnd, ref): #append a line of info at the current position plus 1 # print("Update Buffer appnd and ref are : ",appnd,ref) if appnd + (self.__source_ref != ref): #we adding message and incrementing posn if self.__size < self.__config.text_buffer_length - 1: self.__size += 1 if self.__posn == self.__config.text_buffer_length - 1: # last insert was at the end so go back to beginning@@ self.__posn = 0 else: # first increment insert position by one self.__posn += 1 # then insert a line full of values self.__source_ref = ref else: self.__source_ref = ref if len(values) > self.__width: print("Width Error for :", self.__config.prog_name, len(values), self.__width, values) sys.exit() for i in range(0, len(values)): self.__dta[self.__posn][i] = values[i] #print("Buffer updated and log buffer flag is : ",self.__config.log_buffer_flag) if self.__config.log_buffer_flag: self.__log.log_to_file(self.__headings, values) if fileexists(self.__www_filename): try: self.__log.copy_log_to_www(False) except: print( "Failed to copy log file to www because this not there: ", self.__www_filename) #send log file to website configevery ten scans if self.__send_log_count > 10 and fileexists(self.__ftp_creds): self.__log.send_log_by_ftp(self.__config.ftp_debug, self.__config.log_directory, self.__config.ftp_timeout) self.__send_log_count = 0 else: self.__send_log_count += 1
def main(): global config config = class_config() if fileexists(config.config_filename): print("will try to read Config File : ", config.config_filename) config.read_file() # overwrites from file else: # no file so file needs to be writen config.write_file() print( "New Config File Made with default values, you probably need to edit it" ) logging.basicConfig(level=logging.INFO) credentials = auth_helpers.get_assistant_credentials() with Board() as board, Assistant(credentials) as assistant: for event in assistant.start(): process_event(assistant, board.led, event)
config = class_config("/home/pi/ftp_creds/ftp_creds.csv", "/var/www/html/", "log/", 5, 10) ################################ #print("config.ftp_creds_filename : ",config.ftp_creds_filename) #my_pid = getpid() #config.prog_path = path.dirname(path.realpath(__file__)) + "/" #config.prog_name = str(sys_argv[0][:-3]) #print(config.prog_name) #config.config_filename = config.prog_name + ".cfg" #config.set_filename(config.config_filename) #print("config file is : ",config.config_filename) ############################################### if fileexists(config.config_filename): print("will try to read Config File : ", config.config_filename) config.read_file() # overwrites from file else: # no file so needs to be written config.write_file() print("New Config file made") config.scan_count = 0 example_buffer_width = 11 headings = ["Count", "Val1", "Val2", "Val3", "Val4", "Val5"] buffer_flag = True example_buffer = class_text_buffer(100, headings, config.prog_name, config, buffer_flag) buffer_increment_flag = True
def pr(self,appnd,ref,log_time,refresh_interval): here = "buffer.pr for " + self.__config.prog_name make_values = [" -- "]*self.__width prtime = datetime.now() for_screen = log_time.strftime('%d/%m/%Y %H:%M:%S') # following alternative will show more resolution for fractions of a second # for_screen = log_time.strftime('%d/%m/%Y %H:%M:%S.%f') make_values[0] = for_screen file_start = """<head> <meta http-equiv="refresh" content=""" file_start = file_start + str(refresh_interval) file_start = file_start + """ /> </head> <caption>Rotating Buffer Display</caption>""" tbl_start = """ <p> <table style="float: left;" border="1"> <tbody>""" tbl_start_line = """<tr>""" tbl_end_line = """</tr>""" tbl_start_col = """<td>""" tbl_end_col= """</td>""" tbl_end = """</tbody> </table>""" file_end = """ </body> </html>""" try: for i in range(0,self.__width -1): make_values[i+1] = str(self.line_values[i]) for_screen = for_screen + " " + str(self.line_values[i]) except: print("Error in make values in ...buffer.pr for : ",self.__config.prog_name) print("i,values,len(self.line_value>s),self.__width",i,self.line_values,len(self.line_values),self.__width) sys_exit() # print to screen and to status log and update html file if appnd: print(" appending : " + self.__config.prog_name + " : " + for_screen) else: print("not appending : " + self.__config.prog_name + " : " + for_screen) self.update_buffer(make_values,appnd,ref) with open(self.__html_filename,'w') as htmlfile: htmlfile.write(file_start) if self.__config.log_buffer_flag: htmlfile.write('<p>' + self.__html_filename + ' : ' + make_time_text(datetime.now()) + ' ' + '<a href= "' + self.__config.log_directory + self.__log.log_filename + '" target="_blank"> View CSV Log File </a></p>\n<p>') else: htmlfile.write("<p>" + self.__html_filename + " : " + make_time_text(datetime.now()) + "</p>\n<p>") htmlfile.write(tbl_start + tbl_start_line) for ind in range(0,len(self.__headings)): htmlfile.write(tbl_start_col + self.__headings[ind] + tbl_end_col) htmlfile.write(tbl_end_line) buffer_dta = self.get_dta() for ind in range(self.__size): htmlfile.write(tbl_start_line) for i in range(self.__width): htmlfile.write(tbl_start_col + str(buffer_dta[ind][i]) + tbl_end_col) htmlfile.write(tbl_end_line) htmlfile.write(tbl_end) htmlfile.write(file_end) try: copyfile(self.__html_filename, self.__www_filename) except: print("Not able to copy : ",self.__html_filename, " to ", self.__www_filename) if fileexists(self.__ftp_creds): if self.__send_html_count >= 3: # To debug FTP change end of following line to " = True" !!!!!!!!!!!! FTP_dbug_flag = False ftp_result = send_by_ftp(FTP_dbug_flag,self.__ftp_creds, self.__html_filename_save_as, self.__html_filename,"",self.__config.ftp_timeout) for pres_ind in range(0,len(ftp_result)): pr(FTP_dbug_flag,here, str(pres_ind) + " : ", ftp_result[pres_ind]) self.__send_html_count = 0 else: self.__send_html_count += 1 return