Adding simple server and client script
This commit is contained in:
parent
58a5b12022
commit
4d9e708cdf
5 changed files with 92 additions and 1 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -196,3 +196,6 @@ cython_debug/
|
||||||
# .nfs files are created when an open file is removed but is still being accessed
|
# .nfs files are created when an open file is removed but is still being accessed
|
||||||
.nfs*
|
.nfs*
|
||||||
|
|
||||||
|
# Venv
|
||||||
|
python/
|
||||||
|
venv/
|
||||||
|
|
|
@ -1,3 +1,10 @@
|
||||||
# waffle_counter_backend
|
# waffle_counter_backend
|
||||||
|
|
||||||
Prometheus backend and data collecting tool for waffle buzzer
|
Prometheus backend and data collecting tool for waffle buzzer
|
||||||
|
|
||||||
|
## Orientierung
|
||||||
|
In diesem Git Repo ist der Server Teil.
|
||||||
|
Die Clients die gleichzeitig auch die LED ansteuern sind auf https://git.l3d.ch/c3woc/rpi_zero_neopixel_buzzer.git zu finden.
|
||||||
|
|
||||||
|
## Abhängigkeiten
|
||||||
|
requirements.txt
|
||||||
|
|
29
client_example.py
Normal file
29
client_example.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import requests
|
||||||
|
import json
|
||||||
|
|
||||||
|
# Define the API endpoint URL
|
||||||
|
api_url = 'http://localhost:8042/buzzed' # Update with your actual API URL
|
||||||
|
|
||||||
|
# Data payload to be sent as JSON
|
||||||
|
data_payload = {
|
||||||
|
'buzzer': 'buzzer1',
|
||||||
|
'waffle_ready': 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Convert the payload to JSON format
|
||||||
|
json_payload = json.dumps(data_payload)
|
||||||
|
|
||||||
|
# Set the headers to indicate JSON content
|
||||||
|
headers = {'Content-Type': 'application/json'}
|
||||||
|
|
||||||
|
# Send POST request to the API endpoint
|
||||||
|
response = requests.post(api_url, data=json_payload, headers=headers)
|
||||||
|
|
||||||
|
# Check the response status
|
||||||
|
if response.status_code == 200:
|
||||||
|
print('Data added successfully!')
|
||||||
|
else:
|
||||||
|
print(f'Error adding data: {response.text}')
|
||||||
|
|
3
requirements.txt
Normal file
3
requirements.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
flask
|
||||||
|
prometheus_client
|
||||||
|
requests
|
49
server.py
Normal file
49
server.py
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from flask import Flask, request, jsonify
|
||||||
|
from prometheus_client import Counter, generate_latest, multiprocess, REGISTRY, CollectorRegistry
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
# Create a new CollectorRegistry to avoid duplicate timeseries error
|
||||||
|
custom_registry = CollectorRegistry()
|
||||||
|
|
||||||
|
# Define Prometheus Counters for each input device
|
||||||
|
c3woc_b1_counter = Counter('c3woc_buzzer1_pressed_total', 'Total number the buzzer 1 from C3WOC is pressed', registry=custom_registry)
|
||||||
|
c3woc_b2_counter = Counter('c3woc_buzzer2_pressed_total', 'Total number the buzzer 2 from C3WOC is pressed', registry=custom_registry)
|
||||||
|
c3woc_b3_counter = Counter('c3woc_buzzer3_pressed_total', 'Total number the buzzer 3 from C3WOC is pressed', registry=custom_registry)
|
||||||
|
c3woc_b4_counter = Counter('c3woc_buzzer4_pressed_total', 'Total number the buzzer 4 from C3WOC is pressed', registry=custom_registry)
|
||||||
|
# Define more counters as needed for other devices
|
||||||
|
# TODO: dynamic counter creation
|
||||||
|
|
||||||
|
@app.route('/metrics')
|
||||||
|
def metrics():
|
||||||
|
# Generate and return Prometheus metrics format using custom registry
|
||||||
|
return generate_latest(registry=custom_registry)
|
||||||
|
|
||||||
|
@app.route('/buzzed', methods=['POST'])
|
||||||
|
def add_data():
|
||||||
|
data = request.get_json()
|
||||||
|
device = data.get('buzzer')
|
||||||
|
waffles_counted = data.get('waffle_ready')
|
||||||
|
|
||||||
|
match device:
|
||||||
|
case 'buzzer1':
|
||||||
|
c3woc_b1_counter.inc(waffles_counted)
|
||||||
|
return jsonify({'message': f'Data added for {device}'}), 200
|
||||||
|
case 'buzzer2':
|
||||||
|
c3woc_b2_counter.inc(waffles_counted)
|
||||||
|
return jsonify({'message': f'Data added for {device}'}), 200
|
||||||
|
case 'buzzer3':
|
||||||
|
c3woc_b3_counter.inc(waffles_counted)
|
||||||
|
return jsonify({'message': f'Data added for {device}'}), 200
|
||||||
|
case 'buzzer4':
|
||||||
|
c3woc_b4_counter.inc(waffles_counted)
|
||||||
|
return jsonify({'message': f'Data added for {device}'}), 200
|
||||||
|
case _:
|
||||||
|
return jsonify({'error': 'Invalid device or buzzer specified'}), 400
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run(host='0.0.0.0', port=8042)
|
||||||
|
|
Loading…
Reference in a new issue