Skip to content
Snippets Groups Projects
Unverified Commit 7303ffe6 authored by REIG Julien's avatar REIG Julien
Browse files

add presence array in resource

parent ccd5ac52
No related branches found
No related tags found
1 merge request!16Node knowledge
......@@ -14,9 +14,10 @@ class Resource:
self.commands = [] if commands is None else commands
self.commands_count = len(self.commands)
self.deleted = False
self.presences = []
def __str__(self):
return f"Resource(id={self.id}, name={self.name}, last_update={self.last_update}, commands={self.commands}, commands_count={self.commands_count}, deleted={self.deleted})"
return f"Resource(id={self.id}, name={self.name}, last_update={self.last_update}, commands={self.commands}, commands_count={self.commands_count}, deleted={self.deleted}, presence={self.presences})"
def __repr__(self):
return self.__str__()
......@@ -28,11 +29,14 @@ class Resource:
self.commands_count = len(self.commands)
@staticmethod
def from_tuple(data, commands: list[Command] = None, commands_count: int = None, deleted: bool = False):
def from_tuple(data, commands: list[Command] = None, commands_count: int = None, deleted: bool = False, presence: list[str] = None):
resource = Resource(data[1])
resource.id = data[0]
resource.last_update = data[2]
resource.deleted = deleted
if presence is not None:
resource.presences = presence
if (commands is not None):
resource.commands = commands
resource.commands_count = len(resource.commands)
......@@ -49,25 +53,40 @@ class Resource:
resource.commands = [Command.from_full_dict(
command) for command in data["commands"]]
resource.commands_count = len(resource.commands)
resource.presences = data.get("presence", [])
return resource
# This method returns a tuple with the following structure:
# (id, name, last_update)
# used to insert a resource into the database
def to_tuple(self):
return (self.id, self.name, self.last_update)
def to_presence_tuples(self):
return [(self.id, presence) for presence in self.presences]
def to_full_dict(self):
return {
def to_full_dict(self, include_presence: bool = False):
full_dict = {
"id": self.id,
"name": self.name,
"lastUpdate": self.last_update,
"deleted": self.deleted,
"commands": [command.to_dict() for command in self.commands]
}
if include_presence:
full_dict["presence"] = self.presences
return full_dict
def to_simple_dict(self):
return {
def to_simple_dict(self, include_presence: bool = False):
simple_dict = {
"id": self.id,
"name": self.name,
"lastUpdate": self.last_update,
"deleted": self.deleted,
"commandsCount": self.commands_count
}
if include_presence:
simple_dict["presence"] = self.presences
return simple_dict
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment