-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathishavskraft-client.py
46 lines (42 loc) · 1.9 KB
/
ishavskraft-client.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from atd import atd
import requests
from requests.structures import CaseInsensitiveDict
from datetime import datetime
from datetime import timedelta
from datetime import date
import json
with open("secret") as secret, open("user") as user:
params={"password": secret.read().strip(),"username":user.read().strip()}
r= requests.post("https://utvikler.ishavskraft.no//api/v1/token",json=params)
token=r.json()["token"]
today=date.today()
tomorrow=date.today() + timedelta(days=1)
tomorrowplusone=date.today() + timedelta(days=2)
headers = CaseInsensitiveDict()
headers["Authorization"] = "Bearer %s" % token
request= requests.get("https://utvikler.ishavskraft.no/api/v1/spotpriser/%s/%s/NO4" % (tomorrow.isoformat(),tomorrowplusone.isoformat()),headers=headers)
timedict=dict()
print(request.text)
for klokkeslett in request.json()["spotprisDagList"][0]["spotprisList"]:
timedict[klokkeslett["time"]]=klokkeslett["pris"]
sortedtimedict={k: v for k, v in sorted(timedict.items(), key=lambda item: item[1])}
print(sortedtimedict)
highestprices=list(sortedtimedict)[-3:]
highestprices.sort()
print(highestprices)
prev=None
for hour in highestprices:
hourobject=datetime.fromisoformat(hour)
future=hourobject+timedelta(hours=1)
if prev:
difference=hourobject-prev
if difference == timedelta(hours=1):
print("continious")
else:
print("turn on again at %s" % (hourobject+timedelta(hours=1)))
atd.at("cd /home/bjarte/python/avoidpeakpower/ && ./heaters_on.py",future.astimezone().replace(tzinfo=None))
prev=hourobject
print("Turning off at %s" % hour)
atd.at("cd /home/bjarte/python/avoidpeakpower/ && ./heaters_off.py",hourobject.astimezone().replace(tzinfo=None))
print("turn on again at %s" % (hourobject+timedelta(hours=1)))
atd.at("cd /home/bjarte/python/avoidpeakpower/ && ./heaters_on.py",future.astimezone().replace(tzinfo=None))