def lambda_handler(event, context): try: usr = user.deserialize(event["user"]) #usr = user.deserialize(event["user"]) password = event["password"] except: return respond("400", user.User(-1, "", "", "userAdmin").serialize()) if not all(x is not None for x in [usr, password]): return respond("400", user.User(-1, "", "", "userAdmin").serialize()) try: if usr.role == "citizen": _usr = user.User.create_new_user(usr.name, usr.email, password, usr.role, usr.address, usr.city, usr.postnr) _usr.token = get_auth_token(_usr) return respond("200", _usr.serialize()) else: _usr = user.User.create_new_user(usr.name, usr.email, password, usr.role) _usr.token = get_auth_token(_usr) return respond("200", _usr.serialize()) except Exception as ex: raise ex return respond("400", user.User(-1, "", "", "userAdmin").serialize())
def lambda_handler(event, context): try: alm = alarm_deserializer(event["alarm"]) alm.delete() return respond("200", alarm.Alarm(-1, alm.activatedby, None).serialize()) except: return respond("400", event["alarm"])
def lambda_handler(event, context): try: ctz_id = int(event["id"]) except: return respond("400", alarm.Alarm(-1, None, None).serialize()) if not ctz_id: return respond("400", alarm.Alarm(-1, None, None).serialize()) # Create alarm # To create an alarm, we need database access, so it has to happen on another VPC enabled lambda. # But if we are VPC enabled we are unable to interact with SNS services, so we have to split the task up. try: lambda_client = boto3.client( 'lambda', region_name=region_name, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key) arg = bytes(json.dumps({"id": ctz_id}), 'utf-8') response = lambda_client.invoke(FunctionName=arn_alarm_post_endpoint, InvocationType="RequestResponse", Payload=arg) data = response["Payload"].read().decode() except: return respond("400", alarm.Alarm(-1, None, None).serialize()) # Get the alarm try: dt = json.loads(json.loads(data)) usr = user.deserialize( str(dt["activatedby"] )) #user.Citizen(str(dt["activatedby"]).replace("'", '"')) alm = alarm.Alarm(dt["status"], usr, None) except: return respond("400", alarm.Alarm(-1, None, None).serialize()) for c in alm.activatedby.contacts: for d in c.devices: if d.devicetype == "appdevice": if not d.arn or not d.token: continue push_message(d.arn, alm.serialize()) # elif d.devicetype == "smsdevice": # send_sms(d.phone_number, c.activatedby.name + " has had an falling accident, and requests help.") for d in alm.activatedby.devices: if d.devicetype == "iftttdevice": urllib.request.urlopen( "https://maker.ifttt.com/trigger/fall_detected/with/key/" + d.token).read() return respond("200", alm.serialize())
def lambda_handler(event, context): try: usr = deserialize(event["user"]) except: return respond("400", event["user"]) if not usr: return respond("400", event["user"]) usr.update() return respond("200", usr.serialize())
def lambda_handler(event, context): try: dvc = deserialize(event["device"]) except: return respond("400", event["device"]) if not dvc: return respond("400", event["device"]) dvc.delete() return respond("200", device.Device(-1, "").serialize())
def lambda_handler(event, context): contacts = user.Contact.get_all() return_string = "[" if not contacts: return respond("400", user.User(-1, "", "", "userAdmin").serialize()) else: for ctc in contacts: return_string += ctc.serialize() + "," return_string = return_string[:-1] + "]" return respond("200", return_string)
def lambda_handler(event, context): try: id = event["id"] except: return respond("400", user.User(-1, "", "", "userAdmin").serialize()) if not id: return respond("400", user.User(-1, "", "", "userAdmin").serialize()) usr = user.Contact.get(id) if not usr or usr.id == -1: return respond("400", user.User(-1, "", "", "userAdmin").serialize()) else: return respond("200", usr.serialize())
def lambda_handler(event, context): try: dvc = device.deserialize(event["device"]) usr = user.User.get(event["id"]) except: return respond("400", device.Device(-1, "").serialize()) if not all(x is not None for x in [dvc, usr]): return respond("400", device.Device(-1, "").serialize()) id = dvc.post(usr) dvc.id = id return respond("200", dvc.serialize())
def lambda_handler(event, context): try: usr = usr = user.deserialize(str(event["user"])) except: return respond("400", event["user"]) if not user: return respond("400", event["user"]) try: usr.delete() return respond("200", user.User(-1, "", "", "citizenAdmin").serialize()) except: return respond("400", event["user"])
def lambda_handler(event, context): try: id = event["id"] except: return respond("400", alarm.Alarm(-1, None, None).serialize()) if not id: return respond("400", alarm.Alarm(-1, None, None).serialize()) alm = alarm.Alarm.get(id) if not alm or alm.activatedby == -1: return respond("400", alarm.Alarm(-1, None, None).serialize()) else: return respond("200", alm.serialize())
def lambda_handler(event, context): try: id = event["id"] except Exception as ex: raise ex return respond("400", user.User(-1, "", "", "").serialize()) if not id: return respond("401", user.User(-1, "", "", "").serialize()) ctz = user.Citizen.get(id) if not ctz or ctz.id == -1: return respond("402", user.User(-1, "", "", "").serialize()) else: return respond("200", ctz.serialize())
def lambda_handler(event, context): try: email = event['email'] password = event['password'] except: return respond("400", user.User(-1, "", "", "userAdmin").serialize()) if not all(x is not None for x in [email, password]): return respond("400", user.User(-1, "", "", "userAdmin").serialize()) usr = user.User.attempt_login(email, password) if not usr or usr.id == -1: return respond("400", user.User(-1, "", "", "userAdmin").serialize()) else: usr.token = get_auth_token(usr) return respond("200", usr.serialize())
def lambda_handler(event, context): try: dvc: device.AppDevice = device.deserialize(event["device"]) if not dvc.arn: # Create arn arn_endpoint = create_endpoint(dvc.token) dvc.arn = arn_endpoint return respond("200", dvc.serialize()) else: # Update arn update_endpoint(dvc.arn, dvc.token) return respond("200", dvc.serialize()) except: return respond("400", "")
def lambda_handler(event, context): try: dvc: device.AlexaDevice = deserialize(event["device"]) if dvc.devicetype == "alexadevice": dvc: device.Device = device.Device.get_from_object(dvc.user_id) usr = dvc.get_owner() return respond("200", usr.serialize()) # TODO: Add more devices else: return respond("400", user.User(-1, "", "", "userAdmin").serialize()) except: return respond("401", user.User(-1, "", "", "userAdmin").serialize())
def lambda_handler(event, context): try: dvc = deserialize(event["device"]) if not dvc: return respond("400", event["device"]) if dvc.devicetype == "appdevice": dvc: device.AppDevice = device.deserialize(event["device"]) if not dvc.arn: # Create arn arn_endpoint = create_endpoint(dvc.token) dvc.arn = arn_endpoint else: # Update arn update_endpoint(dvc.arn, dvc.token) lambda_client = boto3.client( 'lambda', region_name=region_name, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key) arg = bytes(json.dumps({"device": dvc.serialize()}), 'utf-8') response = lambda_client.invoke( FunctionName=arn_device_put_endpoint, InvocationType="RequestResponse", Payload=arg) data = response["Payload"].read().decode() res = json.loads(data) if res == "ok": return respond("200", dvc.serialize()) else: return respond("400", event["device"]) except: return respond("400", event["device"])
def lambda_handler(event, context): try: print("STEP 0: STARTING") alm = alarm_deserializer(event["alarm"]) pprint(event) print("STEP 1: PREPARING CLIENT") lambda_client = boto3.client('lambda', region_name=region_name, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key) print("STEP 2: PACKING ARGUMENT") arg = bytes(json.dumps({"alarm": alm.serialize()}), 'utf-8') print("STEP 3: INVOKING") response = lambda_client.invoke( FunctionName=arn_alarm_put_endpoint, InvocationType="RequestResponse", Payload=arg) print("STEP 4: GETTING RESPONSE") data = response["Payload"].read().decode() print("STEP 5: DATA") pprint(data) if not data: print("STEP 6a: NOT DATA") return respond("400", event["alarm"]) print("STEP 6b: DESERIALIZING DATA") alm = alarm_deserializer(json.loads(data)) print("STEP 7: REPLY IF RESPONDER") if alm.responder: for d in alm.activatedby.devices: print("STEP 8: NOTIFYING") pprint(d) if d.devicetype == "appdevice": if not d.arn or not d.token: continue print("STEP 9: PUSHING") try: print(type(d.arn)) print(d.arn) print(alm.responder.serialize()) print(type(alm.responder.serialize())) push_message(d.arn, alm.responder.serialize()) except: print( "Failed to push notification: CitizenID: " + str(alm.activatedby.id) + " Device ID: " + str( d.id)) print("STEP 9: RETURNING") return respond("200", alm.serialize()) except Exception as ex: print("STEP -1: SOMETHING WENT WRONG") raise ex pprint(ex) return respond("400", event["alarm"])