-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcontrol_pipe.py
More file actions
49 lines (41 loc) · 1.41 KB
/
control_pipe.py
File metadata and controls
49 lines (41 loc) · 1.41 KB
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
import os
import time
import threading
from configuration import config
class ControlPipe:
__ctl_path = None
__control = None
__event = None
__termination = None
def __init__(self, event, message):
self.__termination = threading.Event()
self.__event = event
self.__msg = message
self.__ctl_path = config.get_ctl_path()
self.fifo_setup()
# TODO: always try to delete, then make fifo (cleanup events before starting)
def fifo_setup(self):
try:
os.mkfifo(self.__ctl_path)
except FileExistsError:
os.remove(self.__ctl_path)
os.mkfifo(self.__ctl_path)
pass
self.__control = os.open(self.__ctl_path, os.O_RDONLY | os.O_NONBLOCK)
def listen(self):
threading.Thread(target=self.__listen).start()
def __listen(self):
while not self.__termination.is_set():
time.sleep(0.2)
# cmd = os.read(self.__control, 100).decode().strip().lower().split()
try:
cmd = os.read(self.__control, 500).decode().split()
except BlockingIOError:
print("control_pipe: BlockingIOError")
continue
if len(cmd) > 0:
self.__msg["command"] = cmd
self.__msg["source"] = "control_pipe"
self.__event.set()
def stop(self):
self.__termination.set()