Skip to content

Commit

Permalink
Add meter reset
Browse files Browse the repository at this point in the history
  • Loading branch information
laurynas committed Mar 16, 2024
1 parent 1bfbf5e commit ef1381c
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,36 @@ def update_meter(meter_id):

return json_data, 200, {'Content-Type': 'application/json'}

@app.route('/meter/<identifier:meter_id>', methods=['GET'])
def show_meter(meter_id):
json_file = os.path.join(data_dir, f'{meter_id}.json')
if not os.path.exists(json_file):
return 'Meter not found', 404
with open(json_file, 'r') as f:
return f.read(), 200, {'Content-Type': 'application/json'}

@app.route('/meter/<identifier:meter_id>/image', methods=['GET'])
def meter_image(meter_id):
image_path = os.path.join(data_dir, f'{meter_id}.jpg')
if not os.path.exists(image_path):
return 'Meter not found', 404
return open(image_path, 'rb').read(), 200, {'Content-Type': 'image/jpeg'}

@app.route('/meter/<identifier:meter_id>/reset', methods=['GET'])
def reset_meter(meter_id):
value = request.args.get('value', type=float)
json_file = os.path.join(data_dir, f'{meter_id}.json')
image_file = os.path.join(data_dir, f'{meter_id}.jpg')

if os.path.exists(json_file):
os.remove(json_file)

if os.path.exists(image_file):
os.remove(image_file)

if value is not None:
json_data = json.dumps({'value': value})
with open(json_file, 'w') as f:
f.write(json_data)

return 'Meter reset', 200

0 comments on commit ef1381c

Please sign in to comment.