def get_device_emergency_contacts(): if not is_user_in_session(): return "Unauthorized", 401 with sqlite3.connect('deviceservice.db') as context: service = DashboardService(EmergencyRepositoryTest(context)) emergency_contacts = service.get_ice_contacts_for_device( session["user"]["device"]["device_id"]) return jsonify(emergency_contacts)
def remove_device_emergency_contact(ec_id): if not is_user_in_session(): return "Unauthorized", 401 with sqlite3.connect('deviceservice.db') as context: service = DashboardService(EmergencyRepositoryTest(context)) if service.remove_ice_contact_for_device( session["user"]["device"]["device_id"], ec_id): return "Successfully removed emergency contact.", 200 return "The emergency contact does not exist for current device.", 404
def add_device_emergency_contact(): if not is_user_in_session(): return "Unauthorized", 401 json_message = request.json with sqlite3.connect('deviceservice.db') as context: service = DashboardService(EmergencyRepositoryTest(context)) ec_id = service.add_ice_contact_for_device( session["user"]["device"]["device_id"], json_message["phone_number"]) return str(ec_id), 200
def on_message_received(topic, payload, dup, qos, retain, **kwargs): print("Received message from topic '{}': {}".format(topic, payload)) message = json.loads(payload.decode('utf-8')) if message['event'] == 'crash': with pyodbc.connect(db_connection_string) as context: dashboard_service = DashboardService(EmergencyRepository(context)) ice_contacts = dashboard_service.get_ice_contacts_for_device(message['device_id']) for contact in ice_contacts: phone_number = contact['phone_number'] print(phone_number) device_service = LocationService(LocationRepository(context), CumulocityRepository(context)) gps_location = device_service.get_latest_location(message['device_id']) if gps_location: try: msg = message_template.format(gps_location['longitude'], gps_location['latitude']) smsAPI.send_msg(phone_number, msg) print("Sent message to: " + phone_number, '\n', msg) except Exception as e: print('Could not send message due to an exception: ') print(e) else: print('Could not find GPS location') elif message['event'] == 'on_off_switch': bit = None if message['state'] == "on": bit = True elif message['state'] == "off": bit = False else: return "Input was not valid", 400 with pyodbc.connect(db_connection_string) as context: service = LocationService(LocationRepository(context), CumulocityRepository(context)) service.set_state(message['device_id'], bit) print("State is now {}".format(message['state'])) else: print('Invalid event type')