-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwake_desktop.py
66 lines (54 loc) · 1.75 KB
/
wake_desktop.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: utf-8 -*-
# This file is a command-module for Dragonfly by Christo Butcher and castervoice by Synkarius
# Created by Vojtech Drábek on 2020-08-12
# Licensed under the LGPL, see <http://www.gnu.org/licenses/>
#
"""
A Wake On LAN rule adapted for Caster
"""
import socket
import threading
import time
from dragonfly import Function, Key, MappingRule, RunCommand, StartApp
from castervoice.lib.ctrl.mgr.rule_details import RuleDetails
from castervoice.lib.merge.state.short import R
from personal import NETWORK, DESKTOP, DESKTOP_MAC, TERMINAL
from wol import wake
def ping(host, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((host, port))
s.close()
return True
except:
return False
def my_wake():
wake(NETWORK, DESKTOP_MAC)
class WakeChecker(threading.Thread):
def __init__(self, host, port, action):
self._timer = None
self._host = host
self._port = port
self._action = action
self._running = True
super(WakeChecker, self).__init__()
def start(self):
print("Sending Wake On LAN to %s" % DESKTOP_MAC)
super(WakeChecker, self).start()
def check(self):
if ping(self._host, self._port):
self._running = False
self._action.execute()
def run(self):
while self._running:
time.sleep(10)
self.check()
class NetWakeRule(MappingRule):
pronunciation = "net wake rule"
args = TERMINAL.extend(['ssh', DESKTOP])
checker = WakeChecker(DESKTOP, 22, StartApp(*TERMINAL))
mapping = {
"wake desktop": R(Function(my_wake) + Function(checker.start))
}
def get_rule():
return NetWakeRule, RuleDetails(name="Net Wake Rule")