Spaces:
Runtime error
Runtime error
File size: 1,788 Bytes
168653b 60db24d 168653b 3867e60 168653b 3867e60 168653b 60db24d 168653b 3867e60 168653b 3867e60 60db24d 3867e60 60db24d 3867e60 60db24d 3867e60 168653b 3867e60 168653b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | 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) |