Added hidemmsi option for outpeers
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Tue, 26 Apr 2011 16:54:43 +0000 (16:54 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Tue, 26 Apr 2011 16:54:43 +0000 (16:54 +0000)
bin/gpsdec.py
bin/inputs/config.py
bin/inputs/outpeer.py
bin/inputs/run.py

index 113c79aead6bce64e4ee147550bb17806045b0f7..211c52c101cfbdf9d841880400bd63454cd89c11 100755 (executable)
@@ -101,15 +101,16 @@ else:
         
 
 class AivdmProcessor:
-    def __init__(self):
+    def __init__(self, is_db_save):
         self.ais_context = c_buffer('\000' * 8196) #(4+4+91*4+92+2048+21+4+1024))
         self.ais_data = GpsdAisData()
+        self.is_db_save = is_db_save
 
     def process_aivdm_line(self, line, id4):
         timestamp = int(get_timestamp()) # TODO
         if len(line)<3 or line[-3] != '*':
             logging.error('no checksum found in line: %s', repr(line))
-            return
+            return None
         data, checksum = line.split('*', 1)
         crc = 0
         for char in data[1:]:
@@ -117,13 +118,12 @@ class AivdmProcessor:
         crc = '%02X' % crc
         if crc != checksum:
             logging.error('checksum failure in line %s. Recomputed CRC is %s.', repr(line), crc)
-            return
+            return None
         
         if not __gpsd__.aivdm_decode(line, len(line)+1, byref(self.ais_context), byref(self.ais_data)):
-            return # incomplete packet
+            return True # incomplete or invalid packet: always forwards TODO
 
         if self.ais_data.type_ in (1, 2, 3):
-    
             #strdata = cast(byref(self.ais_data), POINTER(c_ubyte*28))
             #raw28  = strdata.contents
             #logging.debug('raw28= %s', ''.join(['%02x' % x for x in raw28]))
@@ -165,23 +165,24 @@ class AivdmProcessor:
             #else:
                 #logging.warning('Received second=%s in AIS type %s message, not applying deltas',
                 #    second, self.ais_data.type_)
-            add_nmea1(mmsi_to_strmmsi(self.ais_data.mmsi),
-                      timestamp,
-                      self.ais_data.type1.status,
-                      self.ais_data.type1.turn,
-                      self.ais_data.type1.speed,
-                      #self.ais_data.type1.accuracy,
-                      self.ais_data.type1.lat,
-                      self.ais_data.type1.lon,
-                      self.ais_data.type1.course,
-                      self.ais_data.type1.heading,
-                      #self.ais_data.type1.second,
-                      #self.ais_data.type1.maneuver,
-                      #self.ais_data.type1.raim,
-                      #self.ais_data.type1.radio,
-                      id4,
-                      )
-
+            if self.is_db_save:
+                add_nmea1(mmsi_to_strmmsi(self.ais_data.mmsi),
+                          timestamp,
+                          self.ais_data.type1.status,
+                          self.ais_data.type1.turn,
+                          self.ais_data.type1.speed,
+                          #self.ais_data.type1.accuracy,
+                          self.ais_data.type1.lat,
+                          self.ais_data.type1.lon,
+                          self.ais_data.type1.course,
+                          self.ais_data.type1.heading,
+                          #self.ais_data.type1.second,
+                          #self.ais_data.type1.maneuver,
+                          #self.ais_data.type1.raim,
+                          #self.ais_data.type1.radio,
+                          id4,
+                          )
+            return self.ais_data
     
         elif self.ais_data.type_ == 5:
             #logging.debug('TYPE 5: %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s',
@@ -204,22 +205,26 @@ class AivdmProcessor:
             #    self.ais_data.type5.destination,
             #    self.ais_data.type5.dte
             #    )
-            add_nmea5_full(mmsi_to_strmmsi(self.ais_data.mmsi),
-                      timestamp,
-                      self.ais_data.type5.imo,
-                      self.ais_data.type5.shipname,
-                      self.ais_data.type5.callsign,
-                      self.ais_data.type5.shiptype,
-                      self.ais_data.type5.to_bow,
-                      self.ais_data.type5.to_stern,
-                      self.ais_data.type5.to_port,
-                      self.ais_data.type5.to_starboard,
-                      #self.ais_data.type5.epfd,
-                      self.ais_data.type5.month,
-                      self.ais_data.type5.day,
-                      self.ais_data.type5.hour,
-                      self.ais_data.type5.minute,
-                      self.ais_data.type5.draught,
-                      self.ais_data.type5.destination,
-                      #self.ais_data.type5.dte
-                      id4)
+            if self.is_db_save:
+                add_nmea5_full(mmsi_to_strmmsi(self.ais_data.mmsi),
+                          timestamp,
+                          self.ais_data.type5.imo,
+                          self.ais_data.type5.shipname,
+                          self.ais_data.type5.callsign,
+                          self.ais_data.type5.shiptype,
+                          self.ais_data.type5.to_bow,
+                          self.ais_data.type5.to_stern,
+                          self.ais_data.type5.to_port,
+                          self.ais_data.type5.to_starboard,
+                          #self.ais_data.type5.epfd,
+                          self.ais_data.type5.month,
+                          self.ais_data.type5.day,
+                          self.ais_data.type5.hour,
+                          self.ais_data.type5.minute,
+                          self.ais_data.type5.draught,
+                          self.ais_data.type5.destination,
+                          #self.ais_data.type5.dte
+                          id4)
+            return self.ais_data # Forward that line
+        # TODO
+        return self.ais_data # Forward that line
index 63dbb9795405710381e89c864be22750be8d4462..e768fc32f70a46e1de7a09ff989cbc7e50ac9272 100644 (file)
@@ -53,7 +53,8 @@ def peers_get_config():
                 lineno
             if not SOURCES[id4].has_key('out'):
                 SOURCES[id4]['out'] = []
-            SOURCES[id4]['out'].append(definition)
+            spl = definition.split(' ')
+            SOURCES[id4]['out'].append(spl)
         elif io ==  'name':
             SOURCES[id4]['name'] = definition
         else:
index 089b20c330bdefd095aee3e4265de37817121cdf..0a479c559d8d83ba2352bd51bb000a93afd1007e 100644 (file)
@@ -10,6 +10,7 @@ import socket
 from threading import Thread, Lock
 
 from ais.ntools import formataddr
+from ais.common import strmmsi_to_mmsi
 
 __all__ = [ 'OutPeers', 'outpeers_from_config', 'TcpInOutPeer', 'TcpInServiceOut', 'tcpin_outpeers' ]
 
@@ -17,7 +18,7 @@ class UdpOutPeer:
     '''
     UDP output feed.
     '''
-    def __init__(self, str_addrport):
+    def __init__(self, str_addrport, options):
         colpos = str_addrport.rfind(':')
         if colpos == 1:
             assert colpos != 1, \
@@ -33,6 +34,14 @@ class UdpOutPeer:
         self.timestamp_getaddrinfo = 0
         self._reinit_socket()
 
+        self.hidemmsi = []
+        logging.warning('%s', options)
+        for option in options:
+            if option.startswith('hidemmsi:'):
+                self.hidemmsi += option[len('hidemmsi:'):].split(',')
+        self.hidemmsi = [ strmmsi_to_mmsi(mmsi) for mmsi in self.hidemmsi ]
+        logging.warning('hidemmsi:%s', self.hidemmsi)
+
     def __repr__(self):
         return '%s:%s' % (self.host, self.port)
 
@@ -84,7 +93,7 @@ class UdpOutPeer:
             except socket.error, msg:
                 logging.error("Can't open socket to %s: %s", self.addr, msg)
         
-    def send_line(self, line):
+    def send_line(self, line, decoded_info):
         '''
         Sends a AIVDM line to that peer.
         CRLF will be added automatically.
@@ -108,7 +117,7 @@ class TcpInOutPeer:
         #self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,
         #                      struct.pack('ii', 1, SERVER_LINGER_TIME))
 
-    def send_line(self, line):
+    def send_line(self, line, decoded_info):
         try:
             self.sock.send(line+'\r\n')
         except socket.error, err:
@@ -204,13 +213,21 @@ class OutPeers:
         for peer in tmp_peers:
             yield peer
 
-    def send_line(self, line):
+    def send_line(self, line, decoded_info):
         '''
         Send a line to all the output peers registered.
         '''
         for outpeer in self.safe_iter():
+            if decoded_info and decoded_info != True:
+                try:
+                    hidemmsi = outpeer.hidemmsi
+                except AttributeError:
+                    hidemmsi = []
+                if decoded_info.mmsi in hidemmsi:
+                    logging.info('Hiding line for mmsi=%s', decoded_info.mmsi)
+                    continue
             logging.debug('OUT %s %s', outpeer, repr(line))
-            outpeer.send_line(line)
+            outpeer.send_line(line, decoded_info)
 
 def outpeers_from_config(list_from_config):
     '''
@@ -218,10 +235,15 @@ def outpeers_from_config(list_from_config):
     Returns an OutPeers object.
     '''
     outpeers = OutPeers()
-    for str_config in list_from_config:
+    for config_with_options in list_from_config:
+        str_config = config_with_options[0]
+        if len(config_with_options):
+            options = config_with_options[1:]
+        else:
+            options = []
         assert str_config.startswith('udp:'), \
                'Unsupported output peer protocol %s' % str_config
-        peer = UdpOutPeer(str_config[4:])
+        peer = UdpOutPeer(str_config[4:], options)
         outpeers.add_peer(peer)
     return outpeers
 
index 54ee7e825734b704e9c752dbd571a93f7bdebc5e..87545aeb3d2ce060a8906192bd220255cd51af17 100644 (file)
@@ -113,17 +113,17 @@ def mainloop(options, args):
                         channel.source.logger.log_line(timestamp, from_, line)
 
                         # process the line internally
-                        if options.todb:
-                            try:
-                                proc = channel.aivdm_processor
-                            except AttributeError:
-                                channel.aivdm_processor = AivdmProcessor()
-                                proc = channel.aivdm_processor
-                            proc.process_aivdm_line(line, channel.source.id4)
+                        try:
+                            proc = channel.aivdm_processor
+                        except AttributeError:
+                            channel.aivdm_processor = AivdmProcessor(options.todb)
+                            proc = channel.aivdm_processor
+                        decoded_info = proc.process_aivdm_line(line, channel.source.id4)
 
                         # forward the line
-                        channel.source.outpeers.send_line(line)
-                        tcpin_outpeers.send_line(line)
+                        if decoded_info:
+                            channel.source.outpeers.send_line(line, decoded_info)
+                            tcpin_outpeers.send_line(line, decoded_info)
                   
                     # we don't want any stdout buffering
                     if options.stdout: