# Path: ogn/parser/parse.py
# def parse(aprs_message, reference_timestamp=None, calculate_relations=False, use_server_timestamp=True):
#     global positions
#     global server_timestamp
# 
#     if use_server_timestamp is True:
#         reference_timestamp = server_timestamp or datetime.utcnow()
#     elif reference_timestamp is None:
#         reference_timestamp = datetime.utcnow()
# 
#     message = parse_aprs(aprs_message, reference_timestamp=reference_timestamp)
#     if message['aprs_type'] == 'position' or message['aprs_type'] == 'status':
#         message.update(parse_comment(message['comment'],
#                                      dstcall=message['dstcall'],
#                                      aprs_type=message['aprs_type']))
# 
#     if message['aprs_type'].startswith('position') and calculate_relations is True:
#         positions[message['name']] = (message['longitude'], message['latitude'])
#         if message['receiver_name'] in positions:
#             cheap_ruler = CheapRuler((message['latitude'] + positions[message['receiver_name']][1]) / 2.0)
#             message['distance'] = cheap_ruler.distance((message['longitude'], message['latitude']), positions[message['receiver_name']])
#             message['bearing'] = cheap_ruler.bearing((message['longitude'], message['latitude']), positions[message['receiver_name']])
#             message['normalized_quality'] = normalized_quality(message['distance'], message['signal_quality']) if 'signal_quality' in message else None
# 
#     if message['aprs_type'] == 'server':
#         server_timestamp = message['timestamp']
# 
#     return message
# 
# Path: ogn/parser/exceptions.py
# class AprsParseError(ParseError):
#     """Parse error while parsing an aprs packet."""
#     def __init__(self, aprs_string):
#         self.aprs_string = aprs_string
# 
#         self.message = "This is not a valid APRS packet: {}".format(aprs_string)
#         super(AprsParseError, self).__init__(self.message)


import unittest
import unittest.mock as mock
import os
from datetime import datetime
from time import sleep
from ogn.parser.parse import parse
from ogn.parser.exceptions import AprsParseError
        message = parse("VITACURA1>APRS,TCPIP*,qAC,GLIDERN4:/201146h3322.79SI07034.80W&/A=002329 Vitacura Municipal Aerodrome, Club de Planeadores Vitacura")

        self.assertEqual(message['user_comment'], "Vitacura Municipal Aerodrome, Club de Planeadores Vitacura")

        message_with_id = parse("ALFALFAL>APRS,TCPIP*,qAC,GLIDERN4:/221830h3330.40SI07007.88W&/A=008659 Alfalfal Hidroelectric Plant, Club de Planeadores Vitacurs")

        self.assertEqual(message_with_id['user_comment'], "Alfalfal Hidroelectric Plant, Club de Planeadores Vitacurs")

    @mock.patch('ogn.parser.parse_module.createTimestamp')
    def test_default_reference_date(self, createTimestamp_mock):
        valid_aprs_string = "Lachens>APRS,TCPIP*,qAC,GLIDERN2:/165334h4344.70NI00639.19E&/A=005435 v0.2.1 CPU:0.3 RAM:1764.4/2121.4MB NTP:2.8ms/+4.9ppm +47.0C RF:+0.70dB"

        parse(valid_aprs_string)
        call_args_before = createTimestamp_mock.call_args

        sleep(1)

        parse(valid_aprs_string)
        call_args_seconds_later = createTimestamp_mock.call_args

        self.assertNotEqual(call_args_before, call_args_seconds_later)

    def test_copy_constructor(self):
        valid_aprs_string = "FLRDDA5BA>APRS,qAS,LFMX:/160829h4415.41N/00600.03E'342/049/A=005524 id0ADDA5BA -454fpm -1.1rot 8.8dB 0e +51.2kHz gps4x5"
        message = parse(valid_aprs_string)

        self.assertEqual(message['name'], 'FLRDDA5BA')
        self.assertEqual(message['address'], 'DDA5BA')



