-
Notifications
You must be signed in to change notification settings - Fork 0
/
energy_transfer_bank.py
115 lines (77 loc) · 2.56 KB
/
energy_transfer_bank.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from flask import Flask, escape, request
import threading, json, time
from spannungsteiler.util import broker_util
import socket, logging
mutex= threading.Lock()
app = Flask(__name__)
log = logging.getLogger('werkzeug')
log.disabled = True
energy_requested = {}
energy_provided = {}
def get_local_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("9.9.9.9", 80))
ip = s.getsockname()[0]
s.close()
return ip
def subscribe_to_topics():
topics = [
"spannungsteiler_demand_publish",
"spannungsteiler_offer_publish"
]
for i in topics:
broker_util.send("subscribe", {
"sender": "energy_bank",
"address": "http://{}:5000/energy_bank".format(get_local_ip()),
"interestedIn": i
})
def start_server():
app = Flask(__name__)
@app.route('/energy_bank', methods=["POST"])
def endpoint():
payload = request.json["event"]
if "demand" in payload["payload"]:
amount = payload["payload"]["demand"]
else:
amount = payload["payload"]["offer"]
sender_id = payload["id"]
mutex.acquire()
if amount < 0:
energy_requested[sender_id] = amount
elif amount > 0:
energy_provided[sender_id] = amount
else:
mutex.release()
return ""
mutex.release()
return json.dumps({'success':True}), 200, {'ContentType':'application/json'}
t = threading.Thread(target=app.run, kwargs={"host": "0.0.0.0"})
t.daemon = False
t.start()
if __name__ == "__main__":
subscribe_to_topics()
start_server()
while True:
mutex.acquire()
provided_sum = 0
requested_sum = 0
provided_sum = sum(energy_provided.values())
requested_sum = sum(energy_requested.values())
for (amount, key) in enumerate(energy_provided):
answere = 0
if amount <= abs(requested_sum):
answere -= amount
else:
answere = requested_sum
broker_util.send_transaction_execution("energy_bank", answere, key)
requested_sum -= answere
for (key, amount) in enumerate(energy_requested):
answere = 0
if abs(amount) <= provided_sum:
answere -= amount
else:
answere = provided_sum
broker_util.send_transaction_execution("energy_bank", answere, key)
provided_sum -= answere
mutex.release()
time.sleep(0.6)