Skip to content

Commit b45a927

Browse files
committed
Merge branch 'issue37' into fl
2 parents 9a4fff7 + 9e69682 commit b45a927

File tree

9 files changed

+120
-18
lines changed

9 files changed

+120
-18
lines changed

nabcommon/network.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import fcntl
2+
import re
3+
import socket
4+
import struct
5+
6+
7+
def ip_address(ifname="wlan0"):
8+
"""
9+
Return the IP address for given interface
10+
"""
11+
try:
12+
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
13+
ip_addr = socket.inet_ntoa(
14+
fcntl.ioctl(
15+
s.fileno(),
16+
0x8915, # SIOCGIFADDR
17+
struct.pack("256s", bytes(ifname[:15], "utf-8")),
18+
)[20:24]
19+
)
20+
matchObj = re.match(r"169\.254", ip_addr)
21+
if matchObj:
22+
# ignore self-assigned link-local address
23+
return None
24+
else:
25+
return ip_addr
26+
except OSError:
27+
return None
28+
29+
30+
def internet_connection():
31+
"""
32+
Return True if connected to Internet, False otherwise
33+
"""
34+
DNS_SERVER_LIST = [
35+
"1.1.1.1", # Cloudflare
36+
"208.67.222.222", # Open DNS
37+
"8.8.8.8", # Google DNS
38+
"1.0.0.1", # Cloudflare
39+
"208.67.220.220", # Open DNS
40+
"8.8.4.4", # Google DNS
41+
]
42+
dns_port = 53
43+
timeout = 3.0
44+
for dns_server in DNS_SERVER_LIST:
45+
try:
46+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
47+
s.settimeout(timeout)
48+
s.connect((dns_server, dns_port))
49+
return True
50+
except OSError:
51+
pass
52+
return False

nabd/nabd.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from lockfile import AlreadyLocked, LockFailed
1919
from lockfile.pidlockfile import PIDLockFile
2020

21-
from nabcommon import nablogging, settings
21+
from nabcommon import nablogging, network, settings
2222
from nabcommon.nabservice import NabService
2323

2424
from .ears import Ears
@@ -30,6 +30,8 @@
3030
TagFlags,
3131
)
3232

33+
_PYTEST = os.path.basename(sys.argv[0]) != "nabd.py"
34+
3335

3436
class State(Enum):
3537
IDLE = "idle"
@@ -110,7 +112,7 @@ async def reload_config(self):
110112
self.nlu = NLU(self._nlu_locale)
111113
Nabd.leds_boot(self.nabio, 4)
112114
self.nabio.set_leds(None, None, None, None, None)
113-
self.nabio.pulse(Led.BOTTOM, (255, 0, 255))
115+
self.nabio.pulse(Led.BOTTOM, (0, 255, 255)) # Cyan
114116

115117
async def _do_transition_to_idle(self):
116118
"""
@@ -119,8 +121,16 @@ async def _do_transition_to_idle(self):
119121
Thread: service or idle_worker_loop
120122
"""
121123
left, right = self.ears["left"], self.ears["right"]
122-
await self.nabio.move_ears_with_leds((255, 0, 255), left, right)
123-
self.nabio.pulse(Led.BOTTOM, (255, 0, 255))
124+
await self.nabio.move_ears_with_leds((0, 255, 255), left, right)
125+
self.nabio.pulse(Led.BOTTOM, (0, 255, 255)) # Cyan
126+
if network.ip_address(self.nabio.network_interface()) is None:
127+
# not even a local network connection: real bad
128+
logging.error("no network connection")
129+
self.nabio.pulse(Led.BOTTOM, (255, 0, 0)) # Red
130+
elif not network.internet_connection():
131+
# local network connection, but no Internet access: not so good
132+
logging.warning("no Internet access")
133+
self.nabio.pulse(Led.BOTTOM, (255, 165, 0)) # Orange
124134

125135
async def sleep_setup(self):
126136
self.nabio.set_leds(None, None, None, None, None)
@@ -843,9 +853,8 @@ async def _shutdown(self, doReboot):
843853
await self._do_system_command("/sbin/halt")
844854

845855
async def _do_system_command(self, sytemCommandStr):
846-
inTesting = os.path.basename(sys.argv[0]) in ("pytest", "py.test")
847-
if not inTesting:
848-
logging.info(f"Initiating system command: {sytemCommandStr}")
856+
logging.info(f"Initiating system command: {sytemCommandStr}")
857+
if not _PYTEST:
849858
os.system(sytemCommandStr)
850859

851860
def ears_callback(self, ear):

