def install_agent(self, package_path): checksum = utils.get_checksum(package_path, self.get_checksum_dir()) if not utils.check_file(package_path, checksum): LOG.debug("check downloaded package failed, removing %s", package_path) os.remove(package_path) utils.remove_checksum(package_path, self.get_checksum_dir()) return None dirname = utils.get_file_name_without_extension(package_path) agent_dir = '%s\%s' % (self.get_install_dir(), dirname) tmp_dir = '%s\%s' % (self.get_tmp_dir(), dirname) try: if utils.unzip(package_path, tmp_dir): tmp_name = os.listdir(tmp_dir)[0] tmp_path = '%s\%s' % (tmp_dir, tmp_name) ren_path = '%s\%s' % (tmp_dir, dirname) if os.system('ren "%s" "%s"' % (tmp_path, dirname)) != 0: return None if os.system('move "%s" "%s"' % (ren_path, self.get_install_dir())) != 0: return None else: return None finally: if os.path.isdir(tmp_dir): os.system('rd /s /q "%s"' % tmp_dir) installer = Installer(agent_dir, self.recycle_queue) installer.install() return agent_dir
def get_on_off_toggle_data(self, command): msg_type = 0x0092 msg_type_list = utils.convert_int16_msb_lsb_list(msg_type) msg_type_msb = msg_type_list['msb'] msg_type_lsb = msg_type_list['lsb'] address_mode = self.address_mode destn_list = utils.convert_int16_msb_lsb_list(self.address) destn_msb = destn_list['msb'] destn_lsb = destn_list['lsb'] source_ep = 0x01 content = [ address_mode, destn_msb, destn_lsb, source_ep, self.endpoint, command ] length = utils.convert_int16_msb_lsb_list(len(content)) length_msb = length['msb'] length_lsb = length['lsb'] checksum = utils.get_checksum(msg_type_msb=msg_type_msb, msg_type_lsb=msg_type_lsb, length_msb=length_msb, length_lsb=length_lsb, data=content) data = [msg_type_msb, msg_type_lsb, length_msb, length_lsb, checksum ] + content return data
def permit_joining(self, interval=0x0f): msg_type = 0x0049 msg_type_list = utils.convert_int16_msb_lsb_list(msg_type) msg_type_msb = msg_type_list['msb'] msg_type_lsb = msg_type_list['lsb'] target_short_address = 0x0 target_short_address_list = utils.convert_int16_msb_lsb_list( target_short_address) target_short_address_msb = target_short_address_list['msb'] target_short_address_lsb = target_short_address_list['lsb'] tcsignificance = 0x0 content = [ target_short_address_msb, target_short_address_lsb, interval, tcsignificance ] length = utils.convert_int16_msb_lsb_list(len(content)) length_msb = length['msb'] length_lsb = length['lsb'] checksum = utils.get_checksum(msg_type_msb=msg_type_msb, msg_type_lsb=msg_type_lsb, length_msb=length_msb, length_lsb=length_lsb, data=content) data = [msg_type_msb, msg_type_lsb, length_msb, length_lsb, checksum ] + content bytes_to_send = utils.create_byte_stream_to_send(data_bytes=data) self.device.write(bytes_to_send) print("permit joining: ", bytes_to_send)
def install_agent(self, package_path): checksum = utils.get_checksum(package_path, self.get_checksum_dir()) if not utils.check_file(package_path, checksum): LOG.debug("check downloaded package failed, removing %s", package_path) os.remove(package_path) utils.remove_checksum(package_path, self.get_checksum_dir()) return None dirname = utils.get_file_name_without_extension(package_path) agent_dir = '%s/%s' % (self.get_install_dir(), dirname) r = os.system('mkdir -p %s && tar xzf %s -C %s --strip-components 1' % (agent_dir, package_path, agent_dir)) != 0 if r != 0: LOG.debug('decompress failed, %d', r) os.remove(package_path) os.rmdir(agent_dir) return None else: LOG.debug('decompress success') installer = Installer(agent_dir, self.recycle_queue) installer.install() return agent_dir
def add_groups(self, light_address, light_ep, group_address, group_name_length=0x10, group_name_max_length=0x1, group_name=0x61): msg_type = 0x0060 msg_type_list = utils.convert_int16_msb_lsb_list(msg_type) msg_type_msb = msg_type_list['msb'] msg_type_lsb = msg_type_list['lsb'] address_mode = 0x02 destn_list = utils.convert_int16_msb_lsb_list(light_address) destn_msb = destn_list['msb'] destn_lsb = destn_list['lsb'] source_ep = 0x01 group_address_list = utils.convert_int16_msb_lsb_list(group_address) group_address_msb = group_address_list['msb'] group_address_lsb = group_address_list['lsb'] content = [ address_mode, destn_msb, destn_lsb, source_ep, light_ep, group_address_msb, group_address_lsb, group_name_length, group_name_max_length, group_name ] length = utils.convert_int16_msb_lsb_list(len(content)) length_msb = length['msb'] length_lsb = length['lsb'] checksum = utils.get_checksum(msg_type_msb=msg_type_msb, msg_type_lsb=msg_type_lsb, length_msb=length_msb, length_lsb=length_lsb, data=content) data = [msg_type_msb, msg_type_lsb, length_msb, length_lsb, checksum ] + content bytes_to_send = utils.create_byte_stream_to_send(data_bytes=data) self.device.write(bytes_to_send) print(bytes_to_send)
words_received = [] buffer = 9 while True: data = connection.recv(buffer) # commented out to make STDOUT more readable # print('received {!r}'.format(data)) if data[:3] == b'BLK': number_of_words = data[4:] # set buffer to 4 as words are 4bytes buffer = 4 connection.sendall( bytes(f'RCV-{int(number_of_words)}', 'UTF-8')) elif data == b'ETB': checksum = get_checksum(words_received) print(f'received ETB, sending checksum: {checksum}') connection.sendall(bytes(f'ACK-{checksum}', 'UTF-8')) buffer = 9 save_to_file(words_received, 'server_received_data.txt') # clear words_received buffer to be ready for next block words_received.clear() elif data == b'EOT': print('received EOT, stopping server...') break elif data: try: words_received.append(data)
print('sending {!r}'.format(message)) sock.sendall(message) i = 1 while True: data = sock.recv(9) if data == bytes(f'RCV-{words_in_block}', 'UTF-8'): print('received {!r}'.format(data)) for word in words: sock.send(word) save_to_file(words, 'client_received_data.txt') sock.sendall(b'ETB') elif data[:3:] == b'ACK': if bytes(str(get_checksum(words)), 'UTF-8') == data[4::]: print('received checksum == internal checksum') if number_of_blocks == i: sock.sendall(b'EOT') break else: i += 1 message, words_in_block, words = get_new_block_announcement_msg( ) print('sending {!r}'.format(message)) sock.sendall(message) else: print('received checksum != internal checksum') print('transmission error, closing connection') break
# Copyright (c) Microsoft Corporation. # Licensed under the MIT license. import argparse import os import sys from utils import get_checksum if __name__ == "__main__": # Load args parser = argparse.ArgumentParser() parser.add_argument('file_path') args = parser.parse_args() if os.path.exists(args.file_path): checksum = get_checksum(file_path=args.file_path) else: checksum = "" # Print job details sys.stdout.write(checksum)
def set_level(self, level, transition_time=1, with_on_off=True, print_return=False, seconds_before_next_command=2): utils.wait_for_prev(self.last_controlled_time, self.delay_between_commands) self.level = level msg_type = 0x0081 msg_type_list = utils.convert_int16_msb_lsb_list(msg_type) msg_type_msb = msg_type_list['msb'] msg_type_lsb = msg_type_list['lsb'] address_mode = self.address_mode destn_list = utils.convert_int16_msb_lsb_list(self.address) destn_msb = destn_list['msb'] destn_lsb = destn_list['lsb'] source_ep = 0x01 on_off = int(with_on_off) if int(level / 100) * 0xFF > 0xFF or level < 0x0: print("out of bounds level : %d. 0x0<=level<=0xff" % level) return -1 transition_time_list = utils.convert_int16_msb_lsb_list( transition_time) transition_time_msb = transition_time_list['msb'] transition_time_lsb = transition_time_list['lsb'] content = [ address_mode, destn_msb, destn_lsb, source_ep, self.endpoint, on_off, int(level / 100 * 0xFF), transition_time_msb, transition_time_lsb ] length = utils.convert_int16_msb_lsb_list(len(content)) length_msb = length['msb'] length_lsb = length['lsb'] checksum = utils.get_checksum(msg_type_msb=msg_type_msb, msg_type_lsb=msg_type_lsb, length_msb=length_msb, length_lsb=length_lsb, data=content) data = [msg_type_msb, msg_type_lsb, length_msb, length_lsb, checksum ] + content bytes_to_send = utils.create_byte_stream_to_send(data_bytes=data) self.device.write(bytes_to_send) self.last_controlled_time = time.time() rsp = [self.synchronous_read()] if print_return: print(rsp) self.device.close() self.device.open() self.device.reset_input_buffer() self.device.reset_output_buffer()
def build_image(self): # clean up cache free_space() self.logger.info("Starting image creation") # write config to a file with open(str(self.config_path), "w") as fd: json.dump(self.task.get("config"), fd, indent=4) args = [ str(Setting.installer_binary_path), "cli", "--root", "--filename", self.img_name, "--build-dir", str(Setting.working_dir), "--config", str(self.config_path), "--size", "{}GB".format(self.task.get("size")), ] if bool(os.getenv("FAKE_CREATION", False)): args = [ "cp", str(Setting.installer_binary_path), str(self.img_path) ] self.logger.info("Starting {args}\n".format(args=" ".join(args))) self.logs["installer_log"] = "{args}\n".format(args=" ".join(args)) log_fd = open(str(self.log_path), "w") ps = subprocess.Popen( args=args, stdout=log_fd, stderr=subprocess.STDOUT, universal_newlines=True, close_fds=True, ) self.logger.info("installer started: {}".format(ps)) while ps.poll() is None: self.logs["installer_log"] = self.read_log(self.log_path) # kill upon request if self.canceled: self.logger.info("terminating installer…") ps.terminate() ps.wait(10) ps.kill() ps.wait(10) break ps.wait(10) successful = ps.returncode == 0 and self.img_path.exists() if successful: self.logger.info("installer ran successfuly.") # get and store size and checksum self.extra["image"] = { "fname": self.img_path.name, "size": self.img_path.stat().st_size, "checksum": "{0}:{1}".format(*get_checksum(str(self.img_path))), } else: self.logger.error("installer failed: {}".format(ps.returncode)) self.logger.info("collecting full terminated log") self.logs["installer_log"] = self.read_log(self.log_path) # clean up working folder self.remove_files() if not successful: raise subprocess.SubprocessError("installer rc: {}".format( ps.returncode))