def get_switch_status(switch_id): # Get the switch switches = lsswitches.get_switches() switch = [switch for switch in switches if switch['id'] == switch_id] if 0 == len(switch): return jsonify({'error' : 'no switch for id ' + str(switch_id)}), 400 switch = switch[0] return jsonify({'status' : switch['status']}), 201
def update_switch(switch_id): # Get the switch switches = lsswitches.get_switches() switch = [switch for switch in switches if switch['id'] == switch_id] if 0 == len(switch): return jsonify({'error' : 'no switch for id ' + str(switch_id)}), 400 if not request.json: abort(400) if 'status' in request.json: if type(request.json['status']) is not bool: abort(400) else: set_light_status(request.json['status']) switch[0]['status'] = request.json.get('status', switch[0]['status']) return jsonify({'switch': switch[0]}), 201
def toggle_switch(switch_id): switches = lsswitches.get_switches() switch = [switch for switch in switches if switch['id'] == switch_id] if 1 != len(switch): return jsonify({'error' : 'switch with id ' + str(switch_id) + ' does not exist'}), 400 switch = switch[0] # Toggle the switch status switch['status'] = not switch['status'] # Update the light status set_light_status(switch['status']) if not lsswitches.update_switch(switch): return jsonify({'error' : 'switch with id ' + str(switch_id) + ' does not exist'}), 400 return jsonify({'switch' : switch})
def route(): switches = lsswitches.get_switches() return jsonify({'switches' : switches})