示例#1
0
    def __init__(self):

        self.header = Header()
        self.type1to3 = Type1to3()
        self.type4and11 = Type4and11()
        self.type5 = Type5()
        self.type6 = Type6()
        self.type7and13 = Type7and13()
        self.type12 = Type12()
        self.type14 = Type14()
        self.type15 = Type15()
        self.type16 = Type16()
        self.type17 = Type17()
        self.type18 = Type18()
        self.type19 = Type19()
        self.type20 = Type20()
        self.type21 = Type21()
        self.type24A = Type24A()
        self.type24B = Type24B()

        self.save24: Dict[str, dict] = {}
示例#2
0
from Engine.header import Structure as Header
from Engine.type1to3 import Structure as Type123
from Engine.type5 import Structure as Type5

if __name__ == '__main__':

    from loguru import logger

    header = Header()
    L = Type123()
    P = Type5()

    with open('../Emulator/payload.txt', 'rt') as f:
        all = f.read().split('\n')
        for line, s in enumerate(all):
            H = header.decode(payload=s)
            if H.completed:
                if H.body['type'] in [1, 2, 3]:
                    r = L.decode(payload=s)
                    if r.completed:
                        heading = r.body['heading']
                        pass
                    else:
                        logger.debug('at %d: %s -> %s' % (line, s, r.reason))
                elif H.body['type'] in [5]:
                    r = P.decode(payload=s)
                    if r.completed:
                        print(r.body['shipname'], r.body['shiptype'])
                        pass
                    else:
                        logger.debug('at %d: %s -> %s' % (line, s, r.reason))
