From 573351760d2d61188527245268be117a98144f34 Mon Sep 17 00:00:00 2001 From: Innovation Date: Wed, 3 Apr 2024 05:41:19 +0100 Subject: [PATCH] nightserver.py more work on cyberware --- src/nightserver.py | 99 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 75 insertions(+), 24 deletions(-) diff --git a/src/nightserver.py b/src/nightserver.py index 67c8899..8835c2b 100644 --- a/src/nightserver.py +++ b/src/nightserver.py @@ -24,7 +24,7 @@ def setVitalsHeartrate(): vitalsHeartrate = json['heartrate'] except: return 'Incorrect usage.\nUsage: { heartrate: INT, uuid: STRING }\n', 400 - return 'Information set successfully', 204 + return '', 204 @app.route('/api/vitals/oxygen') def getVitalsOxygen(): @@ -42,7 +42,7 @@ def setVitalsOxygen(): vitalsOxygen = json['oxygen'] except: return 'Incorrect usage.\nUsage: { oxygen: INT }\n', 400 - return 'Information set successfully', 204 + return '', 204 @app.route('/api/vitals/bodytemp') def getVitalsBodytemp(): @@ -60,7 +60,7 @@ def setVitalsBodytemp(): vitalsBodytemp = json['bodytemp'] except: return 'Incorrect usage.\nUsage: { bodytemp: FLOAT }\n', 400 - return 'Information set successfully', 204 + return '', 204 @app.route('/api/vitals') def getVitals(): @@ -78,7 +78,7 @@ def setVitals(): if not authenticate(json['uuid'], '/api/vitals'): return 'Forbidden.', 403 # This is a bit ugly but its just how I'm checking that everything is there without setting variables if the json is incorrect - tempH = json['heartrate'] + tempH = json['heartrate'] tempO = json['oxygen'] tempB = json['bodytemp'] vitalsHeartrate = tempH @@ -86,7 +86,7 @@ def setVitals(): vitalsBodytemp = tempB except: return 'Incorrect usage.\nUsage: { heartrate: INT, oxygen: INT, bodytemp: FLOAT }\n', 400 - return 'Information set successfully', 204 + return '', 204 # Fitness fitnessSteps = None @@ -106,7 +106,7 @@ def setFitnessSteps(): vitalsBodytemp = json['steps'] except: return 'Incorrect usage.\nUsage: { steps: INT }\n', 400 - return 'Information set successfully', 204 + return '', 204 @app.route('/api/fitness') @@ -125,12 +125,12 @@ def setFitness(): fitnessSteps = json['steps'] except: return 'Incorrect usage.\nUsage: { steps: INT }\n', 400 - return 'Information set successfully', 204 + return '', 204 # Cyberware management cyberware = [] -newCyberwareTemplate = { "uuid": -1, "name": "", "hotpluggable": False, "lastMalfunction": "", "frontendAwknowledge": False, "canSet": [] } +newCyberwareTemplate = { "uuid": None, "name": None, "hotpluggable": False, "lastMalfunction": None, "frontendAwknowledge": False, "canSet": [], "battery": None } # This makes the system aware of a new piece of hardware. @@ -161,7 +161,7 @@ def addCyberware(): cyberware.append(tempNewCyberware) except: return 'Incorrect usage.\nUsage: { name: STRING, hotpluggable: BOOL, canSet: ARRAY }\n', 400 - return jsonify([ { "uuid": tempNewCyberware['uuid'] } ]), 204 + return jsonify([ { "uuid": tempNewCyberware['uuid'] } ]), 200 # Arguments: { uuid: INT } @app.route('/api/cyberware/remove', methods=['POST']) @@ -175,7 +175,7 @@ def removeCyberware(): for c in cyberware: if c['uuid'] == desiredId: del cyberware[i] - return 'Cyberware removed', 204 + return '', 204 i = i + 1 except: return 'Incorrect usage.\nUsage: { uuid: STRING }\n', 400 @@ -194,30 +194,81 @@ def getCyberware(): #if not authenticate(json['uuid'], '/api/cyberware'): # return 'Forbidden.', 403 - r#eturn jsonify(cyberware) + #return jsonify(cyberware) -@app.route('/api/cyberware/get') +@app.route('/api/cyberware/get', methods=['POST']) def getCyberwareSpecific(): json = request.get_json() try: desiredId = json['uuid'] - i = 0 - for c in cyberware: - if c['uuid'] == desiredId: - return jsonify(c), 204 - break; + + requestedCyberware = getCyberwareHelper(desiredId) + + if (requestedCyberware != None): + return jsonify(requestedCyberware), 200 except: return 'Incorrect usage.\nUsage: { uuid: STRING }\n', 400 return 'UUID Invalid\n', 400 # Arguments { uuid: INT } -@app.route('/api/cyberware/reset', methods=['POST']) -def resetCyberwareMalfunction(): - #if not authenticate(json['uuid'], '/api/cyberware/reset'): - # return 'Forbidden.', 403 +@app.route('/api/cyberware/battery') +def getCyberwareBattery(): + json = request.get_json() - return 'Not Implemented', 501 + try: + desiredId = json['uuid'] + + requestedCyberware = getCyberwareHelper(desiredId) + + if (requestedCyberware != None): + return jsonify([ { "battery": requestedCyberware['battery'] } ]), 200 + except: + return 'Incorrect usage.\nUsage: { uuid: STRING }\n', 400 + return 'UUID Invalid\n', 400 + +# Arguments { uuid: STRING, battery: INT } +@app.route('/api/cyberware/battery', methods=['POST']) +def setCyberwareBattery(): + json = request.get_json() + + try: + desiredId = json['uuid'] + battery = json['battery'] + + requestedCyberware = getCyberwareHelper(desiredId) + + if (requestedCyberware != None): + requestedCyberware['battery'] = battery + return '', 204 + except: + return 'Incorrect usage.\nUsage: { battery: INT, uuid: STRING }\n', 400 + return 'UUID Invalid\n', 400 + +# Arguments { uuid: STRING } +@app.route('/api/cyberware/reset_malfunction', methods=['POST']) +def resetCyberwareMalfunction(): + json = request.get_json() + + try: + desiredId = json['uuid'] + + requestedCyberware = getCyberwareHelper(desiredId) + + if (requestedCyberware != None): + requestedCyberware['lastMalfunction'] = None + return '', 204 + except: + return 'Incorrect usage.\nUsage: { uuid: STRING }\n', 400 + return 'UUID Invalid\n', 400 + +def getCyberwareHelper(uuid): + i = 0 + for c in cyberware: + if c['uuid'] == desiredId: + return c + break + return None # Environment data environmentTemperature = None @@ -255,7 +306,7 @@ def getEnvironmentHumidity(): @app.route('/api/environment/humidity', methods=['POST']) def setEnvironmentHumidity(): - global environmentHumidity + global environmentHumidity json = request.get_json() try: @@ -280,7 +331,7 @@ def authenticate(uuid, endpoint): if e == endpoint: return True return False - + @app.route('/') def uiindex():