-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path__init__.py
186 lines (158 loc) · 6.14 KB
/
__init__.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Copyright 2017 Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import time
import os
from ifaddr import get_adapters
from adapt.intent import IntentBuilder
from mycroft.skills.core import MycroftSkill, intent_handler
import mycroft.audio
from subprocess import check_output, CalledProcessError
def get_ifaces(ignore_list=None):
""" Build a dict with device names and their associated ip address.
Arguments:
ignore_list(list): list of devices to ignore. Defaults to "lo"
Returns:
(dict) with device names as keys and ip addresses as value.
"""
ignore_list = ignore_list or ['lo']
res = {}
for iface in get_adapters():
# ignore "lo" (the local loopback)
if iface.ips and iface.name not in ignore_list:
for addr in iface.ips:
if addr.is_IPv4:
res[iface.nice_name] = addr.ip
break
return res
def which(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return False
class IPSkill(MycroftSkill):
SEC_PER_LETTER = 0.65 # timing based on Mark 1 screen
LETTERS_PER_SCREEN = 9.0
def __init__(self):
super(IPSkill, self).__init__(name="IPSkill")
def initialize(self):
# Only register the SSID intent if iwlist is installed on the system
if which("iwlist"):
self.register_intent_file("what.ssid.intent",
self.handle_SSID_query)
@intent_handler(IntentBuilder("IPIntent").require("query").require("IP"))
def handle_query_IP(self, message):
addr = get_ifaces()
dot = self.dialog_renderer.render("dot")
if len(addr) == 0:
self.speak_dialog("no network connection")
return
elif len(addr) == 1:
self.enclosure.deactivate_mouth_events()
iface, ip = addr.popitem()
self.enclosure.mouth_text(ip)
self.gui_show(ip)
ip_spoken = ip.replace(".", " "+dot+" ")
self.speak_dialog("my address is",
{'ip': ip_spoken})
time.sleep((self.LETTERS_PER_SCREEN + len(ip)) *
self.SEC_PER_LETTER)
else:
self.enclosure.deactivate_mouth_events()
for iface in addr:
ip = addr[iface]
self.enclosure.mouth_text(ip)
self.gui_show(ip)
ip_spoken = ip.replace(".", " " + dot + " ")
self.speak_dialog("my address on X is Y",
{'interface': iface, 'ip': ip_spoken})
time.sleep((self.LETTERS_PER_SCREEN + len(ip)) *
self.SEC_PER_LETTER)
mycroft.audio.wait_while_speaking()
self.enclosure.activate_mouth_events()
self.enclosure.mouth_reset()
def handle_SSID_query(self, message):
addr = get_ifaces()
ssid = None
if len(addr) == 0:
self.speak_dialog("no network connection")
return
try:
scanoutput = check_output(["iwlist", "wlan0", "scan"])
for line in scanoutput.split():
line = line.decode("utf-8")
if line[:5] == "ESSID":
ssid = line.split('"')[1]
except CalledProcessError:
# Computer has no wlan0
pass
finally:
if ssid:
self.speak(ssid)
else:
self.speak_dialog("ethernet.connection")
@intent_handler(IntentBuilder("").require("query").require("IP")
.require("last").require("digits"))
def handle_query_last_part_IP(self, message):
ip = None
addr = get_ifaces()
if len(addr) == 0:
self.speak_dialog("no network connection")
return
self.enclosure.deactivate_mouth_events()
if "wlan0" in addr:
# Wifi is probably the one we're looking for
ip = addr['wlan0']
elif "eth0" in addr:
# If there's no wifi report the eth0
ip = addr['eth0']
elif len(addr) == 1:
# If none of the above matches and there's only one device
ip = list(addr.values())[0]
if ip:
self.gui_show(ip)
self.speak_last_digits(ip)
else:
# Ok now I don't know, I'll just report them all
self.speak_multiple_last_digits(addr)
self.enclosure.activate_mouth_events()
self.enclosure.mouth_reset()
def gui_show(self, ip):
self.gui['ip'] = ip
self.gui.show_page("ip-address.qml")
def speak_last_digits(self, ip):
ip_end = ip.split(".")[-1]
self.enclosure.mouth_text(ip_end)
self.speak_dialog("last digits", data={"digits": ip_end})
time.sleep(3) # Show for at least 3 seconds
mycroft.audio.wait_while_speaking()
def speak_multiple_last_digits(self, addr):
for key in addr:
ip_end = addr[key].split(".")[-1]
self.speak_dialog("last digits device",
data={'device': key, 'digits': ip_end})
self.gui_show(addr)
self.enclosure.mouth_text(ip_end)
time.sleep(3) # Show for at least 3 seconds
mycroft.audio.wait_while_speaking()
def create_skill():
return IPSkill()