vehicles_manager.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. class Vehicle:
  2. def __init__(self,
  3. id: str,
  4. name: str,
  5. ap_source_subdir: str,
  6. waf_build_command: str,
  7. ) -> None:
  8. self.id = id
  9. self.name = name
  10. self.ap_source_subdir = ap_source_subdir
  11. self.waf_build_command = waf_build_command
  12. def __eq__(self, other):
  13. if isinstance(other, Vehicle):
  14. return self.id == other.id
  15. return False
  16. def __hash__(self):
  17. return hash(self.id)
  18. # Default vehicles configuration
  19. DEFAULT_VEHICLES = [
  20. Vehicle(
  21. id="copter",
  22. name="Copter",
  23. ap_source_subdir="ArduCopter",
  24. waf_build_command="copter"
  25. ),
  26. Vehicle(
  27. id="plane",
  28. name="Plane",
  29. ap_source_subdir="ArduPlane",
  30. waf_build_command="plane"
  31. ),
  32. Vehicle(
  33. id="rover",
  34. name="Rover",
  35. ap_source_subdir="Rover",
  36. waf_build_command="rover"
  37. ),
  38. Vehicle(
  39. id="sub",
  40. name="Sub",
  41. ap_source_subdir="ArduSub",
  42. waf_build_command="sub"
  43. ),
  44. Vehicle(
  45. id="heli",
  46. name="Heli",
  47. ap_source_subdir="ArduCopter",
  48. waf_build_command="heli"
  49. ),
  50. Vehicle(
  51. id="blimp",
  52. name="Blimp",
  53. ap_source_subdir="Blimp",
  54. waf_build_command="blimp"
  55. ),
  56. Vehicle(
  57. id="tracker",
  58. name="Tracker",
  59. ap_source_subdir="AntennaTracker",
  60. waf_build_command="antennatracker"
  61. ),
  62. Vehicle(
  63. id="ap-periph",
  64. name="AP_Periph",
  65. ap_source_subdir="Tools/AP_Periph",
  66. waf_build_command="AP_Periph"
  67. ),
  68. ]
  69. class VehiclesManager:
  70. __singleton = None
  71. def __init__(self, vehicles: list = DEFAULT_VEHICLES) -> None:
  72. """
  73. Initialize VehiclesManager with a list of vehicles.
  74. Args:
  75. vehicles: List of Vehicle objects. Defaults to DEFAULT_VEHICLES.
  76. """
  77. # Enforce singleton pattern by raising an error if
  78. # an instance already exists.
  79. if VehiclesManager.__singleton:
  80. raise RuntimeError("VehiclesManager must be a singleton.")
  81. self.vehicles = set(vehicles)
  82. VehiclesManager.__singleton = self
  83. def get_all_vehicles(self) -> frozenset:
  84. return frozenset(self.vehicles)
  85. def add_vehicle(self, vehicle: Vehicle) -> None:
  86. return self.vehicles.add(vehicle)
  87. def get_vehicle_by_id(self, vehicle_id: str) -> Vehicle:
  88. if vehicle_id is None:
  89. raise ValueError("vehicle_id is a required parameter.")
  90. return next(
  91. (
  92. vehicle for vehicle in self.get_all_vehicles()
  93. if vehicle.id == vehicle_id
  94. ),
  95. None
  96. )
  97. @staticmethod
  98. def get_singleton() -> "VehiclesManager":
  99. return VehiclesManager.__singleton