Skip to content

Commit

Permalink
Add odometer, range, charging time and firmware version
Browse files Browse the repository at this point in the history
  • Loading branch information
bonnee committed Jul 24, 2020
1 parent df72482 commit d1931da
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
31 changes: 30 additions & 1 deletion niu/niu.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import requests
import datetime
from requests.exceptions import ConnectionError as RequestsConnectionError
from requests.exceptions import HTTPError as RequestsHTTPError

Expand Down Expand Up @@ -104,14 +105,23 @@ def update_vehicles(self):
)
veh.update(resp["data"])

# Get batteries status
# Get battery status
resp = self._request(
"GET",
NIU_API_URL + "/v3/motor_data/battery_info",
params={"sn": veh.get_serial()},
)
veh.update(resp["data"])

# Get odometer
resp = self._request(
"POST",
NIU_API_URL + "/motoinfo/overallTally",
params={"sn": veh.get_serial()},
data={"sn": veh.get_serial(), "token": SESSION.token},
)
veh.update(resp["data"])

def get_vehicles(self):
return SESSION.vehicles

Expand Down Expand Up @@ -156,12 +166,21 @@ def __init__(self, *arg, **kw):
def get_serial(self):
return self["sn_id"]

def get_firmware(self):
return self["soft_version"]

def get_model(self):
return self["scooter_type"]

def get_name(self):
return self["scooter_name"]

def get_odometer(self):
return self["totalMileage"]

def get_range(self):
return self["mileage"]

def get_soc(self, index=-1):
bat = self._get_battery(index)

Expand All @@ -172,6 +191,16 @@ def get_soc(self, index=-1):

return soc / 2

def get_charging_left(self):
if self.is_charging():
left = float(self["leftTime"])
hours = int(left)
minutes = (left - hours) * 60

return datetime.timedelta(hours=hours, minutes=minutes)
else:
return datetime.timedelta(0)

def is_charging(self):
return self["isCharging"] == 1

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="niu",
version="0.1.4",
version="0.1.5",
author="Matteo Bonora",
author_email="bonora.matteo@gmail.com",
description="NIU cloud interface library",
Expand Down
9 changes: 6 additions & 3 deletions tools/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,28 @@
print("Found {} vehicles:".format(len(vehicles)))
for veh in vehicles:
print("\tSerial:\t\t{}".format(veh.get_serial()))
print("\tFirmware:\t{}".format(veh.get_firmware()))
print("\tModel:\t\t{}".format(veh.get_model()))
print("\tName:\t\t{}".format(veh.get_name()))
print("\tOdometer:\t{} Km".format(veh.get_odometer()))
print("\tRange:\t\t{} Km".format(veh.get_range()))

print(
"\tSoC:\t\t{}% {}".format(
veh.get_soc(), [veh.get_soc(x) for x in range(0, veh.get_battery_count())]
)
)
print(f"\tConnected:\t{veh.is_connected()}")
print(f"\tOn:\t\t{veh.is_on()}")
print(f"\tCharging:\t{veh.is_charging()}")
print(f"\tPower:\t\t{veh.is_on()}")
print(f"\tCharging:\t{veh.is_charging()} ({veh.get_charging_left()} left)")
print(f"\tLocked:\t\t{veh.is_locked()}")

descs = veh.get_battery_temp_desc()
temps = veh.get_battery_temp()

print("\tTemps:\t\t", end="")
for i, temp in enumerate(temps):
print(f"{temps[i]} ({descs[i]}), ", end="")
print(f"{temps[i]} °C ({descs[i]}), ", end="")
print() # newline

print("\tLocation:\t{}".format(veh.get_location()))
Expand Down

0 comments on commit d1931da

Please sign in to comment.