Commit fd3be408 authored by arun.uday's avatar arun.uday

migrate to gitlab-pm

parents
import uvicorn
from scripts.config.applications_config import uvicorn_host, uvicorn_port, uvicorn_app
from scripts.logging.loggers import logger
# starting the application
if __name__ == "__main__":
try:
print("redis MQTT task")
uvicorn.run("main:app", host=uvicorn_host, port=int(uvicorn_port))
except Exception as e:
logger.error("Interruption occurred: ", e)
[path]
base_path = scripts/
sub_path = temp/
log_path = log/
[mqtt]
topic = receptionist
mqtt_host = 192.168.0.220
port = 1883
requests = 60
[uvicorn]
uvicorn_host = 127.0.0.1
uvicorn_port = 8001
# database servers
[connection]
mongodb = mongodb://localhost:27017
[redis_db]
redis_host = 127.0.0.1
db_queue_doctor = 0
[encode]
encode = utf-8
[api_routes]
index_route = /
[log]
formatter_time = asctime
formatter_level = levelname
import uvicorn
from fastapi import FastAPI
from scripts.config.applications_config import uvicorn_host, uvicorn_port
from scripts.logging.loggers import logger
from scripts.services.receiver_app_run import receiver_mqtt
app = FastAPI()
app.include_router(receiver_mqtt)
# starting the application
if __name__ == "__main__":
try:
print("redis MQTT task")
uvicorn.run(app, host=uvicorn_host, port=int(uvicorn_port))
except Exception as e:
logger.error("Interruption occurred: ", e)
import configparser
config = configparser.RawConfigParser()
config.read("conf/application.conf")
# path
base_path = config.get("path", 'base_path')
sub_path = config.get("path", "sub_path")
log_path = config.get("path", "log_path")
# log
formatter_time = config.get("log", "formatter_time")
formatter_level = config.get("log", "formatter_level")
# mqtt
topic_name = config.get("mqtt", "topic")
mqtt_host = config.get("mqtt", "mqtt_host")
port = config.get("mqtt", "port")
request_no = config.get("mqtt", "requests")
# db connection
client_connect = config.get("connection", "mongodb")
# uvicorn
uvicorn_host = config.get("uvicorn", "uvicorn_host")
uvicorn_port = config.get("uvicorn", "uvicorn_port")
# redis
redis_host = config.get("redis_db", "redis_host")
db_queue_doctor = config.get("redis_db", "db_queue_doctor")
# encode
utf_encode = config.get("encode", "encode")
import configparser
config = configparser.RawConfigParser()
config.read("conf/application.conf")
# api routes
index_route = config.get("api_routes", 'index_route')
# connecting to the redis
import redis
from scripts.config.applications_config import redis_host, db_queue_doctor
conn = redis.Redis(redis_host, db=db_queue_doctor)
# patient to the queue
from scripts.logging.loggers import logger
def insert_data_redis(conn_queue, doctor, patient):
try:
# set the patient to the doctors queue
conn_queue.set(doctor, patient)
print(doctor, " assigned to ", patient)
except Exception as e:
logger.error("Exception occurred while inserting to redis: ", e)
import functools
from paho.mqtt.client import Client
from scripts.core.handlers.connect_topic import on_connect
from scripts.core.handlers.message_read import on_message_handler
from scripts.logging.loggers import logger
def connecting_mqtt(mqtt_host, port, request_no):
try:
# creating the paho client for mqtt
client = Client()
client.connect(mqtt_host, int(port), int(request_no))
client.on_connect = on_connect
client.on_message = functools.partial(on_message_handler)
client.loop_forever()
except Exception as e:
logger.error("Exception occurred while connecting to mqtt: ", e)
# subscribe to the doctor topics
from scripts.core.database.redis_db import conn
from scripts.core.handlers.reading_doctors import read_doctors
def on_connect(client_name, userdata, flags, rc):
print(userdata, " ", flags)
print("Connected with result code " + str(rc))
read_doctors(conn, client_name)
# printing the messages
from scripts.config.applications_config import utf_encode
from scripts.core.database.redis_db import conn
from scripts.core.handlers.add_patient_queue import insert_data_redis
def on_message_handler(client_name, userdata, msg):
# decoding the msg to string
print(client_name, " ", userdata)
payload_decoded = msg.payload.decode(utf_encode)
# add patient to the queue
insert_data_redis(conn, msg.topic, payload_decoded)
# get the doctor topics to subscribe
from scripts.logging.loggers import logger
def read_doctors(conn, client_name):
try:
no_doctors = conn.get("Doctors")
doctors_list = []
for doctors in range(0, int(no_doctors)):
tup_doctors = ('doctor' + str(doctors), 0)
doctors_list.append(tup_doctors)
client_name.subscribe(doctors_list)
except Exception as e:
logger.error("Exception occurred while reading db: ", e)
import logging
import os
from logging.handlers import RotatingFileHandler
from scripts.config import applications_config
from scripts.config.applications_config import formatter_time, formatter_level
def get_logger():
"""
Creates a rotating log
"""
__logger__ = logging.getLogger('')
__logger__.setLevel(logging.INFO)
log_formatter = f'%({formatter_time})s - %({formatter_level})-6s - %(message)s'
time_format = "%Y-%m-%d %H:%M:%S"
file_path = applications_config.base_path + applications_config.sub_path + applications_config.log_path
formatter = logging.Formatter(log_formatter, time_format)
if not os.path.exists(file_path):
os.makedirs(file_path)
log_file = os.path.join(f"{file_path}{applications_config.topic_name}.log")
temp_handler = RotatingFileHandler(log_file, maxBytes=1)
temp_handler.setFormatter(formatter)
__logger__.addHandler(temp_handler)
return __logger__
logger = get_logger()
from fastapi import APIRouter
from scripts.constants.api_route_config import index_route
from scripts.config.applications_config import mqtt_host, port, request_no
from scripts.core.handlers.connect_mqtt_receive import connecting_mqtt
from scripts.logging.loggers import logger
# This is the Subscriber
receiver_mqtt = APIRouter()
# subscribing to the doctor topics and getting the messages
@receiver_mqtt.get(index_route)
def receiver():
try:
connecting_mqtt(mqtt_host, port, request_no)
except Exception as e:
logger.error("Exception occurred while connecting to mqtt", e)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment