diff --git a/api/documents.py b/api/documents.py index 8c5bfec..ba167e7 100644 --- a/api/documents.py +++ b/api/documents.py @@ -75,6 +75,11 @@ class Django: 'name', 'birthday', 'content', + 'weight', + 'gender', + 'is_neutered', + 'activity_level', + 'der' ] diff --git a/api/management/commands/callback/recordCallBack.py b/api/management/commands/callback/recordCallBack.py index d2d8b08..5fcd901 100644 --- a/api/management/commands/callback/recordCallBack.py +++ b/api/management/commands/callback/recordCallBack.py @@ -19,6 +19,12 @@ def temperatureAndHumidityCallBack(topic: str, body: str, ch=None, method=None, logger.info("[Humidity] Received " + str(data["Humidity"])) # ch.basic_ack(delivery_tag=method.delivery_tag) # machine = models.Machine.objects.get(name=data["machineId"]) + + # Check for abnormal temperature + temperature = float(data["Temperature"]) + if temperature > 27 or temperature < 20: + raise Exception("溫度異常") + try: machine = models.Machine.objects.get(name=topic.split("/")[1]) except models.Machine.DoesNotExist: @@ -44,10 +50,19 @@ def temperatureAndHumidityCallBack(topic: str, body: str, ch=None, method=None, def weightCallBack(topic: str, body: str, ch=None, method=None, properties=None): data = json.loads(body) logger.info("[Weight] Received " + str(data["Weight"])) + + weight = float(data["Weight"]) + if weight < 0: + raise Exception("進食問題") + # ch.basic_ack(delivery_tag=method.delivery_tag) def waterCallBack(topic: str, body: str, ch=None, method=None, properties=None): data = json.loads(body) logger.info("[Water] Received " + str(data["Water"])) + + water = float(data["Water"]) + if water < 0: + raise Exception("攝取水量問題") # ch.basic_ack(delivery_tag=method.delivery_tag) diff --git a/api/management/commands/recordListener.py b/api/management/commands/recordListener.py deleted file mode 100644 index 7f6b446..0000000 --- a/api/management/commands/recordListener.py +++ /dev/null @@ -1,25 +0,0 @@ -from django.core.management.base import BaseCommand -from PetMonitoringSystemBackend.settings import RABBITMQ_CONFIG - - -class Command(BaseCommand): - def handle(self, *args, **options): - from api.utils.Rabbitmqserver import RabbitmqServer - from api.management.commands.callback import recordCallBack,machineCallBack - if RABBITMQ_CONFIG["enable"]: - rabbitmqClient = RabbitmqServer( - username=RABBITMQ_CONFIG["username"], - password=RABBITMQ_CONFIG["password"], - serverip=RABBITMQ_CONFIG["serverip"], - port=RABBITMQ_CONFIG["port"], - virtual_host=RABBITMQ_CONFIG["vhost"] - ) - - rabbitmqClient.connect() - # Listen Record - rabbitmqClient.expense("distance/#", recordCallBack.distanceCallBack) - rabbitmqClient.expense("temperature/#", recordCallBack.temperatureAndHumidityCallBack) - rabbitmqClient.expense("weight/#", recordCallBack.weightCallBack) - rabbitmqClient.expense("water/#", recordCallBack.waterCallBack) - # Listen Machine - rabbitmqClient.expense("machine/status/#", machineCallBack.machineCallBack) diff --git a/api/models.py b/api/models.py index 77fb42b..d82179e 100644 --- a/api/models.py +++ b/api/models.py @@ -18,11 +18,23 @@ def __str__(self): class Pet(models.Model): + ACTIVITY_LEVEL = [ + ('low', 'Low'), + ('moderate', 'Moderate'), + ('high', 'High') + ] + name = models.CharField(max_length=256, verbose_name="寵物名稱") keeper = models.ForeignKey(to=User, on_delete=models.CASCADE, verbose_name="寵物照顧人") type = models.ForeignKey(to=PetType, on_delete=models.CASCADE, verbose_name="寵物種類") birthday = models.DateField(verbose_name="寵物生日", blank=True, null=True, default=datetime.date.today) content = models.TextField(verbose_name="寵物敘述") + weight = models.DecimalField(max_digits=5, decimal_places=2, verbose_name="寵物重量", default=0) # 新增寵物重量欄位 + gender = models.BooleanField(default=False, verbose_name="性別") # 新增性別欄位 + is_neutered = models.BooleanField(default=False, verbose_name="是否結紮") # 新增結紮欄位 + activity_level = models.CharField(max_length=10, + choices=ACTIVITY_LEVEL, default='Low') + der = models.DecimalField(max_digits=6, decimal_places=2, default=0, verbose_name="每日能量需求") # der欄位 image = models.ImageField(upload_to=upload_to, blank=True, null=True) # External Columns diff --git a/api/serializers.py b/api/serializers.py index a8bbece..634cbdc 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -23,8 +23,35 @@ class PetRequestSerializer(serializers.ModelSerializer): class Meta: model = Pet - fields = ['id', 'name', 'keeper', 'type', 'birthday', 'content', 'image'] - read_only_fields = ('id',) + fields = ['id', 'name', 'keeper', 'type', 'birthday', 'content', 'image', 'weight', 'gender', + 'is_neutered', 'activity_level', 'der'] + read_only_fields = ('id', 'der') + + def calculate_resting_energy_requirement(self, weight): + return 70 * (weight ** 0.75) + + def calculate_daily_energy_requirement(self, weight, activity_level): + activity_levels = { + 'low': 1.2, + 'moderate': 1.4, + 'high': 1.6, + } + + if activity_level not in activity_levels: + raise serializers.ValidationError("Invalid activity level") + + levels = activity_levels[activity_level] + return levels * self.calculate_resting_energy_requirement(float(weight)) + + def create(self, validated_data): + weight = validated_data.get('weight', 0) + activity_level = validated_data.get('activity_level') + + if activity_level: + der = self.calculate_daily_energy_requirement(weight, activity_level) + validated_data['der'] = der + + return super().create(validated_data) class PetSerializer(serializers.ModelSerializer): @@ -34,7 +61,8 @@ class PetSerializer(serializers.ModelSerializer): class Meta: model = Pet - fields = ['id', 'name', 'keeper', 'type', 'birthday', 'content', 'image'] + fields = ['id', 'name', 'keeper', 'type', 'birthday', 'content', 'image', 'weight', 'gender', + 'is_neutered', 'activity_level', ] read_only_fields = ('id',) depth = 1 @@ -45,7 +73,8 @@ class PetUploadImageSerializer(serializers.ModelSerializer): class Meta: model = Pet fields = ['id', 'image'] - read_only_fields = ('id', 'name', 'keeper', 'type', 'birthday', 'content') + read_only_fields = ('id', 'name', 'keeper', 'type', 'birthday', 'content', 'weight', 'gender', + 'is_neutered', 'activity_level', 'der',) depth = 1 diff --git a/api/views/petViews.py b/api/views/petViews.py index 229cdb4..63bd7e5 100644 --- a/api/views/petViews.py +++ b/api/views/petViews.py @@ -108,3 +108,5 @@ def post(self, request: Request, pk: int): pet.image = request.FILES.get("image") pet.save() return Response(data=PetSerializer(pet).data, status=status.HTTP_200_OK) + +