89 lines
2.8 KiB
Python
89 lines
2.8 KiB
Python
"""
|
|
设备模块序列化器
|
|
"""
|
|
from rest_framework import serializers
|
|
from .models import DeviceType, DeviceBatch, Device, UserDevice
|
|
|
|
|
|
class DeviceTypeSerializer(serializers.ModelSerializer):
|
|
"""设备类型序列化器"""
|
|
|
|
class Meta:
|
|
model = DeviceType
|
|
fields = ['id', 'brand', 'product_code', 'name', 'is_network_required', 'is_active', 'created_at']
|
|
read_only_fields = ['id', 'is_network_required', 'created_at']
|
|
|
|
|
|
class DeviceBatchSerializer(serializers.ModelSerializer):
|
|
"""设备批次序列化器"""
|
|
|
|
device_type_info = DeviceTypeSerializer(source='device_type', read_only=True)
|
|
|
|
class Meta:
|
|
model = DeviceBatch
|
|
fields = ['id', 'device_type', 'device_type_info', 'batch_no', 'production_date',
|
|
'production_week', 'quantity', 'remark', 'status', 'created_at']
|
|
read_only_fields = ['id', 'production_week', 'status', 'created_at']
|
|
|
|
|
|
class DeviceBatchCreateSerializer(serializers.ModelSerializer):
|
|
"""创建设备批次序列化器"""
|
|
|
|
class Meta:
|
|
model = DeviceBatch
|
|
fields = ['device_type', 'batch_no', 'production_date', 'quantity', 'remark']
|
|
|
|
|
|
class DeviceSerializer(serializers.ModelSerializer):
|
|
"""设备序列化器"""
|
|
|
|
device_type_info = DeviceTypeSerializer(source='device_type', read_only=True)
|
|
|
|
class Meta:
|
|
model = Device
|
|
fields = ['id', 'sn', 'device_type', 'device_type_info', 'mac_address',
|
|
'name', 'status', 'firmware_version', 'last_online_at', 'created_at']
|
|
|
|
|
|
class DeviceSimpleSerializer(serializers.ModelSerializer):
|
|
"""设备简单序列化器"""
|
|
|
|
class Meta:
|
|
model = Device
|
|
fields = ['id', 'sn', 'mac_address', 'status', 'created_at']
|
|
|
|
|
|
class UserDeviceSerializer(serializers.ModelSerializer):
|
|
"""用户设备绑定序列化器"""
|
|
|
|
device = DeviceSerializer(read_only=True)
|
|
spirit_name = serializers.CharField(source='spirit.name', read_only=True, allow_null=True)
|
|
|
|
class Meta:
|
|
model = UserDevice
|
|
fields = ['id', 'device', 'spirit', 'spirit_name', 'bind_type', 'bind_time', 'is_active']
|
|
|
|
|
|
class BindDeviceSerializer(serializers.Serializer):
|
|
"""绑定设备序列化器"""
|
|
|
|
sn = serializers.CharField(max_length=50, help_text='设备SN码')
|
|
spirit_id = serializers.IntegerField(required=False, allow_null=True, help_text='智能体ID')
|
|
|
|
|
|
class DeviceVerifySerializer(serializers.Serializer):
|
|
"""验证设备SN序列化器"""
|
|
|
|
sn = serializers.CharField(max_length=50, help_text='设备SN码')
|
|
|
|
|
|
class QueryByMacSerializer(serializers.Serializer):
|
|
"""通过MAC查询SN序列化器"""
|
|
|
|
mac = serializers.CharField(max_length=20, help_text='MAC地址')
|
|
|
|
def validate_mac(self, value):
|
|
# 统一MAC地址格式
|
|
value = value.upper().replace('-', ':')
|
|
return value
|