machine-speak / data_builders /mock_sensor_data_generator.py
Kim Adams
setting up lookups
c3aea2a
import json
import random
from datetime import datetime, timedelta
# Initialize starting values
sensor_id = "TS110-112"
location = "server room"
start_time = datetime.strptime("2023-07-18T10:15:30Z", "%Y-%m-%dT%H:%M:%SZ")
temp = 75.2
humidity = 40.0
pressure = 1013
battery_life = 85
state = "normal"
# Initialize data list
data = []
halt=False
for i in range(120):
# Create data point
data_point = {
"sensorId": sensor_id,
"location": {"description": location},
"timeStamp": (start_time + timedelta(minutes=i)).strftime("%Y-%m-%dT%H:%M:%SZ"),
"readings": {
"temperature": round(temp, 1),
"humidity": round(humidity, 1),
"pressure": pressure,
"units": {"temperature": "Celsius", "humidity": "Percent", "pressure": "hPa"}
},
"state": {"device": state, "batteryLife": round(battery_life, 1)}
}
# Append to data list
data.append(data_point)
# Update temperature, humidity and battery life
if halt==False:
temp += 1.5 if i < 60 else 2.9 # increase temperature faster after 60 minutes
humidity += random.uniform(-0.2, 0.2) # fluctuate humidity
battery_life -= 0.235 # decrease battery life
# Update status
if temp < 140:
state = "normal"
elif temp >= 140 and temp < 240:
state = "warning"
elif temp >= 240 and temp < 300:
state = "failure"
elif temp >= 300:
state = "shutdown"
halt=True
elif halt==True:
battery_life = 0
pressure = 0
humidity = 0
temp -= 4.5
# Write data to json file
with open("ml_to_nl_translation/data/temperature_sensor_data.json", 'w') as f:
json.dump(data, f, indent=4)