示例#3
0
class Dispatcher(object):
    def __init__(self):

        self.header = CommonHeader()
        self.type1to3 = Type1to3()
        self.type4and11 = Type4and11()
        self.type5 = Type5()
        self.type6 = Type6()
        self.type7and13 = Type7and13()
        self.type12 = Type12()
        self.type14 = Type14()
        self.type15 = Type15()
        self.type16 = Type16()
        self.type17 = Type17()
        self.type18 = Type18()
        self.type19 = Type19()
        self.type20 = Type20()
        self.type21 = Type21()
        self.type24A = Type24A()
        self.type24B = Type24B()
        self.type27 = Type27()

        self.save24: Dict[str, dict] = {}

    def parse(self, *, payload: str, vdo: bool) -> DispatchResult:
        result = DispatchResult(vdo=vdo)
        pr = self.header.decode(payload=payload)
        if pr.completed:
            header = pr.body
            result.header = Header(mmsi=header['mmsi'],
                                   repeat=header['repeat'],
                                   type=header['type'])
            thisType = header['type']
            thisMMSI = header['mmsi']
            if thisType in (1, 2, 3):
                s = self.type1to3.decode(payload=payload)
                if s.completed:
                    result.body = s.body
                else:
                    result.completed = False
                    result.reason = s.reason
                    logger.error(s.reason)

            elif thisType in [4, 11]:
                s = self.type4and11.decode(payload=payload)
                if s.completed:
                    result.body = s.body
                else:
                    result.completed = False
                    result.reason = s.reason
                    logger.error(s.reason)

            elif thisType == 5:
                s = self.type5.decode(payload=payload)
                if s.completed:
                    result.body = s.body
                else:
                    result.completed = False
                    result.reason = s.reason
                    logger.error(s.reason)

            elif thisType == 6:
                s = self.type6.decode(payload=payload)
                if s.completed:
                    result.body = s.body
                else:
                    result.completed = False
                    result.reason = s.reason
                    logger.error(s.reason)

            elif thisType in [7, 13]:
                result.support = False

            elif thisType == 8:
                result.support = False

            elif thisType == 12:
                s = self.type12.decode(payload=payload)
                if s.completed:
                    result.body = s.body
                else:
                    result.completed = False
                    result.reason = s.reason
                    logger.error(s.reason)

            elif thisType == 14:
                s = self.type14.decode(payload=payload)
                if s.completed:
                    result.body = s.body
                else:
                    result.completed = False
                    result.reason = s.reason
                    logger.error(s.reason)

            elif thisType == 15:
                result.support = False

            elif thisType == 16:
                s = self.type16.decode(payload=payload)
                if s.completed:
                    result.body = s.body
                else:
                    result.completed = False
                    result.reason = s.reason
                    logger.error(s.reason)

            elif thisType == 17:
                s = self.type17.decode(payload=payload)
                if s.completed:
                    result.body = s.body
                else:
                    result.completed = False
                    result.reason = s.reason
                    logger.error(s.reason)

            elif thisType == 18:
                s = self.type18.decode(payload=payload)
                if s.completed:
                    result.body = s.body
                else:
                    result.completed = False
                    result.reason = s.reason
                    logger.error(s.reason)

            elif thisType == 19:
                s = self.type19.decode(payload=payload)
                if s.completed:
                    result.body = s.body
                else:
                    result.completed = False
                    result.reason = s.reason
                    logger.error(s.reason)

            # elif thisType == 20:
            #     s = self.type20.parse(payload=payload)
            #     if s.completed:
            #         result.body = s.body
            #     else:
            #         result.completed = False
            #         result.reason = s.reason
            #         logger.error(s.reason)

            elif thisType == 21:
                s = self.type21.decode(payload=payload)
                if s.completed:
                    result.body = s.body
                else:
                    result.completed = False
                    result.reason = s.reason
                    logger.error(s.reason)

            elif thisType == 24:

                if thisMMSI not in self.save24:
                    self.save24[thisMMSI] = {}

                target = self.save24[thisMMSI]

                ooo = self.type24A.decode(payload=payload)
                if ooo.completed:
                    partA = ooo.body
                    if partA and partA['partno'] == 0:
                        target['A'] = partA
                else:
                    result.completed = False
                    result.reason = ooo.reason
                    logger.error(ooo.reason)

                ooo = self.type24B.decode(payload=payload)
                if ooo.completed:
                    partB = ooo.body
                    if partB and partB['partno'] == 1:
                        target['B'] = partB
                else:
                    result.completed = False
                    result.reason = ooo.reason
                    logger.error(ooo.reason)

                if 'A' in target and 'B' in target:
                    s = target['A']
                    s.update(target['B'])
                    del (self.save24[thisMMSI])
                    result.body = s
                    result.completed = True
                else:
                    # result['completed'] = False
                    result.completed = False

            elif thisType == 27:
                s = self.type27.decode(payload=payload)
                if s.completed:
                    result.body = s.body
                else:
                    result.completed = False
                    result.reason = s.reason
                    logger.error(s.reason)

            else:
                result.completed = False
                raise ValueError('void AIS type %d' % (thisType, ))
                # logger.debug('void %d' % (thisType,))
                pass
        else:
            result.completed = False
            raise ValueError('parse error')
            # logger.critical('No type')
            pass

        return result
示例#4
0
from Engine.header import Structure as Header
from Engine.type1to3 import Structure as Type123
from Engine.type5 import Structure as Type5

if __name__ == '__main__':

    from loguru import logger

    header = Header()
    L = Type123()
    P = Type5()

    with open('../Emulator/payload.txt', 'rt') as f:
        all = f.read().split('\n')
        for line, s in enumerate(all):
            H = header.parse(payload=s)
            if H.completed:
                if H.body['type'] in [1, 2, 3]:
                    r = L.parse(payload=s)
                    if r.completed:
                        heading = r.body['heading']
                        pass
                    else:
                        logger.debug('at %d: %s -> %s' % (line, s, r.reason))
                elif H.body['type'] in [5]:
                    r = P.parse(payload=s)
                    if r.completed:
                        print(r.body['shipname'], r.body['shiptype'])
                        pass
                    else:
                        logger.debug('at %d: %s -> %s' % (line, s, r.reason))
