Пример #1
0
def run(hs, gpu, epochs, snaps, batchsize, lr, thre):
    planes = []
    train, test = chainer.datasets.get_mnist()
    xp = cp if gpu >=0 else np
    test_xs, test_ys = tuple2array(test, xp)
    size = 10000
    test_xs = test_xs[:size]; test_ys = test_ys[:size]
    bins = np.linspace(-1, 1, 30)
    Ts_index = np.array(np.linspace(0, 1, snaps) * (epochs - 1)).astype(np.int)
    least = 10
    if Ts_index[1] > least:
        Ts_index = np.unique(np.append(np.array(range(least)), Ts_index[1:]))

    model = MLP(hs)
    if gpu >= 0:
        model.to_gpu()

    train_iter = chainer.iterators.SerialIterator(train,
                                                  batch_size=batchsize)
    Ts_list = train_model(model, train_iter, test_xs, test_ys,
                          epochs, batchsize, Ts_index, lr, hs, thre)
    feature = '{}_thre{}_epoch{}_bs{}_lr{}'.format(
        '_'.join([str(h) for h in hs]), thre, epochs, batchsize, lr)
    points = calc_mi(to_cpu(test_xs), to_cpu(test_ys)[:, None],
                     Ts_list, bins)
    plot_planes(points, Ts_index, hs, feature)
    pr('')
Пример #2
0
	def copy_log_to_www(self,dbug_flag):
		here = "copy_log_to_www"
		try:
			# send the same html file to the local web site
			copyfile(self.__log_filename_save_as, self.__local_www_log_filename)
			pr(dbug_flag,0, "Sent : " + self.__log_filename_save_as + " to : ", self.__local_www_log_filename)
		except:
			pr(True,0,"Fail with copy " + self.__log_filename_save_as + " to : ", self.__local_www_log_filename)
Пример #3
0
 def send_log_by_ftp(self, FTP_dbug_flag, remote_log_dir, timeout):
     here = "my_sensors.send_log_by_ftp"
     ftp_result = send_by_ftp(FTP_dbug_flag,self.__ftp_creds, self.__log_filename_save_as, \
      self.__log_filename,remote_log_dir,timeout)
     # Folowing useful to debug the remote directory
     # pr(FTP_dbug_flag,here,"### FTP_dbug_flag,self.__ftp_creds, self.__log_filename_save_as, self.__log_filename,remote_log_dir", \
     #	str(FTP_dbug_flag) + ":" + str(self.__ftp_creds) + ":" + str(self.__log_filename_save_as) + \
     #	":" + str(self.__log_filename) + ":" + str(remote_log_dir))
     for pres_ind in range(0, len(ftp_result)):
         pr(FTP_dbug_flag, here,
            str(pres_ind) + " : ", ftp_result[pres_ind])
     return
