class Elevator_tracker(object): def __init__(self, building, TRACKER_INTERVAL=1): self.building = building self.elevators = building.get_elevators() self.elevator_count = len(self.elevators) self.TRACKER_INTERVAL = TRACKER_INTERVAL self.visited_floors = [ x[:] for x in [[] * self.elevator_count] * self.elevator_count ] self.tracker_timer = RepeatedTimer(self.TRACKER_INTERVAL, self.get_elevators_floors) def start_tracking(self): self.tracker_timer.start() def end_tracking(self): self.tracker_timer.stop() return self.visited_floors def get_tracker_status(self): if self.tracker_timer.isRunning(): return "Running" else: return "Terminaed" def get_elevators_floors(self): for i in range(0, self.elevator_count): self.visited_floors[i].append( self.elevators[i].get_current_floor()) def save_result(self): print(self.visited_floors) with open('visited_floors.csv', 'a') as fp: a = csv.writer(fp, delimiter=',', lineterminator="\n") a.writerows(self.result) def plot_track(self): #plt.hold(True) fig = plt.figure() ax = fig.add_subplot(1, 1, 1) x_axis = numpy.arange( 0, len(self.visited_floors[0]) * self.TRACKER_INTERVAL, self.TRACKER_INTERVAL) labels = [] lines = [] for i in range(0, self.elevator_count): lines += plt.plot(x_axis, self.visited_floors[i], label='Elevator ' + str(i)) #plt.plot(x_axis, self.visited_floors[i]) # Plot decorations major_ticks = numpy.arange(0, self.building.get_floor_count(), 1) ax.set_yticks(major_ticks) labels = [l.get_label() for l in lines] plt.legend(lines, labels) plt.xlabel('Time [s]') plt.ylabel('Floor') plt.show()
def main(): camera_timer = RepeatedTimer(60, take_image) while (True): if get_sunrise() < datetime.now() < get_sunset(): camera_timer.start() else: camera_timer.stop() time.sleep(60)
class Elevator_tracker(object): def __init__(self, building, TRACKER_INTERVAL = 1): self.building = building self.elevators = building.get_elevators() self.elevator_count = len(self.elevators) self.TRACKER_INTERVAL = TRACKER_INTERVAL self.visited_floors = [x[:] for x in [[]*self.elevator_count]*self.elevator_count] self.tracker_timer = RepeatedTimer(self.TRACKER_INTERVAL, self.get_elevators_floors) def start_tracking(self): self.tracker_timer.start() def end_tracking(self): self.tracker_timer.stop() return self.visited_floors def get_tracker_status(self): if self.tracker_timer.isRunning(): return "Running" else: return "Terminaed" def get_elevators_floors(self): for i in range(0, self.elevator_count): self.visited_floors[i].append(self.elevators[i].get_current_floor()) def save_result(self): print (self.visited_floors) with open('visited_floors.csv', 'a') as fp: a = csv.writer(fp, delimiter=',', lineterminator="\n") a.writerows(self.result) def plot_track(self): #plt.hold(True) fig = plt.figure() ax = fig.add_subplot(1,1,1) x_axis = numpy.arange(0, len(self.visited_floors[0])*self.TRACKER_INTERVAL, self.TRACKER_INTERVAL) labels = [] lines = [] for i in range(0, self.elevator_count): lines += plt.plot(x_axis, self.visited_floors[i], label='Elevator '+ str(i)) #plt.plot(x_axis, self.visited_floors[i]) # Plot decorations major_ticks = numpy.arange(0, self.building.get_floor_count(), 1) ax.set_yticks(major_ticks) labels = [l.get_label() for l in lines] plt.legend(lines, labels) plt.xlabel('Time [s]') plt.ylabel('Floor') plt.show()
class CLI: conf = Configuration() disc = Discovery(conf.getNumberOfHosts(), conf.getSimilarResponses(), conf.getNumberOfHosts()) dns = None arp = Arp() PiIP = "" ARPresult = False thread = None mainText = "Enter the number of the method you want to use:\n\ 1. Pi-hole discovery\n\ 2. ARP Poisoning\n\ 3. DNS Poisoning\n\ 4. Exit\n" def mainCLI(self): while True: inp = input(self.mainText) if inp.lower().strip() == "1": # Discovery self.discoveryCLI() elif inp.lower().strip() == "2": # ARP Poisoning self.ARPCLI() elif inp.lower().strip() == "3": # DNS Poisoning self.DNSCLI() elif inp.lower().strip() == "4": # Exit print("Quitting...") if self.thread is not None: print(" Stopping ARP poisoning") self.thread.stop() sys.exit() else: # Error print("Please only enter a number 1-4\n") # =========================== Discovery ============================== def discoveryCLI(self): print("You are about to search for the Pi-hole with settings: ") print(" DnsQueryTimeout: {} ms".format(self.conf.getDNSQueryTimeout())) print(" SimilarResp: {}%".format(self.conf.getSimilarResponses())) print(" NumberOfHosts: {}".format(self.conf.getNumberOfHosts())) print(" DNSServer: {}".format(self.conf.getDNSsetting())) print(" HostsURL {}".format(self.conf.getHostsURL())) inp = input("Do you want to continue? (Y/n): ") if inp.lower().strip() == "y" or inp.lower().strip() == "yes" or len(inp.strip()) == 0: print("\n") self.PiIP = self.disc.getPi(self.conf.getDNSQueryTimeout(), self.conf.getDNSsetting()) if not self.PiIP == None: print("Pi-hole was found at " + self.PiIP + "\nYou can continue with ARP Poisoning") else: print("No Pi-hole was found") return elif inp.lower().strip() == "n" or inp.lower().strip() == "no": return else: print("Invalid answer, please answer Y or N\n") self.discoveryCLI() return # ================================= ARP ==================================== # For multi-threading def ARPPoisoning(self, setting): if len(setting) == 2: self.arp.poison_all(self.conf.getDNSsetting(), self.PiIP, self.arp.get_dns_mac(self.PiIP)) else: self.arp.poison_all(self.conf.getARPtarget(), self.PiIP, self.arp.get_dns_mac(self.PiIP)) def ARPCLI(self): if self.thread is None: if self.PiIP == "" or self.PiIP is None: print("IP of the Pi-hole was not set, please run Discovery first.") return print("You are about to initiate ARP poisoning with settings: ") print(" NetworkInterface: " + self.conf.getNetworkInterface()) setting = '{}'.format(self.conf.getARPtarget()) if len(setting) == 2: print(" ARPtargets: " + self.conf.getDNSsetting()) else: print(" ARPtargets: " + setting) print(" ARPdelay: {} sec".format(self.conf.getARPdelay())) inp = input("Do you want to continue? (Y/n): ") if inp.lower().strip() == "y" or inp.lower().strip() == "yes" or len(inp.strip()) == 0: # ARP poisoning, initial call # If target is all hosts on DNS server's subnet if len(setting) == 2: print("Performing poisoning on " + self.conf.getDNSsetting()) if self.arp.poison_all(self.conf.getDNSsetting(), self.PiIP, self.arp.get_dns_mac(self.PiIP)): self.ARPresult = True # Otherwise else: print("Performing poisoning on " + self.conf.getARPtarget()) if self.arp.poison_all(self.conf.getARPtarget(), self.PiIP, self.arp.get_dns_mac(self.PiIP)): self.ARPresult = True if self.ARPresult: print("Pi-hole was successfully poisoned") # ARP poisoning, threading self.thread = RepeatedTimer(self.conf.getARPdelay(), self.ARPPoisoning, setting) self.thread.start() self.mainText = "Enter the number of the method you want to use:\n\ 1. Pi-hole discovery\n\ 2. Stop ARP Poisoning\n\ 3. DNS Poisoning\n\ 4. Exit\n" return else: print("Poisoning was not successful. Please try again.") return elif inp.lower().strip() == "n" or inp.lower().strip() == "no": return else: print("Invalid answer, please answer Y or N\n") self.ARPCLI() return # ARP Poisoning already running else: inp = input("You are about to stop the ARP poisoning, are you sure? (Y/N)") if inp.lower().strip() == "y" or inp.lower().strip() == "yes" or len(inp.strip()) == 0: # Stop thread and restore ARP tables self.thread.stop() self.arp.restore_all(self.conf.getARPtarget(), self.PiIP, self.arp.get_dns_mac(self.PiIP)) self.thread = None print("ARP poisoning successfully stopped.") self.mainText = "Enter the number of the method you want to use:\n\ 1. Pi-hole discovery\n\ 2. ARP Poisoning\n\ 3. DNS Poisoning\n\ 4. Exit\n" return elif inp.lower().strip() == "n" or inp.lower().strip() == "no": print("Cancelling...") return else: print("Invalid answer, please answer Y or N\n") self.ARPCLI() return # ================================= DNS =================================== def DNSCLI(self): if not self.ARPresult: print("ARP Poisoning was not completed successfully, please do this first.") return print("You are about to replace DNS responses of the Pi-hole with settings: ") print(" PoisonType: {}".format(self.conf.getPoisonType())) print(" ReplaceIP: {}".format(self.conf.getReplaceIP())) print(" SpoofingTimeout: {}".format(self.conf.getSpoofingTimeout())) inp = input("Do you want to continue? (Y/n): ") if inp.lower().strip() == "y" or inp.lower().strip() == "yes" or len(inp.strip()) == 0: # Ask if we should run in verbose mode verbose = input("Do you want to run in verbose mode? (Y/n): ") if verbose.lower().strip() == "y" or verbose.lower().strip() == "yes" or len(verbose.strip()) == 0: self.dns = Dns(self.PiIP, self.conf.getReplaceIP(), self.conf.getPoisonType(), self.conf.getNetworkInterface(), True) else: self.dns = Dns(self.PiIP, self.conf.getReplaceIP(), self.conf.getPoisonType(), self.conf.getNetworkInterface(), False) print("\n") # Start spoofing self.dns.poison() elif inp.lower().strip() == "n" or inp.lower().strip() == "no": return else: print("Invalid answer, please answer Y or N\n") self.DNSCLI() return
class Sequencer: def __init__(self, sound, sequence=[0], bpm=120): self._sound = sound if len(sequence) > 0: self._sequence = sequence else: raise SequencerException("please provide a sequence as a list with length > 0.") self._repetitions = 0 self._sequence_index = 0 self._repetitions_played = 0 self._bpm = bpm self._interval = self.bpm_to_interval(self.bpm) self._timer = RepeatedTimer(self._interval, self.execute_sequence_index) @property def sound(self): return self._sound @sound.setter def sound(self, sound): self._sound = sound @property def sequence(self): return self._sequence @sequence.setter def sequence(self, sequence): if len(sequence) > 0: self._sequence = sequence else: raise SequencerException("please provide a sequence as a list with length > 0.") @property def bpm(self): return self._bpm @bpm.setter def bpm(self,bpm): self._bpm = bpm def bpm_to_interval(self, bpm): # translates to 1/16th notes return 15/bpm def play(self,repetitions): self._repetitions = repetitions self._timer.start() print(self._sound.path, "started") def stop(self): self._timer.stop() def execute_sequence_index(self): if(self.sequence[self._sequence_index] == 1): self._sound.play() print(self._sound.path,1) else: print(self._sound.path,0) self._sequence_index += 1 if self._sequence_index >= len(self._sequence): self._sequence_index = 0 self._repetitions_played += 1 if(self._repetitions_played == self._repetitions and self._repetitions != 0): print(self._sound.path, "sequence ended") self.stop() self._sound.wait_done()
class Elevator(object): 'An elevator' elevator_count = 0 def __init__(self, building, name, current_floor): self.name = name self.current_floor = current_floor self.building = building self.destination_floor = [] self.active_calls = [] self.SPEED = 1 # s / floor self.TIME_AT_FLOOR = 5 # s / floor Elevator.elevator_count += 1 self.run_timer = RepeatedTimer(self.SPEED, self.execute_call()) def start_elevator(self): self.run_timer.start() def get_name(self): return self.name def get_current_floor(self): return self.current_floor def get_destination_floor(self): return self.destination_floor def get_direction(self): if not self.destination_floor:# or len(self.destination_floor) == 0: return 0 else: return numpy.sign(self.destination_floor[0] - self.current_floor) def get_speed(self): return self.SPEED def get_time_to_floor(self, floor): distance_to_destination = abs(floor - self.get_current_floor()) time_to_destination = self.get_speed()*distance_to_destination return time_to_destination def set_current_floor(self, floor): self.current_floor = floor def set_destination_floor(self, floor, on_the_way = False): if self.current_floor != floor: if on_the_way: self.destination_floor = [floor] + self.destination_floor else: self.destination_floor.append(floor) def execute_call(self): ''' checks if there is a call to execute. if yes, run elevator ''' print('execute_call()') if self.active_calls: print("I got a call, Elevator " + str(self.name) + " will start!") self.run_timer.stop() self.run_elevator() def run_elevator(self): curret_call = self.active_calls[0] while self.current_floor != self.destination_floor[0]: new_floor = self.current_floor + self.get_direction() self.set_current_floor(new_floor) sleep(self.SPEED) self.stop_at_floor(curret_call) def stop_at_floor(self, call): floor = call.get_floor() print ("Elevator " + str(self.name) + " now at floor " + str(floor) + ".") sleep(self.TIME_AT_FLOOR) self.destination_floor = [x for x in self.destination_floor if x != floor] call.get_selected_floor() self.run_timer.start() def recieve_call(self, call): print ("Elevator " + str(self.name) + "has recieved a call.") self.active_calls.append(call) self.set_destination_floor(call.get_floor(), call.get_on_the_way()) def stop_elevator(self): print ("Elevator to stop: " + str(self.name)) if self.get_elevator_status() == "Running": self.run_timer.stop() def get_elevator_status(self): if self.run_timer.isRunning(): return "Running" else: return "Terminated"