summaryrefslogtreecommitdiff
path: root/boot/boot.py
blob: 658748a443c3145747e1e2466baca643573b0294 (plain)
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
#!/usr/bin/env python

import os, json
import urllib2
import subprocess
import yaml
from wifi import *

import vocabcompiler

def say(phrase, OPTIONS = " -vdefault+m3 -p 40 -s 160 --stdout > say.wav"):

    os.system("espeak " + json.dumps(phrase) + OPTIONS)
    os.system("aplay -D hw:1,0 say.wav")



# check if there is network connection
def configure():
    try:

        urllib2.urlopen("http://www.google.com").getcode()

        print "CONNECTED TO INTERNET"
        print "COMPILING DICTIONARY"
        vocabcompiler.compile()

        print "STARTING CLIENT PROGRAM"

        try:
            os.system("/home/pi/jasper/client/start.sh &")
        except:
            os.system("/home/pi/jasper/backup/start.sh &")
        finally:
            return

    except:

        networks = yaml.safe_load(open("networks.yml", "r"))

        wifi = Wifi()

        for network in networks:
            wifi.set_default_wifi(network['SSID'], network['KEY'])
            say("Attempting to connect to network " + network['SSID'])

            try:

                urllib2.urlopen("http://www.google.com").getcode()

                print "CONNECTED TO INTERNET"
                print "COMPILING DICTIONARY"
                vocabcompiler.compile()

                print "STARTING CLIENT PROGRAM"

                try:
                    os.system("/home/pi/jasper/client/start.sh &")
                except:
                    os.system("/home/pi/jasper/backup/start.sh &")
                finally:
                    return

            except:
                pass

        print "NOT CONNECTED TO INTERNET. RUNNING AD HOC NETWORK."

        wifi.setup_adhoc()

        os.system("sudo app/app.sh &")
        say("Hello.... I could not connect to a wifi network... Please log in with your computer to help me")

        original = open("networks.yml",'r').readlines()
        while True:
            list = open("networks.yml",'r').readlines()
            if list != original:
                break

        say("Thank you for adding a wifi network. Just give me a few minutes to restart.")
        os.system("sudo shutdown -r now")

if __name__ == "__main__":
    print "==========STARTING JASPER CLIENT=========="
    print "=========================================="
    print "COPYRIGHT 2013 SHUBHRO SAHA, CHARLIE MARSH"
    print "=========================================="
    say("Hello.... I am Jasper... Please wait one moment.")
    configure()