def main(): (options, args) = handle_args() client = HMTLClient(options) if options.scanevery: modules = scan_every(client) else: modules = scan_broadcast(client) client.close() print("\nFound %d modules:" % (len(modules))) print(" %s" % (HMTLprotocol.PollHdr.headers())) for m in modules: print(" %s" % (m.dump())) exit(0)
class Client: """ This class controls a connection to a hmtl server as well as tracking the state of the devices it is connected to. """ def __init__(self, address, verbose=False): self.address = address self.hmtl_client = None self.verbose = verbose self.cleared = False def connect(self, force_reconnect = False): if self.is_connected() and not force_reconnect: print("* Client is already connected") return True if self.is_connected() and force_reconnect: print("* Disconnecting client") self.hmtl_client.close() self.hmtl_client = None try: print("* Creating HMTL Client. address=%s" % self.address) self.hmtl_client = HMTLClient(address=self.address, verbose=self.verbose) return True except Exception as e: print("ERROR: Failed to create client: %s" % e) self.hmtl_client = None return False def is_connected(self): return self.hmtl_client is not None def send_msg(self, msg, expect_response=False): if not self.is_connected(): raise ClientDisconnectedException("Client is not connected") start_time = time.time() self.hmtl_client.send_and_ack(msg, expect_response) end_time = time.time() print("Client: sent and acked in %.6fs" % (end_time - start_time))
def connect(self, force_reconnect = False): if self.is_connected() and not force_reconnect: print("* Client is already connected") return True if self.is_connected() and force_reconnect: print("* Disconnecting client") self.hmtl_client.close() self.hmtl_client = None try: print("* Creating HMTL Client. address=%s" % self.address) self.hmtl_client = HMTLClient(address=self.address, verbose=self.verbose) return True except Exception as e: print("ERROR: Failed to create client: %s" % e) self.hmtl_client = None return False
def main(): options = parse_options() client = HMTLClient(address=options.address, port=options.port, hmtladdress=options.hmtladdress, verbose=options.verbose) msg = None expect_response = False if options.commandtype == "rgb": if not options.foreground: print("RGB command requires foreground color") exit(1) fg = [ int(x) for x in options.foreground.split(",") ] print("Sending RGB message. Address=%d Output=%d fg=%s" % (options.hmtladdress, options.output, fg)) msg = HMTLprotocol.get_rgb_msg(options.hmtladdress, options.output, fg[0], fg[1], fg[2]) if options.commandtype == "static": if not options.foreground or not options.background or not options.period or (options.threshold == None): print("STATIC command requires foreground, background, period, and threshold") exit(1) fg = [ int(x) for x in options.foreground.split(",") ] bg = [ int(x) for x in options.background.split(",") ] print("Sending STATIC message. Address=%d Output=%d period=%d fg=%s bg=%s threshold=%d" % (options.hmtladdress, options.output, options.period, fg, bg, options.threshold)) static = TriangleStatic(options.period, fg, bg, options.threshold) msg = static.msg(options.hmtladdress, options.output) elif options.commandtype == "snake": if not options.background or not options.period or \ (options.colormode == None): print("SNAKE command requires background, colormode, and period") exit(1) bg = [ int(x) for x in options.background.split(",") ] print("Sending SNAKE message. Address=%d Output=%d period=%d bg=%s colormode=%d" % (options.hmtladdress, options.output, options.period, bg, options.colormode)) snake = TriangleSnake(options.period, bg, options.colormode) msg = snake.msg(options.hmtladdress, options.output) elif options.commandtype == "none": print("Sending NONE message. Output=%d" % (options.output)) msg = HMTLprotocol.get_program_none_msg(options.hmtladdress, options.output) if (msg != None): starttime = time.time() client.send_and_ack(msg, expect_response) endtime = time.time() print("Sent and acked in %.6fs" % (endtime - starttime)) client.close() print("Done") exit(0)
def main(): handle_args() client = HMTLClient(options) msg = None expect_response = False if (options.commandtype == "value"): print("Sending value message. Address=%d Output=%d Value=%d" % (options.hmtladdress, options.output, int(options.commandvalue))) msg = HMTLprotocol.get_value_msg(options.hmtladdress, options.output, int(options.commandvalue)) elif (options.commandtype == "rgb"): (r,g,b) = options.commandvalue.split(",") print("Sending RGB message. Address=%d Output=%d Value=%d,%d,%d" % (options.hmtladdress, options.output, int(r), int(g), int(b))) msg = HMTLprotocol.get_rgb_msg(options.hmtladdress, options.output, int(r), int(g), int(b)) elif (options.commandtype == "blink"): (a,b,c,d, e,f,g,h) = options.commandvalue.split(",") print("Sending BLINK message. Address=%d Output=%d on_period=%d on_value=%s off_period=%d off_values=%s" % (options.hmtladdress, options.output, int(a), [int(b),int(c),int(d)], int(e), [int(f),int(g),int(h)])) msg = HMTLprotocol.get_program_blink_msg(options.hmtladdress, options.output, int(a), [int(b),int(c),int(d)], int(e), [int(f),int(g),int(h)]) elif (options.commandtype == "timedchange"): (a, b,c,d, e,f,g,) = options.commandvalue.split(",") print("Sending TIMED CHANGE message. Address=%d Output=%d change_period=%d start_value=%s stop_values=%s" % (options.hmtladdress, options.output, int(a), [int(b),int(c),int(d)], [int(e),int(f),int(g)])) msg = HMTLprotocol.get_program_timed_change_msg(options.hmtladdress, options.output, int(a), [int(b),int(c),int(d)], [int(e),int(f),int(g)]) elif (options.commandtype == "levelvalue"): print("Sending LEVEL VALUE message. Address=%d Output=%d" % (options.hmtladdress, options.output)) msg = HMTLprotocol.get_program_level_value_msg(options.hmtladdress, options.output) elif (options.commandtype == "soundvalue"): print("Sending SOUND VALUE message. Address=%d Output=%d" % (options.hmtladdress, options.output)) msg = HMTLprotocol.get_program_sound_value_msg(options.hmtladdress, options.output) elif (options.commandtype == "none"): print("Sending NONE message. Output=%d" % (options.output)) msg = HMTLprotocol.get_program_none_msg(options.hmtladdress, options.output) elif (options.commandtype == "poll"): print("Sending poll message. Address=%d" % (options.hmtladdress)) msg = HMTLprotocol.get_poll_msg(options.hmtladdress) expect_response = True elif (options.commandtype == "setaddr"): (device_id, new_address) = options.commandvalue.split(",") print("Sending set address message. Address=%d Device=%d NewAddress=%d" % (options.hmtladdress, int(device_id), int(new_address))) msg = HMTLprotocol.get_set_addr_msg(options.hmtladdress, int(device_id), int(new_address)) elif (options.commandtype == "fade"): (a, b,c,d, e,f,g,) = options.commandvalue.split(",") print("Sending FADE message. Address=%d Output=%d fade_period=%d start_value=%s stop_values=%s" % (options.hmtladdress, options.output, int(a), [int(b),int(c),int(d)], [int(e),int(f),int(g)])) msg = HMTLprotocol.get_program_fade_msg(options.hmtladdress, options.output, int(a), [int(b),int(c),int(d)], [int(e),int(f),int(g)]) if (msg != None): starttime = time.time() client.send_and_ack(msg, expect_response) endtime = time.time() print("Sent and acked in %.6fs" % (endtime - starttime)) if (options.killserver): # Send an exit message to the server print("Sending EXIT message to server") client.send_exit() client.close() print("Done.") exit(0)