def register(): msg = '' if request.method == 'POST' and 'deviceName' in request.form and 'password' in request.form \ and 'vehicle' in request.form and 'deviceid' in request.form: deviceid = request.form['deviceid'] name = request.form['deviceName'] password = request.form['password'] vehicle = request.form['vehicle'] cumulocity_name = request.form['cumulocityName'] cumulocity_tenant_id = request.form['tenantId'] cumulocity_password = request.form['cumulocityPassword'] with sqlite3.connect('deviceservice.db') as context: account_service = AccountService(DeviceRepositoryTest(context), CumulocityRepository(context)) account = account_service.get_user(deviceid) if account: msg = 'Account already exists !' elif not re.match(r'[A-Za-z0-9]+', name): msg = 'Username must contain only characters and numbers !' elif not name or not password or not deviceid or not vehicle or not cumulocity_name or not cumulocity_tenant_id or not cumulocity_password: msg = 'Please fill out the form !' else: account_service.add_user( AddUserDto(deviceid, name, password, cumulocity_name, cumulocity_tenant_id, cumulocity_password)) msg = 'You have successfully registered !' elif request.method == 'POST': msg = 'Please fill out the form !' return render_template('register.html', msg=msg)
def login(): error = None if request.method == 'GET' and is_user_in_session(): # No login required return redirect(url_for('home')) elif request.method == 'POST': device_id = request.form['deviceID'] req_password = request.form['password'] with sqlite3.connect('deviceservice.db') as context: service = AccountService(DeviceRepositoryTest(context), CumulocityRepository(context)) correct_password = service.get_user_password(device_id) if req_password == correct_password: user = service.get_user(device_id) add_user_to_session(user) session_cookie = session_serializer.dumps(dict(session)) print(session_cookie) resp = make_response(redirect(url_for('home'))) # Client code can access cookies resp.set_cookie('client_session', session_cookie) return resp else: error = 'Invalid Credentials. Please try again.' return render_template('login.html', error=error)
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')
def get_current_location(): if not is_user_in_session(): return "Unauthorized", 401 with sqlite3.connect('deviceservice.db') as context: service = LocationService(LocationRepository(context), CumulocityRepository(context)) location = service.get_realtime_location( session["user"]["device"]["device_id"]) print(location) return location
def get_last_known_location(): if not is_user_in_session(): return "Unauthorized", 401 with sqlite3.connect('deviceservice.db') as context: service = LocationService(LocationRepository(context), CumulocityRepository(context)) last_known_location = service.get_latest_location( session["user"]["device"]["device_id"]) if not last_known_location: return "No location logs exists for this device.", 404 return last_known_location
def activate_cumulocity(state): if not is_user_in_session(): return "Unauthorized", 401 bit = None if state == "on": bit = True elif state == "off": bit = False else: return "Input was not valid", 400 with sqlite3.connect('deviceservice.db') as context: service = LocationService(LocationRepository(context), CumulocityRepository(context)) service.set_state(session["user"]["device"]["device_id"], bit) return "State is now {}".format(state)
def realtime_device_location_polling(): """Polls locations of active devices forever""" i = 0 while True: time.sleep(3) with sqlite3.connect('deviceservice.db') as context: service = LocationService(LocationRepository(context), CumulocityRepository(context)) active_devices = service.get_active_cumulocity_devices() for ad in active_devices: location, status_code = service.get_realtime_location(ad[0]) if not location: print( "[{}]: Something went wrong (status code: {})".format( i, status_code)) continue service.add_location(ad[0], location['lat'], location['lng'], location['alt']) print("[{}]: {}".format(i, location)) i += 1