11import logging
22from typing import Union
33
4+ from api .generate_wis2_payload import generate_wis2_payload
5+ from api .generate_wis2_payload import generate_wis2_topic
46import grpc
57import json
68
@@ -28,26 +30,38 @@ def __init__(
2830 self ,
2931 mqtt_conf : dict ,
3032 uuid_prefix : str ,
33+ mqtt_WIS2_conf : dict | None = None ,
3134 ):
3235 self .uuid_prefix = uuid_prefix
3336 self .client = None
34- if mqtt_conf ["host" ]:
35- try :
37+ self .WIS2_client = None
38+ try :
39+ if mqtt_conf ["host" ]:
3640 self .client = connect_mqtt (mqtt_conf )
37- except Exception as e :
38- logger .error ("Failed to establish connection to mqtt, " + "\n " + str (e ))
39- raise e
41+ if mqtt_WIS2_conf :
42+ self .WIS2_client = connect_mqtt (mqtt_WIS2_conf )
43+ except Exception as e :
44+ logger .error (
45+ "Failed to establish connection to mqtt, "
46+ + "\n "
47+ + str (e )
48+ + "\n "
49+ + json .dumps (mqtt_conf )
50+ + "\n "
51+ + json .dumps (mqtt_WIS2_conf )
52+ )
53+ raise e
4054
41- async def ingest (self , message : Union [str , object ]):
55+ async def ingest (self , message : Union [str , object ], publishWIS2 : bool , baseURL : str ):
4256 """
4357 This method will interpret call all methods for deciding input type, build the mqtt messages, and
4458 publish them.
4559
4660 """
4761 messages = build_messages (message , self .uuid_prefix )
48- await self .publish_messages (messages )
62+ await self .publish_messages (messages , publishWIS2 , baseURL )
4963
50- async def publish_messages (self , messages : list ):
64+ async def publish_messages (self , messages : list , publishWIS2 : bool , baseURL : str ):
5165 """
5266 This method accepts a list of json strings ready to be ingest to datastore
5367 and published to the mqtt topic.
@@ -60,7 +74,7 @@ async def publish_messages(self, messages: list):
6074 logger .error ("Failed to reach datastore, " + "\n " + str (e ))
6175 raise HTTPException (status_code = 500 , detail = "API could not reach datastore" )
6276
63- if self .client is not None :
77+ if self .client or self . WIS2_client :
6478 for msg in messages :
6579 topic = (
6680 msg ["properties" ]["naming_authority" ]
@@ -76,11 +90,27 @@ async def publish_messages(self, messages: list):
7690 msg ["properties" ]["level" ] = level_string
7791 msg ["properties" ]["period" ] = period_iso
7892 try :
79- send_message (topic , json .dumps (msg ), self .client )
80- logger .debug ("Succesfully published to mqtt" )
93+ if self .client :
94+ send_message (topic , json .dumps (msg ), self .client )
95+ logger .debug ("Succesfully published to mqtt" )
8196 except Exception as e :
8297 logger .error ("Failed to publish to mqtt, " + str (e ))
8398 raise HTTPException (
8499 status_code = 500 ,
85100 detail = "Data ingested to datastore. But unable to publish to mqtt" ,
86101 )
102+ try :
103+ if publishWIS2 and self .WIS2_client :
104+ send_message (
105+ generate_wis2_topic (),
106+ generate_wis2_payload (msg , baseURL ).model_dump (exclude_unset = True , exclude_none = True ),
107+ self .WIS2_client ,
108+ )
109+ logger .debug ("Succesfully published to mqtt" )
110+ except Exception as e :
111+ logger .error ("Failed to publish to WIS2 mqtt, " + str (e ))
112+ print (e )
113+ raise HTTPException (
114+ status_code = 500 ,
115+ detail = "Data ingested to datastore. But unable to publish to WIS2 mqtt" ,
116+ )
0 commit comments