Beispiel #1
0
def events():
    global client
    global active_scouts
    req = request.get_json()
    # Create scouts instance
    nightscouts = scouts()
    print(req)
    if "status" in req:
        if req["status"] == "completed":
            phone = req["to"]
            # The next line is not recomended its functional but use the global active_scouts variable
            # This variable is updated by the daemon process.. that run in another context
            # Not the flask context, for that reason we are going to use firebase to get
            # fresh data
            # uscout = [active_scout for active_scout in active_scouts if active_scout['phone'] == phone]
            uscout = nightscouts.getby_personal_phone(phone)
            if uscout != None:
                entries = requests.get(uscout["nightscout_api"]).json()
                glucose = entries[0]['sgv']
                sms_glucose_alert(
                    uscout["emerg_contact"], uscout["username"], glucose)
                # print('sms simulation to: {0} {1} {2}'.format(uscout["emerg_contact"], uscout["username"], glucose))
                for phone in uscout["extra_contacts"]:
                    # print('sms simulation to: {0} {1} {2}'.format(phone, uscout["username"], glucose))
                    sms_glucose_alert(phone, uscout["username"], glucose)
    return "Event Received"
Beispiel #2
0
def home():
    # Create scouts instance
    nightscouts = scouts()
    if get_session("user") != None:
        if request.method == "POST":
            extra_contacts = request.form.getlist('extra_contacts[]')
            if request.form.get("cmd") == "new":
                nightscouts.add(
                    scout(email=get_session("user")["email"],
                          username=get_session("user")["username"],
                          nightscout_api=request.form.get('nightscout_api'),
                          phone=request.form.get('phone'),
                          emerg_contact=request.form.get('emerg_contact'),
                          extra_contacts=extra_contacts))
            else:
                nightscouts.update(
                    {
                        u'nightscout_api': request.form.get('nightscout_api'),
                        u'phone': request.form.get('phone'),
                        u'emerg_contact': request.form.get('emerg_contact'),
                        u'extra_contacts': extra_contacts
                    }, request.form.get('id'))
        return render_template("home.html",
                               client_id=os.getenv("GOOGLE_CLIENT_ID"),
                               user=get_session("user"),
                               scout=nightscouts.get_by_email(
                                   get_session("user")["email"]))
    else:
        return render_template("login.html",
                               client_id=os.getenv("GOOGLE_CLIENT_ID"),
                               site_url=os.getenv("SITE_URL"))
Beispiel #3
0
    raise ApplicationKilledException("Killing signal detected")


start_scouts = False


def refresh_scouts(id):
    global active_scouts
    global start_scouts
    # Import Scout models for thread
    import models
    from models import model, scouts, scout


try:
    nightscouts = scouts()
    active_scouts = nightscouts.get_all()
    print("Refresh Scouts Job " + id + "")
    if start_scouts == False:
        start_scouts = True

except:
    print("Error when refresh scouts")


# This job is executed certain periods of time to get the last nightscout entry
# If the glucose is not between the range then make a call to the personal phone notifiying the level
# If the call is not answered then events url is going to serve a NCCO sending SMS to the emergency number
# And if applicable any extra contacts