5okin
4 years ago
4 changed files with 188 additions and 0 deletions
@ -0,0 +1,113 @@ |
|||
import random |
|||
import json |
|||
import time |
|||
from datetime import datetime |
|||
from requests import put |
|||
from selection_menus import select_operating_mode, server_ip_address |
|||
|
|||
|
|||
def percentage(new, old): |
|||
'''Check if numbers are within 10% of eachother (old value ±10%)''' |
|||
return bool(not (old - old * 10 / 100.0) <= new <= (old + old * 10 / 100.0)) |
|||
#return False |
|||
|
|||
|
|||
def weather_station(): |
|||
'''Create random weather data''' |
|||
|
|||
temp_min = 30 #Temperature range in Celsius |
|||
temp_max = 45 |
|||
|
|||
airhum_min = 40 #Air humidity range in percentage |
|||
airhum_max = 90 |
|||
|
|||
grdhum_min = 60 #Ground humidity range in percentage |
|||
grdhum_max = 70 |
|||
|
|||
wind_min = 0 #Wind speed in Km/h |
|||
wind_max = 10 |
|||
|
|||
measurment = { |
|||
"temperature": random.randrange(temp_min * 10, temp_max * 10)/10, |
|||
"air-humidity": random.randrange(airhum_min, airhum_max), |
|||
"ground-humidity": random.randrange(grdhum_min, grdhum_max), |
|||
"windspeed": random.randrange(wind_min, wind_max), |
|||
"time": datetime.now().strftime("%H:%M:%S"), #Log current time |
|||
} |
|||
return measurment |
|||
|
|||
|
|||
def more_than_10_percent(last_send_data, current_measurment): |
|||
'''Check if data current measurment is 10% different than last send data''' |
|||
|
|||
if not last_send_data: |
|||
return True |
|||
else: |
|||
for data in current_measurment: |
|||
if data != 'time' and data != 'sensor-id': |
|||
if percentage(current_measurment[data], last_send_data[data]): |
|||
print(data) |
|||
print("compaired " + str(current_measurment) + " with " + str(last_send_data)) |
|||
return True |
|||
|
|||
|
|||
def five_minute_passed(time_last_send): |
|||
'''Checks if 5min have passed''' |
|||
|
|||
time_last_send = time_last_send['time'] |
|||
time_now = datetime.now().strftime("%H:%M:%S") |
|||
tdelta = datetime.strptime(time_now, '%H:%M:%S') - datetime.strptime(time_last_send, '%H:%M:%S') |
|||
|
|||
if tdelta.seconds >= 300: |
|||
print("SEND CAUSE TIME PASSED") |
|||
return True |
|||
|
|||
|
|||
def fake_data_from_json(i): |
|||
'''Imports measurments from .json file named fake_weather for testing''' |
|||
with open('./fake_weather.json') as json_file: |
|||
data = json.load(json_file) |
|||
|
|||
data[i]['time'] = datetime.now().strftime("%H:%M:%S") |
|||
return data[i], len(data) |
|||
|
|||
|
|||
def main(url, mode): |
|||
'''Main function runs selected mode''' |
|||
|
|||
last_send = {} |
|||
curr_measurement = {} |
|||
|
|||
i = 0 |
|||
while True: |
|||
if mode is "json": |
|||
if i < fake_data_from_json(i)[1]-1: |
|||
curr_measurement = fake_data_from_json(i)[0] |
|||
else: |
|||
print("\nTesting over\n") |
|||
break |
|||
i += 1 |
|||
else: |
|||
curr_measurement = weather_station() |
|||
print("Current sensor value is: " + str(curr_measurement)) |
|||
|
|||
if more_than_10_percent(last_send, curr_measurement) or five_minute_passed(last_send): |
|||
last_send = curr_measurement.copy() |
|||
print("Send: " + str(last_send) + "\n") |
|||
try: |
|||
put(url=url, json=json.dumps(last_send)) |
|||
except: |
|||
print("ERROR: Lost connection with server\nmake sure server is still running.") |
|||
break |
|||
time.sleep(30) |
|||
|
|||
|
|||
if __name__ == "__main__": |
|||
URL = server_ip_address() |
|||
|
|||
if select_operating_mode(): |
|||
print("IMPORT FROM JSON") |
|||
main(URL, "json") |
|||
else: |
|||
print("GENERATE RANDOM DATA") |
|||
main(URL, "random") |
@ -0,0 +1,35 @@ |
|||
[ |
|||
{"temperature": 17.4, "air-humidity": 72, "ground-humidity": 70, "windspeed": 8, "time": ""}, |
|||
{"temperature": 19.8, "air-humidity": 70, "ground-humidity": 69, "windspeed": 7, "time": ""}, |
|||
{"temperature": 19.4, "air-humidity": 71, "ground-humidity": 65, "windspeed": 7, "time": ""}, |
|||
{"temperature": 18.0, "air-humidity": 64, "ground-humidity": 63, "windspeed": 7, "time": ""}, |
|||
{"temperature": 17.9, "air-humidity": 65, "ground-humidity": 62, "windspeed": 8, "time": ""}, |
|||
|
|||
{"temperature": 16.5, "air-humidity": 64, "ground-humidity": 60, "windspeed": 8, "time": ""}, |
|||
{"temperature": 16.7, "air-humidity": 62, "ground-humidity": 61, "windspeed": 8, "time": ""}, |
|||
{"temperature": 16.9, "air-humidity": 60, "ground-humidity": 58, "windspeed": 8, "time": ""}, |
|||
{"temperature": 17.5, "air-humidity": 61, "ground-humidity": 58, "windspeed": 8, "time": ""}, |
|||
{"temperature": 17.7, "air-humidity": 59, "ground-humidity": 57, "windspeed": 8, "time": ""}, |
|||
{"temperature": 17.9, "air-humidity": 59, "ground-humidity": 63, "windspeed": 8, "time": ""}, |
|||
{"temperature": 18.5, "air-humidity": 59, "ground-humidity": 64, "windspeed": 8, "time": ""}, |
|||
{"temperature": 18.6, "air-humidity": 60, "ground-humidity": 60, "windspeed": 8, "time": ""}, |
|||
{"temperature": 19.0, "air-humidity": 61, "ground-humidity": 58, "windspeed": 8, "time": ""}, |
|||
{"temperature": 19.1, "air-humidity": 65, "ground-humidity": 66, "windspeed": 8, "time": ""}, |
|||
|
|||
{"temperature": 20.0, "air-humidity": 65, "ground-humidity": 66, "windspeed": 0, "time": ""}, |
|||
{"temperature": 21.3, "air-humidity": 71, "ground-humidity": 68, "windspeed": 9, "time": ""}, |
|||
{"temperature": 32.9, "air-humidity": 79, "ground-humidity": 64, "windspeed": 6, "time": ""}, |
|||
{"temperature": 23.7, "air-humidity": 47, "ground-humidity": 60, "windspeed": 6, "time": ""}, |
|||
{"temperature": 24.9, "air-humidity": 82, "ground-humidity": 69, "windspeed": 1, "time": ""}, |
|||
{"temperature": 25.4, "air-humidity": 82, "ground-humidity": 67, "windspeed": 5, "time": ""}, |
|||
{"temperature": 26.7, "air-humidity": 32, "ground-humidity": 68, "windspeed": 4, "time": ""}, |
|||
|
|||
{"temperature": 34.9, "air-humidity": 84, "ground-humidity": 63, "windspeed": 7, "time": ""}, |
|||
{"temperature": 31.3, "air-humidity": 82, "ground-humidity": 68, "windspeed": 5, "time": ""}, |
|||
{"temperature": 42.1, "air-humidity": 54, "ground-humidity": 67, "windspeed": 1, "time": ""}, |
|||
{"temperature": 35.7, "air-humidity": 58, "ground-humidity": 64, "windspeed": 8, "time": ""}, |
|||
{"temperature": 42.6, "air-humidity": 42, "ground-humidity": 61, "windspeed": 7, "time": ""}, |
|||
{"temperature": 42.6, "air-humidity": 86, "ground-humidity": 69, "windspeed": 1, "time": ""}, |
|||
{"temperature": 38.9, "air-humidity": 57, "ground-humidity": 65, "windspeed": 7, "time": ""}, |
|||
{"temperature": 36.9, "air-humidity": 54, "ground-humidity": 60, "windspeed": 1, "time": ""} |
|||
] |
@ -0,0 +1 @@ |
|||
requests==2.25.1 |
@ -0,0 +1,39 @@ |
|||
import requests |
|||
|
|||
|
|||
def select_operating_mode(): |
|||
'''Mode selection and menu draw''' |
|||
|
|||
draw_menu = u""" |
|||
╭────────────────────────────────────╮ |
|||
│\u001b[4m\u001b[44m Please select one of the two modes \u001b[0m│ |
|||
│ \u001b[31m1.\u001b[0m Generate random data │ |
|||
│ \u001b[31m2.\u001b[0m Import data from json │ |
|||
╰────────────────────────────────────╯""" |
|||
draw_question = u"\nType the \u001b[31mnumber\u001b[0m of the mode you want (1/2): " |
|||
|
|||
print(draw_menu) |
|||
while True: |
|||
ans = input(draw_question) |
|||
if ans == "1": |
|||
return 0 |
|||
elif ans == "2": |
|||
return 1 |
|||
print("ERROR") |
|||
|
|||
|
|||
def server_ip_address(): |
|||
'''Server ip user input and menu draw''' |
|||
|
|||
print("Please enter the server ip") |
|||
while True: |
|||
url = "http://" + input("IP: ") + ":" + "5000" |
|||
|
|||
print("Using "+url) |
|||
timeout = 5 |
|||
try: |
|||
requests.get(url, timeout=timeout) |
|||
print("Connected to the url") |
|||
return url+"/api/send" |
|||
except: |
|||
print("No internet connection.") |
Loading…
Reference in new issue