-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from PinoutLTD/development
receive reports via libp2p
- Loading branch information
Showing
6 changed files
with
105 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import json | ||
|
||
from tenacity import * | ||
|
||
from helpers.logger import Logger | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import json | ||
import os | ||
|
||
import websocket | ||
from dotenv import load_dotenv | ||
|
||
from helpers.logger import Logger | ||
from rrs_operator.src.report_manager import ReportManager | ||
from rrs_operator.utils.messages import message_for_subscribing | ||
|
||
load_dotenv() | ||
|
||
LIBP2P_WS_SERVER = os.getenv("LIBP2P_WS_SERVER") | ||
ADMIN_SEED = os.getenv("ADMIN_SEED") | ||
|
||
class WSClient: | ||
def __init__(self, odoo) -> None: | ||
self.odoo = odoo | ||
self._logger = Logger("operator-ws") | ||
self._connect2server() | ||
|
||
def _connect2server(self): | ||
self.ws = websocket.WebSocketApp( | ||
url=LIBP2P_WS_SERVER, | ||
on_open=self._on_connection, | ||
on_message=self._on_message, | ||
on_error=self._on_error, | ||
on_close=self._on_close, | ||
) | ||
|
||
def run(self) -> None: | ||
self.ws.run_forever(reconnect=5) | ||
|
||
def _on_connection(self, ws): | ||
self._logger.debug(f"Connected to {LIBP2P_WS_SERVER}") | ||
msg = message_for_subscribing() | ||
self._logger.debug(f"Connection msg: {msg}") | ||
self.ws.send(msg) | ||
|
||
def _on_message(self, ws, message): | ||
json_message = json.loads(message) | ||
self._logger.debug(f"Got msg: {json_message}") | ||
if "peerId" in json_message: | ||
return | ||
message_data = json_message["data"] | ||
if "report" in message_data: | ||
sender_address = message_data["address"] | ||
json_report_message = json.dumps(message_data["report"]) | ||
email = self.odoo.find_user_email(sender_address) | ||
if email: | ||
report_manager = ReportManager(sender_address, json_report_message) | ||
report_manager.process_report() | ||
descriptions_list, priority = report_manager.get_description_and_priority() | ||
logs_hashes = report_manager.get_logs_hashes() | ||
self._logger.debug(f"Data from ipfs: {email}, {descriptions_list}, priority: {priority}") | ||
for description in descriptions_list: | ||
ticket_id = self.odoo.find_ticket_with_description(description, email) | ||
if not ticket_id: | ||
ticket_id = self.odoo.create_ticket(email, sender_address, description, priority) | ||
|
||
if logs_hashes: | ||
for hash in logs_hashes: | ||
self.odoo.create_note_with_logs_hash(ticket_id, hash) | ||
else: | ||
self._logger.debug(f"Address {sender_address} is not registred in Odoo. Email is: {email}") | ||
|
||
|
||
def _on_error(self, ws, error): | ||
self._logger.error(f"{error}") | ||
|
||
def _on_close(self, ws, close_status_code, close_msg): | ||
self._logger.debug(f"Connection closed with status code {close_status_code} and message {close_msg}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import json | ||
|
||
|
||
def message_for_subscribing() -> str: | ||
msg = {"protocols_to_listen": ["/report"]} | ||
return json.dumps(msg) |