from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType


class SmartModule(models.Model):
    name = models.CharField(max_length=50, verbose_name='模块名称')
    desc = models.CharField(max_length=200, verbose_name='简介')
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')
    device_id = models.CharField(max_length=100, verbose_name='模块ID')
    chip_id = models.CharField(max_length=100, verbose_name='单片机ID')

    class Meta:
        verbose_name = '智能模块'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class SmartPush(models.Model):
    smart = models.ForeignKey(SmartModule, on_delete=models.PROTECT, verbose_name='智能模块')
    signal = models.IntegerField(verbose_name='信号')
    coordinate = models.CharField(max_length=100, verbose_name='坐标')
    time = models.DateTimeField(auto_now=False, auto_now_add=True)

    class Meta:
        verbose_name = '智能模块数据'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '{0}: {1}'.format(self.smart.name, self.signal)

    def get_api_url(self):
        pass

