import sys
import os
import datetime
from peewee import *
import json


from utils.DBModels.BaseModel import BaseModel

sys.path.append('../')
sys.path.append('../../../')


    

class PressureTranslationBlock(BaseModel):
    index = CharField()
    paramName = CharField()
    objectType = CharField()
    dataType = CharField()
    saveType = CharField()
    dataSize = CharField()
    accessType = CharField()
    transferType = CharField()
    description = CharField()
    createTime = CharField()

     # 查询设备是否存在
    @classmethod
    def getallParame(cls):
        params = cls.get_all()
        if params is 'error':
            return
        l = []
        for x in params:
            l.append([x.index, x.paramName, x.description, x.dataType, x.accessType, x.dataSize])
        return l

    @classmethod
    def getByName(cls, paramName):
        try:
            return cls.get(cls.paramName == str(paramName))
        except Exception as e:
            return 

    def addParame(self, index, paramName, objectType, dataType, saveType, dataSize, accessType, transferType, description):
        self.index = index
        self.paramName = paramName
        self.objectType = objectType
        self.dataType = dataType
        self.saveType = saveType
        self.dataSize = dataSize
        self.accessType = accessType
        self.transferType = transferType
        self.description = description
        self.createTime = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')
        # print(self.createTime)
        self.save()

  


class PhysicalBlock(PressureTranslationBlock):
    index = CharField()
    paramName = CharField()
    objectType = CharField()
    dataType = CharField()
    saveType = CharField()
    dataSize = CharField()
    accessType = CharField()
    transferType = CharField()
    description = CharField()
    createTime = CharField()



class AIFunctionBlock(PressureTranslationBlock):
    index = CharField()
    paramName = CharField()
    objectType = CharField()
    dataType = CharField()
    saveType = CharField()
    dataSize = CharField()
    accessType = CharField()
    transferType = CharField()
    description = CharField()
    createTime = CharField()

class TemperatureTranslationBlock(PressureTranslationBlock):
    index = CharField()
    paramName = CharField()
    objectType = CharField()
    dataType = CharField()
    saveType = CharField()
    dataSize = CharField()
    accessType = CharField()
    transferType = CharField()
    description = CharField()
    createTime = CharField()

class LevelTranslationBlock(PressureTranslationBlock):
    index = CharField()
    paramName = CharField()
    objectType = CharField()
    dataType = CharField()
    saveType = CharField()
    dataSize = CharField()
    accessType = CharField()
    transferType = CharField()
    description = CharField()
    createTime = CharField()


class FlowTranslationBlock(PressureTranslationBlock):
    index = CharField()
    paramName = CharField()
    objectType = CharField()
    dataType = CharField()
    saveType = CharField()
    dataSize = CharField()
    accessType = CharField()
    transferType = CharField()
    description = CharField()
    createTime = CharField()
    

class UnitTable(BaseModel):
    unitValue = CharField()
    unitSymbol = CharField()
    description = CharField()
    createTime = CharField()

     # 查询设备是否存在
    @classmethod
    def getallParame(cls):
        devices = cls.get_all()
        if devices is 'error':
            return
        l = []
        for x in devices:
            l.append([x.unitValue, x.unitSymbol, x.description])
        return l

    @classmethod
    def getByValue(cls, unitValue):
        try:
            return cls.get(cls.unitValue == str(unitValue))
        except Exception as e:
            return 

    def addParame(self, unitValue, unitSymbol, description):
        self.unitValue = unitValue
        self.unitSymbol = unitSymbol
        self.description = description
        self.createTime = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')
        # print(self.createTime)
        self.save()