Пример #4
0
 def send_command(self, cmded, ip, port, dbug_flag):
     here = "743 smartplug send command"
     pr(dbug_flag, here, "command and ip : ", str(cmded) + " : " + str(ip))
     try:
         self.cmnd_start = datetime.now()
         sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         sock_tcp.settimeout(self.timeout / 1000000)
         sock_tcp.connect((ip, port))
         sock_tcp.send(self.encrypt(cmded))
         data = sock_tcp.recv(2048)
         sock_tcp.close()
         return self.decrypt(data[4:])
     except:
         return "error"
 def write_file(self):
     here = "config.write_file"
     config_write = RawConfigParser()
     config_write.add_section('SetUp')
     config_write.set('SetUp', 'scan_delay', self.scan_delay)
     config_write.set('SetUp', 'max_scans', self.max_scans)
     config_write.set('SetUp', 'log_directory', self.log_directory)
     config_write.set('SetUp', 'local_dir_www', self.local_dir_www)
     config_write.set('SetUp', 'sensor_config_filename',
                      self.sensor_config_filename)
     config_write.set('SetUp', 'ftp_creds_filename',
                      self.ftp_creds_filename)
     config_write.set('SetUp', 'delay_limit', self.delay_limit)
     config_write.set('SetUp', 'delay_increment', self.delay_increment)
     config_write.set('SetUp', 'ftp_log_max_count', self.ftp_log_max_count)
     config_write.set('SetUp', 'heaterIP0', self.heaterIP0)
     config_write.set('SetUp', 'heaterIP0_power_scale',
                      self.heaterIP0_power_scale)
     config_write.set('SetUp', 'heaterIP1', self.heaterIP1)
     config_write.set('SetUp', 'heaterIP1_power_scale',
                      self.heaterIP1_power_scale)
     config_write.set('SetUp', 'heaterIP2', self.heaterIP2)
     config_write.set('SetUp', 'heaterIP2_power_scale',
                      self.heaterIP2_power_scale)
     config_write.set('SetUp', 'heaterIP3', self.heaterIP3)
     config_write.set('SetUp', 'heaterIP3_power_scale',
                      self.heaterIP3_power_scale)
     config_write.set('SetUp', 'sensor4readings', self.sensor4readings)
     config_write.set('SetUp', 'change4log', self.change4log)
     config_write.set('SetUp', 'control_hysteresis',
                      self.control_hysteresis)
     config_write.set('SetUp', 'default_target', self.default_target)
     config_write.set('SetUp', 'default_target_full_power',
                      self.default_target_full_power)
     config_write.set('SetUp', 'max_target', self.max_target)
     config_write.set('SetUp', 'min_target', self.min_target)
     config_write.set('SetUp', 'precision', self.precision)
     config_write.set('SetUp', 'target_integral', self.target_integral)
     config_write.set('SetUp', 'one_heater_select', self.one_heater_select)
     config_write.set('SetUp', 'percent_full_power',
                      self.percent_full_power)
     config_write.set('SetUp', 'watchdog_time', self.percent_full_power)
     config_write.set('SetUp', 'ftp_timeout', self.percent_full_power)
     # Writing our configuration file to 'c_filename'
     pr(self.dbug, here,
        "ready to write new config file withdefault values: ",
        self.__c_filename)
     with open(self.__c_filename, 'w+') as configfile:
         config_write.write(configfile)
     return 0
