Skip to content

Commit

Permalink
Merge pull request #66 from fan9704/fix_dependencies
Browse files Browse the repository at this point in the history
fix: Listener Exception Error
  • Loading branch information
fan9704 authored Oct 19, 2023
2 parents 0ac1745 + 137c794 commit 9c526ec
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
11 changes: 3 additions & 8 deletions api/management/commands/callback/machineCallBack.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@ def machine_callback(topic: str, body: str, ch=None, method=None, properties=Non
logger.debug(f"Topic:{topic} CH:{ch} Method:{method} Properties:{properties}")

content = json.loads(body)
try:
machine = Machine.objects.get(
name=content["machineId"]
)
except Machine.DoesNotExist:
machine = Machine.objects.create(
name=content["machineId"]
)
machine = Machine.objects.get_or_create(
name=content["machineId"]
)
if content["onlineStatus"]:
machine.onlineStatus = True
else:
Expand Down
26 changes: 17 additions & 9 deletions api/management/commands/callback/recordCallBack.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from api import models
from api.management.commands.logger.commandLogger import CommandLogger
from api.utils.notify import notify

logger = CommandLogger(__name__).get_logger()

Expand All @@ -19,15 +20,16 @@ def temperature_humidity_callback(topic: str, body: str, ch=None, method=None, p

# Check for abnormal temperature
temperature = float(data["Temperature"])
humidity = float(data["Humidity"])
if temperature > 27 or temperature < 20:
raise ValueError("溫度異常")
notify_content = f'''
[寵物環境溫度] {temperature}
[寵物環境濕度] {humidity}
'''
notify("溫度異常", notify_content, 1)
# Todo: 溫度異常
machine = models.Machine.objects.get_or_create(name=topic.split("/")[1])

try:
machine = models.Machine.objects.get(name=topic.split("/")[1])
except models.Machine.DoesNotExist:
machine = models.Machine.objects.create(
name=topic.split("/")[1]
)
temperature_record_type = models.RecordType.objects.get(type="temperature")
humidity_record_type = models.RecordType.objects.get(type="humidity")
temperature = models.Record.objects.create(
Expand All @@ -51,7 +53,10 @@ def weight_callback(topic: str, body: str, ch=None, method=None, properties=None

weight = float(data["Weight"])
if weight < 0:
raise ValueError("進食問題")
notify_content = f'''
[寵物進食量] {weight}
'''
notify("進食過少", notify_content, 1)


def water_callback(topic: str, body: str, ch=None, method=None, properties=None):
Expand All @@ -61,4 +66,7 @@ def water_callback(topic: str, body: str, ch=None, method=None, properties=None)

water = float(data["Water"])
if water < 0:
raise ValueError("攝取水量問題")
notify_content = f'''
[寵物喝水量] {water}
'''
notify("喝水過少", notify_content, 1)

0 comments on commit 9c526ec

Please sign in to comment.