Exemple #1
0
 def __init__(self):
     self.iface = pyw.winterfaces()[0]
     self.ap = AccessPoint(self.iface)
     self.server = None
     self.ws = WebsocketClient()
     self.init_events()
     self.conn_monitor = None
     self.conn_monitor_stop = threading.Event()
Exemple #2
0
def main():
    global ws

    ws = WebsocketClient()

    def _log(message):
        # If a message appear, log it
        logger.debug(message)

    ws.on('message', _log)

    ws.on('open', _load_neurons)
    ws.on('onyx_recognizer:utterance', _talk)
    ws.run_forever()
Exemple #3
0
def main():
    global ws
    #lock = Lock('skills')  # prevent multiple instances of this service

    # Connect this Skill management process to the websocket
    ws = WebsocketClient()

    # Listen for messages and echo them for logging
    def _echo(message):
        logger.debug(message)

    ws.on('message', _echo)

    # Kick off loading of skills
    ws.once('open', _load_skills)
    ws.run_forever()
Exemple #4
0
        def create_ws_detect():
            def onConnected(event=None):
                ws.emit(Message('onyx_detect'))
                t.close()

            ws = WebsocketClient()
            ws.on('connected', onConnected)
            # This will block until the client gets closed
            ws.run_forever()
Exemple #5
0
            def create_ws_send():
                def onConnected(event=None):
                    print("Sending message...")
                    payload = {'utterance': result, 'token': token}
                    ws.emit(Message('onyx_recognizer:utterance', payload))
                    ws.emit(Message('onyx_detect_finish'))
                    t.close()

                ws = WebsocketClient()
                ws.on('connected', onConnected)
                # This will block until the client gets closed
                ws.run_forever()
Exemple #6
0
token = config.API_TOKEN

LOG = getLogger('SpeechClient')
import speech_recognition as sr


def handle_speak(event):
    utterance = event.data.get('utterance')
    tts.execute(utterance)


def connect():
    ws.run_forever()


ws = WebsocketClient()
ws.on('speak', handle_speak)

event_thread = Thread(target=connect)
event_thread.setDaemon(True)
event_thread.start()


class Detector:
    def __init__(self):
        self.lang = config.LANG

    def detected_callback(self):
        def create_ws_detect():
            def onConnected(event=None):
                ws.emit(Message('onyx_detect'))
Exemple #7
0
def handle_speak(event):
    utterance = event.data.get('utterance')
    print(">> " + utterance)

def handle_test(event):
    print(event.data['utterances'][0])

def handle_finish(event):
    print("Finish")

def connect():
    # Once the websocket has connected, just watch it for speak events
    ws.run_forever()

ws = WebsocketClient()
ws.on('speak', handle_speak)
ws.on('onyx_recognizer:utterance', handle_test)
ws.on('finish', handle_finish)
event_thread = Thread(target=connect)
event_thread.setDaemon(True)
event_thread.start()

