def set_create_time(self, create_time): self.create_time = create_time if self.create_time != None: self.create_ts = TimeStamp() self.create_ts.from_secs(create_time) else: self.create_ts = None
def set_mod_time(self, mod_time): self.mod_time = mod_time if self.mod_time != None: self.mod_ts = TimeStamp() self.mod_ts.from_secs(mod_time) else: self.mod_ts = None
def set_disk_time(self, disk_time): self.disk_time = disk_time if self.disk_time != None: self.disk_ts = TimeStamp() self.disk_ts.from_secs(disk_time) else: self.disk_ts = None
def newTimeStamp(old=None, TimeStamp=TimeStamp.TimeStamp, time=time.time, gmtime=time.gmtime): t = time() ts = TimeStamp(gmtime(t)[:5]+(t%60,)) if old is not None: return ts.laterThan(old) return ts
def logValues(): if count == len(sensors) - 1: log.temp(str(sensors[count].temp) + ";\n") else: log.temp(str(sensors[count].temp) + ";") log.prnt(TS.most(1) + ". " + sensors[count].name + ", homersekleti erteke: " + "{:.3f}".format( sensors[count].temp / float(1000)))
# GPIO.output(id,state) # # GPIO.output(id,False); # time.sleep(2) # GPIO.output(id,True) return True except: return False def mizu(id): try: # GPIO.setwarnings(False); # GPIO.setmode(GPIO.BCM); # GPIO.setup(id,GPIO.OUT); # state = GPIO.input(id) return True except: return str("lofasz") if __name__ == '__main__': #time.sleep(5) id = 17 if kapcs(id, True) == 1: print(TS.most(1) + "Rele kikapcsolva") else: print( TS.most(1) + "Gaz van, relet nem sikerult kikapcsolni, pedig megprobaltam")
class MetaInfo: def __init__(self, protect=None, mod_ts=None, comment=None): self.set_protect(protect) self.set_mod_ts(mod_ts) self.set_comment(comment) def __str__(self): res = [] res.append(self.get_protect_str()) res.append(self.get_time_str()) res.append(self.get_comment_str()) return " ".join(res) def get_time_str(self): if self.mod_ts != None: return str(self.mod_ts) else: return ts_empty_string def get_protect_str(self): if self.protect_flags != None: return str(self.protect_flags) else: return ProtectFlags.empty_string def get_comment_str(self): if self.comment != None: return self.comment else: return "" def set_protect(self, protect): self.protect = protect if self.protect != None: self.protect_flags = ProtectFlags(protect) else: self.protect_flags = None def set_default_protect(self): self.protect = 0 self.protect_flags = ProtectFlags(self.protect) def set_current_time(self): mod_time = time.mktime(time.localtime()) self.set_mod_time(mod_time) def set_mod_time(self, mod_time): self.mod_time = mod_time if self.mod_time != None: self.mod_ts = TimeStamp() self.mod_ts.from_secs(mod_time) else: self.mod_ts = None def set_mod_ts(self, mod_ts): self.mod_ts = mod_ts if self.mod_ts != None: self.mod_time = self.mod_ts.get_secsf() else: self.mod_time = None def set_comment(self, comment): self.comment = comment def get_protect(self): return self.protect def get_protect_flags(self): return self.protect_flags def get_mod_time(self): return self.mod_time def get_mod_ts(self): return self.mod_ts def get_comment(self): return self.comment
sensor = Sensor() sensor.id = '28-0417c2d68cff' sensor.name = "Medence (1)" sensor.temp = GetTemp.gettemp(sensor.id) sensors.append(sensor) sensor = Sensor() sensor.id = '28-0417c3b93dff' sensor.name = "Panel (1)" sensor.temp = GetTemp.gettemp(sensor.id) sensors.append(sensor) count = 0 while count < len(sensors): log.prnt( TS.most(1) + sensors[count].name + " szenzor inicializálva, hőmérsékleti értéke: " + "{:.3f}".format(sensors[count].temp / float(1000))) count += 1 # count = 0 # while count < 2: # id = "28-0417c2d68cff" # sensor_new = Sensor(id,"ciklusbol_generalt %s" %(count),GetTemp.gettemp(id)) # sensors.append(sensor_new) # count += 1 #print(sensor[0]name) # id = '28-0417c2d68cff' # mytemp_1 = GetTemp.gettemp(id);
def main(): """Main body.""" global timestamp_pattern # capture timing information cputime_0 = psutil.cpu_times() # Self-identification for the run # This gives us YYYY-MM-DDTHH:MM:SS+HH:MM ts0 = ts.TimeStamp() timestamp_pattern = ts0.get_recognizer_re() print "# analyze_pings.py" print "# analyze_pings.py: start: timestamp: " + ts0.get_timestamp() parser = argparse.ArgumentParser(description='Analyze a ping log') parser.add_argument('-f', nargs='?',\ default='stdin', help="input file name") parser.add_argument('-v', nargs='?', default='command line',\ help="git information about build state") parser.add_argument('-D', type=int, nargs='?',\ default=0, help="Debug flag (int: default to 0)") args = parser.parse_args() input_file_name = args.f build_version = args.v print "# analyze_pings.py: build version:" + build_version print "# analyze_pings.py: input_file_name: " + input_file_name line_queue = LineQueue(4, input_file_name) # LineQueue returns a comment-structured self identification print line_queue.signature() # Initialize the counters classifications = [ "Comment", "Down", "GWFailure", "Initialization", "NegativeRTT", "Normal", "Route", "RTTTooLong", "Timeout", "Timestamp", "Unexpected"] down_classifications = [ "Down", "GWFailure", "NegativeRTT", "Route", "RTTTooLong", "Timeout" ] counters = {} for key in classifications: counters[key] = 0 linecount = 0 line = line_queue.get_line() recent_num = 0 # variables used for online mean and standard deviation current = {} previous = {} previous['rtt'] = None current['rtt'] = None previous['mean'] = 0.0 current['mean'] = 0.0 previous['variance'] = 0.0 current['variance'] = 0.0 normal_ping_count = 0 sequence_number = -1 sequence_offset = 0 # duration and state variables network_state = "None" up_start = -1 up_end = -1 down_start = -1 down_end = -1 previous['time'] = "unknown" current['time'] = "unknown" previous['timestamp'] = None current['timestamp'] = None previous['sequence'] = sequence_number current['sequence'] = sequence_number current['linenumber'] = linecount # reference_linenumber = linecount rtt_stats = None zrtt = None explanation = "" while line: linecount += 1 # print "linecount: '" + str(linecount) # print "line: '" + line.strip() + "'" # TODO threshold should be dynamically calculated (kind, seq_num) = classify(line_queue, line.strip(), linecount, 250) # print " kind: " + kind if kind: counters[kind] += 1 if kind == "Timestamp": # oops - seq_num is not a number, it's a string! previous['time'] = current['time'] previous['timestamp'] = current['timestamp'] previous['sequence'] = current['sequence'] # current['time'] = seq_num current['timestamp'] = ts.TimeStamp(current['time']) current['sequence'] = sequence_number current['linenumber'] = linecount # # if previous_time != "unknown": if previous['time'] != "unknown": delta_t = current['timestamp'].minus_small(\ previous['timestamp']) delta_r = current['sequence'] - previous['sequence'] print "# time check: delta_r: " + str(delta_r) +\ " delta_t: " + str(delta_t) elif kind == "Initialization": pass elif kind == "Normal": (ip, num, rtt) = \ parse_normal_return(line.strip(), linecount) # result is a tuple of strings: # (ip_address, sequence_number, rtt) # ping sends pings once per second, so sequence_number # is roughly a count of seconds. if not zrtt: rtt_stats = ss.SequenceStats(float(rtt), False) current['mean'] = float(rtt) zrtt = float(rtt) inum = int(num) if inum == 0 and network_state != "None": # The sequence number only goes to 65535, so we # will keep track of the rolls sequence_offset += 65536 sequence_number = inum + sequence_offset # Handle network state stuff if network_state == "None": up_start = sequence_number up_end = sequence_number # print "Network state initialization: Up" # print " sequence_number: " + str(sequence_number) elif network_state == "Down": down_end = sequence_number - 1 up_start = sequence_number up_end = sequence_number # print "Down: " + str(down_start) +\ " - " + str(down_end) + \ "[ " + str(down_end - down_start - 1) + " ]" if explanation == "RTTTooLong": explanation += " RTT: " + str(zrtt) print " explanation: " + explanation if current['time'] != "unknown": print " current['time']: " +\ str(current['time']) print " plus (~seconds): " +\ str(sequence_number - current['sequence']) # print "linecount: " + str(linecount) else: up_end = sequence_number network_state = "Up" # We use the online algorithm documented in Wikipedia article: # https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance # Use the new stats class to accumulate the data rtt_stats.accumulate(zrtt) normal_ping_count += 1 if previous['rtt'] > 0.0: temp = current['mean'] current['mean'] += \ (zrtt - previous['mean']) /\ float(normal_ping_count) previous['mean'] = temp previous['rtt'] = zrtt # previous_rtt = zrtt # This works because the first time through # previous_variance is zero # this is the population variance temp = current['variance'] current['variance'] = \ ( (normal_ping_count - 1) * previous['variance'] + \ (zrtt - previous['mean']) * \ (zrtt - current['mean']) ) / normal_ping_count previous['variance'] = temp elif kind in down_classifications: # Handle network state stuff explanation = kind if network_state == "None": down_start = sequence_number down_end = sequence_number # print "Network state initialization: Down" # print " sequence_number: " + str(sequence_number) elif network_state == "Up": up_end = sequence_number - 1 down_start = sequence_number down_end = sequence_number # print "Up: " + str(up_start) +\ " - " + str(up_end) + \ " [ " + str(up_end - up_start - 1) + " ]" if current['time'] != "unknown": print " current['time']: " +\ str(current['time']) print " plus (~seconds): " +\ str(sequence_number - current['sequence']) else: down_end = sequence_number network_state = "Down" # print "kind: " + kind elif kind == "Comment": pass else: # Failed to classify: print "Failed to classify:" print " kind: " + kind print " linecount: " + str(linecount) print " line: '" + line.strip() + "'" counters["Unexpected"] += 1 line = line_queue.get_line() print "linecount", linecount for key in classifications: print key + ": " + str(counters[key]) print "sequence_number: " + str(sequence_number) print "sequence_offset: " + str(sequence_offset) print "normal_ping_count: " + str(normal_ping_count) print "Mean: " + str(current['mean']) print "Mean RTT (two ways): " + str(rtt_stats.get_mean()) print "Variance: " + str(current['variance']) print "Variance RTT (two ways): " + str(rtt_stats.get_variance()) print "rtt_stats: " + str(rtt_stats) checksum = linecount for key in classifications: checksum -= counters[key] print "checksum: " + str(checksum) cputime_1 = psutil.cpu_times() print # index 0 is user # index 1 is nice # index 2 is system # index 3 is idle ts1 = ts.TimeStamp() print "# analyze_pings.py: end: timestamp: " + ts1.get_timestamp() print "# analyze_pings.py: User time: " +\ str(cputime_1[0] - cputime_0[0]) + " S" print "# analyze_pings.py: User time per record: " +\ str(1e6 * (cputime_1[0] - cputime_0[0]) / linecount) +\ " uS" print "# analyze_pings.py: System time: " +\ str(cputime_1[2] - cputime_0[2]) + " S" print "# analyze_pings.py: System time per record: " +\ str(1e6 * (cputime_1[2] - cputime_0[2]) / linecount) +\ " uS"
from model import Relay if __name__ == '__main__': time_start=10 time_stop=18 sleep_timer = 30 dif_temp_be = 15000 dif_temp_ki = 5000 # Script has been called directly log.prnt(TS.most(1) + "---------------Rendszerinditas------------------") #Init sensors log.prnt(TS.most(1) + ". Szenzorok inicializalasa...") global sensors=[] medence = Sensor() medence.id = '28-0417c2d68cff' medence.name = "Medence (1)" medence.temp = GetTemp.gettemp(medence.id); # sensors.append(medence) sensor1 = Sensor()
class MetaInfo: def __init__(self, protect=None, mod_ts=None, comment=None, protect_flags=None): if protect_flags != None: self.set_protect_flags(protect_flags) else: self.set_protect(protect) self.set_mod_ts(mod_ts) self.set_comment(comment) def get_str_line(self): """Return a unicode string with protect flags, mod time and (optional) comment""" res = [] res.append(self.get_protect_str()) res.append(self.get_mod_time_str()) comment = self.get_comment() if comment == None: res.append(u'') else: res.append(self.get_comment().get_unicode()) return u' '.join(res) def get_mod_time_str(self): if self.mod_ts != None: return str(self.mod_ts) else: return ts_empty_string def get_protect_str(self): if self.protect_flags != None: return str(self.protect_flags) else: return ProtectFlags.empty_string def get_protect_short_str(self): if self.protect_flags != None: return self.protect_flags.short_str() else: return "" def set_protect(self, protect): self.protect = protect if self.protect != None: self.protect_flags = ProtectFlags(protect) else: self.protect_flags = None def set_protect_flags(self, pf): self.protect_flags = pf self.protect = pf.mask def set_default_protect(self): self.protect = 0 self.protect_flags = ProtectFlags(self.protect) def set_current_as_mod_time(self): mod_time = time.mktime(time.localtime()) self.set_mod_time(mod_time) def set_mod_time(self, mod_time): self.mod_time = mod_time if self.mod_time != None: self.mod_ts = TimeStamp() self.mod_ts.from_secs(mod_time) else: self.mod_ts = None def set_mod_ts(self, mod_ts): self.mod_ts = mod_ts if self.mod_ts != None: self.mod_time = self.mod_ts.get_secsf() else: self.mod_time = None def set_comment(self, comment): """Set comment as a FSString""" if comment != None and not isinstance(comment, FSString): raise ValueError("Comment must be a FSString") self.comment = comment def get_protect(self): return self.protect def get_protect_flags(self): return self.protect_flags def get_mod_time(self): return self.mod_time def get_mod_ts(self): return self.mod_ts def get_comment(self): return self.comment def get_comment_ami_str(self): if self.comment != None: return self.comment.get_ami_str() else: return "" def get_comment_unicode_str(self): if self.comment != None: return self.comment.get_unicode() else: return u""
class RootMetaInfo: def __init__(self, create_ts=None, disk_ts=None, mod_ts=None): self.set_create_ts(create_ts) self.set_disk_ts(disk_ts) self.set_mod_ts(mod_ts) def __str__(self): res = [] res.append(self.get_create_time_str()) res.append(self.get_disk_time_str()) res.append(self.get_mod_time_str()) return " ".join(res) # create_ts def set_create_time(self, create_time): self.create_time = create_time if self.create_time != None: self.create_ts = TimeStamp() self.create_ts.from_secs(create_time) else: self.create_ts = None def set_create_ts(self, create_ts): self.create_ts = create_ts if self.create_ts != None: self.create_time = self.create_ts.get_secsf() else: self.create_time = None def get_create_time(self): return self.create_time def get_create_ts(self): return self.create_ts def get_create_time_str(self): if self.create_ts != None: return str(self.create_ts) else: return ts_empty_string def set_current_as_create_time(self): create_time = time.mktime(time.localtime()) self.set_create_time(create_time) # disk_ts def set_disk_time(self, disk_time): self.disk_time = disk_time if self.disk_time != None: self.disk_ts = TimeStamp() self.disk_ts.from_secs(disk_time) else: self.disk_ts = None def set_disk_ts(self, disk_ts): self.disk_ts = disk_ts if self.disk_ts != None: self.disk_time = self.disk_ts.get_secsf() else: self.disk_time = None def get_disk_time(self): return self.disk_time def get_disk_ts(self): return self.disk_ts def get_disk_time_str(self): if self.disk_ts != None: return str(self.disk_ts) else: return ts_empty_string def set_current_as_disk_time(self): disk_time = time.mktime(time.localtime()) self.set_disk_time(disk_time) # mod_ts def set_mod_time(self, mod_time): self.mod_time = mod_time if self.mod_time != None: self.mod_ts = TimeStamp() self.mod_ts.from_secs(mod_time) else: self.mod_ts = None def set_mod_ts(self, mod_ts): self.mod_ts = mod_ts if self.mod_ts != None: self.mod_time = self.mod_ts.get_secsf() else: self.mod_time = None def get_mod_time(self): return self.mod_time def get_mod_ts(self): return self.mod_ts def get_mod_time_str(self): if self.mod_ts != None: return str(self.mod_ts) else: return ts_empty_string def set_current_as_mod_time(self): mod_time = time.mktime(time.localtime()) self.set_mod_time(mod_time)
def main(): """Main body.""" # capture timing information cputime_0 = psutil.cpu_times() # Self-identification for the run # This gives us YYYY-MM-DDTHH:MM:SS+HH:MM ts0 = ts.TimeStamp() timestamp_pattern = ts0.get_recognizer_re() print "# SequenceStats.py" print "# SequenceStats.py: start: timestamp: " + ts0.get_timestamp() # Some data pulled from Matt Teachout's page # (http://www.matt-teachout.org/data-sets-for-stats.html) page # ANOVA Data / Sheet 1 / Column O (Social Media Minutes - Instagram) # Excel results: # N: 124 # Mean: 83.20564516 # Variance(P): 6803.274242 # Variance(S): 6858.585415 excel_mean = 83.20564516 excel_variance_p = 6803.274242 excel_variance_s = 6858.585415 data1 = [\ 300, 60, 60, 30, 45, 60, 100, 120, 45, 30, 45, 120, 40, 10, 180, 90, 240, 60, 3, 30, 190, 5, 60, 60, 120, 5, 60, 45, 20, 120, 200, 180, 180, 35, 120, 120, 1.5, 120, 10, 120, 60, 2, 120, 30, 60, 60, 120, 60, 30, 15, 90, 2, 60, 120, 60, 30, 60, 25, 150, 90, 180, 20, 30, 3, 100, 60, 20, 60, 65, 120, 180, 60, 120, 180, 30, 60, 30, 60, 190, 300, 25, 60, 60, 120, 3, 200, 65, 2, 2, 2, 2, 2, 2, 2, 120, 4, 30, 1, 120, 120, 120, 65, 60, 120, 1, 420, 90, 60, 60, 180, 300, 180, 500, 3, 3, 80, 90, 80, 45, 5, 2, 120, 120, 120 ] n1 = len(data1) np_data1 = np.array(data1) np_mean1 = np.mean(np_data1) np_variance1 = np.var(np_data1) print "n1: " + str(n1) print "excel_mean: " + str(excel_mean) print "excel_variance_p: " + str(excel_variance_p) print "excel_variance_s: " + str(excel_variance_s) print print "np_mean1: " + str(np_mean1) print "np_variance1: " + str(np_variance1) print "np_variance (to sample): " +\ str(np_variance1 * float(n1) / float(n1 - 1)) # Now we run the incremental code print ss1 = SequenceStats(data1[0]) for j in range(1, 124): # print "accumulate(data1[" + str(j) + "]: " + str(data1[j]) ss1.accumulate(data1[j]) print "incremental mean1: " + str(ss1.get_mean()) print "incremental variance1: " + str(ss1.get_variance()) data2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] n2 = len(data2) print "n2: " + str(n2) np_data2 = np.array(data2) np_mean2 = np.mean(np_data2) np_variance2 = np.var(np_data2) print "np_mean2: " + str(np_mean2) print "np_variance2: " + str(np_variance2) print ss2 = SequenceStats(data2[0]) for j in range(1, 10): # print "accumulate(data2[" + str(j) + "]: " + str(data2[j]) ss2.accumulate(data2[j]) print "incremental mean2: " + str(ss2.get_mean()) print "incremental variance2: " + str(ss2.get_variance()) cputime_1 = psutil.cpu_times() print # index 0 is user # index 1 is nice # index 2 is system # index 3 is idle ts1 = ts.TimeStamp() print "# SequenceStats.py: end: timestamp: " + ts1.get_timestamp() print "# SequenceStats.py: User time: " +\ str(cputime_1[0] - cputime_0[0]) + " S" print "# SequenceStats.py: System time: " +\ str(cputime_1[2] - cputime_0[2]) + " S"
def __init__(self): self._os = OS.OS() self._ts = TimeStamp.TimeStamp() self._existing_log_file = "mocks/test.log"