Commit fb68d3ee authored by Sebastian Böhm's avatar Sebastian Böhm
Browse files

add env-example

parent cac5c6e9
......@@ -44,14 +44,18 @@ Other available services follow the same decoupled architecture.
## TemperatureService (`core.temperature_svc.py`)
This service is continuously transmitting temperature values to an mqtt endpoint.
This service is continuously transmitting temperature values to an mqtt endpoint.
### HTTP-Endpoints:
### HTTP-Endpoints
* (UI) `/` \[GET\]: Current temperature value
* (API) `/temperature` \[GET\]: Current temperature value as json
### MQTT-Topics:
All of the endpoints are protected with login.
You can specify the initial user by defining `FLASK_WEB_USER` and `FLASK_WEB_PASSWORD` in the `env` file.
Just have a look at section *Run the project locally*.
### MQTT-Topics
**TemperatureMessage:** `sensors/<MQTT_CLIENT_ID>/data`
......@@ -72,7 +76,7 @@ For example, all logical cores can be utilized to run at 50% CPU utilization for
### HTTP_Endpoints
* (UI) `/` `/cpu-load`: Allow to create new cpu load jobs and to manage already existing ones
* (UI) `/cpu-load` \[GET\]: Allow to create new cpu load jobs and to manage already existing ones
### MQTT_TOPICS
......@@ -104,21 +108,45 @@ On Error:
}
```
**Request Reading current QueuedCPULoadJobAllCores:** `services/cpuLoadSvc/<MQTT_CLIENT_ID>/jobs/read/req`
**Request Reading all QueuedCPULoadJobAllCores:** `services/cpuLoadSvc/<MQTT_CLIENT_ID>/jobs/read/req`
Payload intentionally left blank.
**Response Reading current QueuedCPULoadJobAllCores:** `services/cpuLoadSvc/<MQTT_CLIENT_ID>/jobs/read/res`
**Response Reading all QueuedCPULoadJobAllCores:** `services/cpuLoadSvc/<MQTT_CLIENT_ID>/jobs/read/res`
```json
{
"duration": 60,
"target_load": 0.55,
"id": 1,
"pid": 596,
"state": "RUNNING"
"success":{
"1":{
"duration":60,
"target_load":0.25,
"id":1,
"pid":30040,
"state":"RUNNING"
},
"2":{
"duration":60,
"target_load":0.55,
"id":2,
"pid":null,
"state":"CREATED"
},
"3":{
"duration":60,
"target_load":0.55,
"id":3,
"pid":null,
"state":"CREATED"
}
}
}
```
PLEASE NOTE:
* The **CPULoadService** is running a SQLite database to store the username and password for the web login.
* The submitted CPULoadJobs are only stored in-memory. That means, a shutdown of the application will lead to data loss. If you are restarting your application, you need recreate your job, respectively your job profile.
### General Information
All components can easily be configured with the following `.env` file with should be present in the root of this project.
......@@ -172,6 +200,8 @@ FLASK_SQLITE_DB_PATH=sqlite:///../../flask-db.db
```
Furthermore, you can find additional information MQTT topic configuration in `settings.py`.
## Prerequisites
* Make sure that you installed `python3, python3-venv, python3-pip` on your system
......
#
# Example env file - DO NOT USE IN PRODUCTION!!!
#
# MAKE SURE YOU ARE OVERRIDING AT LEAST
#
# - MQTT_CLIENT_ID
# - FLASK_WEB_USER
# - FLASK_WEB_PASSWORD
# - FLASK_SECRET_KEY
# - FLASK_SECURITY_PASSWORD_SALT
# messaging
MQTT_SERVER_NAME=localhost
MQTT_PORT=1883
MQTT_TLS=False
MQTT_CA_CERTS=
MQTT_CERTFILE=
MQTT_KEYFILE=
MQTT_TLS_INSECURE=False
MQTT_USERNAME=
MQTT_PASSWORD=
MQTT_MAX_CONNECT_RETRIES=5
MQTT_RECONNECT_TIMEOUT=30
MQTT_CLIENT_ID=ab444537-cf67-47aa-a5ef-3548292e225b
MQTT_PUBLISH_QOS=2
MQTT_SUBSCRIBE_QOS=2
MQTT_TOPIC_PUBLISHER=publisher
MQTT_TOPIC_PUBLISHER_STATE=state
# core
MQTT_TOPIC_REQ_TYPE_CREATE=create/req
MQTT_TOPIC_RES_TYPE_CREATE=create/res
MQTT_TOPIC_REQ_TYPE_READ=read/req
MQTT_TOPIC_RES_TYPE_READ=read/res
MQTT_TOPIC_REQ_TYPE_DELETE=delete/req
MQTT_TOPIC_RES_TYPE_DELETE=delete/res
## temperature
MQTT_TOPIC_SENSORS=sensors
MQTT_TOPIC_SENSORS_DATA=data
## cpu
MQTT_TOPIC_SERVICES=services
MQTT_TOPIC_CPU_LOAD_SERVICE=cpuLoadSvc
MQTT_TOPIC_CPU_LOAD_SERVICE_DATA=jobs
CPU_LOAD_SVC_MIN_DURATION_SECONDS=30
CPU_LOAD_SVC_MAX_DURATION_SECONDS=3600
CPU_LOAD_SVC_MIN_TARGET_LOAD=0.25
CPU_LOAD_SVC_MAX_TARGET_LOAD=0.75
# web
WEB_HOSTNAME=localhost
FLASK_WEB_USER=test@test.com
FLASK_WEB_PASSWORD=asdherz
FLASK_SECRET_KEY=pf9Wkove4IKEAXvy-cQkeDPhv9Cb3Ag-wyJILbq_dFw
FLASK_SECURITY_PASSWORD_SALT=146585145368132386173505678016728509634
FLASK_SQLITE_DB_PATH=sqlite:///../../flask-db.db
......@@ -140,8 +140,6 @@ class CPULoadService(threading.Thread):
def get_cpu_load_job_history(self):
with self.lock:
print(self.CPULoadJobHistory)
print(type(self.CPULoadJobHistory))
return copy.deepcopy(self.CPULoadJobHistory)
def get_cpu_load_job_all_cores_by_id(self, id):
......
......@@ -137,6 +137,7 @@ class MessageBroker(threading.Thread):
try:
if (mqtt_message.topic == topic_cpuLoadSvc_cpuLoadJob_create_req):
response_topic = topic_cpuLoadSvc_cpuLoadJob_create_res
json_cpu_load_job_all_cores=json.loads(mqtt_message.payload.decode())
if (json_cpu_load_job_all_cores['duration'] is None):
raise ValueError('duration is missing')
......@@ -147,18 +148,25 @@ class MessageBroker(threading.Thread):
self.publisher_queue.put(MqttMessage(topic_cpuLoadSvc_cpuLoadJob_create_res, MqttSuccessMessage(created_cpu_load_job).to_json()))
elif (mqtt_message.topic == topic_cpuLoadSvc_cpuLoadJob_read_req):
self.publisher_queue.put(MqttMessage(topic_cpuLoadSvc_cpuLoadJob_read_res, self.cpu_load_svc.get_current_cpu_load_job_all_cores().to_json()))
response_topic = topic_cpuLoadSvc_cpuLoadJob_read_res
self.publisher_queue.put(MqttMessage(topic_cpuLoadSvc_cpuLoadJob_read_res, MqttSuccessMessage(self.cpu_load_svc.get_cpu_load_job_history()).to_json()))
elif (mqtt_message.topic == topic_cpuLoadSvc_cpuLoadJob_delete_req):
response_topic = topic_cpuLoadSvc_cpuLoadJob_delete_res
if (len(mqtt_message.payload) == 0):
raise ValueError('payload is missing')
json_cpu_load_job_delete_req = json.loads(mqtt_message.payload.decode())
if (json_cpu_load_job_delete_req['id'] is None ):
raise ValueError('id is missing')
self.publisher_queue.put(MqttMessage(topic_cpuLoadSvc_cpuLoadJob_delete_res, MqttSuccessMessage(self.cpu_load_svc.delete_cpu_load_job_by_id(json_cpu_load_job_delete_req['id']).to_json())))
self.publisher_queue.put(MqttMessage(topic_cpuLoadSvc_cpuLoadJob_delete_res, MqttSuccessMessage(self.cpu_load_svc.delete_cpu_load_job_by_id(json_cpu_load_job_delete_req['id'])).to_json()))
except (ValueError, JSONDecodeError, KeyError) as e:
error_message = 'Illegal message received: ' + str(e)
self.logger.error(error_message)
self.publisher_queue.put(MqttMessage(topic_cpuLoadSvc_cpuLoadJob_read_res, MqttErrorMessage(error_message)))
self.publisher_queue.put(MqttMessage(response_topic, MqttErrorMessage(error_message).to_json()))
def stop(self):
self.logger.info('Message broker received shutdown signal...')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment