mirror of
https://github.com/flobz/psa_car_controller.git
synced 2026-08-22 01:16:14 +00:00
handle PT0S hour format
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import re
|
||||
from functools import wraps
|
||||
from threading import Semaphore, Timer
|
||||
import socket
|
||||
@@ -47,3 +48,19 @@ def rate_limit(limit, every):
|
||||
def is_port_in_use(ip, port):
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||||
return s.connect_ex((ip, port)) == 0
|
||||
|
||||
|
||||
def parse_hour(hour_str):
|
||||
reg = r"PT([0-9]{1,2})H([0-9]{1,2})?|PT([0-9]{1,2})S"
|
||||
hour_minute = re.findall(reg, hour_str)[0]
|
||||
second = 0
|
||||
if hour_minute[0] == '':
|
||||
hour = 0
|
||||
second = hour_minute[2]
|
||||
else:
|
||||
hour = int(hour_minute[0])
|
||||
if hour_minute[1] == '':
|
||||
minute = 0
|
||||
else:
|
||||
minute = hour_minute[1]
|
||||
return hour, minute, second
|
||||
|
||||
+2
-10
@@ -1,5 +1,4 @@
|
||||
import json
|
||||
import re
|
||||
import threading
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
@@ -22,7 +21,7 @@ from otp.otp import load_otp, new_otp_session, save_otp, ConfigException, Otp
|
||||
from psa_connectedcar.rest import ApiException
|
||||
from mylogger import logger
|
||||
|
||||
from libs.utils import rate_limit
|
||||
from libs.utils import rate_limit, parse_hour
|
||||
from web.abrp import Abrp
|
||||
from web.db import Database
|
||||
|
||||
@@ -337,17 +336,10 @@ class MyPSACC:
|
||||
return json.dumps(data)
|
||||
|
||||
def __get_charge_hour(self, vin):
|
||||
reg = r"PT([0-9]{1,2})H([0-9]{1,2})?"
|
||||
data = self.get_vehicle_info(vin)
|
||||
hour_str = data.get_energy('Electric').charging.next_delayed_time
|
||||
try:
|
||||
hour_minute = re.findall(reg, hour_str)[0]
|
||||
hour = int(hour_minute[0])
|
||||
if hour_minute[1] == '':
|
||||
minute = 0
|
||||
else:
|
||||
minute = hour_minute[1]
|
||||
return hour, minute
|
||||
return parse_hour(hour_str)[:2]
|
||||
except IndexError:
|
||||
logger.exception("Can't get charge hour: %s", hour_str)
|
||||
return None
|
||||
|
||||
+6
-1
@@ -16,7 +16,7 @@ from mylogger import my_logger
|
||||
from otp.otp import load_otp, save_otp
|
||||
from charge_control import ChargeControls
|
||||
from trip import Trips
|
||||
from libs.utils import get_temp
|
||||
from libs.utils import get_temp, parse_hour
|
||||
from web.db import Database
|
||||
from web.figures import get_figures, get_battery_curve_fig, get_altitude_fig
|
||||
import pytz
|
||||
@@ -250,6 +250,11 @@ class TestUnit(unittest.TestCase):
|
||||
Database.record_position(None, "xx", 11, latitude, longitude - 0.05, None, date0, 40, None, False)
|
||||
assert old_dummy_value != dummy_value
|
||||
|
||||
def test_parse_hour(self):
|
||||
expected_res = [(2, 0, 0), (3, '14', 0), (0, 0, '2')]
|
||||
assert expected_res == [parse_hour(h) for h in ["PT2H", "PT3H14", "PT2S"]]
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
my_logger(handler_level=os.environ.get("DEBUG_LEVEL", 20))
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user