Repository: MycroftAI/mycroft-core

Issue:
Mycroft should indicate when there is no internet connection.,
Since an internet connection is required, we need to know if there is one at any given time.



Discussion:
Is it required?

I am not planning on mine being anything more than a home automation
controller.

While finding out what capital city is the highest above sea level is
interesting there is a whole lot you can do inside the network.

GR

Sent from my Nexus 5.

On 9 Aug 2016 3:10 p.m., "Arron Atchison" notifications@github.com wrote:

> Since an internet connection is required, we need to know if there is one
> at any given time.
> 
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> https://github.com/MycroftAI/mycroft-core/issues/321, or mute the thread
> https://github.com/notifications/unsubscribe-auth/AEy9nNY54N6aQDFSxloseZDQLiZRlMyBks5qeIpvgaJpZM4JgHmA
> .

Yeah, it requires an internet connection to use the STT services.


Resolving patch:
diff --git a/mycroft/client/speech/listener.py b/mycroft/client/speech/listener.py
--- a/mycroft/client/speech/listener.py
+++ b/mycroft/client/speech/listener.py
@@ -31,7 +31,7 @@
 from mycroft.messagebus.message import Message
 from mycroft.metrics import MetricsAggregator
 from mycroft.session import SessionManager
-from mycroft.util import CerberusAccessDenied
+from mycroft.util import CerberusAccessDenied, connected
 from mycroft.util.log import getLogger
 
 logger = getLogger(__name__)
@@ -168,27 +168,30 @@ def runnable():
     def transcribe(self, audio_segments):
         utterances = []
         threads = []
-        for audio in audio_segments:
-            if self._audio_length(audio) < self.MIN_AUDIO_SIZE:
-                logger.debug("Audio too short to send to STT")
-                continue
-
-            target = self._create_remote_stt_runnable(audio, utterances)
-            t = threading.Thread(target=target)
-            t.start()
-            threads.append(t)
-
-        for thread in threads:
-            thread.join()
-        if len(utterances) > 0:
-            payload = {
-                'utterances': utterances,
-                'session': SessionManager.get().session_id
-            }
-            self.emitter.emit("recognizer_loop:utterance", payload)
-            self.metrics.attr('utterances', utterances)
-        else:
-            raise sr.UnknownValueError
+        if connected():
+            for audio in audio_segments:
+                if self._audio_length(audio) < self.MIN_AUDIO_SIZE:
+                    logger.debug("Audio too short to send to STT")
+                    continue
+
+                target = self._create_remote_stt_runnable(audio, utterances)
+                t = threading.Thread(target=target)
+                t.start()
+                threads.append(t)
+
+            for thread in threads:
+                thread.join()
+            if len(utterances) > 0:
+                payload = {
+                    'utterances': utterances,
+                    'session': SessionManager.get().session_id
+                }
+                self.emitter.emit("recognizer_loop:utterance", payload)
+                self.metrics.attr('utterances', utterances)
+            else:
+                raise sr.UnknownValueError
+        else:  # TODO: Localization
+            self.__speak("This device is not connected to the Internet")
 
 
 class RecognizerLoopState(object):
diff --git a/mycroft/client/speech/main.py b/mycroft/client/speech/main.py
--- a/mycroft/client/speech/main.py
+++ b/mycroft/client/speech/main.py
@@ -25,7 +25,7 @@
 from mycroft.messagebus.message import Message
 from mycroft.tts import tts_factory
 from mycroft.util.log import getLogger
-from mycroft.util import kill
+from mycroft.util import kill, connected
 
 logger = getLogger("SpeechClient")
 client = None
@@ -118,6 +118,10 @@ def main():
     event_thread = Thread(target=connect)
     event_thread.setDaemon(True)
     event_thread.start()
+
+    if connected() is False:  # TODO: Localization
+        mute_and_speak("This device is not connected to the Internet")
+
     try:
         loop.run()
     except KeyboardInterrupt, e:
diff --git a/mycroft/util/__init__.py b/mycroft/util/__init__.py
--- a/mycroft/util/__init__.py
+++ b/mycroft/util/__init__.py
@@ -19,6 +19,7 @@
 import os
 import subprocess
 from os.path import dirname
+import socket
 
 import psutil
 
@@ -88,5 +89,25 @@ def kill(names):
                 pass
 
 
+def connected(host="8.8.8.8", port=53, timeout=3):
+    """
+    Thanks to 7h3rAm on
+    Host: 8.8.8.8 (google-public-dns-a.google.com)
+    OpenPort: 53/tcp
+    Service: domain (DNS/TCP)
+    """
+    try:
+        socket.setdefaulttimeout(timeout)
+        socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
+        return True
+    except IOError:
+        try:
+            socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(
+                ("8.8.4.4", port))
+            return True
+        except IOError:
+            return False
+
+
 class CerberusAccessDenied(Exception):
     pass


Test patch: