Commit 47af8ab6 authored by arjun.b's avatar arjun.b

subscriber

parents
FILE_PATH=scripts/external/logging_error.log
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="N801" />
<option value="N803" />
</list>
</option>
</inspection_tool>
</profile>
</component>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (subscriber)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/subscriber.iml" filepath="$PROJECT_DIR$/.idea/subscriber.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
import uvicorn
from scripts.logging.logging import logger
if __name__ == "__main__":
try:
uvicorn.run("main:app", port=8765)
except Exception as e:
logger.error(e)
print(e)
import uvicorn
from fastapi import FastAPI
from scripts.logging.logging import logger
from scripts.services.subscribe import sub
app = FastAPI()
app.include_router(sub)
if __name__ == "__main__":
try:
uvicorn.run(app, port=8764)
except Exception as e:
logger.error(e)
print(e)
from dotenv import load_dotenv
import os
try:
load_dotenv()
file_name = os.getenv("FILE_PATH")
except Exception as e:
print(e)
import redis
# db on redis
try:
conn1 = redis.Redis('127.0.0.1', port=6379, db=3)
conn2 = redis.Redis('127.0.0.1', port=6379, db=4)
conn3 = redis.Redis('127.0.0.1', port=6379, db=5)
except Exception as e:
print(e)
class EndPoints:
root = "/"
subscribe = "/subscribe"
import json
from paho.mqtt import client as mqtt_client
from scripts.config.db_connect import conn1, conn2, conn3
broker = "192.168.0.220"
port = 1883
topic = "site"
def connect_mqtt() -> mqtt_client:
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
client = mqtt_client.Client()
client.on_connect = on_connect
client.connect(broker, port)
return client
def subscribe(client: mqtt_client):
def on_message(client, userdata, msg):
print(f"Received `{msg.payload.decode('utf-8')}` from `{msg.topic}` topic")
message = json.loads(msg.payload.decode('utf-8'))
key = message["site_id"]
value = json.dumps(message["data"])
if message["data_quality"] == 0:
conn1.set(key, value)
if message["data_quality"] == 1:
conn2.set(key, value)
if message["data_quality"] == 2:
conn3.set(key, value)
client.subscribe(topic)
client.on_message = on_message
def run():
client = connect_mqtt()
subscribe(client)
client.loop_forever()
import logging
import os
from logging.handlers import RotatingFileHandler
from scripts.config.application_config import file_name
def get_logger():
"""
Creates a rotating log
"""
__logger__ = logging.getLogger('')
__logger__.setLevel("ERROR")
log_formatter = '%(asctime)s - %(levelname)-6s - [%(threadName)5s:%(funcName)5s():%(lineno)s] - %(message)s'
time_format = "%Y-%m-%d %H:%M:%S"
formatter = logging.Formatter(log_formatter, time_format)
log_file = os.path.join(f"{file_name}_ERROR.log")
temp_handler = RotatingFileHandler(log_file,
maxBytes=100000000,
backupCount=5)
temp_handler.setFormatter(formatter)
__logger__.addHandler(temp_handler)
return __logger__
logger = get_logger()
from fastapi import APIRouter
from scripts.constants.endpoints import EndPoints
from scripts.core.handlers.redis_store import run
sub = APIRouter()
@sub.post(EndPoints.subscribe,tags=["data subscribe"])
def data_subscribe():
run()
return {"data": "data subscribed"}
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