def cli():
    while True:
        try:
            time.sleep(1.5)
            result = input('You: ')

            print ("Sending message...")
            payload = {
Exemple #8
0
from onyx.sockyx.client.ws import WebsocketClient
from onyx.sockyx.message import Message

if len(sys.argv) == 2:
    messageToSend = sys.argv[1]
elif len(sys.argv) > 2:
    messageToSend = " ".join(sys.argv[2:])
else:
    filename = os.path.basename(__file__)
    print(filename)
    print("Simple command line interface to the messagebus.")
    print("Usage:   sockyx <utterance>\n")
    print("         where <utterance> is treated as if spoken to Onyx.")
    print("Example: " + filename + " onyx.wifi.start")
    exit()


def onConnected(event=None):
    print("Sending message...'" + messageToSend + "'")
    sockyx.emit(Message(messageToSend))
    sockyx.close()
    exit()


# Establish a connection with the messagebus
sockyx = WebsocketClient()
sockyx.on('connected', onConnected)

# This will block until the client gets closed
sockyx.run_forever()
Exemple #9
0
json = Json()
LOG = getLogger('Client')

def handle_speak(event):
    utterance = event.data.get('utterance')
    print(">> " + utterance)

def handle_finish(event):
    print("Finish")

def connect():
    # Once the websocket has connected, just watch it for speak events
    ws.run_forever()

ws = WebsocketClient()
ws.on('speak', handle_speak)
ws.on('finish', handle_finish)
event_thread = Thread(target=connect)
event_thread.setDaemon(True)
event_thread.start()


def cli():
    while True:
        try:
            time.sleep(1.5)
            result = input('You: ')

            print ("Sending message...")
            payload = {
Exemple #10
0
class WiFi:
    def __init__(self):
        self.iface = pyw.winterfaces()[0]
        self.ap = AccessPoint(self.iface)
        self.server = None
        self.ws = WebsocketClient()
        self.init_events()
        self.conn_monitor = None
        self.conn_monitor_stop = threading.Event()

    def init_events(self):
        '''
            Register handlers for various websocket events used
            to communicate with outside systems.
        '''

        # This event is generated by an outside mechanism.  On a
        # Holmes unit this comes from the Enclosure's menu item
        # being selected.
        self.ws.on('onyx.wifi.start', self.start)

        # These events are generated by Javascript in the captive
        # portal.
        self.ws.on('onyx.wifi.stop', self.stop)
        self.ws.on('onyx.wifi.scan', self.scan)
        self.ws.on('onyx.wifi.connect', self.connect)

    def start(self, event=None):
        '''
           Fire up the onyx access point for the user to connect to
           with a phone or computer.
        '''
        LOG.info("Starting access point...")

        # Fire up our access point
        self.ap.up()
        if not self.server:
            LOG.info("Creating web server...")
            self.server = WebServer(self.ap.ip, 80)
            LOG.info("Starting web server...")
            self.server.start()
            LOG.info("Created web server.")

        LOG.info("Access point started!\n%s" % self.ap.__dict__)
        self._start_connection_monitor()

    def _connection_prompt(self, prefix):
        # let the user know to connect to it...
        passwordSpelled = ",  ".join(self.ap.password)
        self._speak_and_show(
            prefix + " Use your mobile device or computer to "
                     "connect to the wifi network "
                     "'onyx';  Then enter the uppercase "
                     "password " + passwordSpelled,
            self.ap.password)

    def _speak_and_show(self, speak, show):
        ''' Communicate with the user throughout the process '''
        self.ws.emit(Message("speak", {'utterance': speak}))
        if show is None:
            return

        # TODO: This sleep should not be necessary, but without it the
        #       text to be displayed by enclosure.mouth_text() gets
        #       wiped out immediately when the utterance above is
        #       begins processing.
        #       Remove the sleep once this behavior is corrected.

    def _start_connection_monitor(self):
        LOG.info("Starting monitor thread...\n")
        if self.conn_monitor is not None:
            LOG.info("Killing old thread...\n")
            self.conn_monitor_stop.set()
            self.conn_monitor_stop.wait()

        self.conn_monitor = threading.Thread(
            target=self._do_connection_monitor,
            args={})
        self.conn_monitor.daemon = True
        self.conn_monitor.start()
        LOG.info("Monitor thread setup complete.\n")

    def _stop_connection_monitor(self):
        ''' Set flag that will let monitoring thread close '''
        self.conn_monitor_stop.set()

    def _do_connection_monitor(self):
        LOG.info("Invoked monitor thread...\n")
        mtimeLast = os.path.getmtime('/var/lib/misc/dnsmasq.leases')
        bHasConnected = False
        cARPFailures = 0
        timeStarted = time.time()
        timeLastAnnounced = 0  # force first announcement to now
        self.conn_monitor_stop.clear()

        while not self.conn_monitor_stop.isSet():
            # do our monitoring...
            mtime = os.path.getmtime('/var/lib/misc/dnsmasq.leases')
            if mtimeLast != mtime:
                # Something changed in the dnsmasq lease file -
                # presumably a (re)new lease
                bHasConnected = True
                cARPFailures = 0
                mtimeLast = mtime
                timeStarted = time.time()  # reset start time after connection
                timeLastAnnounced = time.time() - 45  # announce how to connect

            if time.time() - timeStarted > 60 * 5:
                # After 5 minutes, shut down the access point
                LOG.info("Auto-shutdown of access point after 5 minutes")
                self.stop()
                continue

            if time.time() - timeLastAnnounced >= 45:
                if bHasConnected:
                    self._speak_and_show(
                        "Now you can open your browser and go to start dot "
                        "onyx dot A I, then follow the instructions given "
                        " there",
                        "start.onyx.ai")
                else:
                    self._connection_prompt("Allow me to walk you through the "
                                            " wifi setup process; ")
                timeLastAnnounced = time.time()

            if bHasConnected:
                # Flush the ARP entries associated with our access point
                # This will require all network hardware to re-register
                # with the ARP tables if still present.
                if cARPFailures == 0:
                    res = cli_no_output('ip', '-s', '-s', 'neigh', 'flush',
                                        self.ap.subnet + '.0/24')
                    # Give ARP system time to re-register hardware
                    sleep(5)

                # now look at the hardware that has responded, if no entry
                # shows up on our access point after 2*5=10 seconds, the user
                # has disconnected
                if not self._is_ARP_filled():
                    cARPFailures += 1
                    if cARPFailures > 2:
                        self._connection_prompt("Connection lost,")
                        bHasConnected = False
                else:
                    cARPFailures = 0
            sleep(5)  # wait a bit to prevent thread from hogging CPU

        LOG.info("Exiting monitor thread...\n")
        self.conn_monitor_stop.clear()

    def _is_ARP_filled(self):
        res = cli_no_output('/usr/sbin/arp', '-n')
        out = str(res.get("stdout"))
        if out:
            # Parse output, skipping header
            for o in out.split("\n")[1:]:
                if o[0:len(self.ap.subnet)] == self.ap.subnet:
                    if "(incomplete)" in o:
                        # ping the IP to get the ARP table entry reloaded
                        ip_disconnected = o.split(" ")[0]
                        cli_no_output('/bin/ping', '-c', '1', '-W', '3',
                                      ip_disconnected)
                    else:
                        return True  # something on subnet is connected!
        return False

    def scan(self, event=None):
        LOG.info("Scanning wifi connections...")
        networks = {}
        status = self.get_status()

        for cell in Cell.all(self.iface):
            update = True
            ssid = cell.ssid
            quality = self.get_quality(cell.quality)

            # If there are duplicate network IDs (e.g. repeaters) only
            # report the strongest signal
            if networks.__contains__(ssid):
                update = networks.get(ssid).get("quality") < quality
            if update and ssid:
                networks[ssid] = {
                    'quality': quality,
                    'encrypted': cell.encrypted,
                    'connected': self.is_connected(ssid, status)
                }
        self.ws.emit(Message("onyx.wifi.scanned",
                             {'networks': networks}))
        LOG.info("Wifi connections scanned!\n%s" % networks)

    @staticmethod
    def get_quality(quality):
        values = quality.split("/")
        return float(values[0]) / float(values[1])

    def connect(self, event=None):
        if event and event.data:
            ssid = event.data.get("ssid")
            connected = self.is_connected(ssid)

            if connected:
                LOG.warn("onyx is already connected to %s" % ssid)
            else:
                self.disconnect()
                LOG.info("Connecting to: %s" % ssid)
                nid = wpa(self.iface, 'add_network')
                wpa(self.iface, 'set_network', nid, 'ssid', '"' + ssid + '"')

                if event.data.__contains__("pass"):
                    psk = '"' + event.data.get("pass") + '"'
                    wpa(self.iface, 'set_network', nid, 'psk', psk)
                else:
                    wpa(self.iface, 'set_network', nid, 'key_mgmt', 'NONE')

                wpa(self.iface, 'enable', nid)
                connected = self.get_connected(ssid)
                if connected:
                    wpa(self.iface, 'save_config')

            self.ws.emit(Message("onyx.wifi.connected",
                                 {'connected': connected}))
            LOG.info("Connection status for %s = %s" % (ssid, connected))

            if connected:
                self.ws.emit(Message("speak", {
                    'utterance': "Thank you, I'm now connected to the "
                                 "internet and ready for use"}))
                # TODO: emit something that triggers a pairing check

    def disconnect(self):
        status = self.get_status()
        nid = status.get("id")
        if nid:
            ssid = status.get("ssid")
            wpa(self.iface, 'disable', nid)
            LOG.info("Disconnecting %s id: %s" % (ssid, nid))

    def get_status(self):
        res = cli('wpa_cli', '-i', self.iface, 'status')
        out = str(res.get("stdout"))
        if out:
            return dict(o.split("=") for o in out.split("\n")[:-1])
        return {}

    def get_connected(self, ssid, retry=5):
        connected = self.is_connected(ssid)
        while not connected and retry > 0:
            sleep(2)
            retry -= 1
            connected = self.is_connected(ssid)
        return connected

    def is_connected(self, ssid, status=None):
        status = status or self.get_status()
        state = status.get("wpa_state")
        return status.get("ssid") == ssid and state == "COMPLETED"

    def stop(self, event=None):
        LOG.info("Stopping access point...")
        self._stop_connection_monitor()
        self.ap.down()
        if self.server:
            self.server.server.shutdown()
            self.server.server.server_close()
            self.server.join()
            self.server = None
        LOG.info("Access point stopped!")

    def _do_net_check(self):
        # give system 5 seconds to resolve network or get plugged in
        sleep(5)

        LOG.info("Checking internet connection again")
        if not connected() and self.conn_monitor is None:
            # TODO: Enclosure/localization
            self._speak_and_show(
                "This device is not connected to the Internet. Either plug "
                "in a network cable or hold the button on top for two "
                "seconds, then select wifi from the menu", None)

    def run(self):
        try:
            # When the system first boots up, check for a valid internet
            # connection.
            LOG.info("Checking internet connection")
            if not connected():
                LOG.info("No connection initially, waiting 20...")
                self.net_check = threading.Thread(
                    target=self._do_net_check,
                    args={})
                self.net_check.daemon = True
                self.net_check.start()
            else:
                LOG.info("Connection found!")

            self.ws.run_forever()
        except Exception as e:
            LOG.error("Error: {0}".format(e))
            self.stop()
Exemple #11
0
def ws():
    ws = WebsocketClient()
    return ws