mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-07-24 07:42:05 +08:00
93286505e1
This change enhances the Sunnylink registration process by including better error checks and exception handling for cases when the public key is in use or known to be not unique. The commit also adjusts the setting of the ping time right after a successful registration. Additionally, cleanup occurred in the UI to streamline some code relating to the display of Sunnylink status. The handling of the last Sunnylink ping time has been removed from the system manager.
61 lines
2.0 KiB
Python
Executable File
61 lines
2.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
from cereal import log
|
|
from openpilot.common.api.sunnylink import SunnylinkApi, UNREGISTERED_SUNNYLINK_DONGLE_ID
|
|
from openpilot.common.params import Params
|
|
from openpilot.system.hardware import HARDWARE
|
|
from openpilot.system.version import is_prebuilt
|
|
import time
|
|
|
|
NetworkType = log.DeviceState.NetworkType
|
|
|
|
|
|
def is_network_connected() -> bool:
|
|
"""Check if the device is connected to a network."""
|
|
return HARDWARE.get_network_type() != NetworkType.none
|
|
|
|
|
|
def get_sunnylink_status(params=Params()) -> tuple[bool, bool]:
|
|
"""Get the status of Sunnylink on the device. Returns a tuple of (is_sunnylink_enabled, is_registered)."""
|
|
is_sunnylink_enabled = params.get_bool("SunnylinkEnabled")
|
|
is_registered = params.get("SunnylinkDongleId", encoding='utf-8') not in (None, UNREGISTERED_SUNNYLINK_DONGLE_ID)
|
|
return is_sunnylink_enabled, is_registered
|
|
|
|
|
|
def sunnylink_ready(params=Params()) -> bool:
|
|
"""Check if the device is ready to communicate with Sunnylink. That means it is enabled and registered."""
|
|
is_sunnylink_enabled, is_registered = get_sunnylink_status(params)
|
|
return is_sunnylink_enabled and is_registered
|
|
|
|
|
|
def sunnylink_need_register(params=Params()) -> bool:
|
|
"""Check if the device needs to be registered with Sunnylink."""
|
|
is_sunnylink_enabled, is_registered = get_sunnylink_status(params)
|
|
return is_sunnylink_enabled and not is_registered and is_network_connected()
|
|
|
|
|
|
def register_sunnylink():
|
|
"""Register the device with Sunnylink if it is enabled."""
|
|
extra_args = {}
|
|
|
|
if not Params().get_bool("SunnylinkEnabled"):
|
|
print("Sunnylink is not enabled. Exiting.")
|
|
exit(0)
|
|
|
|
if not is_prebuilt():
|
|
extra_args = {
|
|
"verbose": True,
|
|
"timeout": 60
|
|
}
|
|
|
|
sunnylink_id = SunnylinkApi(None).register_device(None, **extra_args)
|
|
print(f"SunnyLinkId: {sunnylink_id}")
|
|
|
|
|
|
def main():
|
|
"""The main method is expected to be called by the manager when the device boots up."""
|
|
register_sunnylink()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|