-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
115 lines (93 loc) · 3.7 KB
/
Copy path__init__.py
File metadata and controls
115 lines (93 loc) · 3.7 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File Path Manager
# import os
# sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
import json
import os
import time
from mycroft import MycroftSkill
from mycroft.util import play_wav
from mycroft.util.log import LOG
from websocket import create_connection
from .file_path_manager import FilePathManager
LOG.warning('Skill Keypad is Running ')
try:
from .code.keypad import Keypad
from mycroft.messagebus.client.ws import WebsocketClient
from mycroft.messagebus.message import Message
except ImportError:
# re-install yourself
from msm import MycroftSkillsManager
msm = MycroftSkillsManager()
msm.install("https://github.com/ITE-5th/skill-keypad")
sleep_time = 1
class KeypadSkill(MycroftSkill):
def __init__(self):
super(KeypadSkill, self).__init__("ImageCaptionSkill")
self.keypad_client = Keypad()
self.keypad_client.start(self.keypad_callback)
self.last_msg_time = 0
@staticmethod
def callback_fn(command):
try:
command_strip__lower = command.strip().lower()
if command_strip__lower == "":
return None
if command_strip__lower == 'shutdown':
return lambda: os.system('systemctl poweroff -i')
if command_strip__lower == 'reboot':
return lambda: os.system('reboot')
return lambda: send_message(command)
except:
return None
def get_callbacks(self):
return {
0: self.callback_fn(self.settings.get("key_0", "")),
1: self.callback_fn(self.settings.get("key_1", "")),
2: self.callback_fn(self.settings.get("key_2", "")),
3: self.callback_fn(self.settings.get("key_3", "")),
4: self.callback_fn(self.settings.get("key_4", "")),
5: self.callback_fn(self.settings.get("key_5", "")),
6: self.callback_fn(self.settings.get("key_6", "")),
7: self.callback_fn(self.settings.get("key_7", "")),
8: self.callback_fn(self.settings.get("key_8", "")),
9: self.callback_fn(self.settings.get("key_9", "")),
"A": self.callback_fn(self.settings.get("key_a", "")),
"B": self.callback_fn(self.settings.get("key_b", "")),
"C": self.callback_fn(self.settings.get("key_c", "")),
"D": self.callback_fn(self.settings.get("key_d", "")),
"*": self.callback_fn(self.settings.get("key_star", "")),
"#": self.callback_fn(self.settings.get("key_hash", "")),
}
def keypad_callback(self, key):
print(key)
if (self.last_msg_time + sleep_time) < time.time():
self.last_msg_time = time.time()
callbacks = self.get_callbacks()
if callbacks[key] is not None:
click_file = FilePathManager.resolve('/resources/click.wav')
# os.system('aplay -Dhw:0,0 ' + )
play_wav(click_file)
callbacks[key]()
else:
LOG.warning('NOT DEFINED')
else:
LOG.warning('Ignoring')
def stop(self):
super(KeypadSkill, self).shutdown()
LOG.warning("Keypad Skill CLOSED")
self.keypad_client.cleanup()
URL_TEMPLATE = "{scheme}://{host}:{port}{path}"
def send_message(message, host="localhost", port=8181, path="/core", scheme="ws"):
payload = json.dumps({
"type": "recognizer_loop:utterance",
"context": "",
"data": {
"utterances": [message]
}
})
url = URL_TEMPLATE.format(scheme=scheme, host=host, port=str(port), path=path)
ws = create_connection(url)
ws.send(payload)
ws.close()
def create_skill():
return KeypadSkill()