def request_data(address): """ Send REQ_UD2 to (address), store the response in the database. :param address: int """ if address > 250: ping(address) print('Waiting for response from meter...') for n in range(11, 1, -1): print('{}..'.format(n)) sleep(1) mbus_response = nm.send(MBus.req_ud2(address)) else: mbus_response = nm.send(MBus.req_ud2(address)) print('Sent: {}'.format(MBus.pretty_hex(MBus.req_ud2(address)))) print('Received: {}'.format(MBus.pretty_hex(mbus_response))) if mbus_response: mbus_response = MBus.parse_telegram(mbus_response) logging.debug(MBus.pretty_print(mbus_response)) print('Storing stuff in database...') open_and_store(mbus_response) return mbus_response else: print('Sent request to {}, but did not get a response.'.format(address)) return
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 telegram = client_socket.recv(1024) while telegram: mbt = MBus.parse_telegram(telegram) print('Received: {t} from {src}'.format(t=mbt, src=(str(address)))) a = int(mbt.fields['address'], 16) if mbt.type == 'SND_NKE': if 0 <= a < len(meter_units): print('Responded with E5\n') client_socket.sendall(MBus.ACK) elif mbt.type == 'SND_UD': client_socket.sendall(MBus.ACK) elif mbt.type == 'REQ_UD2': if 0 <= a < len(meter_units): v = meter_units[a].get_value() response = MBus.rsp_ud(v, a) client_socket.sendall(response) telegram = client_socket.recv(1024) except ConnectionResetError:
def send_custom(t): response = nm.send(bytes.fromhex(t)) if response is None: return 'No response' else: return MBus.parse_telegram(response)
def ping(address): """ Ping address and return the result, True or False. :param address: int """ address = int(address) print('Sent: {}'.format(MBus.parse_telegram(MBus.snd_nke(address)))) return MBus.parse_telegram(nm.send(MBus.snd_nke(address)))