from smartfarming.models.device import MongoDevice
from rest_framework import serializers
from django.conf import settings

device_type = (settings.CONFIG).get("device_type_zh")
device_type_ck = (settings.CONFIG).get("device_type_ck")


class DeviceSerializers(serializers.ModelSerializer):

    device_type_name = serializers.SerializerMethodField()
    device_addr = serializers.SerializerMethodField()
    device_type = serializers.SerializerMethodField()

    def get_device_type(self, obj):
        return device_type_ck

    def get_device_type_name(self, obj):
        return device_type.get(str(obj.device_type_id))

    def get_device_addr(self, obj):
        return obj.province + obj.city + obj.district

    class Meta:
        model = MongoDevice
        fields = [
            "device_type_name",
            "device_type_id",
            "device_type",
            "device_id",
            "device_name",
            "device_addr",
            "lng",
            "lat",
            "device_status"
        ]
