wake_on_lan.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import ipaddress
  2. import logging
  3. from core.NeuronModule import NeuronModule, MissingParameterException, InvalidParameterException
  4. from wakeonlan import wol
  5. logging.basicConfig()
  6. logger = logging.getLogger("kalliope")
  7. class Wake_on_lan(NeuronModule):
  8. def __init__(self, **kwargs):
  9. super(Wake_on_lan, self).__init__(**kwargs)
  10. self.mac_address = kwargs.get('mac_address', None)
  11. self.broadcast_address = kwargs.get('broadcast_address', '255.255.255.255')
  12. self.port = kwargs.get('port', 9)
  13. # check parameters
  14. if self._is_parameters_ok():
  15. # convert to unicode for testing
  16. broadcast_address_unicode = self.broadcast_address.decode('utf-8')
  17. # check the ip address is a valid one
  18. ipaddress.ip_address(broadcast_address_unicode)
  19. logger.debug("Call Wake_on_lan_neuron with parameters: mac_address: %s, broadcast_address: %s, port: %s"
  20. % (self.mac_address, self.broadcast_address, self.port))
  21. # send the magic packet, the mac address format will be check by the lib
  22. wol.send_magic_packet(self.mac_address, ip_address=self.broadcast_address, port=self.port)
  23. def _is_parameters_ok(self):
  24. """
  25. Check if received parameters are ok to perform operations in the neuron
  26. :return: true if parameters are ok, raise an exception otherwise
  27. .. raises:: InvalidParameterException, MissingParameterException
  28. """
  29. # check we provide a mac address
  30. if self.mac_address is None:
  31. raise MissingParameterException("mac_address parameter required")
  32. # check the port
  33. if type(self.port) is not int:
  34. raise InvalidParameterException(
  35. "port argument must be an integer. Remove quotes in your configuration.")
  36. return True