Skip to content

Commit

Permalink
저장 형식 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick0503 committed Jun 22, 2024
1 parent 066ff82 commit 30c16bd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
59 changes: 36 additions & 23 deletions autosink_data_elt/log/filehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ def read_file(self):
data = json.load(file)
version = data.get('version', 1)
if version == 1:
logger.info(f'Read data with version 1 from {self.filename}')
logger.info(
f'Read data with version 1 from {self.filename}')
return DishwashingDataV1(**data)
elif version == 2:
logger.info(f'Read data with version 2 from {self.filename}')
logger.info(
f'Read data with version 2 from {self.filename}')
return DishwashingDataV2(**data)
else:
logger.error(f'Unsupported version: {version}')
Expand All @@ -58,27 +60,27 @@ def write_file(self, data):
self.image_counter = 0 # Reset the image counter after writing to file
logger.info(f'Wrote data to {self.filename} and reset image counter')

def create_default_data(self, dishwashing_start=None, version=2, deque_size=10):
def create_default_data(self,
dishwashing_start=None,
version=2,
deque_size=10):
if dishwashing_start is None:
dishwashing_start = datetime.now(self.timezone).isoformat(timespec='seconds')
dishwashing_start = datetime.now(
self.timezone).isoformat(timespec='seconds')

if version == 1:
logger.info(f'Creating default data with version 1')
return DishwashingDataV1(
version=1,
user_id=self.user_id,
dishwashing_id=self.dishwashing_id,
dishwashing_start=dishwashing_start
)
return DishwashingDataV1(version=1,
user_id=self.user_id,
dishwashing_id=self.dishwashing_id,
dishwashing_start=dishwashing_start)
elif version == 2:
logger.info(f'Creating default data with version 2')
return DishwashingDataV2(
version=2,
user_id=self.user_id,
dishwashing_id=self.dishwashing_id,
dishwashing_start=dishwashing_start,
interaction=deque(maxlen=deque_size)
)
return DishwashingDataV2(version=2,
user_id=self.user_id,
dishwashing_id=self.dishwashing_id,
dishwashing_start=dishwashing_start,
interactions=deque(maxlen=deque_size))
else:
logger.error(f'Unsupported version: {version}')
raise ValueError(f'Unsupported version: {version}')
Expand All @@ -88,14 +90,16 @@ def add_interaction(self, data, **kwargs):
# logger.info(f'Adding interaction with image from kwargs')

if data.version == 1:
interaction = InteractionV1.create(timestamp, kwargs.pop('image'), **kwargs)
interaction = InteractionV1.create(timestamp, kwargs.pop('image'),
**kwargs)
elif data.version == 2:
interaction = InteractionV2.create(timestamp, kwargs.pop('image'), **kwargs)
interaction = InteractionV2.create(timestamp, kwargs.pop('image'),
**kwargs)
else:
logger.error(f'Unsupported version: {data.version}')
raise ValueError(f'Unsupported version: {data.version}')

data.interaction.append(interaction)
data.interactions.append(interaction)
# logger.info(f'Added interaction: {interaction}')
return data

Expand All @@ -109,9 +113,18 @@ def add_interaction(self, data, **kwargs):
data = file_handler.create_default_data(dishwashing_start, version=2)

# 상호작용 객체 생성 및 추가
data = file_handler.add_interaction(data, model_output=0, arduino_output=0, magnetic_status=0)
data = file_handler.add_interaction(data, model_output=0, arduino_output=0, magnetic_status=0)
data = file_handler.add_interaction(data, model_output=0, arduino_output=1, magnetic_status=1)
data = file_handler.add_interaction(data,
model_output=0,
arduino_output=0,
magnetic_status=0)
data = file_handler.add_interaction(data,
model_output=0,
arduino_output=0,
magnetic_status=0)
data = file_handler.add_interaction(data,
model_output=0,
arduino_output=1,
magnetic_status=1)

# 파일에 쓰기
file_handler.write_file(data)
Expand Down
12 changes: 6 additions & 6 deletions autosink_data_elt/log/template/dishwashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class DishwashingDataV1:
user_id: str
dishwashing_id: str
dishwashing_start: str
interaction: List[InteractionV1] = field(default_factory=list)
interactions: List[InteractionV1] = field(default_factory=list)

def to_dict(self):
data_dict = asdict(self)
data_dict['interaction'] = [
interaction.to_dict() for interaction in self.interaction
data_dict['interactions'] = [
interaction.to_dict() for interaction in self.interactions
]
return data_dict

Expand All @@ -29,12 +29,12 @@ class DishwashingDataV2:
user_id: str
dishwashing_id: str
dishwashing_start: str
interaction: Deque[InteractionV2] = field(
interactions: Deque[InteractionV2] = field(
default_factory=lambda: deque(maxlen=10))

def to_dict(self):
data_dict = asdict(self)
data_dict['interaction'] = [
interaction.to_dict() for interaction in self.interaction
data_dict['interactions'] = [
interaction.to_dict() for interaction in self.interactions
]
return data_dict

0 comments on commit 30c16bd

Please sign in to comment.