# Path: ogn/parser/parse.py
# def parse(aprs_message, reference_timestamp=None, calculate_relations=False, use_server_timestamp=True):
#     global positions
#     global server_timestamp
# 
#     if use_server_timestamp is True:
#         reference_timestamp = server_timestamp or datetime.utcnow()
#     elif reference_timestamp is None:
#         reference_timestamp = datetime.utcnow()
# 
#     message = parse_aprs(aprs_message, reference_timestamp=reference_timestamp)
#     if message['aprs_type'] == 'position' or message['aprs_type'] == 'status':
#         message.update(parse_comment(message['comment'],
#                                      dstcall=message['dstcall'],
#                                      aprs_type=message['aprs_type']))
# 
#     if message['aprs_type'].startswith('position') and calculate_relations is True:
#         positions[message['name']] = (message['longitude'], message['latitude'])
#         if message['receiver_name'] in positions:
#             cheap_ruler = CheapRuler((message['latitude'] + positions[message['receiver_name']][1]) / 2.0)
#             message['distance'] = cheap_ruler.distance((message['longitude'], message['latitude']), positions[message['receiver_name']])
#             message['bearing'] = cheap_ruler.bearing((message['longitude'], message['latitude']), positions[message['receiver_name']])
#             message['normalized_quality'] = normalized_quality(message['distance'], message['signal_quality']) if 'signal_quality' in message else None
# 
#     if message['aprs_type'] == 'server':
#         server_timestamp = message['timestamp']
# 
#     return message
# 
# Path: ogn/parser/exceptions.py
# class AprsParseError(ParseError):
#     """Parse error while parsing an aprs packet."""
#     def __init__(self, aprs_string):
#         self.aprs_string = aprs_string
# 
#         self.message = "This is not a valid APRS packet: {}".format(aprs_string)
#         super(AprsParseError, self).__init__(self.message)


# Path: tests/parser/test_parse.py
import unittest
import unittest.mock as mock
import os
from datetime import datetime
from time import sleep
from ogn.parser.parse import parse
from ogn.parser.exceptions import AprsParseError

        message = parse("VITACURA1>APRS,TCPIP*,qAC,GLIDERN4:/201146h3322.79SI07034.80W&/A=002329 Vitacura Municipal Aerodrome, Club de Planeadores Vitacura")

        self.assertEqual(message['user_comment'], "Vitacura Municipal Aerodrome, Club de Planeadores Vitacura")

        message_with_id = parse("ALFALFAL>APRS,TCPIP*,qAC,GLIDERN4:/221830h3330.40SI07007.88W&/A=008659 Alfalfal Hidroelectric Plant, Club de Planeadores Vitacurs")

        self.assertEqual(message_with_id['user_comment'], "Alfalfal Hidroelectric Plant, Club de Planeadores Vitacurs")

    @mock.patch('ogn.parser.parse_module.createTimestamp')
    def test_default_reference_date(self, createTimestamp_mock):
        valid_aprs_string = "Lachens>APRS,TCPIP*,qAC,GLIDERN2:/165334h4344.70NI00639.19E&/A=005435 v0.2.1 CPU:0.3 RAM:1764.4/2121.4MB NTP:2.8ms/+4.9ppm +47.0C RF:+0.70dB"

        parse(valid_aprs_string)
        call_args_before = createTimestamp_mock.call_args

        sleep(1)

        parse(valid_aprs_string)
        call_args_seconds_later = createTimestamp_mock.call_args

        self.assertNotEqual(call_args_before, call_args_seconds_later)

    def test_copy_constructor(self):
        valid_aprs_string = "FLRDDA5BA>APRS,qAS,LFMX:/160829h4415.41N/00600.03E'342/049/A=005524 id0ADDA5BA -454fpm -1.1rot 8.8dB 0e +51.2kHz gps4x5"
        message = parse(valid_aprs_string)

        self.assertEqual(message['name'], 'FLRDDA5BA')
        self.assertEqual(message['address'], 'DDA5BA')



if __name__ == '__main__':