From 2a489c9e125c723e6d37b2d18c204d7d6fb457d4 Mon Sep 17 00:00:00 2001 From: Jihun Kim Date: Thu, 19 Jan 2023 11:02:14 +0900 Subject: [PATCH 1/4] =?UTF-8?q?set=20property=20handler=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modi_plus/module/module.py | 75 ++++++++++++++++++++--- modi_plus/module/output_module/display.py | 28 ++++++--- modi_plus/module/output_module/led.py | 2 +- modi_plus/module/output_module/motor.py | 16 +++-- modi_plus/module/output_module/speaker.py | 13 ++-- modi_plus/module/setup_module/network.py | 16 ++--- 6 files changed, 116 insertions(+), 34 deletions(-) diff --git a/modi_plus/module/module.py b/modi_plus/module/module.py index f1bef12..d7dcaf5 100644 --- a/modi_plus/module/module.py +++ b/modi_plus/module/module.py @@ -101,8 +101,12 @@ def __init__(self, id_, uuid, connection_task): self.module_type = str() self._properties = dict() + self.__set_properties = dict() + self.__last_set_property_num = None + # sampling_rate = (100 - property_sampling_frequency) * 11, in ms self.prop_samp_freq = 91 + self.prop_request_period = 2 # [s] self.is_connected = True self.is_usb_connected = False @@ -238,7 +242,8 @@ def _get_property(self, property_type: int) -> bytearray: def _set_property(self, destination_id: int, property_num: int, - property_values: Union[Tuple, str]) -> None: + property_values: Union[Tuple, str], + force: bool = False) -> None: """Send the message of set_property command to the module :param destination_id: Id of the destination module @@ -247,15 +252,71 @@ def _set_property(self, destination_id: int, :type property_num: int :param property_values: Property Values :type property_values: Tuple + :param force: Force data to be sent + :type force: bool :return: None """ - message = parse_set_property_message( - destination_id, - property_num, - property_values, - ) - self._connection.send(message) + do_send = False + now_time = time.time() + + if property_num in self.__set_properties: + if property_values == self.__set_properties[property_num].value: + duration = now_time - self.__set_properties[property_num].last_update_time + if force or duration > self.prop_request_period: + # 마지막으로 보낸 데이터와 같은 경우, 2초마다 전송 or force가 true인 경우 + self.__set_properties[property_num].value = property_values + self.__set_properties[property_num].last_update_time = now_time + do_send = True + else: + # 마지막으로 보낸 데이터와 다른 경우, 바로 전송 + self.__set_properties[property_num].value = property_values + self.__set_properties[property_num].last_update_time = now_time + do_send = True + else: + # 데이터를 한번도 안 보낸 경우, 바로 전송 + self.__set_properties[property_num] = self.Property() + self.__set_properties[property_num].value = property_values + self.__set_properties[property_num].last_update_time = now_time + do_send = True + + if do_send: + message = parse_set_property_message( + destination_id, + property_num, + property_values, + ) + self._connection.send_nowait(message) + + def __check_last_set_property(self, property_num: int) -> bool: + if self.__last_set_property_num == None: + return False + else: + return self.__last_set_property_num == property_num + + def set_property(self, destination_id: int, + property_num: int, + property_values: Union[Tuple, str], + force: bool = False) -> None: + """Send the message of set_property command to the module + + :param destination_id: Id of the destination module + :type destination_id: int + :param property_num: Property Type + :type property_num: int + :param property_values: Property Values + :type property_values: Tuple + :param force: Force data to be sent + :type force: bool + :return: None + """ + + if self.__check_last_set_property(property_num): + self._set_property(destination_id, property_num, property_values, force) + else: + self._set_property(destination_id, property_num, property_values, True) + + self.__last_set_property_num = property_num def update_property(self, property_type: int, property_value: bytearray) -> None: """ Update property value and time diff --git a/modi_plus/module/output_module/display.py b/modi_plus/module/output_module/display.py index efb904b..23d5748 100644 --- a/modi_plus/module/output_module/display.py +++ b/modi_plus/module/output_module/display.py @@ -1,5 +1,6 @@ """Display module.""" +import time from typing import List from modi_plus.module.module import OutputModule @@ -205,13 +206,15 @@ def write_text(self, text: str) -> None: encoding_data = str.encode(str(text)) + bytes(1) splited_data = [encoding_data[x - n:x] for x in range(n, len(encoding_data) + n, n)] for index, data in enumerate(splited_data): - self._set_property( + self.set_property( self._id, Display.PROPERTY_DISPLAY_WRITE_TEXT, - property_values=(("bytes", data), ) + property_values=(("bytes", data), ), + force=True ) self._text = text + time.sleep(0.02 + 0.003 * len(encoding_data)) def write_variable(self, x: int, y: int, variable: float) -> None: """Show the input variable on the display. @@ -225,7 +228,7 @@ def write_variable(self, x: int, y: int, variable: float) -> None: :return: None """ - self._set_property( + self.set_property( self._id, Display.PROPERTY_DISPLAY_WRITE_VARIABLE, property_values=(("u8", x), @@ -233,6 +236,7 @@ def write_variable(self, x: int, y: int, variable: float) -> None: ("float", variable), ) ) self._text += str(variable) + time.sleep(0.01) def draw_picture(self, x: int, y: int, name: int) -> None: """Clears the display and show the input picture on the display. @@ -250,7 +254,7 @@ def draw_picture(self, x: int, y: int, name: int) -> None: if file_name is None: raise ValueError(f"{file_name} is not on the list, check 'Display.preset_pictures()'") - self._set_property( + self.set_property( self._id, Display.PROPERTY_DISPLAY_DRAW_PICTURE, property_values=(("u8", x), @@ -259,6 +263,7 @@ def draw_picture(self, x: int, y: int, name: int) -> None: ("u8", Display.HEIGHT), ("string", file_name), ) ) + time.sleep(0.05) def draw_dot(self, dot: bytes) -> None: """Clears the display and show the input dot on the display. @@ -279,11 +284,12 @@ def draw_dot(self, dot: bytes) -> None: for index, data in enumerate(splited_data): send_data = bytes([index]) + data - self._set_property( + self.set_property( self._id, Display.PROPERTY_DISPLAY_DRAW_DOT, property_values=(("bytes", send_data), ) ) + time.sleep(0.3) def set_offset(self, x: int, y: int) -> None: """Set origin point on the screen @@ -295,11 +301,12 @@ def set_offset(self, x: int, y: int) -> None: :return: None """ - self._set_property( + self.set_property( self.id, Display.PROPERTY_DISPLAY_SET_OFFSET, property_values=(("s8", x), ("s8", y), ) ) + time.sleep(0.01) def move_screen(self, x: int, y: int) -> None: """Move the screen by x and y @@ -311,11 +318,13 @@ def move_screen(self, x: int, y: int) -> None: :return: None """ - self._set_property( + self.set_property( self.id, Display.PROPERTY_DISPLAY_MOVE_SCREEN, - property_values=(("s8", x), ("s8", y), ) + property_values=(("s8", x), ("s8", y), ), + force=True ) + time.sleep(0.01) def reset(self) -> None: """Clear the screen. @@ -323,9 +332,10 @@ def reset(self) -> None: :return: None """ - self._set_property( + self.set_property( self._id, Display.PROPERTY_DISPLAY_RESET, property_values=(("u8", 0), ) ) self._text = "" + time.sleep(0.01) diff --git a/modi_plus/module/output_module/led.py b/modi_plus/module/output_module/led.py index 1ce71ac..166b01e 100644 --- a/modi_plus/module/output_module/led.py +++ b/modi_plus/module/output_module/led.py @@ -116,7 +116,7 @@ def set_rgb(self, red: int, green: int, blue: int) -> None: :return: None """ - self._set_property( + self.set_property( destination_id=self._id, property_num=Led.PROPERTY_LED_SET_RGB, property_values=(("u16", red), diff --git a/modi_plus/module/output_module/motor.py b/modi_plus/module/output_module/motor.py index fa73718..95f01d1 100644 --- a/modi_plus/module/output_module/motor.py +++ b/modi_plus/module/output_module/motor.py @@ -1,5 +1,6 @@ """Motor module.""" +import time import struct from typing import Tuple from modi_plus.module.module import OutputModule @@ -103,12 +104,13 @@ def set_angle(self, target_angle: int, target_speed: int = 70) -> None: :return: None """ - self._set_property( + self.set_property( destination_id=self._id, property_num=Motor.PROPERTY_MOTOR_ANGLE, property_values=(("u16", target_angle), ("u16", target_speed), ) ) + time.sleep(0.01) def set_speed(self, target_speed: int) -> None: """Sets the speed of the motor @@ -118,11 +120,12 @@ def set_speed(self, target_speed: int) -> None: :return: None """ - self._set_property( + self.set_property( destination_id=self._id, property_num=Motor.PROPERTY_MOTOR_SPEED, property_values=(("s32", target_speed), ) ) + time.sleep(0.01) def append_angle(self, target_angle: int, target_speed: int = 70) -> None: """append the angle form current angle of the motor @@ -134,12 +137,14 @@ def append_angle(self, target_angle: int, target_speed: int = 70) -> None: :return: None """ - self._set_property( + self.set_property( destination_id=self._id, property_num=Motor.PROPERTY_MOTOR_ANGLE_APPEND, property_values=(("s16", target_angle), - ("u16", target_speed), ) + ("u16", target_speed), ), + force=True ) + time.sleep(0.01) def stop(self) -> None: """Stop operating motor @@ -147,8 +152,9 @@ def stop(self) -> None: :return: None """ - self._set_property( + self.set_property( destination_id=self._id, property_num=Motor.PROPERTY_MOTOR_STOP, property_values=() ) + time.sleep(0.01) diff --git a/modi_plus/module/output_module/speaker.py b/modi_plus/module/output_module/speaker.py index 8693e71..c387bc2 100644 --- a/modi_plus/module/output_module/speaker.py +++ b/modi_plus/module/output_module/speaker.py @@ -1,5 +1,6 @@ """Speaker module.""" +import time import struct from typing import List, Tuple, Union from modi_plus.module.module import OutputModule @@ -211,11 +212,12 @@ def set_tune(self, frequency: Union[int, str], volume: int) -> None: if frequency < 0: raise ValueError("Not a supported frequency value") - self._set_property( + self.set_property( destination_id=self._id, property_num=Speaker.PROPERTY_SPEAKER_SET_TUNE, property_values=(("u16", frequency), ("u16", volume), ) ) + time.sleep(0.01) def play_music(self, name: str, volume: int) -> None: """Play music in speaker module @@ -234,13 +236,14 @@ def play_music(self, name: str, volume: int) -> None: property_num = Speaker.PROPERTY_SPEAKER_MELODY if ".mid" in file_name else Speaker.PROPERTY_SPEAKER_MUSIC self.playing_file_name = file_name - self._set_property( + self.set_property( self._id, property_num, property_values=(("u8", Speaker.STATE_START), ("u8", volume), ("string", self.playing_file_name), ) ) + time.sleep(0.1) def stop_music(self) -> None: """Stop music in speaker module @@ -253,7 +256,7 @@ def stop_music(self) -> None: property_num = Speaker.PROPERTY_SPEAKER_MELODY if ".mid" in self.playing_file_name else Speaker.PROPERTY_SPEAKER_MUSIC - self._set_property( + self.set_property( self._id, property_num, property_values=(("u8", Speaker.STATE_STOP), @@ -272,7 +275,7 @@ def pause_music(self) -> None: property_num = Speaker.PROPERTY_SPEAKER_MELODY if ".mid" in self.playing_file_name else Speaker.PROPERTY_SPEAKER_MUSIC - self._set_property( + self.set_property( self._id, property_num, property_values=(("u8", Speaker.STATE_PAUSE), @@ -291,7 +294,7 @@ def resume_music(self) -> None: property_num = Speaker.PROPERTY_SPEAKER_MELODY if ".mid" in self.playing_file_name else Speaker.PROPERTY_SPEAKER_MUSIC - self._set_property( + self.set_property( self._id, property_num, property_values=(("u8", Speaker.STATE_RESUME), diff --git a/modi_plus/module/setup_module/network.py b/modi_plus/module/setup_module/network.py index 31fa6ac..9e925a0 100644 --- a/modi_plus/module/setup_module/network.py +++ b/modi_plus/module/setup_module/network.py @@ -327,10 +327,11 @@ def send_data(self, index: int, data: int) -> None: property_num = Network.PROPERTY_NETWORK_SEND_DATA + 0x100 * index - self._set_property( + self.set_property( destination_id=self._id, property_num=property_num, - property_values=(("s32", data),) + property_values=(("s32", data),), + force=True ) @check_connection @@ -342,10 +343,11 @@ def send_text(self, text: str) -> None: :return: None """ - self._set_property( + self.set_property( destination_id=self._id, property_num=Network.PROPERTY_NETWORK_SEND_TEXT, - property_values=(("string", text),) + property_values=(("string", text),), + force=True ) @check_connection @@ -359,7 +361,7 @@ def buzzer_on(self) -> None: self.buzzer_off() self.__buzzer_flag = False - self._set_property( + self.set_property( destination_id=self._id, property_num=Network.PROPERTY_NETWORK_BUZZER, property_values=(("u8", Network.STATE_BUZZER_ON),) @@ -372,7 +374,7 @@ def buzzer_off(self) -> None: :return: None """ - self._set_property( + self.set_property( destination_id=self._id, property_num=Network.PROPERTY_NETWORK_BUZZER, property_values=(("u8", Network.STATE_BUZZER_OFF),) @@ -386,7 +388,7 @@ def take_picture(self) -> None: :return: None """ - self._set_property( + self.set_property( destination_id=self._id, property_num=Network.PROPERTY_NETWORK_CAMERA, property_values=(("u8", Network.STATE_CAMERA_PICTURE),) From 00c2eab86ad869768760157b2e59ed433c6546a3 Mon Sep 17 00:00:00 2001 From: Jihun Kim Date: Thu, 19 Jan 2023 11:06:03 +0900 Subject: [PATCH 2/4] =?UTF-8?q?test=20connection=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modi_plus/util/unittest_util.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modi_plus/util/unittest_util.py b/modi_plus/util/unittest_util.py index 6e885fa..f48d905 100644 --- a/modi_plus/util/unittest_util.py +++ b/modi_plus/util/unittest_util.py @@ -19,6 +19,9 @@ def __init__(self): def send(self, pkt): self.send_list.append(pkt) + def send_nowait(self, pkt): + self.send_list.append(pkt) + def recv(self): return "Test" From e7f2f4b0634e73635e586e537b3fc02131d712d2 Mon Sep 17 00:00:00 2001 From: Jihun Kim Date: Thu, 19 Jan 2023 11:31:43 +0900 Subject: [PATCH 3/4] =?UTF-8?q?set=20property=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modi_plus/module/module.py | 31 +++++------------------ modi_plus/module/output_module/display.py | 14 +++++----- modi_plus/module/output_module/led.py | 2 +- modi_plus/module/output_module/motor.py | 8 +++--- modi_plus/module/output_module/speaker.py | 10 ++++---- modi_plus/module/setup_module/network.py | 10 ++++---- 6 files changed, 28 insertions(+), 47 deletions(-) diff --git a/modi_plus/module/module.py b/modi_plus/module/module.py index d7dcaf5..48f31a0 100644 --- a/modi_plus/module/module.py +++ b/modi_plus/module/module.py @@ -260,6 +260,9 @@ def _set_property(self, destination_id: int, do_send = False now_time = time.time() + if not self.__check_last_set_property(property_num): + force = True + if property_num in self.__set_properties: if property_values == self.__set_properties[property_num].value: duration = now_time - self.__set_properties[property_num].last_update_time @@ -288,36 +291,14 @@ def _set_property(self, destination_id: int, ) self._connection.send_nowait(message) + self.__last_set_property_num = property_num + def __check_last_set_property(self, property_num: int) -> bool: - if self.__last_set_property_num == None: + if self.__last_set_property_num is None: return False else: return self.__last_set_property_num == property_num - def set_property(self, destination_id: int, - property_num: int, - property_values: Union[Tuple, str], - force: bool = False) -> None: - """Send the message of set_property command to the module - - :param destination_id: Id of the destination module - :type destination_id: int - :param property_num: Property Type - :type property_num: int - :param property_values: Property Values - :type property_values: Tuple - :param force: Force data to be sent - :type force: bool - :return: None - """ - - if self.__check_last_set_property(property_num): - self._set_property(destination_id, property_num, property_values, force) - else: - self._set_property(destination_id, property_num, property_values, True) - - self.__last_set_property_num = property_num - def update_property(self, property_type: int, property_value: bytearray) -> None: """ Update property value and time diff --git a/modi_plus/module/output_module/display.py b/modi_plus/module/output_module/display.py index 23d5748..c46f139 100644 --- a/modi_plus/module/output_module/display.py +++ b/modi_plus/module/output_module/display.py @@ -206,7 +206,7 @@ def write_text(self, text: str) -> None: encoding_data = str.encode(str(text)) + bytes(1) splited_data = [encoding_data[x - n:x] for x in range(n, len(encoding_data) + n, n)] for index, data in enumerate(splited_data): - self.set_property( + self._set_property( self._id, Display.PROPERTY_DISPLAY_WRITE_TEXT, property_values=(("bytes", data), ), @@ -228,7 +228,7 @@ def write_variable(self, x: int, y: int, variable: float) -> None: :return: None """ - self.set_property( + self._set_property( self._id, Display.PROPERTY_DISPLAY_WRITE_VARIABLE, property_values=(("u8", x), @@ -254,7 +254,7 @@ def draw_picture(self, x: int, y: int, name: int) -> None: if file_name is None: raise ValueError(f"{file_name} is not on the list, check 'Display.preset_pictures()'") - self.set_property( + self._set_property( self._id, Display.PROPERTY_DISPLAY_DRAW_PICTURE, property_values=(("u8", x), @@ -284,7 +284,7 @@ def draw_dot(self, dot: bytes) -> None: for index, data in enumerate(splited_data): send_data = bytes([index]) + data - self.set_property( + self._set_property( self._id, Display.PROPERTY_DISPLAY_DRAW_DOT, property_values=(("bytes", send_data), ) @@ -301,7 +301,7 @@ def set_offset(self, x: int, y: int) -> None: :return: None """ - self.set_property( + self._set_property( self.id, Display.PROPERTY_DISPLAY_SET_OFFSET, property_values=(("s8", x), ("s8", y), ) @@ -318,7 +318,7 @@ def move_screen(self, x: int, y: int) -> None: :return: None """ - self.set_property( + self._set_property( self.id, Display.PROPERTY_DISPLAY_MOVE_SCREEN, property_values=(("s8", x), ("s8", y), ), @@ -332,7 +332,7 @@ def reset(self) -> None: :return: None """ - self.set_property( + self._set_property( self._id, Display.PROPERTY_DISPLAY_RESET, property_values=(("u8", 0), ) diff --git a/modi_plus/module/output_module/led.py b/modi_plus/module/output_module/led.py index 166b01e..1ce71ac 100644 --- a/modi_plus/module/output_module/led.py +++ b/modi_plus/module/output_module/led.py @@ -116,7 +116,7 @@ def set_rgb(self, red: int, green: int, blue: int) -> None: :return: None """ - self.set_property( + self._set_property( destination_id=self._id, property_num=Led.PROPERTY_LED_SET_RGB, property_values=(("u16", red), diff --git a/modi_plus/module/output_module/motor.py b/modi_plus/module/output_module/motor.py index 95f01d1..e680f4a 100644 --- a/modi_plus/module/output_module/motor.py +++ b/modi_plus/module/output_module/motor.py @@ -104,7 +104,7 @@ def set_angle(self, target_angle: int, target_speed: int = 70) -> None: :return: None """ - self.set_property( + self._set_property( destination_id=self._id, property_num=Motor.PROPERTY_MOTOR_ANGLE, property_values=(("u16", target_angle), @@ -120,7 +120,7 @@ def set_speed(self, target_speed: int) -> None: :return: None """ - self.set_property( + self._set_property( destination_id=self._id, property_num=Motor.PROPERTY_MOTOR_SPEED, property_values=(("s32", target_speed), ) @@ -137,7 +137,7 @@ def append_angle(self, target_angle: int, target_speed: int = 70) -> None: :return: None """ - self.set_property( + self._set_property( destination_id=self._id, property_num=Motor.PROPERTY_MOTOR_ANGLE_APPEND, property_values=(("s16", target_angle), @@ -152,7 +152,7 @@ def stop(self) -> None: :return: None """ - self.set_property( + self._set_property( destination_id=self._id, property_num=Motor.PROPERTY_MOTOR_STOP, property_values=() diff --git a/modi_plus/module/output_module/speaker.py b/modi_plus/module/output_module/speaker.py index c387bc2..79083b1 100644 --- a/modi_plus/module/output_module/speaker.py +++ b/modi_plus/module/output_module/speaker.py @@ -212,7 +212,7 @@ def set_tune(self, frequency: Union[int, str], volume: int) -> None: if frequency < 0: raise ValueError("Not a supported frequency value") - self.set_property( + self._set_property( destination_id=self._id, property_num=Speaker.PROPERTY_SPEAKER_SET_TUNE, property_values=(("u16", frequency), ("u16", volume), ) @@ -236,7 +236,7 @@ def play_music(self, name: str, volume: int) -> None: property_num = Speaker.PROPERTY_SPEAKER_MELODY if ".mid" in file_name else Speaker.PROPERTY_SPEAKER_MUSIC self.playing_file_name = file_name - self.set_property( + self._set_property( self._id, property_num, property_values=(("u8", Speaker.STATE_START), @@ -256,7 +256,7 @@ def stop_music(self) -> None: property_num = Speaker.PROPERTY_SPEAKER_MELODY if ".mid" in self.playing_file_name else Speaker.PROPERTY_SPEAKER_MUSIC - self.set_property( + self._set_property( self._id, property_num, property_values=(("u8", Speaker.STATE_STOP), @@ -275,7 +275,7 @@ def pause_music(self) -> None: property_num = Speaker.PROPERTY_SPEAKER_MELODY if ".mid" in self.playing_file_name else Speaker.PROPERTY_SPEAKER_MUSIC - self.set_property( + self._set_property( self._id, property_num, property_values=(("u8", Speaker.STATE_PAUSE), @@ -294,7 +294,7 @@ def resume_music(self) -> None: property_num = Speaker.PROPERTY_SPEAKER_MELODY if ".mid" in self.playing_file_name else Speaker.PROPERTY_SPEAKER_MUSIC - self.set_property( + self._set_property( self._id, property_num, property_values=(("u8", Speaker.STATE_RESUME), diff --git a/modi_plus/module/setup_module/network.py b/modi_plus/module/setup_module/network.py index 9e925a0..bf3bdc5 100644 --- a/modi_plus/module/setup_module/network.py +++ b/modi_plus/module/setup_module/network.py @@ -327,7 +327,7 @@ def send_data(self, index: int, data: int) -> None: property_num = Network.PROPERTY_NETWORK_SEND_DATA + 0x100 * index - self.set_property( + self._set_property( destination_id=self._id, property_num=property_num, property_values=(("s32", data),), @@ -343,7 +343,7 @@ def send_text(self, text: str) -> None: :return: None """ - self.set_property( + self._set_property( destination_id=self._id, property_num=Network.PROPERTY_NETWORK_SEND_TEXT, property_values=(("string", text),), @@ -361,7 +361,7 @@ def buzzer_on(self) -> None: self.buzzer_off() self.__buzzer_flag = False - self.set_property( + self._set_property( destination_id=self._id, property_num=Network.PROPERTY_NETWORK_BUZZER, property_values=(("u8", Network.STATE_BUZZER_ON),) @@ -374,7 +374,7 @@ def buzzer_off(self) -> None: :return: None """ - self.set_property( + self._set_property( destination_id=self._id, property_num=Network.PROPERTY_NETWORK_BUZZER, property_values=(("u8", Network.STATE_BUZZER_OFF),) @@ -388,7 +388,7 @@ def take_picture(self) -> None: :return: None """ - self.set_property( + self._set_property( destination_id=self._id, property_num=Network.PROPERTY_NETWORK_CAMERA, property_values=(("u8", Network.STATE_CAMERA_PICTURE),) From d78ad819d5f036d226238c3027a4e475c97e3463 Mon Sep 17 00:00:00 2001 From: Jihun Kim Date: Thu, 19 Jan 2023 14:10:46 +0900 Subject: [PATCH 4/4] =?UTF-8?q?get=20property=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modi_plus/module/module.py | 48 ++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/modi_plus/module/module.py b/modi_plus/module/module.py index 48f31a0..b73918f 100644 --- a/modi_plus/module/module.py +++ b/modi_plus/module/module.py @@ -97,17 +97,15 @@ def __init__(self, id_, uuid, connection_task): self._id = id_ self._uuid = uuid self._connection = connection_task - self.module_type = str() - self._properties = dict() + # property + self.prop_samp_freq = 91 # sampling_rate[ms] = (100 - property_sampling_frequency) * 11 + self.prop_request_period = 2 # [s] + self.__get_properties = dict() self.__set_properties = dict() self.__last_set_property_num = None - # sampling_rate = (100 - property_sampling_frequency) * 11, in ms - self.prop_samp_freq = 91 - self.prop_request_period = 2 # [s] - self.is_connected = True self.is_usb_connected = False self.has_printed = False @@ -217,28 +215,28 @@ def _get_property(self, property_type: int) -> bytearray: """ # Register property if not exists - if property_type not in self._properties: - self._properties[property_type] = self.Property() + if property_type not in self.__get_properties: + self.__get_properties[property_type] = self.Property() self.__request_property(self._id, property_type) - # Request property value if not updated for 1.5 sec - last_update = self._properties[property_type].last_update_time - if time.time() - last_update > 1.5: + # Request property value if not updated for 2 sec + last_update = self.__get_properties[property_type].last_update_time + if time.time() - last_update > self.prop_request_period: self.__request_property(self._id, property_type) - if self._properties[property_type].value is None: + if self.__get_properties[property_type].value is None: if self._enable_get_property_timeout: first_request_time = time.time() # 3s timeout - while self._properties[property_type].value is None: + while self.__get_properties[property_type].value is None: if time.time() - first_request_time > 3: raise Module.GetValueInitTimeout time.sleep(0.1) else: return bytearray(12) - return self._properties[property_type].value + return self.__get_properties[property_type].value def _set_property(self, destination_id: int, property_num: int, @@ -293,12 +291,6 @@ def _set_property(self, destination_id: int, self.__last_set_property_num = property_num - def __check_last_set_property(self, property_num: int) -> bool: - if self.__last_set_property_num is None: - return False - else: - return self.__last_set_property_num == property_num - def update_property(self, property_type: int, property_value: bytearray) -> None: """ Update property value and time @@ -308,10 +300,10 @@ def update_property(self, property_type: int, property_value: bytearray) -> None :type property_value: bytearray """ - if property_type not in self._properties: - self._properties[property_type] = self.Property() - self._properties[property_type].value = property_value - self._properties[property_type].last_update_time = time.time() + if property_type not in self.__get_properties: + self.__get_properties[property_type] = self.Property() + self.__get_properties[property_type].value = property_value + self.__get_properties[property_type].last_update_time = time.time() def __request_property(self, destination_id: int, property_type: int) -> None: """ Generate message for request property @@ -323,10 +315,16 @@ def __request_property(self, destination_id: int, property_type: int) -> None: :return: None """ - self._properties[property_type].last_update_time = time.time() + self.__get_properties[property_type].last_update_time = time.time() req_prop_msg = parse_get_property_message(destination_id, property_type, self.prop_samp_freq) self._connection.send(req_prop_msg) + def __check_last_set_property(self, property_num: int) -> bool: + if self.__last_set_property_num is None: + return False + else: + return self.__last_set_property_num == property_num + class SetupModule(Module): pass