autos working!
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
from fastapi import APIRouter, Request
|
||||
from datetime import datetime
|
||||
from datetime import datetime, date
|
||||
from database import session
|
||||
from pyowm import OWM
|
||||
|
||||
from app.models.auto import Auto_Delivery, Auto_Temp
|
||||
from app.models.auto import Auto_Delivery, Auto_Temp, Auto_Update
|
||||
from app.models.delivery import Delivery
|
||||
|
||||
|
||||
@@ -15,99 +15,145 @@ router = APIRouter(
|
||||
responses={404: {"description": "Not found"}},
|
||||
)
|
||||
|
||||
|
||||
def Average(lst):
|
||||
return sum(lst) / len(lst)
|
||||
|
||||
|
||||
@router.get("/update", status_code=200)
|
||||
def update_auto_customers():
|
||||
|
||||
today_temp = session\
|
||||
.query(Auto_Temp)\
|
||||
.order_by(Auto_Temp.id.desc())\
|
||||
.first()
|
||||
|
||||
# get all automatic customers
|
||||
auto_customers = session\
|
||||
.query(Auto_Delivery)\
|
||||
.order_by(Auto_Delivery.last_updated.desc())\
|
||||
.limit(25)
|
||||
|
||||
for f in auto_customers:
|
||||
|
||||
# figure out how much temperature effects oil
|
||||
if today_temp >= 60:
|
||||
use_day = 1
|
||||
elif 40 >= today_temp >= 59:
|
||||
use_day = 3
|
||||
elif 30 >= today_temp >= 39:
|
||||
use_day = 5
|
||||
elif 0 >= today_temp >= 29:
|
||||
use_day = 7
|
||||
elif -20 >= today_temp >= -1:
|
||||
use_day = 10
|
||||
else:
|
||||
use_day = 0
|
||||
try:
|
||||
|
||||
# times temp factory by house factor
|
||||
gallon_use_today = f.house_factor * use_day
|
||||
see_if_autos_updated = (session.query(Auto_Update)
|
||||
.filter(Auto_Update.last_updated == date.today())
|
||||
.first()
|
||||
)
|
||||
except:
|
||||
session.rollback()
|
||||
see_if_autos_updated = None
|
||||
|
||||
# get estimated gallons left
|
||||
get_gallons_left = f.estimated_gallons_left_prev_day - gallon_use_today
|
||||
if see_if_autos_updated is not None:
|
||||
return ({"ok": True}), 200
|
||||
else:
|
||||
|
||||
create_new_update = Auto_Update(
|
||||
last_updated = date.today()
|
||||
)
|
||||
today_temp = (session
|
||||
.query(Auto_Temp)
|
||||
.order_by(Auto_Temp.id.desc())
|
||||
.first())
|
||||
|
||||
# get all automatic customers
|
||||
auto_customers = (session
|
||||
.query(Auto_Delivery)
|
||||
.order_by(Auto_Delivery.last_updated.desc())
|
||||
.limit(25))
|
||||
|
||||
# get previous day gallons left
|
||||
f.estimated_gallons_left_prev_day = f.estimated_gallons_left + gallon_use_today
|
||||
for f in auto_customers:
|
||||
|
||||
# figure out days since last fill
|
||||
d1 = date.today()
|
||||
d0 = f.last_fill
|
||||
|
||||
delta = d1 - d0
|
||||
f.days_since_last_fill = delta.days
|
||||
# figure out how much temperature effects oil
|
||||
today_temptemp_avg = int(today_temp.temp_avg)
|
||||
if today_temptemp_avg >= 65.01:
|
||||
use_day = 1
|
||||
elif 50.01 <= today_temptemp_avg <= 65:
|
||||
use_day = 2
|
||||
elif 35.01 <= today_temptemp_avg <= 50:
|
||||
use_day = 3
|
||||
elif 30.01 <= today_temptemp_avg <= 35:
|
||||
use_day = 4
|
||||
elif 25.01 <= today_temptemp_avg <= 30:
|
||||
use_day = 5
|
||||
elif 20.01 <= today_temptemp_avg <= 25:
|
||||
use_day = 7
|
||||
elif 15.01 <= today_temptemp_avg <= 20:
|
||||
use_day = 8
|
||||
elif 10.01 <= today_temptemp_avg <= 15:
|
||||
use_day = 9
|
||||
elif 5.01 <= today_temptemp_avg <= 10:
|
||||
use_day = 10
|
||||
elif 0.01 <= today_temptemp_avg <= 5:
|
||||
use_day =12
|
||||
elif -20 <= today_temptemp_avg <= -1:
|
||||
use_day = 15
|
||||
else:
|
||||
use_day = 0
|
||||
|
||||
f.estimated_gallons_left = get_gallons_left
|
||||
# times temp factory by house factor
|
||||
|
||||
session.add(f)
|
||||
gallon_use_today = f.house_factor * use_day
|
||||
|
||||
|
||||
# get previous day gallons left
|
||||
f.estimated_gallons_left_prev_day = f.estimated_gallons_left
|
||||
|
||||
# get estimated gallons left
|
||||
get_gallons_left = f.estimated_gallons_left_prev_day - gallon_use_today
|
||||
|
||||
|
||||
|
||||
f.estimated_gallons_left = get_gallons_left
|
||||
|
||||
session.add(create_new_update)
|
||||
session.add(f)
|
||||
session.commit()
|
||||
|
||||
|
||||
|
||||
return ({"ok": True}), 200
|
||||
|
||||
|
||||
@router.get("/temp", status_code=200)
|
||||
def update_temp():
|
||||
|
||||
temps = []
|
||||
|
||||
owm = OWM('21648d8c8d1a4ae495ace0b7810b4d36')
|
||||
mgr = owm.weather_manager()
|
||||
|
||||
# Search for current weather in London (Great Britain) and get details
|
||||
observation = mgr.weather_at_place('Worcester, US')
|
||||
w = observation.weather
|
||||
|
||||
temp_dict_fahrenheit = w.temperature() # a dict in Kelvin units (default when no temperature units provided)
|
||||
temp_dict_fahrenheit['temp_min']
|
||||
temp_dict_fahrenheit['temp_max']
|
||||
temp_dict_fahrenheit = w.temperature('fahrenheit')
|
||||
|
||||
low_temp = temp_dict_fahrenheit['temp_min']
|
||||
high_temp = temp_dict_fahrenheit['temp_max']
|
||||
temps.append(temp_dict_fahrenheit['temp_max'])
|
||||
temps.append(temp_dict_fahrenheit['temp_min'])
|
||||
|
||||
get_avg = Average(temps)
|
||||
rounded_temp = round(get_avg)
|
||||
|
||||
dday = (65 - ((low_temp + high_temp) /2))
|
||||
|
||||
add_new_temp = Auto_Temp(
|
||||
temp = temp_dict_fahrenheit['temp'],
|
||||
temp_max = temp_dict_fahrenheit['temp_max'],
|
||||
temp_min = temp_dict_fahrenheit['temp_min'],
|
||||
temp_avg = rounded_temp,
|
||||
degree_day = dday
|
||||
)
|
||||
|
||||
session.add(add_new_temp)
|
||||
session.commit()
|
||||
try:
|
||||
see_if_temp_exists = (session.query(Auto_Temp)
|
||||
.filter(Auto_Temp.todays_date == date.today())
|
||||
.first())
|
||||
except:
|
||||
see_if_temp_exists = None
|
||||
if see_if_temp_exists is not None:
|
||||
return ({"ok": True}), 200
|
||||
else:
|
||||
temps = []
|
||||
|
||||
return ({"ok": True}), 200
|
||||
|
||||
|
||||
def Average(lst):
|
||||
return sum(lst) / len(lst)
|
||||
owm = OWM('21648d8c8d1a4ae495ace0b7810b4d36')
|
||||
mgr = owm.weather_manager()
|
||||
|
||||
# Search for current weather in London (Great Britain) and get details
|
||||
observation = mgr.weather_at_place('Worcester, US')
|
||||
w = observation.weather
|
||||
|
||||
temp_dict_fahrenheit = w.temperature() # a dict in Kelvin units (default when no temperature units provided)
|
||||
temp_dict_fahrenheit['temp_min']
|
||||
temp_dict_fahrenheit['temp_max']
|
||||
temp_dict_fahrenheit = w.temperature('fahrenheit')
|
||||
|
||||
low_temp = temp_dict_fahrenheit['temp_min']
|
||||
high_temp = temp_dict_fahrenheit['temp_max']
|
||||
temps.append(temp_dict_fahrenheit['temp_max'])
|
||||
temps.append(temp_dict_fahrenheit['temp_min'])
|
||||
|
||||
get_avg = Average(temps)
|
||||
rounded_temp = round(get_avg)
|
||||
|
||||
dday = (65 - ((low_temp + high_temp) /2))
|
||||
|
||||
add_new_temp = Auto_Temp(
|
||||
temp = temp_dict_fahrenheit['temp'],
|
||||
temp_max = temp_dict_fahrenheit['temp_max'],
|
||||
temp_min = temp_dict_fahrenheit['temp_min'],
|
||||
temp_avg = rounded_temp,
|
||||
degree_day = dday,
|
||||
todays_date = date.today()
|
||||
)
|
||||
|
||||
session.add(add_new_temp)
|
||||
session.commit()
|
||||
|
||||
return ({"ok": True}), 200
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user