Changeset - 76bc951639c1
[Not reviewed]
default
0 1 0
Dennis Fink - 10 years ago 2015-07-07 19:20:24
dennis.fink@c3l.lu
Fixed sensors.py
1 file changed with 14 insertions and 13 deletions:
0 comments (0 inline, 0 general)
spaceapi/sensors.py
Show inline comments
 
@@ -3,7 +3,7 @@ import json
 
from pkg_resources import resource_filename
 

	
 
import jsonschema
 
from flask import Blueprint, jsonify
 
from flask import Blueprint, jsonify, request, abort, current_app
 

	
 
from .auth import httpauth
 
from .active import active_json, save_last_state
 
@@ -30,11 +30,11 @@ def set_value(data, key):
 
	try:
 
		subkey = get_identification_key(data)
 
	except ValueError:
 
		return 400
 
        return abort(400)
 

	
 
	try:
 
		index = fuzzy_list_find(active_json['sensors'][key],
 
								key, data[subkey])
 
                                subkey, data[subkey])
 
		active_json['sensors'][key][index].update(data)
 
	except ValueError:
 
		active_json['sensors'][key].append(data)
 
@@ -48,30 +48,29 @@ def set_radiation_value(data):
 
	radiation_keys = [k for k in RADIATON_SUBKEYS if k in data]
 

	
 
	if not radiation_keys:
 
		return 400
 
        return abort(400)
 

	
 
	for first_subkey in radiation_keys:
 

	
 
		try:
 
			second_subkey = get_identification_key(data[first_subkey])
 
		except ValueError:
 
			return 400
 
            return abort(400)
 

	
 
		try:
 
			index = fuzzy_list_find(
 
				active_json['sensors']['radiation'][first_subkey],
 
				second_subkey,
 
				data[first_subkey][second_subkey])
 
			active_json['sensors'][
 
				'radiation'
 
			][first_subkey][index].update(data)
 
            active_json['sensors']['radiation'][first_subkey][index].update(
 
                data)
 
		except ValueError:
 
			active_json['sensors']['radiaton'][first_subkey].append(data)
 

	
 

	
 
	save_last_state()
 
	return jsonify(active_json)
 

	
 

	
 
@sensors_views.route('/set/<key>', methods=['POST'])
 
@httpauth.login_required
 
def set_sensors(key):
 
@@ -79,18 +78,20 @@ def set_sensors(key):
 
    if key in ALLOWED_SENSORS_KEYS and key in active_json['sensors']:
 
        data = request.data
 
        try:
 
            data = json.loads(data)
 
            data = json.loads(data.decode('utf-8'))
 

	
 
            try:
 
                jsonschema.validate(data, ALLOWED_SENSORS_KEYS[key])
 
            except jsonschema.ValidationError:
 
                return 400
 
                current_app.logger.info('Validation Error')
 
                return abort(400)
 

	
 
            if key != 'radiation':
 
                return set_value(data, key)
 
            else:
 
                return set_radiation_value(data)
 

	
 
        except ValueError:
 
            return 400
 
            return abort(400)
 
    else:
 
        return 400
 
        return abort(400)
0 comments (0 inline, 0 general)