|
5 | 5 | -} |
6 | 6 | {-# LANGUAGE RecordWildCards #-} |
7 | 7 |
|
8 | | -module Network.GRPC.MQTT.RemoteClient (runRemoteClient) where |
| 8 | +module Network.GRPC.MQTT.RemoteClient (runRemoteClient, runRemoteClientWithConnect) where |
9 | 9 |
|
10 | 10 | import Relude |
11 | 11 |
|
@@ -113,10 +113,25 @@ runRemoteClient :: |
113 | 113 | -- | A map from gRPC method names to functions that can make requests to an appropriate gRPC server |
114 | 114 | MethodMap -> |
115 | 115 | IO () |
116 | | -runRemoteClient logger cfg baseTopic methodMap = do |
| 116 | +runRemoteClient = runRemoteClientWithConnect connectMQTT |
| 117 | + |
| 118 | +runRemoteClientWithConnect :: |
| 119 | + -- | A function that creates a client for us to use |
| 120 | + (MQTTGRPCConfig -> IO MQTTClient) -> |
| 121 | + Logger -> |
| 122 | + -- | MQTT configuration for connecting to the MQTT broker |
| 123 | + MQTTGRPCConfig -> |
| 124 | + -- | Base topic which should uniquely identify the device |
| 125 | + Topic -> |
| 126 | + -- | A map from gRPC method names to functions that can make requests to an appropriate gRPC server |
| 127 | + MethodMap -> |
| 128 | + IO () |
| 129 | +runRemoteClientWithConnect connect logger cfg baseTopic methodMap = do |
117 | 130 | sharedSessionMap <- newTVarIO mempty |
118 | 131 | let gatewayConfig = cfg{_msgCB = gatewayHandler sharedSessionMap} |
119 | | - bracket (connectMQTT gatewayConfig) normalDisconnect $ \gatewayMQTTClient -> do |
| 132 | + |
| 133 | + logInfo logger "Connecting to MQTT Broker" |
| 134 | + bracket (connect gatewayConfig) normalDisconnect $ \gatewayMQTTClient -> do |
120 | 135 | logInfo logger "Connected to MQTT Broker" |
121 | 136 |
|
122 | 137 | handle (logException "MQTT client connection") $ do |
@@ -163,7 +178,6 @@ runRemoteClient logger cfg baseTopic methodMap = do |
163 | 178 | Nothing -> logInfo taggedLogger "Received control message for non-existant session" |
164 | 179 | Just session -> controlMsgHandler taggedLogger session mqttMessage |
165 | 180 | _ -> logErr logger $ "Failed to parse topic: " <> unTopic topic |
166 | | - |
167 | 181 | logException :: Text -> SomeException -> IO () |
168 | 182 | logException name e = |
169 | 183 | logErr logger $ |
|
0 commit comments