99from hat import aio
1010
1111from hat .drivers .iec103 import common
12+ from hat .drivers .iec103 .master import logger
1213from hat .drivers .iec60870 .encodings import iec103
1314from hat .drivers .iec60870 import link
1415
@@ -31,8 +32,8 @@ def __init__(self,
3132 self ._data_cb = data_cb
3233 self ._generic_data_cb = generic_data_cb
3334
34- self ._log = _create_logger_adapter ( False , conn .info )
35- self ._comm_log = _create_logger_adapter ( True , conn .info )
35+ self ._log = logger . create_logger ( mlog , conn .info )
36+ self ._comm_log = logger . CommunicationLogger ( mlog , conn .info )
3637
3738 self ._encoder = iec103 .Encoder ()
3839
@@ -72,7 +73,9 @@ def __init__(self,
7273
7374 self .async_group .spawn (self ._receive_loop )
7475
75- self ._comm_log .debug ('master connection created' )
76+ self .async_group .spawn (aio .call_on_cancel , self ._comm_log .log ,
77+ common .CommLogAction .CLOSE )
78+ self ._comm_log .log (common .CommLogAction .OPEN )
7679
7780 @property
7881 def async_group (self ):
@@ -99,7 +102,7 @@ async def time_sync(self,
99102
100103 data = self ._encoder .encode_asdu (asdu )
101104
102- self ._comm_log .debug ( 'sending %s' , asdu )
105+ self ._comm_log .log ( common . CommLogAction . SEND , asdu )
103106
104107 await self ._conn .send (data )
105108
@@ -123,7 +126,7 @@ async def interrogate(self, asdu_address: common.AsduAddress):
123126 data = self ._encoder .encode_asdu (asdu )
124127
125128 try :
126- self ._comm_log .debug ( 'sending %s' , asdu )
129+ self ._comm_log .log ( common . CommLogAction . SEND , asdu )
127130
128131 self ._interrogate_req_id = scan_number
129132 self ._interrogate_future = asyncio .Future ()
@@ -157,7 +160,7 @@ async def send_command(self,
157160 data = self ._encoder .encode_asdu (asdu )
158161
159162 try :
160- self ._comm_log .debug ( 'sending %s' , asdu )
163+ self ._comm_log .log ( common . CommLogAction . SEND , asdu )
161164
162165 self ._send_command_req_id = return_identifier
163166 self ._send_command_future = asyncio .Future ()
@@ -189,7 +192,7 @@ async def interrogate_generic(self, asdu_address: common.AsduAddress):
189192 data = self ._encoder .encode_asdu (asdu )
190193
191194 try :
192- self ._comm_log .debug ( 'sending %s' , asdu )
195+ self ._comm_log .log ( common . CommLogAction . SEND , asdu )
193196
194197 self ._interrogate_generic_req_id = return_identifier
195198 self ._interrogate_generic_future = asyncio .Future ()
@@ -206,7 +209,7 @@ async def _receive_loop(self):
206209 data = await self ._conn .receive ()
207210 asdu , _ = self ._encoder .decode_asdu (data )
208211
209- self ._comm_log .debug ( 'received %s' , asdu )
212+ self ._comm_log .log ( common . CommLogAction . RECEIVE , asdu )
210213
211214 for io in asdu .ios :
212215 if asdu .type in self ._process_single_element_fns :
@@ -233,8 +236,6 @@ async def _receive_loop(self):
233236 _try_set_exception (self ._interrogate_generic_future ,
234237 ConnectionError ())
235238
236- self ._comm_log .debug ('master connection closed' )
237-
238239 async def _process_TIME_TAGGED_MESSAGE (self , cause , asdu_address , io_address , element ): # NOQA
239240 if cause == iec103 .Cause .GENERAL_COMMAND :
240241 if element .value .supplementary == self ._send_command_req_id :
@@ -464,13 +465,3 @@ async def _try_aio_call(cb, *args):
464465 if not cb :
465466 return
466467 return await aio .call (cb , * args )
467-
468-
469- def _create_logger_adapter (communication , info ):
470- extra = {'meta' : {'type' : 'Iec103Master' ,
471- 'communication' : communication ,
472- 'name' : info .name ,
473- 'port' : info .port ,
474- 'address' : info .address }}
475-
476- return logging .LoggerAdapter (mlog , extra )
0 commit comments