Browse Source

Merge pull request #8 from kalliope-project/wol

add wake on lan neuron
Nicolas Marcq 8 years ago
parent
commit
d55685278e

+ 2 - 0
Docs/dev_env_install.md

@@ -29,6 +29,8 @@ pip install python-crontab
 pip install cffi
 pip install pygmail
 pip install pushetta
+pip install wakeonlan
+pip install ipaddress
 pip install pyowm
 ```
 

+ 7 - 0
brain.yml

@@ -162,3 +162,10 @@
           message: "lost"
     signals:
       - order: "où est mon téléphone"
+
+  - name: "wake my PC"
+    neurons:
+      - wake_on_lan:
+          mac_address: "00-00-00-00-00-00"
+    signals:
+      - order: "réveil mon PC"

+ 4 - 0
core/NeuronModule.py

@@ -13,6 +13,10 @@ logging.basicConfig()
 logger = logging.getLogger("kalliope")
 
 
+class InvalidParameterException(Exception):
+    pass
+
+
 class MissingParameterException(Exception):
     pass
 

+ 2 - 0
install/files/python_requirements.txt

@@ -8,4 +8,6 @@ python-crontab==2.1.1
 cffi==1.8.3
 pygmail==0.0.5.4
 pushetta==1.0.15
+wakeonlan==0.2.2
+ipaddress==1.0.16
 pyowm==2.5.0

+ 1 - 0
neurons/__init__.py

@@ -9,3 +9,4 @@ from gmail_checker import Gmail_checker
 from push_message import Push_message
 from openweathermap import Openweathermap
 from tasker_autoremote import Tasker_autoremote
+from wake_on_lan import Wake_on_lan

+ 50 - 0
neurons/wake_on_lan/README.md

@@ -0,0 +1,50 @@
+# wake_on_lan
+
+## Synopsis
+
+Allows a computer to be turned on or awakened from the [WOL](https://en.wikipedia.org/wiki/Wake-on-LAN) protocol by Kalliope.
+
+## Options
+
+| parameter         | required | default         | choices  | comment                                                                                                                                               |
+|-------------------|----------|-----------------|----------|-------------------------------------------------------------------------------------------------------------------------------------------------------|
+| mac_address       | yes      |                 |          | Mac address of the target PC to wake up. Accepted format: 'ff.ff.ff.ff.ff.ff', '00-00-00-00-00-00', 'FFFFFFFFFFFF'                                    |
+| broadcast_address | no       | 255.255.255.255 |          | Broadcast address where the magic packet will bee sent. By default on most LAN is 255.255.255.255                                                     |
+| port              | no       | 9               |          | The magic packet is typically sent as a UDP datagram to port 0,6 7 or 9. This parameter must be an integer. Do not add 'quotes' in your configuration |
+
+
+## Return Values
+
+None
+
+
+## Synapses example
+
+Kalliope will send a magic packet to the mac address `00-00-00-00-00-00`
+```
+- name: "wake my PC"
+    neurons:
+      - wake_on_lan:
+          mac_address: "00-00-00-00-00-00"
+    signals:
+      - order: "wake my PC"
+```
+
+If your broadcast address is not 255.255.255.255, or if your ethernet card does not listen on the standard 9 port, you can override default parameters.
+In the following example, we suppose that kalliope is on a local areal network 172.16.0.0/16. The broadcast address would be 172.16.255.255.
+```
+- name: "wake my PC"
+    neurons:
+      - wake_on_lan:
+          mac_address: "00-00-00-00-00-00"
+          broadcast_address: "172.16.255.255"
+          port: 7
+    signals:
+      - order: "wake my PC"
+```
+
+## Notes
+
+> **Note:** The target computer must be on the same local area network as Kalliope.
+
+> **Note:** The target computer must has wake on lan activated in BIOS settings and my be in [OS settings](http://www.groovypost.com/howto/enable-wake-on-lan-windows-10/) too.

+ 36 - 0
neurons/wake_on_lan/Wake_on_lan.py

@@ -0,0 +1,36 @@
+import ipaddress
+import logging
+
+from core.NeuronModule import NeuronModule, MissingParameterException, InvalidParameterException
+from wakeonlan import wol
+
+logging.basicConfig()
+logger = logging.getLogger("kalliope")
+
+
+class Wake_on_lan(NeuronModule):
+    def __init__(self, **kwargs):
+        super(Wake_on_lan, self).__init__(**kwargs)
+
+        mac_address = kwargs.get('mac_address', None)
+        broadcast_address = kwargs.get('broadcast_address', '255.255.255.255')
+        port = kwargs.get('port', 9)
+
+        # check we provide a mac address
+        if mac_address is None:
+            raise MissingParameterException("mac_address parameter required")
+
+        # convert to unicode for testing
+        broadcast_address_unicode = broadcast_address.decode('utf-8')
+        # check the ip address is a valid one
+        ipaddress.ip_address(broadcast_address_unicode)
+
+        # check the port
+        if type(port) is not int:
+            raise InvalidParameterException("port argument must be an integer. Remove quotes in your configuration.")
+
+        logger.debug("Call Wake_on_lan_neuron with parameters: mac_address: %s, broadcast_address: %s, port: %s"
+                     % (mac_address, broadcast_address, port))
+
+        # send the magic packet, the mac address format will be check by the lib
+        # wol.send_magic_packet(mac_address, ip_address=broadcast_address, port=port)

+ 1 - 0
neurons/wake_on_lan/__init__.py

@@ -0,0 +1 @@
+from Wake_on_lan import Wake_on_lan

+ 1 - 1
test.py

@@ -13,7 +13,7 @@ logger.setLevel(logging.DEBUG)
 
 
 
-order = "test heure"
+order = "réveil mon PC"
 oa = OrderAnalyser(order)
 oa.start()