nabd/nabio.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,11 @@ def has_rfid(self):
328328
"""Determine if we have an rfid reader"""
329329
raise NotImplementedError("Should have implemented")
330330

331+
@abc.abstractmethod
332+
def network_interface(self):
333+
"""Return name of network interface"""
334+
raise NotImplementedError("Should have implemented")
335+
331336
async def test(self, test):
332337
if test == "ears":
333338
(

nabd/nabio_hw.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ def has_sound_input(self):
2727
def has_rfid(self):
2828
return self.model == NabIOHW.MODEL_2019_TAGTAG
2929

30+
def network_interface(self):
31+
return "wlan0"
32+
3033
async def gestalt(self):
3134
MODEL_NAMES = {
3235
NabIO.MODEL_2018: "2018",

nabd/nabio_virtual.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ def has_sound_input(self):
4949
def has_rfid(self):
5050
return True
5151

52+
def network_interface(self):
53+
return "eth0"
54+
5255
def update_rabbit(self):
5356
for writer in self.virtual_clients:
5457
self.display_rabbit(writer)

nabd/tests/mock.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ def has_sound_input(self):
9494
def has_rfid(self):
9595
return False
9696

97+
def network_interface(self):
98+
return "eth0"
99+
97100
async def gestalt(self):
98101
return {"model": "Test mock"}
99102

nabd/tests/nabd_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def test_init(self):
7676
self.assertEqual(self.nabio.left_led, None)
7777
self.assertEqual(self.nabio.center_led, None)
7878
self.assertEqual(self.nabio.right_led, None)
79-
self.assertEqual(self.nabio.bottom_led, "pulse((255, 0, 255))")
79+
self.assertEqual(self.nabio.bottom_led, "pulse((0, 255, 255))") # Cyan
8080
self.assertEqual(self.nabio.nose_led, None)
8181

8282
def service_socket(self):

nabweb/locale/fr_FR/LC_MESSAGES/django.po

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2021-06-18 22:58+0200\n"
11+
"POT-Creation-Date: 2021-07-14 14:10+0200\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -175,49 +175,75 @@ msgstr ""
175175
"En dernier ressort, vous pouvez aussi arrêter le lapin en appuyant le bouton "
176176
"sur sa tête rapidement trois fois à la suite."
177177

178-
#: templates/nabweb/help.html:15
178+
#: templates/nabweb/help.html:14
179+
msgid "Base LED pulse color"
180+
msgstr "La couleur de pulsation de la LED de base"
181+
182+
#: templates/nabweb/help.html:14
183+
msgid "indicates the rabbit's network connection state"
184+
msgstr "indique l'état de la connexion réseau du lapin"
185+
186+
#: templates/nabweb/help.html:14
187+
msgid ""
188+
"<span class=\"badge badge-pill badge-info\">Cyan</span> means <i>all is "
189+
"fine</i>: rabbit has an IP address on your local network, and access to the "
190+
"Internet.<br><span class=\"badge badge-pill badge-warning\">Orange</span> "
191+
"means <i>not so good</i>: rabbit has a local IP address, but no Internet "
192+
"connection (check your router).<br><span class=\"badge badge-pill badge-"
193+
"danger\">Red</span> means <i>real bad</i>: rabbit has no IP address (check "
194+
"your WiFi access point / DHCP server)."
195+
msgstr ""
196+
"<span class=\"badge badge-pill badge-info\">Cyan</span> signifie <i>tout va "
197+
"bien</i> : le lapin a une adresse IP sur votre réseau local, et accès à "
198+
"l'Internet.<br><span class=\"badge badge-pill badge-warning\">Orange</span> "
199+
"signifie <i>pas très bon</i> : le lapin a une adresse IP locale, mais pas de "
200+
"connexion Internet (vérifiez votre routeur).<br><span class=\"badge badge-"
201+
"pill badge-danger\">Rouge</span> signifie <i>pas bon du tout</i> : le lapin "
202+
"n'a pas d'adresse IP (vérifiez votre point d'accès WiFi / serveur DHCP)."
203+
204+
#: templates/nabweb/help.html:16
179205
msgid "On the Internet"
180206
msgstr "Sur l'Internet"
181207

182-
#: templates/nabweb/help.html:16
208+
#: templates/nabweb/help.html:17
183209
msgid ""
184210
"<a target='_blank' href='https://tinyletter.com/nabaztag' rel='noopener "
185211
"noreferrer'>Nabaztag is back!</a>"
186212
msgstr ""
187213
"<a target='_blank' href='https://tinyletter.com/nabaztag' rel='noopener "
188214
"noreferrer'>Nabaztag revient !</a>"
189215

190-
#: templates/nabweb/help.html:16
216+
#: templates/nabweb/help.html:17
191217
msgid "Subscribe to the newsletter."
192218
msgstr "Abonnez vous à la lettre d'information."
193219

194-
#: templates/nabweb/help.html:17
220+
#: templates/nabweb/help.html:18
195221
msgid ""
196222
"The <a target='_blank' href='https://www.tagtagtag.fr/forum/' rel='noopener "
197223
"noreferrer'>Tag:Tag:Tag user forum</a>"
198224
msgstr ""
199225
"Le <a target='_blank' href='https://www.tagtagtag.fr/forum/' rel='noopener "
200226
"noreferrer'>forum d'utilisateurs Tag:Tag:Tag</a>"
201227

202-
#: templates/nabweb/help.html:17
228+
#: templates/nabweb/help.html:18
203229
msgid "Share your problems and solutions with other rabbit breeders."
204230
msgstr ""
205231
"Partagez vos problèmes et vos solutions avec d'autres éleveurs de lapins."
206232

207-
#: templates/nabweb/help.html:18
233+
#: templates/nabweb/help.html:19
208234
msgid ""
209235
"The <a target='_blank' href='https://github.com/nabaztag2018/pynab/#readme' "
210236
"rel='noopener noreferrer'>GitHub repository</a>"
211237
msgstr ""
212238
"Le <a target='_blank' href='https://github.com/nabaztag2018/pynab/#readme' "
213239
"rel='noopener noreferrer'>référentiel GitHub</a>"
214240

215-
#: templates/nabweb/help.html:18
241+
#: templates/nabweb/help.html:19
216242
msgid "Report bugs or contribute to the project with other developers."
217243
msgstr ""
218244
"Signalez des bogues ou contribuez au projet avec d'autres développeurs."
219245

220-
#: templates/nabweb/help.html:19
246+
#: templates/nabweb/help.html:20
221247
msgid ""
222248
"<a target='_blank' href='https://f-laurens.github.io/pynab_cli/' "
223249
"rel='noopener noreferrer'>Pynab CLI</a>"

nabweb/templates/nabweb/help.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ <h1 class="display-5">{% trans "Nabaztag help" %}</h1>
1111
<p>{% trans "<b>The " %}<a href='/help/airquality' title='{% trans "The air quality visual animations" %}'>{% trans "air quality visual animations" %}</b></a>.</p>
1212
<p id="asr"><b>{% trans "Talking to the rabbit (speech recognition)" %}</b><br>{% trans "You need to push and hold the button on the rabbit's head long enough to hear a 'beep', then wait a couple of seconds and speak. When you are done just release the button." %}<br>{% trans "The speech processing is done locally and the answer should come shortly!" %}</p>
1313
<p><b>{% trans "Reboot or shutdown the rabbit" %}</b><br>{% blocktrans %}You can do this cleanly by using the <kbd class="btn-warning">Reboot</kbd> or <kbd class="btn-danger">Shutdown</kbd> buttons on the <a target='_parent' href='/system-info/'>System information</a> tab of the configuration page.{% endblocktrans %}<br>{% trans "As a last resort, you can also shutdown the rabbit by pushing its head button quickly three times in a row." %}</p>
14-
<hr class="my-4">
14+
<p><b>{% trans "Base LED pulse color" %}</b> {% trans "indicates the rabbit's network connection state" %}<br>{% blocktrans %}<span class="badge badge-pill badge-info">Cyan</span> means <i>all is fine</i>: rabbit has an IP address on your local network, and access to the Internet.<br><span class="badge badge-pill badge-warning">Orange</span> means <i>not so good</i>: rabbit has a local IP address, but no Internet connection (check your router).<br><span class="badge badge-pill badge-danger">Red</span> means <i>real bad</i>: rabbit has no IP address (check your WiFi access point / DHCP server).{% endblocktrans %}</p>
15+
<hr class="my-4">
1516
<p class="lead">{% trans "On the Internet" %}</p>
1617
<p><b>{% trans "<a target='_blank' href='https://tinyletter.com/nabaztag' rel='noopener noreferrer'>Nabaztag is back!</a>" %}</b><br>{% trans "Subscribe to the newsletter." %}</p>
1718
<p><b>{% trans "The <a target='_blank' href='https://www.tagtagtag.fr/forum/' rel='noopener noreferrer'>Tag:Tag:Tag user forum</a>" %}</b><br>{% trans "Share your problems and solutions with other rabbit breeders." %}</p>

0 commit comments

Comments
 (0)