from celery import Task, shared_task import os import sys import pickle import time import sqlite3 from peewee import * sys.path.append('../../../') sys.path.append('../') # from protocol.ModBus.rtumaster_example import RTUMaster from protocol.Hart.HartProtocol import HartProtocol from protocol.Celery.MBRTUMaster import app from utils.DBModels.BaseModel import * from utils.DBModels.ProjectBaseModel import * from utils.DBModels.ClientModels import ClientDB class HARTTask(Task): _hartMaster = None _cursor = None _dbPath = None def __init__(self): super(HARTTask, self).__init__() # @property # def dbPath(self): # if self._dbPath is None: # dbPath = os.path.join('../../../', 'db', 'client.db') # self.clientDB = SqliteDatabase(dbPath) # self.client_proxy = client_proxy # self.client_proxy.initialize(self.clientDB) # self.clientDB.connect() # name = ClientDB.getByName().value # self._dbPath = os.path.join('../../../project', name, 'db', 'project.db') # return self._dbPath @property def hartMaster(self): if self._hartMaster is None: hartMaster = HartProtocol() self._hartMaster = hartMaster return self._hartMaster @shared_task(base = HARTTask, bind = True) def readValues(self): # 读取变量 hartMaster = self.hartMaster app.backend.set('HART', str(self.request.id)) lis = hartMaster.readValues() return lis