Пример #6
0
	def send_log_by_ftp(self,FTP_dbug_flag,remote_log_dir,ftp_timeout):
		here = "bffr_log_log_by_ftp"
		ftp_result = send_by_ftp(FTP_dbug_flag,self.__config.ftp_creds_filename, self.__log_filename_save_as, \
			self.__log_filename,remote_log_dir,ftp_timeout)
		for pres_ind in range(0,len(ftp_result)):
			pr(FTP_dbug_flag,here, str(pres_ind) + " : ", ftp_result[pres_ind])
		if self.__send_plain_count < 0 :
			ftp_result = send_by_ftp(FTP_dbug_flag,self.__config.ftp_creds_filename, self.__log_filename_save_as, \
				"log.csv",remote_log_dir,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_plain_count = 10
		else:
			self.__send_plain_count -= 1
			#print("Send plain count : ",self.__send_plain_count)
		return
Пример #7
0
 def set_status_text(self, config):
     # set the status text based on the results of the last scan
     self.status_text_error_count = 0
     here = "mysensors.set_status_text"
     for z in range(0, len(self.code), 1):
         pr(
             self.dbug, here, "setting status text (index:error:reading) ",
             str(z) + " : " + str(self.error_number[z]) + " : " +
             str(self.reading[z]))
         if self.error_number[z] == 0:
             self.status_text[z] = ("{0:.4}".format(float(self.reading[z])))
         else:
             self.status_text_error_count += 1
             if self.delay[z] >= config.delay_limit:
                 self.status_text[z] = ("Wait : " + str(int(self.delay[z])))
             else:
                 self.status_text[z] = ("Err# : " +
                                        str(self.error_number[z]))
Пример #8
0
    def write_file(self):
        here = "config.write_file"
        config_write = RawConfigParser()
        section = "Scan"
        config_write.add_section(section)
        config_write.set(section, 'scan_delay', self.scan_delay)
        config_write.set(section, 'max_scans', self.max_scans)
        section = "Log"
        config_write.add_section(section)
        config_write.set(section, 'log_directory', self.log_directory)
        config_write.set(section, 'local_dir_www', self.local_dir_www)
        config_write.set(section, 'log_buffer_flag', self.log_buffer_flag)
        config_write.set(section, 'text_buffer_length',
                         self.text_buffer_length)
        section = "Ftp"
        config_write.add_section(section)
        config_write.set(section, 'ftp_creds_filename',
                         self.ftp_creds_filename)
        config_write.set(section, 'ftp_log_max_count', self.ftp_log_max_count)
        config_write.set(section, 'ftp_timeout', self.ftp_timeout)
        config_write.set(section, 'ftplog', self.ftplog)
        config_write.set(section, 'ftp_debug', self.ftp_debug)
        section = "Sauna"
        config_write.add_section(section)
        config_write.set(section, 'max_temp', self.max_temp)
        config_write.set(section, 'min_temp', self.min_temp)
        config_write.set(section, 'min_speed', self.min_speed)
        config_write.set(section, 'max_speed', self.max_speed)
        config_write.set(section, 'min_freq', self.min_freq)
        config_write.set(section, 'max_freq', self.max_freq)
        config_write.set(section, 'sauna_GPIO_port', self.sauna_GPIO_port)
        config_write.set(section, 'sensor4readings', self.sensor4readings)
        section = "Lamps_and_logging"
        config_write.set(section, 'lamps_control', self.lamps_control)
        config_write.set(section, 'latitude', self.latitude)
        config_write.set(section, 'longitude', self.longitude)

        # Writing our configuration file to 'self.config_filename'
        pr(self.debug, here,
           "ready to write new config file with default values: ",
           self.config_filename)
        with open(self.config_filename, 'w+') as configfile:
            config_write.write(configfile)
        return 0
Пример #9
0
    def write_file(self):
        here = "config.write_file"
        config_write = RawConfigParser()
        config_write.add_section('SetUp')
        config_write.set('SetUp', 'scan_delay', self.scan_delay)
        config_write.set('SetUp', 'max_scans', self.max_scans)
        config_write.set('SetUp', 'log_directory', self.log_directory)
        config_write.set('SetUp', 'local_dir_www', self.local_dir_www)
        config_write.set('SetUp', 'ftp_creds_filename',
                         self.ftp_creds_filename)
        config_write.set('SetUp', 'ftp_log_max_count', self.ftp_log_max_count)

        config_write.set('SetUp', 'ftp_timeout', self.ftp_timeout)
        # Writing our configuration file to 'c_filename'
        pr(self.dbug, here,
           "ready to write new config file withdefault values: ",
           self.config_filename)
        with open(self.config_filename, 'w+') as configfile:
            config_write.write(configfile)
        return 0
Пример #10
0
def calc_information_for_layer_with_other(data, bins, unique_inverse_x,
                                          unique_inverse_y, label, b, b1,
                                          len_unique_a, pxs, p_YgX, pys1, e, l,
                                          percent_of_sampling=50):
    pr('Calculating MI for SNAP {} LAYER {}...'.format(e + 1, l + 1))
    IXT, ITY = calc_information_sampling(data, bins, pys1, pxs, label, b, b1,
                                         len_unique_a, p_YgX, unique_inverse_x,
                                         unique_inverse_y)
    number_of_indexs = int(data.shape[1] * (1. / 100 * percent_of_sampling))
    indexs_of_sampls = np.random.choice(data.shape[1], number_of_indexs,
                                        replace=False)
    if percent_of_sampling != 100:
        sampled_data = data[:, indexs_of_sampls]
        sampled_IXT, sampled_ITY = calc_information_sampling(
            sampled_data, bins, pys1, pxs, label, b, b1,
            len_unique_a, p_YgX, unique_inverse_x, unique_inverse_y)

    params = {}
    params['ixt'] = IXT
    params['ity'] = ITY
    params['epoch'] = e
    params['layer'] = l
    return params
Пример #11
0
def run(gpu, epochs, snaps, batchsize, lr):
    train, test = chainer.datasets.get_mnist()
    xp = cp if gpu >=0 else np
    test_xs, test_ys = tuple2array(test, xp)
    size = 10000
    test_xs = test_xs[:size]; test_ys = test_ys[:size]

    bins = np.linspace(-1, 1, 30)
    Ts_index = np.array(np.linspace(0, 1, snaps) * epochs).astype(np.int)

    model = CNN()
    if gpu >= 0:
        model.to_gpu()

    train_iter = chainer.iterators.SerialIterator(train,
                                                  batch_size=batchsize)
    Ts_list = train_model(model, train_iter, test_xs, test_ys,
                          epochs, batchsize, Ts_index, lr)
    points = calc_mi(to_cpu(test_xs), to_cpu(test_ys)[:, None], Ts_list, bins)
    # points = calc_mmds(to_cpu(test_xs), to_cpu(test_ys)[:, None], Ts_list)
    feature = 'cnn_epoch{}_bs{}_lr{}'.format(epochs, batchsize, lr)
    plot_planes(points, Ts_index[:-1], feature)
    pr('')
Пример #12
0
    def write_file(self):
        here = "config.write_file"
        config_write = RawConfigParser()
        section = "Scan"
        config_write.add_section(section)
        config_write.set(section, 'scan_delay', self.scan_delay)
        config_write.set(section, 'max_scans', self.max_scans)
        section = "Log"
        config_write.add_section(section)
        config_write.set(section, 'log_directory', self.log_directory)
        config_write.set(section, 'local_dir_www', self.local_dir_www)
        config_write.set(section, 'log_buffer_flag', self.log_buffer_flag)
        config_write.set(section, 'text_buffer_length',
                         self.text_buffer_length)
        section = "Ftp"
        config_write.add_section(section)
        config_write.set(section, 'ftp_creds_filename',
                         self.ftp_creds_filename)
        config_write.set(section, 'ftp_log_max_count', self.ftp_log_max_count)
        section = "Fan"
        config_write.add_section(section)
        config_write.set(section, 'max_temp', self.max_temp)
        config_write.set(section, 'min_temp', self.min_temp)
        config_write.set(section, 'min_speed', self.min_speed)
        config_write.set(section, 'max_speed', self.max_speed)
        config_write.set(section, 'min_freq', self.min_freq)
        config_write.set(section, 'max_freq', self.max_freq)
        config_write.set(section, 'brightness', self.max_freq)

        # Writing our configuration file to 'self.config_filename'
        pr(self.dbug, here,
           "ready to write new config file with default values: ",
           self.config_filename)
        with open(self.config_filename, 'w+') as configfile:
            config_write.write(configfile)
        return 0
Пример #13
0
    def write_file(self, new_data_count, new_file):
        # add a new record to the sensor file
        #global my_sensors
        #global config
        #global smartplug_info

        here = "mysensors.write_file"
        pr(self.dbug, here, "write_sensor_data will write : ", new_data_count)
        fields = ['number', 'code', 'location', 'stype', 'comment']
        # 'at' mode adds toimport sys, getopt end of the file and opens file as text
        if new_file:
            mode = 'wb'
        else:
            mode = 'at'
        if self.__s_filename == "":
            pr(True, here, "No Sensor File Name set", 1.234)
            sys_exit()
        try:
            with open(self.__s_filename, mode) as sensorcsv_file:
                writer = csv_DictWriter(sensorcsv_file, fieldnames=fields)
                if new_file:  # this is a blank file
                    writer.writeheader()  # new file needs headings.
                for line_count in range(self.width - new_data_count,
                                        self.width, 1):
                    writer.writerow({
                        'number': self.number[line_count],
                        'code': self.code[line_count],
                        'location': self.location[line_count],
                        'stype': self.stype[line_count],
                        'comment': self.comment[line_count]
                    })
        except:
            pr(True, here, "Error accessing the existing sensor info file",
               self.__s_filename)
            pr(True, here, "Try deleting file let prog make a new one",
               self.__s_filename)
            sys_exit()
        return
Пример #14
0
 def turn_off_smartplug(self, index):
     here = "turn_off_smartplug"
     cmd = self.commands["off"]
     # print("off called : ",index)
     try:
         if self.ip[index] == "not set":
             pr(self_dbug, here, "Smart Plug no ip address for Index : ",
                index)
         else:
             pr(self.dbug, here, "Smart Plug cmd and index : ",
                str(cmd) + " : " + str(self.ip[index]))
             responce = self.send_command(cmd, self.ip[index], 9999, False)
             pr(self.dbug, here, "Index, IP, Off responce : ",
                str(index) + " : " + self.ip[index] + " : " + responce)
             return responce
     except:
         return "error"
Пример #15
0
    def log_to_file(self, config, smartplug_info, target_temp, ref_sensor,
                    dbug_flag, smart_log_width):
        here = "log_temperature_data_to_file"
        #write the time at the start of the line in logging file

        if self.__log_count == 0:
            config.log_outfile.write("Time,")
            config.log_outfile.write("Count,")
            config.log_outfile.write("Target,")
            config.log_outfile.write("Ref Sensor,")
            if config.sauna:
                config.log_outfile.write("On,")
                config.log_outfile.write("Offset,")
                config.log_outfile.write("Detect Off")
                config.log_outfile.write("\n")
            else:
                for hdg_ind in range(1, (self.max_width + 1)):
                    config.log_outfile.write("T" + str(hdg_ind) + ",")
                config.log_outfile.write("I1,")
                config.log_outfile.write("V1,")
                config.log_outfile.write("P1,")
                config.log_outfile.write("T1,")
                config.log_outfile.write("I2,")
                config.log_outfile.write("V2,")
                config.log_outfile.write("P2,")
                config.log_outfile.write("T2,")
                config.log_outfile.write("\n")
        self.__log_count += 1
        logtime = datetime.now()
        logtime_text = (str(logtime.day).zfill(2) + "/" +
                        str(logtime.month).zfill(2) + "/" +
                        str(logtime.year).zfill(2) + " " +
                        str(logtime.hour).zfill(2) + ":" +
                        str(logtime.minute).zfill(2) + ":" +
                        str(logtime.second).zfill(2))
        config.log_outfile.write(logtime_text)
        if (self.sensor_present == False):
            config.log_outfile.write(" : no sensors with Trg Temp of : " +
                                     str(target_temp) + "\n")
        else:
            config.log_outfile.write("," + str(self.__log_count) + ",")
            config.log_outfile.write(str(target_temp) + ",")
            config.log_outfile.write(str(ref_sensor) + ",")
            if config.sauna:
                config.log_outfile.write(
                    str(config.percent_full_power * config.sauna_on) + ",")
                config.log_outfile.write(str(config.target_offset) + ",")
                config.log_outfile.write(str(config.detect_off_count))
            else:
                for z in range(0, self.max_width, 1):
                    #record the data last saved for this sensor
                    #send data to the file only if the sensor is connected
                    config.log_outfile.write(str(self.status_text[z]) + ",")
                    self.last_logged[z] = self.reading[z]
                    self.last_logged_error_number[z] = self.error_number[z]
                config.log_outfile.write(str(smartplug_info.current[0]))
                config.log_outfile.write("," + str(smartplug_info.voltage[0]))
                config.log_outfile.write("," + str(smartplug_info.power[0]))
                config.log_outfile.write("," + str(smartplug_info.total[0]))
                pr(dbug_flag, here, "Total power going to log file",
                   smartplug_info.total[0])
                #config.log_outfile.write("," + str(smartplug_info.error[1]))
                #config.log_outfile.write("," + str(smartplug_info.state[1]))
                config.log_outfile.write("," + str(smartplug_info.current[1]))
                config.log_outfile.write("," + str(smartplug_info.voltage[1]))
                config.log_outfile.write("," + str(smartplug_info.power[1]))
                config.log_outfile.write("," + str(smartplug_info.total[1]))
                pr(dbug_flag, here, "Total power going to log file",
                   smartplug_info.total[1])
                #config.log_outfile.write("," + str(smartplug_info.error[1]))
            config.log_outfile.write("\n")
            config.log_outfile.flush()
        return
Пример #16
0
def parse(data):
    res = [line.split(',') for line in data.split('\n')]
    return [[float(e) for e in line] for line in res]


getfloat = lambda msg: float(input(msg))
T1 = getfloat('Temp 1:')
T2 = getfloat('Temp 2:')
T = getfloat('Temp interp:')

data1, data2 = None, None
data = parse(data)
for l in data:
    if l[0] == T1:
        data1 = l[1:]
    elif l[0] == T2:
        data2 = l[1:]
del data

if data1 is None or data2 is None:
    print('T1 or T2 not in data')
    exit()

interpdata = []
for i in range(len(cols)):
    val = interp(T, T1, T2, data1[i], data2[i])
    interpdata.append(val * 10**(-exponent[i]))

pr(dict(zip(cols, interpdata)))
Пример #17
0
    def write_file(self):
        here = "config.write_file"
        config_write = RawConfigParser()
        section = "Debug"
        config_write.add_section(section)
        config_write.set(section, 'debug_reread_config',
                         self.debug_reread_config)
        config_write.set(section, 'debug_flag_1', self.debug_flag_1)
        config_write.set(section, 'debug_flag_2', self.debug_flag_2)
        config_write.set(section, 'debug_flag_ftp', self.debug_flag_ftp)
        section = "Scan"
        config_write.add_section(section)
        config_write.set(section, 'scan_delay', self.scan_delay)
        config_write.set(section, 'max_scans', self.max_scans)
        section = "Log"
        config_write.add_section(section)
        config_write.set(section, 'log_directory', self.log_directory)
        config_write.set(section, 'local_dir_www', self.local_dir_www)
        config_write.set(section, 'log_buffer_flag', self.log_buffer_flag)
        config_write.set(section, 'text_buffer_length',
                         self.text_buffer_length)
        section = "Ftp"
        config_write.add_section(section)
        config_write.set(section, 'ftp_creds_filename',
                         self.ftp_creds_filename)
        config_write.set(section, 'ftp_log_max_count', self.ftp_log_max_count)
        section = "Heating_Fan"
        config_write.add_section(section)
        config_write.set(section, 'heat_max_temp', self.heat_max_temp)
        config_write.set(section, 'heat_min_temp', self.heat_min_temp)
        config_write.set(section, 'heat_max_speed', self.heat_max_speed)
        config_write.set(section, 'heat_min_speed', self.heat_min_speed)
        config_write.set(section, 'heat_max_freq', self.heat_max_freq)
        config_write.set(section, 'heat_min_freq', self.heat_min_freq)
        section = "Sauna"
        config_write.add_section(section)
        config_write.set(section, 'sauna_max_temp', self.sauna_max_temp)
        config_write.set(section, 'sauna_min_temp', self.sauna_min_temp)
        config_write.set(section, 'sauna_max_speed', self.sauna_max_speed)
        config_write.set(section, 'sauna_min_speed', self.sauna_min_speed)
        config_write.set(section, 'sauna_max_freq', self.sauna_max_freq)
        config_write.set(section, 'sauna_min_freq', self.sauna_min_freq)
        config_write.set(section, 'sauna_GPIO_port', self.sauna_GPIO_port)
        config_write.set(section, 'sauna_brightness', self.sauna_brightness)
        section = "Power_Log"
        config_write.add_section(section)
        config_write.set(section, 'adc_scan_size', self.adc_scan_size)
        config_write.set(section, 'adc_target_scan_msec',
                         self.adc_target_scan_msec)
        config_write.set(section, 'adc_channel', self.adc_channel)
        config_write.set(section, 'adc_default_gain ', self.adc_default_gain)
        config_write.set(section, 'adc_top_limit', self.adc_top_limit)
        config_write.set(section, 'adc_bottom_limit', self.adc_bottom_limit)
        config_write.set(section, 'adc_input_offset_mv',
                         self.adc_input_offset_mv)
        config_write.set(section, 'adc_input_amp_gain',
                         self.adc_input_amp_gain)
        config_write.set(section, 'adc_CT_ratio', self.adc_CT_ratio)
        config_write.set(section, 'adc_CT_resister', self.adc_CT_resister)

        # Writing our configuration file to 'self.config_filename'
        pr(self.debug_flag_1, here,
           "ready to write new config file with default values: ",
           self.config_filename)
        with open(self.config_filename, 'w+') as configfile:
            config_write.write(configfile)
        return 0
Пример #18
0
    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
Пример #19
0
 def get_smartplug_status(self, dbug_flag, config):
     here = "get_smartplug_status"
     read_cmd = self.commands["read"]
     info_cmd = self.commands["info"]
     read_results = ["not set"] * config.number_heaters
     info_results = ["not set"] * config.number_heaters
     self.get_status_error_count = 0
     for index in range(0, config.number_heaters, 1):
         # print("###### getting status index :",index, "config.number_heaters : ",config.number_heaters)
         if self.ip[index] == "not set":
             pr(dbug_flag, here, "Smart Plug no ip address for Index : ",
                index)
         else:
             read_results[index] = self.send_command(
                 read_cmd, self.ip[index], 9999, dbug_flag)
             if read_results[index] == "error":
                 pr(True, here,
                    "Error connecting for read index: " + str(index),
                    " IP : " + self.ip[index])
                 read_results[index] = "error"
                 self.get_status_error_count += 1
             else:
                 self.do_read_time_taken = self.calculate_time("Do Read")
                 ##pr(True,here,"Result for Read Index : " + str(index), read_results[index])
                 pr(dbug_flag, here,
                    "Result for Read Index : " + str(index),
                    read_results[index])
                 self.read_received[index] = read_results[index]
                 self.current[index] = self.get_json(
                     read_results[index], "current")
                 self.voltage[index] = self.get_json(
                     read_results[index], "voltage")
                 self.power[index] = self.get_json(
                     read_results[index],
                     "power") * self.heater_power_scale[index]
                 self.total[index] = self.get_json(read_results[index],
                                                   "total")
             info_results[index] = self.send_command(
                 info_cmd, self.ip[index], 9999, dbug_flag)
             if info_results[index] == "error":
                 pr(True, here,
                    "Error connecting for info index: " + str(index),
                    " IP : " + self.ip[index])
                 info_results[index] = "error"
                 self.get_status_error_count += 1
             else:
                 self.do_info_time_taken = self.calculate_time("Do Info")
                 pr(dbug_flag, here,
                    "Result for Info Index : " + str(index),
                    info_results[index])
                 ##pr(True,here,"Result for Info Index : " + str(index), info_results[index])
                 self.state[index] = self.get_json(info_results[index],
                                                   "relay_state")
                 self.error[index] = self.get_json(info_results[index],
                                                   "err_code")
                 pr(
                     dbug_flag, here,
                     "Relay, Volts, Amps, Power, Total Power & Error Code for Plug : "
                     + str(self.ip[index]),
                     str(self.state[index]) + " : " +
                     str(self.voltage[index]) + " : " +
                     str(self.current[index]) + " : " +
                     str(self.power[index]) + " : " +
                     str(self.total[index]) + " : " +
                     str(self.error[index]))
                 self.sent[
                     index] = " Read: " + read_cmd + " : Info: " + info_cmd
                 self.info_received[index] = info_results[index]
     if self.get_status_error_count == 0:
         return "Got Status OK"
     else:
         return "error"
Пример #20
0
    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
Пример #21
0
    def get_temps(self):
        # check which sensors are connected and update relavant flags
        # build a list of all the new sensors and then add them to the data in use
        # note :  requires that my_sensors variable is populated
        #global my_sensors
        #global smartplug_info
        #global config
        here = "get_temps"
        # Look for what sensors are connected
        sensors = W1ThermSensor.get_available_sensors()
        connected_codes = ["nocodeyet"] * len(sensors)
        connected_temp = [-100] * len(sensors)

        pr(self.dbug, here, " number seen", len(sensors))

        ind = 0

        for individual_sensor in W1ThermSensor.get_available_sensors():
            #follwoing does not work so commented out
            #try:
            #individual_sensor.set_precision(config.precision)
            #except:
            #	pr(self.dbug,here, "Cannot Change Precision" , str(individual_sensor.id) )
            connected_codes[ind] = individual_sensor.id
            try:
                connected_temp[ind] = individual_sensor.get_temperature()
            except:
                pr(self.dbug, here, "Error Reading scanned item",
                   str(ind) + " : " + individual_sensor.id)
                connected_temp[ind] = -100
            pr(
                self.dbug, here, "Scan of seen sensors",
                str(ind) + " : " + connected_codes[ind] + " : " +
                str(connected_temp[ind]))
            ind += 1

        self.number_seen = ind

        if len(sensors) > 0:
            self.sensor_present = True
            pr(self.dbug, here,
               "Sensor present Set True with count equal to : ",
               str(len(connected_codes)))
        else:
            pr(self.dbug, here,
               "Sensor present not Set with count equal to : ",
               str(len(connected_codes)))
            self.sensor_present = False

        new_codes = []
        self.ref_sensor_index = -1
        for my_sensor_count in range(0, len(self.code)):
            self.code_seen[my_sensor_count] = False
            self.reading[my_sensor_count] = -100  # signal that no data seen
            self.error_number[my_sensor_count] += 1
            for seen_count in range(0, self.number_seen):
                if connected_codes[seen_count] == self.code[my_sensor_count]:
                    # set flag to indicate has been seen during this run of program
                    pr(
                        self.dbug, here, "Scanning My Count, seen count ",
                        str(my_sensor_count) + " :" + str(seen_count) + " : " +
                        str(connected_temp[seen_count]))
                    self.code_seen[my_sensor_count] = True
                    self.connected[my_sensor_count] = True
                    self.error_number[
                        my_sensor_count] = 0  # signal that its not in error
                    if connected_temp[seen_count] != -100:
                        # codeget_temps there but not ready to be read
                        self.reading[my_sensor_count] = connected_temp[
                            seen_count]  # copy across id temp found
                    else:
                        self.reading[my_sensor_count] = -100
                        self.code_seen[my_sensor_count] = False
                        self.error_number[my_sensor_count] = 1
                    if self.code[my_sensor_count] == self.sensor4readings:
                        self.ref_sensor_index = my_sensor_count
                    self.code_seen_but_disconnected[my_sensor_count] = False
        count_connected = 0
        new_codes = []
        for element in connected_codes:
            if not element in self.code:
                new_codes.append(element)
            count_connected += 1
        if len(new_codes) > 0:
            pr(self.dbug, here, str(len(new_codes)), " new sensors found")
            new_ind = 0  # index for new_codes
            start_entering_data = self.width
            end_at = self.width + len(new_codes)
            self.width = end_at  # the number sensors whose data stored and pointer where put next
            for ind in range(start_entering_data, end_at):
                if ind > self.max_width:
                    print("Too Many sensors, new ones not added")
                    break
                #if len(self.number) > 0:
                #	self.number.append("n" + str(count_connected-len(new_codes)+ind+1))
                #else:
                #	self.number.append("n" + str(1))
                print("for debug;   ind : ", ind)
                self.number[ind] = "nxx"
                self.code[ind] = new_codes[new_ind]
                self.connected[ind] = True
                self.reading[ind] = -102
                self.last_logged[ind] = -102
                self.code_seen[ind] = True
                self.code_seen_but_disconnected[ind] = False
                self.location[ind] = "Lxx"
                self.stype[ind] = "Txx"
                self.comment[ind] = "Ccc"
                self.delay[ind] = 0
                self.error_number[ind] = 2
                self.status_text[ind] = "New"
                self.last_logged_error_number[ind] = 2
                new_ind += 1
        else:
            pr(self.dbug, here, "no new codes, still only : ",
               str(count_connected) + " connected")
        return (len(new_codes))
Пример #22
0
    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)