def run(self): while self.active and self.value < 15000: self.consume() sleep(1) def shutdown(self): self.active = 0 if __name__ == '__main__': print('Starting simulator...') var = input('Number of units to simulate: ') if var.isdigit(): NUM_UNITS = int(var) else: print('Input not recognized as digits, using default value (1) instead.') nm = NetworkManager() nm.open_server_socket() print('Listening for incoming connections on port {} at address {}'.format(str(nm.port), nm.local_host)) meter_units = [] print('Starting {NU} meter units'.format(NU=NUM_UNITS)) for x in range(0, NUM_UNITS): meter_units.append(MeterUnit(x, 'thread_name', x)) for mu in meter_units: mu.setDaemon(1) mu.start() print('Simulator is running with {NU} units(threads), waiting for requests...'.format(NU=NUM_UNITS)) while True: try: client_socket, address = nm.accept_connection() if client_socket is 0: continue
def read_file(file): res = [] with open(file, newline='') as csvfile: reader = csv.reader(csvfile) for row in reader: if row[1].isdigit(): res.append(int(row[1])) return res if __name__ == '__main__': logging.basicConfig(level=logging.INFO) print('Welcome to the Control Unit, please wait a moment!') logging.info("Logging level is " + logging.getLevelName(logging.getLogger().getEffectiveLevel())) nm = NetworkManager() logging.info("Remote host is " + nm.remote_host) user_choice = '' while alive: choice = input('Please select option:\n' '1. Scan MBus for units.\n' '2. Request data from one unit.\n' '3. Ping one unit.\n' '4. Get addresses from file.\n' '5. Switch between sim/live.\n' '6. Collect data from 1 to 99.\n' '7. Set debugging level.\n' '8. Analyse last response.\n' '0. Exit.\n' ': ') if choice in ('1', 'scan', 's'):