def __init__(self, config): self.__send_plain_count = 5 self.__no_heading_yet = True self.__config = config self.debug = self.__config.ftp_debug starttime = datetime.now() timestamp = make_time_text(starttime) self.log_filename = timestamp + "_" + self.__config.prog_name + "_" + "lg.csv" self.__log_filename_save_as = self.__config.prog_path + self.__config.log_directory + self.log_filename self.__local_www_log_filename = self.__config.local_dir_www + "/" + self.__config.log_directory + self.log_filename
def __init__(self, name, config): self.dbug = False self.__log_filename = "not set" self.__log_filename_save_as = "not_set" self.__local_www_log_filename = "not set" self.__ftp_creds = "not set" self.__send_plain_count = 5 self.__no_heading_yet = True self.__name = name starttime = datetime.now() timestamp = make_time_text(starttime) self.__log_filename = timestamp + "_" + self.__name + "_" + "lg.csv" print(config.prog_path, config.log_directory) self.__log_filename_save_as = config.prog_path + config.log_directory + self.__log_filename self.__local_www_log_filename = config.local_dir_www + config.log_directory + self.__log_filename self.__ftp_creds = config.ftp_creds_filename
def pr(self, appnd, ref, log_time, refresh_interval): here = "buffer.pr for " + self.__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.__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.__name + " : " + for_screen) else: print("not appending : " + self.__name + " : " + for_screen) self.update_buffer(make_values, appnd, ref) with open(self.__html_filename, 'w') as htmlfile: htmlfile.write(file_start) 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) copyfile(self.__html_filename, self.__www_filename) # 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]) return
def send_html_by_ftp(self, config, smartplug_info, target_temp, target_full_power): here = "sensors.send_by_ftp" ftp_text_linestart = "<tr align=\"center\" valign=\"middle\"><td>" ftp_text_line_end = "</td></tr>" ftp_text_end = ["</tbody>"] ftp_text_between = "</td><td>" ftp_start = """<!-- Temperature Logging and Control --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.oset_log_html_filenamerg/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Temperature Logging and Control</title> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <meta http-equiv="refresh" content="15" /> </head> <body><table style="background-color: #f4e7e7; width: 350px; height: 150px; border: 1px solid #1b58e4;\" cellpadding=\"5\" align=\"center\"> <caption>Temperatures Logging</caption><tbody>""" ftp_end = """</td></tr> </table>") </body> </html>""" if self.__html_filename == "not_set": pr(self.dbug, here, "Error", "html filename not set") if self.__www_filename == "not_set": pr(self.dbug, here, "Error", "html filename not set") if self.__ftp_creds == "not_set": pr(self.dbug, here, "Error", "html filename not set") with open(self.__html_filename, 'w') as htmlfile: htmlfile.write(ftp_start) htmlfile.write(ftp_text_linestart + " Program Name: " + ftp_text_between \ + config.prog_name + ftp_text_line_end) htmlfile.write(ftp_text_linestart + " Scan Count: " + ftp_text_between \ + str(config.scan_count) + ftp_text_line_end) htmlfile.write(ftp_text_linestart + " System Time: " + ftp_text_between \ + make_time_text(datetime.now()) + ftp_text_line_end) htmlfile.write(ftp_text_linestart + " Html Log File: " + ftp_text_between \ + "<a href=\"" + self.__log_html_filename + "\" target = \"_blank\">" \ + self.__log_html_filename + "</a>" + ftp_text_line_end) htmlfile.write(ftp_text_linestart + " Status File: " + ftp_text_between + "<a href=\"" + \ str(self.__status_html_filename) + "\" target = \"_blank\">" + \ str(self.__status_html_filename) + "</a>" + ftp_text_line_end) htmlfile.write(ftp_text_linestart + " Debug File: " + ftp_text_between + "<a href=\"" + \ str("debug.html") + "\" target = \"_blank\">" + \ str("debug.html") + "</a>" + ftp_text_line_end) htmlfile.write(ftp_text_linestart + " WD Log File: " + ftp_text_between + "<a href=\"" + \ str("wd_log.html") + "\" target = \"_blank\">" + \ str("wd_log.html") + "</a>" + ftp_text_line_end) htmlfile.write(ftp_text_linestart + " CSV Log File: " + ftp_text_between + "<a href=" + "\"" + str(self.__log_filename_save_as) + "\"" + "target = \"_blank\">" + str(self.__log_filename) + "</a>" + ftp_text_line_end) s_numb = 0 for element in self.number: htmlfile.write(ftp_text_linestart + str(element) + ftp_text_between + str(self.location[s_numb]) + \ ftp_text_between + str(self.status_text[s_numb]) + ftp_text_line_end) s_numb += 1 if config.sauna: htmlfile.write(ftp_text_linestart + "Sauna" + ftp_text_between + "Target/Offset/On" + \ ftp_text_between + "{0:.4}/{1:.4}/{2:.4}".format( \ float(config.default_target),float(config.target_offset),float(config.percent_full_power * config.sauna_on)) + \ ftp_text_line_end) else: htmlfile.write(ftp_text_linestart + "Plug 1 Power and Total" + ftp_text_between + str(smartplug_info.power[0]) + ftp_text_between + str(smartplug_info.total[0]) + ftp_text_line_end) htmlfile.write(ftp_text_linestart + "Plug 2 Power and Total" + ftp_text_between + str(smartplug_info.power[1]) + ftp_text_between + str(smartplug_info.total[1]) + ftp_text_line_end) htmlfile.write(ftp_text_linestart + "Scheduled" + ftp_text_between + "Target Temp : " + ftp_text_between + str(target_temp) + ftp_text_line_end) htmlfile.write(ftp_text_linestart + "Scheduled" + ftp_text_between + "Target Full Power : " + ftp_text_between + str(target_full_power) + ftp_text_line_end) for element in ftp_text_end: htmlfile.write(element) # Change "False" to "True" on next line to debug FTP FTP_dbug_flag = False ftp_result = send_by_ftp(FTP_dbug_flag, self.__ftp_creds, self.__html_filename, "index.html", "", config.ftp_timeout) for pres_ind in range(0, len(ftp_result)): pr(FTP_dbug_flag, here, str(pres_ind) + " : ", ftp_result[pres_ind]) try: # send the same html file to the local web site copyfile(self.__html_filename, self.__www_filename) pr(self.dbug, 0, "Sent : " + config.html_filename + " to : ", config.local_www_html_filename) except: pr(self.dbug, 0, "Fail with copy " + config.html_filename + " to : ", config.local_www_html_filename)
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) message = self.line_values[1] try: self.__mqttc.publish(self.__config.topic, message, retain=True) self.__mqttc.loop(2) #timeout = 2s except: print("Mqtt cant send") 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