Я делаю простой регистратор температуры, я использую mqtt вместо http. Когда я пытаюсь сохранить временную шкалу в моей базе данных с помощью приведенного ниже примера, я получаю сообщение об ошибке ..
No application found. Either work inside a view function or push an application context.
Но я не могу понять, как импортировать контекст приложения в свой файл, чтобы я мог записать в базу данных , Как импортировать, чтобы получить доступ к моему приложению в mqtt_logi c .py? Я действительно столкнулся с препятствиями на этом ..
mqtt_logi c .py
@mqtt.on_topic('test/logthis')
def handle_mytopic(client, userdata, message):
print('Saving to db on this special topic {}: {}'.format(message.topic, message.payload.decode()))
temp = message.payload.decode()
temp_log = temp_logging(
sensor = 'test_sensor',
temp = float(temp)
)
db.session.add(temp_log)
db.session.commit()
init .py
def create_app(config_file='config.py'):
app = Flask(__name__)
app.config.from_pyfile(config_file)
db.init_app(app)
mqtt.init_app(app)
with app.app_context():
from .mqtt_logic import handle_connect, handle_mqtt_message, handle_mytopic
return app