Commit 47ab9e24 by arun.uday

v5

parent c5dd2c9a
...@@ -4,14 +4,16 @@ ...@@ -4,14 +4,16 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="530f9a40-4e97-41f4-9693-adc3b4faba7d" name="Changes" comment="v3"> <list default="true" id="530f9a40-4e97-41f4-9693-adc3b4faba7d" name="Changes" comment="v4">
<change afterPath="$PROJECT_DIR$/scripts/core/database/redis_db.py" afterDir="false" /> <change afterPath="$PROJECT_DIR$/main.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/scripts/core/handlers/add_patient_queue.py" afterDir="false" /> <change afterPath="$PROJECT_DIR$/scripts/core/handlers/connect_mqtt_receive.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/sonarlint/issuestore/index.pb" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/sonarlint/issuestore/index.pb" afterDir="false" /> <change afterPath="$PROJECT_DIR$/scripts/core/handlers/connect_topic.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/scripts/core/handlers/message_read.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/scripts/logging/loggers.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app.py" beforeDir="false" afterPath="$PROJECT_DIR$/app.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/conf/application.conf" beforeDir="false" afterPath="$PROJECT_DIR$/conf/application.conf" afterDir="false" /> <change beforePath="$PROJECT_DIR$/conf/application.conf" beforeDir="false" afterPath="$PROJECT_DIR$/conf/application.conf" afterDir="false" />
<change beforePath="$PROJECT_DIR$/scripts/config/applications_config.py" beforeDir="false" afterPath="$PROJECT_DIR$/scripts/config/applications_config.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/scripts/config/applications_config.py" beforeDir="false" afterPath="$PROJECT_DIR$/scripts/config/applications_config.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/scripts/core/engine/redis_db.py" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/scripts/services/receiver_app_run.py" beforeDir="false" afterPath="$PROJECT_DIR$/scripts/services/receiver_app_run.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/scripts/services/receiver_app_run.py" beforeDir="false" afterPath="$PROJECT_DIR$/scripts/services/receiver_app_run.py" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
...@@ -43,12 +45,17 @@ ...@@ -43,12 +45,17 @@
"RunOnceActivity.OpenProjectViewOnStart": "true", "RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true", "RunOnceActivity.ShowReadmeOnStart": "true",
"SONARLINT_PRECOMMIT_ANALYSIS": "true", "SONARLINT_PRECOMMIT_ANALYSIS": "true",
"last_opened_file_path": "E:/KL projects/task9receiverredis", "last_opened_file_path": "E:/KL projects/task9receiverredis/main.py",
"settings.editor.selected.configurable": "preferences.pluginManager" "settings.editor.selected.configurable": "preferences.pluginManager"
} }
}]]></component> }]]></component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="E:\KL projects\task9receiverredis\scripts" />
</key>
</component>
<component name="RunManager"> <component name="RunManager">
<configuration name="app" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true"> <configuration name="main" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true">
<module name="redis receiver" /> <module name="redis receiver" />
<option name="INTERPRETER_OPTIONS" value="" /> <option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" /> <option name="PARENT_ENVS" value="true" />
...@@ -61,7 +68,7 @@ ...@@ -61,7 +68,7 @@
<option name="IS_MODULE_SDK" value="false" /> <option name="IS_MODULE_SDK" value="false" />
<option name="ADD_CONTENT_ROOTS" value="true" /> <option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" /> <option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/app.py" /> <option name="SCRIPT_NAME" value="$PROJECT_DIR$/main.py" />
<option name="PARAMETERS" value="" /> <option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" /> <option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" /> <option name="EMULATE_TERMINAL" value="false" />
...@@ -101,24 +108,21 @@ ...@@ -101,24 +108,21 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1675318625371</updated> <updated>1675318625371</updated>
</task> </task>
<option name="localTasksCounter" value="4" /> <task id="LOCAL-00004" summary="v4">
<created>1675663187416</created>
<option name="number" value="00004" />
<option name="presentableId" value="LOCAL-00004" />
<option name="project" value="LOCAL" />
<updated>1675663187416</updated>
</task>
<option name="localTasksCounter" value="5" />
<servers /> <servers />
</component> </component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<MESSAGE value="v1" /> <MESSAGE value="v1" />
<MESSAGE value="v2" /> <MESSAGE value="v2" />
<MESSAGE value="v3" /> <MESSAGE value="v3" />
<option name="LAST_COMMIT_MESSAGE" value="v3" /> <MESSAGE value="v4" />
</component> <option name="LAST_COMMIT_MESSAGE" value="v4" />
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" suspend="THREAD" type="python-line">
<url>file://$PROJECT_DIR$/scripts/services/receiver_app_run.py</url>
<line>9</line>
<option name="timeStamp" value="1" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
</component> </component>
</project> </project>
\ No newline at end of file
import uvicorn import uvicorn
from scripts.config.applications_config import uvicorn_host, uvicorn_port, uvicorn_app from scripts.config.applications_config import uvicorn_host, uvicorn_port, uvicorn_app
from scripts.logging.loggers import logger
# starting the application # starting the application
if __name__ == "__main__": if __name__ == "__main__":
print("redis MQTT task") try:
uvicorn.run(uvicorn_app, host=uvicorn_host, port=int(uvicorn_port)) 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] [path]
base_path = scripts/ base_path = scripts/
sub_path = temp/ sub_path = temp/
log_path = log/
[mqtt] [mqtt]
topic = receptionist topic = receptionist
......
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)
...@@ -8,6 +8,7 @@ config.read("conf/application.conf") ...@@ -8,6 +8,7 @@ config.read("conf/application.conf")
# path # path
base_path = config.get("path", 'base_path') base_path = config.get("path", 'base_path')
sub_path = config.get("path", "sub_path") sub_path = config.get("path", "sub_path")
log_path = config.get("path", "log_path")
# mqtt # mqtt
......
# patient to the queue # patient to the queue
from scripts.logging.loggers import logger
def insert_data_redis(conn_queue, doctor, patient): def insert_data_redis(conn_queue, doctor, patient):
# set the patient to the doctors queue try:
conn_queue.set(doctor, patient) # set the patient to the doctors queue
print(doctor, " assigned to ", patient) 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 # get the doctor topics to subscribe
from scripts.logging.loggers import logger
def read_doctors(conn, client_name): def read_doctors(conn, client_name):
no_doctors = conn.get("Doctors") try:
doctors_list = [] no_doctors = conn.get("Doctors")
for doctors in range(0, int(no_doctors)): doctors_list = []
tup_doctors = ('doctor' + str(doctors), 0) for doctors in range(0, int(no_doctors)):
doctors_list.append(tup_doctors) tup_doctors = ('doctor' + str(doctors), 0)
client_name.subscribe(doctors_list) 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
def get_logger():
"""
Creates a rotating log
"""
__logger__ = logging.getLogger('')
__logger__.setLevel(logging.INFO)
log_formatter = '%(asctime)s - %(levelname)-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()
import functools from fastapi import APIRouter
import paho.mqtt.client as mqtt
from fastapi import FastAPI
from scripts.config.api_route_config import index_route from scripts.config.api_route_config import index_route
from scripts.config.applications_config import mqtt_host, port, request_no, utf_encode from scripts.config.applications_config import mqtt_host, port, request_no
from scripts.core.database.redis_db import conn from scripts.core.handlers.connect_mqtt_receive import connecting_mqtt
from scripts.core.handlers.add_patient_queue import insert_data_redis from scripts.logging.loggers import logger
from scripts.core.handlers.reading_doctors import read_doctors
# This is the Subscriber # This is the Subscriber
app = FastAPI() receiver_mqtt = APIRouter()
# subscribing to the doctor topics and getting the messages # subscribing to the doctor topics and getting the messages
@app.get(index_route) @receiver_mqtt.get(index_route)
def receiver(): def receiver():
# subscribe to the doctor topics try:
def on_connect(client_name, userdata, flags, rc): connecting_mqtt(mqtt_host, port, request_no)
print(userdata, " ", flags) except Exception as e:
print("Connected with result code " + str(rc)) logger.error("Exception occurred while connecting to mqtt", e)
read_doctors(conn, client_name)
# printing the messages
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)
# creating the paho client for mqtt
client = mqtt.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()
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