示例#5
0
class Dispatcher(object):
    def __init__(self):

        self.header = Header()
        self.type1to3 = Type1to3()
        self.type4and11 = Type4and11()
        self.type5 = Type5()
        self.type6 = Type6()
        self.type7and13 = Type7and13()
        self.type12 = Type12()
        self.type14 = Type14()
        self.type15 = Type15()
        self.type16 = Type16()
        self.type17 = Type17()
        self.type18 = Type18()
        self.type19 = Type19()
        self.type20 = Type20()
        self.type21 = Type21()
        self.type24A = Type24A()
        self.type24B = Type24B()

        self.save24: Dict[str, dict] = {}

    def parse(self, *, payload: str) -> Dict[str, any]:
        result: Dict[str, any] = {}
        header = self.header.parse(payload=payload)
        if 'type' in header:
            result['header'] = header
            thisType = header['type']
            thisMMSI = header['mmsi']
            if thisType in (1, 2, 3):
                s = self.type1to3.parse(payload=payload)
                result['body'] = s
                # print('Type[%d] = %s' % (thisType, s))

            elif thisType in [4, 11]:
                s = self.type4and11.parse(payload=payload)
                result['body'] = s
                # print('Type[%d] = %s' % (thisType, s))

            elif thisType == 5:
                s = self.type5.parse(payload=payload)
                result['body'] = s
                # print('Type[%d] = %s' % (thisType, s))

            elif thisType == 6:
                s = self.type6.parse(payload=payload)
                result['body'] = s
                # print('Type[%d] = %s' % (thisType, s))

            # elif thisType in [7, 13]:
            #     s = self.type7and13.parse(payload=payload)
            #     result['body'] = s
            #     print('Type[%d] = %s' % (thisType, s))

            elif thisType == 12:
                s = self.type12.parse(payload=payload)
                result['body'] = s
                # print('Type[%d] = %s' % (thisType, s))

            elif thisType == 14:
                s = self.type14.parse(payload=payload)
                result['body'] = s
                # print('Type[%d] = %s' % (thisType, s))

            # elif thisType == 15:
            #     s = self.type15.parse(payload=payload)
            #     result['body'] = s
            #     print('Type[%d] = %s' % (thisType, s))

            elif thisType == 16:
                s = self.type16.parse(payload=payload)
                result['body'] = s
                # print('Type[%d] = %s' % (thisType, s))

            elif thisType == 17:
                s = self.type17.parse(payload=payload)
                result['body'] = s
                # print('Type[%d] = %s' % (thisType, s))

            elif thisType == 18:
                s = self.type18.parse(payload=payload)
                result['body'] = s
                # print('Type[%d] = %s' % (thisType, s))
            elif thisType == 19:
                s = self.type19.parse(payload=payload)
                result['body'] = s
                # print('Type[%d] = %s' % (thisType, s))

            # elif thisType == 20:
            #     s = self.type20.parse(payload=payload)
            #     result['body'] = s
            #     print('Type[%d] = %s' % (thisType, s))

            elif thisType == 21:
                s = self.type21.parse(payload=payload)
                result['body'] = s
                # print('Type[%d] = %s' % (thisType, s))

            elif thisType == 24:

                if thisMMSI not in self.save24:
                    self.save24[thisMMSI] = {}

                target = self.save24[thisMMSI]

                partA = self.type24A.parse(payload=payload)
                if partA and partA['partno'] == 0:
                    target['A'] = partA
                partB = self.type24B.parse(payload=payload)
                if partB and partB['partno'] == 1:
                    target['B'] = partB

                if 'A' in target and 'B' in target:
                    s = target['A']
                    s.update(target['B'])
                    del (self.save24[thisMMSI])
                    result['body'] = s
                    # print('Type[%d] = %s' % (thisType, s))
            else:
                # logger.debug('void %d' % (thisType,))
                pass
        else:
            logger.critical('No type')

        return result