-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwake_on_lan.py
45 lines (34 loc) · 1.74 KB
/
wake_on_lan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import ipaddress
import logging
from kalliope.core.NeuronModule import NeuronModule, MissingParameterException, InvalidParameterException
from wakeonlan import send_magic_packet
logging.basicConfig()
logger = logging.getLogger("kalliope")
class Wake_on_lan(NeuronModule):
def __init__(self, **kwargs):
super(Wake_on_lan, self).__init__(**kwargs)
self.mac_address = kwargs.get('mac_address', None)
self.broadcast_address = kwargs.get('broadcast_address', '255.255.255.255')
self.port = kwargs.get('port', 9)
# check parameters
if self._is_parameters_ok():
# check the ip address is a valid one
ipaddress.ip_address(self.broadcast_address)
logger.debug("Call Wake_on_lan_neuron with parameters: mac_address: %s, broadcast_address: %s, port: %s"
% (self.mac_address, self.broadcast_address, self.port))
# send the magic packet, the mac address format will be check by the lib
send_magic_packet(self.mac_address, ip_address=self.broadcast_address, port=self.port)
def _is_parameters_ok(self):
"""
Check if received parameters are ok to perform operations in the neuron
:return: true if parameters are ok, raise an exception otherwise
.. raises:: InvalidParameterException, MissingParameterException
"""
# check we provide a mac address
if self.mac_address is None:
raise MissingParameterException("mac_address parameter required")
# check the port
if type(self.port) is not int:
raise InvalidParameterException(
"port argument must be an integer. Remove quotes in your configuration.")
return True