-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather_simulation.py
29 lines (22 loc) · 860 Bytes
/
weather_simulation.py
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
import threading
from random import randint
import os
import json
from config import SERVICE_URL
class Weather:
def __init__(self):
self.temp = 15
self.pressure = 1000
self.humidity = 30
self.wind = dict(speed=10, angle=210)
def simulate_weather(self):
self.temp = min(max(self.temp + randint(-1, 1), -30), 30)
self.humidity = min(max(self.humidity + randint(-1, 1), 70), 90)
self.wind['angle'] = (self.wind['angle'] + randint(-1, 1)) % 360
self.wind['speed'] = min(max(self.wind['speed'] + randint(-1, 1), 0), 20)
w = Weather()
def send_data():
w.simulate_weather()
os.system('curl -X POST -H "Content-Type: application/json" -d \'{0}\' http://{1}/api/weather_sensor_data'.format(json.dumps(w.__dict__), SERVICE_URL))
threading.Timer(5, send_data).start()
send_data()