class Test(unittest.TestCase):

    def setUp(self):
        self.test_state = DataState()
        self.test_parser = AccelerometerParser(self.test_state)

    def testParse(self):
        test_line = "$AC001,477.72,524,598,626"
        self.test_parser.parse(test_line)
        self.assertEqual(477.72, self.test_state.get_data_item(DataState.get_data_name_at_idx(DataState.names.Time)), 'Time was not parsed correctly.')
        self.assertAlmostEqual(0.23, self.test_state.get_data_item(DataState.get_data_name_at_idx(DataState.names.X)), 2, 'X acceleration was not parsed correctly.')
        self.assertAlmostEqual(0.84, self.test_state.get_data_item(DataState.get_data_name_at_idx(DataState.names.Y)), 2, 'Y acceleration was not parsed correctly.')
        self.assertAlmostEqual(1.03, self.test_state.get_data_item(DataState.get_data_name_at_idx(DataState.names.Z)), 2, 'Z acceleration was not parsed correctly.')                 
        pass
Пример #2
0
def process(input_paths, output_url, output_format, tags, output_batch_size,
            start_date_datum, start_time_datum):
    data = DataState()
    time_util = TimeUtils(start_date_datum, start_time_datum)
    data.set_data_item('DateTime',
                       time_util.calculateTimestamp(None, None, 0.0))
    last_data_time = data.state_time
    parser = Parser(data)
    formatter = FormatterFactory.getFormatter(output_format, tags)
    ostream = io.StringIO()
    header_line = formatter.formatHeading(data)
    if header_line is not None:
        if verbose > 0:
            print(header_line)
        print(header_line, end='\n', file=ostream)
    line_count = 0
    for path in input_paths:
        print("Processing {0}".format(path))
        with open(path, "r") as ifile:
            for data_line in ifile:
                input_line = data_line.strip()
                if verbose > 0:
                    print(input_line)
                parser.parse(input_line)
                output_lines = formatter.formatData(data)
                if output_lines is not None:
                    for output_line in output_lines:
                        if verbose > 0:
                            print(output_line)
                        print(output_line, end='\n', file=ostream)
                        line_count = line_count + 1
                if data.state_time > last_data_time:
                    last_data_time = data.state_time
                    data.set_data_item(
                        'DateTime',
                        time_util.calculateTimestamp(
                            data.get_data_item('GPS_Date'),
                            data.get_data_item('GPS_Time'), data.state_time))
                    output_line = formatter.formatTimeIncrement(data)
                    if output_line is not None:
                        if verbose > 0:
                            print(output_line)
                        print(output_line, end='\n', file=ostream)
                        line_count = line_count + 1
                if line_count >= output_batch_size:
                    writeBatch(output_url, ostream)
                    ostream.close()
                    ostream = io.StringIO()
                    line_count = 0
        footer_line = formatter.formatFooter(data)
        if footer_line is not None:
            if verbose > 0:
                print(footer_line)
            print(footer_line, end='\n', file=ostream)
        writeBatch(output_url, ostream)
        ostream.close()
def scan(inpath, verbose):
    data = DataState()
    parser = Parser(data)
    line_count = 0
    datum_datetime = None
    accumulated_delta = 0.0
    delta_count = 0
    with open(inpath, "r") as ifile:
        for data_line in ifile:
            input_line = data_line.strip()
            if verbose > 0:
                print(input_line)
            if input_line.startswith('$AC') or input_line.startswith('$GPRMC'):
                parser.parse(input_line)
                print("Line {3} - Time: {0}    GPS Timestamp: {1} {2}".format(
                    data.get_data_item('Time'), data.get_data_item('GPS_Date'),
                    data.get_data_item('GPS_Time'), line_count))
                if data.is_dirty('GPS_Date') and data.is_dirty('GPS_Time'):
                    data_datetime = datetime.strptime(
                        "{0} {1}".format(data.get_data_item('GPS_Date'),
                                         data.get_data_item('GPS_Time')),
                        '%Y-%m-%d %H:%M:%S.%f')
                    data_deltatime = timedelta(0, data.get_data_item('Time'))
                    calculated_datum_datetime = data_datetime - data_deltatime
                    if datum_datetime is None:
                        datum_datetime = calculated_datum_datetime
                    print("Calculated Datum: {0}".format(
                        calculated_datum_datetime.isoformat()))
                    datum_delta = calculated_datum_datetime - datum_datetime
                    print("Calculated Delta: {0}".format(
                        datum_delta.total_seconds()))
                    accumulated_delta = accumulated_delta + datum_delta.total_seconds(
                    )
                    delta_count = delta_count + 1
        average_delta = accumulated_delta / delta_count
        print("Average Delta: {0}".format(average_delta))
        datum_datetime = datum_datetime + timedelta(0, average_delta)
        print("Datum: {0}".format(datum_datetime.isoformat()))