We will create as many classes as we will use to represent the different components of a drone. In a real-life example, these classes will interact with a library that interacts with sensors and actuators. In order to keep our example simple, we will make calls to time.sleep
to simulate interactions that take some time to set or get values to and from sensors and actuators.
First, we will create a Hexacopter
class that we will use to represent the hexacopter and a HexacopterStatus
class that we will use to store status data for the hexacopter. Create a new drone.py
file. The following lines shows all the necessary imports for the classes that we will create and the code that declares the Hexacopter
and HexacopterStatus
classes in the drone.py
file. The code file for the sample is included in the restful_python_chapter_09_01
folder:
from random import randint from time import sleep class HexacopterStatus: def __init__(self, motor_speed, turned_on): self.motor_speed = motor_speed self.turned_on = turned_on class Hexacopter: MIN_SPEED = 0 MAX_SPEED = 1000 def __init__(self): self.motor_speed = self.__class__.MIN_SPEED self.turned_on = False def get_motor_speed(self): return self.motor_speed def set_motor_speed(self, motor_speed): if motor_speed < self.__class__.MIN_SPEED: raise ValueError('The minimum speed is {0}'.format(self.__class__.MIN_SPEED)) if motor_speed > self.__class__.MAX_SPEED: raise ValueError('The maximum speed is {0}'.format(self.__class__.MAX_SPEED)) self.motor_speed = motor_speed self.turned_on = (self.motor_speed is not 0) sleep(2) return HexacopterStatus(self.get_motor_speed(), self.is_turned_on()) def is_turned_on(self): return self.turned_on def get_hexacopter_status(self): sleep(3) return HexacopterStatus(self.get_motor_speed(), self.is_turned_on())
The HexacopterStatus
class just declares a constructor, that is, the __init__
method. This method receives many arguments and uses them to initialize the attributes with the same names: motor_speed
and turned_on
.
The Hexacopter
class declares two class attributes that specify the minimum and maximum speed values: MIN_SPEED
and MAX_SPEED
. The constructor, that is, the __init__
method, initializes the motor_speed
attribute with the MIN_SPEED
value and sets the turned_on
attribute to False
.
The get_motor_speed
method returns the value of the motor_speed
attribute. The set_motor_speed
method checks whether the value for the motor_speed
argument is in the valid range. In case the validation fails, the method raises a ValueError
exception. Otherwise, the method sets the value of the motor_speed
attribute with the received value and sets the value for the turned_on
attribute to True
if the motor_speed
is greater than 0. Finally, the method calls sleep to simulate it takes two seconds to retrieve the hexacopter status and then returns a HexacopterStatus
instance initialized with the motor_speed
and turned_on
attribute values, retrieved through specific methods.
The get_hexacopter_status
method calls sleep to simulate it takes three seconds to retrieve the hexacopter status and then returns a HexacopterStatus
instance initialized with the motor_speed
and turned_on
attribute values.
Now, we will create a LightEmittingDiode
class that we will use to represent each LED. Open the previously created drone.py
file and add the following lines. The code file for the sample is included in the restful_python_chapter_09_01
folder:
class LightEmittingDiode: MIN_BRIGHTNESS_LEVEL = 0 MAX_BRIGHTNESS_LEVEL = 255 def __init__(self, identifier, description): self.identifier = identifier self.description = description self.brightness_level = self.__class__.MIN_BRIGHTNESS_LEVEL def get_brightness_level(self): sleep(1) return self.brightness_level def set_brightness_level(self, brightness_level): if brightness_level < self.__class__.MIN_BRIGHTNESS_LEVEL: raise ValueError('The minimum brightness level is {0}'.format(self.__class__.MIN_BRIGHTNESS_LEVEL)) if brightness_level > self.__class__.MAX_BRIGHTNESS_LEVEL: raise ValueError('The maximum brightness level is {0}'.format(self.__class__.MAX_BRIGHTNESS_LEVEL)) sleep(2) self.brightness_level = brightness_level
The LightEmittingDiode
class declares two class attributes that specify the minimum and maximum brightness level values: MIN_BRIGHTNESS_LEVEL
and MAX_BRIGHTNESS_LEVEL
. The constructor, that is, the __init__
method, initializes the brightness_level
attribute with the MIN_BRIGHTNESS_LEVEL
and the id
and description
attributes with the values received in the arguments with the same names.
The get_brightness_level
method calls sleep to simulate, it takes 1 second to retrieve the brightness level for the wired LED and then returns the value of the brightness_level
attribute.
The set_brightness_level
method checks whether the value for the brightness_level
argument is in the valid range. In case the validation fails, the method raises a ValueError
exception. Otherwise, the method calls sleep
to simulate it takes two seconds to set the new brightness level and finally sets the value of the brightness_level
attribute with the received value.
Now, we will create an Altimeter
class that we will use to represent the altimeter. Open the previously created drone.py
file and add the following lines. The code file for the sample is included in the restful_python_chapter_09_01
folder:
class Altimeter: def get_altitude(self): sleep(1) return randint(0, 3000)
The Altimeter
class declares a get_altitude
method that calls sleep
to simulate it takes one second to retrieve the altitude from the altimeter and finally generates a random integer from 0 to 3000
(inclusive) and returns it.
Finally, we will create a Drone
class that we will use to represent the drone with its sensors and actuators. Open the previously created drone.py
file and add the following lines. The code file for the sample is included in the restful_python_chapter_09_01
folder
class Drone: def __init__(self): self.hexacopter = Hexacopter() self.altimeter = Altimeter() self.blue_led = LightEmittingDiode(1, 'Blue LED') self.white_led = LightEmittingDiode(2, 'White LED') self.leds = { self.blue_led.identifier: self.blue_led, self.white_led.identifier: self.white_led }
The Drone
class just declares a constructor, that is, the __init__
method that creates instances of the previously declared classes that represent the different components for the drone. The leds
attribute saves a dictionary that has a key-value pair for each LightEmittingDiode
instance with its id and its instance.
18.117.98.250