working mqtt connection

This commit is contained in:
2025-10-16 21:09:16 +03:00
parent 1b7acff7f9
commit 71621896eb
4 changed files with 92 additions and 25 deletions

39
main.py Normal file
View File

@@ -0,0 +1,39 @@
import datetime
from time import sleep, time
from multiprocessing import Process, Queue
import source.vqe as vqe
import source.mqtt as mqtt
if __name__ == "__main__":
broker_address = "mqtt.deowl.ru" # Example public broker
broker_port = 1883
mqttBroker = mqtt.vqeMqttBroker(broker_address, broker_port, max_connection_attempts=5)
print(mqttBroker.get_status())
mqttBroker.connect_to_server()
while True:
print(mqttBroker.get_status())
sleep(1)
# GLOBAL SYSTEM PARAMETERS
active_electrons=2
active_orbitals=2
max_iterations = 500
conv_tol = 1e-04
step_size = 0.05
q = Queue()
p = Process(target=vqe.run_vqe, args=(q, symbols, coordinates, active_electrons, active_orbitals, max_iterations, conv_tol, step_size))
p.start()
while p.is_alive():
try:
print(q.get_nowait(), datetime.datetime.now())
except Exception as e:
print("no_data_to_get", e)
sleep(0.1)
client.loop_stop() # Stop the background loop thread if used
client.disconnect()

View File

@@ -1 +1,2 @@
basis-set-exchange==0.11 basis-set-exchange==0.11
paho-mqtt==2.1.0

46
source/mqtt.py Normal file
View File

@@ -0,0 +1,46 @@
import paho.mqtt.client as paho
import paho.mqtt.enums as paho_enums
class vqeMqttBroker:
def __init__(self, broker_address, broker_port, username="", password="", use_tls=True, max_connection_attempts=30):
self.server = broker_address
self.port = broker_port
self.client = paho.Client(callback_api_version=paho.CallbackAPIVersion.VERSION2, client_id="my_python_client", reconnect_on_failure=True)
self.client.username_pw_set(username, password)
if use_tls:
self.client.tls_set()
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
self.client.on_connect_fail = self.on_connect_fail
self.client.on_disconnect = self.on_disconnect
self.max_connection_attempts=max_connection_attempts
self.connection_attempt_number=0
def connect_to_server(self):
self.connection_attempt_number=0
self.client.connect_async(self.server, self.port, 5)
self.client.loop_start()
def get_status(self):
return self.client._state #self.client.is_connected()
def on_connect(self, client, userdata, flags, reason_code, properties):
print(f"Connected with result code {reason_code}")
# Subscribe to topics here if needed
client.subscribe("my/topic")
def on_message(self, client, userdata, msg):
print(f"Received message: {msg.payload.decode()} on topic {msg.topic}")
def on_connect_fail(self, client, userdata):
self.connection_attempt_number+=1
print("fail")
def on_disconnect(self, client, userdata, disconnect_flags, reason_code, properties):
self.connection_attempt_number+=1
print("disconnected")
if (self.connection_attempt_number > self.max_connection_attempts):
self.client.disconnect()
self.client.loop_stop()
self.connection_attempt_number=0

View File

@@ -1,11 +1,12 @@
import datetime
from time import sleep, time
from pennylane import numpy as np from pennylane import numpy as np
import pennylane as qml import pennylane as qml
import os import os
from multiprocessing import Process, Queue from multiprocessing import Queue
os.environ["OMP_NUM_THREADS"] = '16' os.environ["OMP_NUM_THREADS"] = '16'
def get_sctructure_from_xyz_path(path: str):
return qml.qchem.read_structure("methane.xyz")
def run_vqe(q: Queue, symbols, coordinates, active_electrons, active_orbitals, max_iterations, conv_tol, step_size): def run_vqe(q: Queue, symbols, coordinates, active_electrons, active_orbitals, max_iterations, conv_tol, step_size):
molecule = qml.qchem.Molecule(symbols, coordinates, load_data=True) molecule = qml.qchem.Molecule(symbols, coordinates, load_data=True)
@@ -42,23 +43,3 @@ def run_vqe(q: Queue, symbols, coordinates, active_electrons, active_orbitals, m
if conv <= conv_tol: if conv <= conv_tol:
break break
if __name__ == "__main__":
symbols, coordinates = qml.qchem.read_structure("methane.xyz")
active_electrons=2
active_orbitals=2
max_iterations = 500
conv_tol = 1e-04
step_size = 0.05
q = Queue()
p = Process(target=run_vqe, args=(q, symbols, coordinates, active_electrons, active_orbitals, max_iterations, conv_tol, step_size))
p.start()
while p.is_alive():
try:
print(q.get_nowait(), datetime.datetime.now())
except Exception as e:
print("no_data_to_get", e)
sleep(0.1)