Commit ece4100a authored by vipul.v's avatar vipul.v

mqtt and postgresql

parents
POSTGRESQL_URI = postgresql://interns:interns%40123@192.168.0.220:5432/internsb2
\ 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.11 (task7)" 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/task7.iml" filepath="$PROJECT_DIR$/.idea/task7.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
[
{
"invoice_number": "INV-0002",
"client_name": "Client B",
"invoice_date": "2022-02-01",
"due_date": "2022-02-28",
"items": [
{
"description": "Product A",
"quantity": 1,
"price": 100
},
{
"description": "Product D",
"quantity": 3,
"price": 200
}
],
"subtotal": 250,
"tax_rate": 0.07,
"tax_amount": 17.5,
"total": 267.5,
"paid": true,
"payment_date": "2022-02-15",
"payment_method": {
"method_name": "ACH Transfer",
"routing_number": "123456789",
"account_number": "123456789",
"account_type": "Checking",
"billing_address": {
"street": "123 Main St.",
"city": "Anytown",
"state": "CA",
"zip": "12345"
}
}
},
{
"invoice_number": "INV-0003",
"client_name": "Client C",
"invoice_date": "2022-03-01",
"due_date": "2022-03-31",
"items": [
{
"description": "Product B",
"quantity": 2,
"price": 55
},
{
"description": "Product H",
"quantity": 2,
"price": 90
}
],
"subtotal": 320,
"tax_rate": 0.07,
"tax_amount": 22.4,
"total": 342.4,
"paid": false
},
{
"invoice_number": "INV-0004",
"client_name": "Client D",
"invoice_date": "2022-04-01",
"due_date": "2022-04-30",
"items": [
{
"description": "Product C",
"quantity": 1,
"price": 75
},
{
"description": "Product J",
"quantity": 2,
"price": 45
}
],
"subtotal": 380,
"tax_rate": 0.07,
"tax_amount": 26.6,
"total": 406.6,
"paid": false,
"payment_date": "",
"payment_method": ""
},
{
"invoice_number": "INV-0005",
"client_name": "Client E",
"invoice_date": "2022-05-01",
"due_date": "2022-05-31",
"items": [
{
"description": "Product E",
"quantity": 2,
"price": 50
},
{
"description": "Product K",
"quantity": 3,
"price": 120
}
],
"subtotal": 430,
"tax_rate": 0.07,
"tax_amount": 30.1,
"total": 460.1,
"paid": true,
"payment_date": "2022-05-15",
"payment_method": {
"method_name": "Credit Card",
"card_type": "Visa",
"card_number": "**** **** **** 1234",
"expiration_date": "01/25",
"billing_address": {
"street": "123 Main St.",
"city": "Anytown",
"state": "CA",
"zip": "12345"
}
}
},
{
"invoice_number": "INV-0006",
"client_name": "Client F",
"invoice_date": "2022-06-01",
"due_date": "2022-06-30",
"items": [
{
"description": "Product G",
"quantity": 4,
"price": 80
},
{
"description": "Product L",
"quantity": 1,
"price": 80
}
],
"subtotal": 490,
"tax_rate": 0.07,
"tax_amount": 34.3,
"total": 524.3,
"paid": true,
"payment_date": "2022-06-15",
"payment_method": {
"method_name": "Credit Card",
"card_type": "Mastercard",
"card_number": "**** **** **** 5678",
"expiration_date": "02/26",
"billing_address": {
"street": "234 Elm St.",
"city": "Otherville",
"state": "NY",
"zip": "54321"
}
}
}
]
\ No newline at end of file
[MQTT_SERVICE]
broker_host = 192.168.0.220
broker_port = 1883
\ No newline at end of file
from script.core.engine.subscriber import MqttConnectPostgresql
mqtt_data = MqttConnectPostgresql()
mqtt_data.mqtt_file()
\ No newline at end of file
import json
import time
import paho.mqtt.client as mqtt
from script.config.app_confing import Services
# publish
topic1 = "test/Vipul"
with open('billing_output.json', 'rb') as f:
data = json.load(f)
message1 = data
client = mqtt.Client()
client.connect(Services.host, Services.port)
while True:
if client.publish(topic1, json.dumps(message1)):
print(f"The message got published to MQTT {message1}")
time.sleep(5)
client.disconnect()
break
\ No newline at end of file
from dotenv import load_dotenv
import os
from configparser import SafeConfigParser
load_dotenv()
config = SafeConfigParser()
config.read('conf/application.conf')
class Sql:
postgresql: str = os.environ.get("POSTGRESQL_URI")
class Services:
host = config.get("MQTT_SERVICE", "broker_host")
port = int(config.get("MQTT_SERVICE", "broker_port"))
\ No newline at end of file
import sqlalchemy
from sqlalchemy import create_engine, MetaData
from sqlalchemy.orm import sessionmaker
from script.config.app_confing import Sql
engine = create_engine(Sql.postgresql)
LocalSession = sessionmaker(bind=engine)
session = LocalSession()
Base = sqlalchemy.orm.declarative_base()
metadata = MetaData()
\ No newline at end of file
import json
import paho.mqtt.client as mqtt
from script.config.app_confing import Services
from script.core.db.postgresql_connector import session
from script.core.schema.schema_file import Items, InvoiceDetails
# subscribe
topic = "test/#"
class MqttConnectPostgresql:
try:
@staticmethod
def mqtt_file():
def on_connect(client, userdata, flags, rc):
print("Connected to MQTT broker with result code " + str(rc))
client.subscribe(topic)
def on_message(client, userdata, msg):
print(
"Received message on topic '" + msg.topic + "' with message '" + msg.payload.decode('utf-8') + "'")
data = json.loads(msg.payload.decode('utf-8'))
print(data)
for invoice in data:
invoice_number = invoice['invoice_number']
client_name = invoice['client_name']
invoice_date = invoice['invoice_date']
due_date = invoice['due_date']
subtotal = invoice['subtotal']
tax_rate = invoice['tax_rate']
total = invoice['total']
paid = invoice['paid']
if paid:
payment_date = invoice['payment_date']
payment_method = invoice['payment_method']
billing_address = payment_method['billing_address']
invoice_data = InvoiceDetails(
invoice_number=invoice_number,
client_name=client_name,
invoice_date=invoice_date,
due_date=due_date,
subtotal=subtotal,
tax_rate=tax_rate,
total=total,
paid=paid,
payment_date=payment_date,
payment_method=payment_method,
billing_address=billing_address
)
else:
invoice_data = InvoiceDetails(
invoice_number=invoice_number,
client_name=client_name,
invoice_date=invoice_date,
due_date=due_date,
subtotal=subtotal,
tax_rate=tax_rate,
total=total,
paid=paid
)
session.add(invoice_data)
session.flush()
invoice_id = invoice_data.id
for item_data in invoice['items']:
item = Items(
invoice_id=invoice_id,
description=item_data['description'],
quantity=item_data['quantity'],
price=item_data['price']
)
session.add(item)
session.commit()
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(Services().host, Services().port)
client.loop_forever()
except Exception as e:
print(e, "Error detected in subscribe")
from sqlalchemy import Column, Integer, String, Date, Float, Boolean, ForeignKey
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.ext.declarative import declarative_base
from script.core.db.postgresql_connector import engine
Base = declarative_base()
class InvoiceDetails(Base):
__tablename__ = 'invoice_details'
id = Column('id', Integer, primary_key=True, autoincrement=True)
invoice_number = Column('invoice_number', String, unique=True)
client_name = Column('client_name', String)
invoice_date = Column('invoice_data', Date)
due_date = Column('due_date', Date)
subtotal = Column('subtotal', Integer)
tax_rate = Column('tax_rate', Float)
total = Column('total', Float)
paid = Column('paid', Boolean)
payment_date = Column('payment_date', Date)
payment_method = Column('payment_method', JSONB)
billing_address = Column('billing_address', JSONB)
class Items(Base):
__tablename__ = 'items'
id = Column('id', Integer, primary_key=True, autoincrement=True)
invoice_id = Column("invoice_id", ForeignKey(InvoiceDetails.id), nullable=False)
description = Column('description', String)
quantity = Column('quantity', Integer)
price = Column('price', Integer)
Base.metadata.create_all(engine)
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