async def test_gps_time_sync(ovshell: testing.OpenVarioShellStub, monkeypatch) -> None: # GIVEN subpr_mock = mock.Mock() monkeypatch.setattr("ovshell_core.gpstime.subprocess", subpr_mock) gprmc_fields = "225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3" ovshell.devices.stub_add_nmea( [ api.NMEA("", "", "ZZZ", []), api.NMEA("", "", "GPRMC", gprmc_fields.split(",")), api.NMEA("", "", "AAA", []), ] ) state = gpstime.GPSTimeState() # WHEN await gpstime.gps_time_sync(ovshell, state) # THEN assert state.acquired is True datebin = ovshell.os.path("//bin/date") subpr_mock.run.assert_called_with( [datebin, "+%F %H:%M:%S", "-s", "1994-11-19 22:54:46"], check=True, capture_output=True, )
def test_parse_gps_datetime_longtime() -> None: gprmc_fields = "121931.00,A,4801.86153,N,01056.69289,E,53.587,8.64,270520,,,A" nmea = api.NMEA("", "", "GPRMC", gprmc_fields.split(",")) # WHEN dt = gpstime.parse_gps_datetime(nmea) # THEN assert dt == datetime(2020, 5, 27, 12, 19, 31)
def test_parse_gps_datetime_bad_sentence() -> None: # GIVEN gprmc_fields = "225446,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3" nmea = api.NMEA("", "", "XXXXX", gprmc_fields.split(",")) # WHEN dt = gpstime.parse_gps_datetime(nmea) # THEN assert dt is None
def test_parse_gps_datetime_correct() -> None: # GIVEN gprmc_fields = "225446,A,4916.45,N,12311.12,W,000.5,054.7,191103,020.3" nmea = api.NMEA("", "", "GPRMC", gprmc_fields.split(",")) # WHEN dt = gpstime.parse_gps_datetime(nmea) # THEN assert dt == datetime(2003, 11, 19, 22, 54, 46)
def test_parse_gps_datetime_notime() -> None: # GIVEN gprmc_fields = ",,,,,,,,," nmea = api.NMEA("", "", "GPRMC", gprmc_fields.split(",")) # WHEN dt = gpstime.parse_gps_datetime(nmea) # THEN assert dt is None
def parse_nmea(device_id: str, message: bytes) -> api.NMEA: strmsg = message.decode().strip() if not is_nmea_valid(strmsg): raise InvalidNMEA() msg, chksum = strmsg.rsplit("*", 1) parts = msg.split(",") datatype = parts[0][1:] return api.NMEA( device_id=device_id, raw_message=strmsg, datatype=datatype, fields=parts[1:] )