-
Notifications
You must be signed in to change notification settings - Fork 1
/
streaming.py
85 lines (74 loc) · 2.3 KB
/
streaming.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
import logging
from time import sleep
import numpy as np
logger = logging.getLogger(__name__)
try:
from dime import DimeClient
except ImportError:
logger.debug("Dime import failed.")
class Streaming:
def __init__(self, market, port):
self.params_built = False
self.market = market
self.dimec = None
self.SysParam = dict()
self.LMP = dict()
self.port = port
self.recipent = "geovis1"
def connect(self):
self.dimec = DimeClient("tcp", "localhost", self.port)
self.dimec.join("market")
def build_init(self):
bus_name = []
latitude = []
longitude = []
for bus in self.market.bus:
bus_name.append(bus.name)
latitude.append(bus.latitude)
longitude.append(bus.longitude)
self.dimec["Bus"] = bus_name
self.dimec["latitude"] = np.array(latitude)
self.dimec["longitude"] = np.array(longitude)
def send_init(self):
if not self.market.dime:
return
self.connect()
if not self.params_built:
self.build_init()
self.params_built = True
t_sleep = 0.05
self.dimec2 = DimeClient("tcp", "localhost", self.port)
# self.dimec2.join("Vis")
sleep(t_sleep)
self.dimec.send(self.recipent, "Bus")
sleep(t_sleep)
# self.dimec2.sync()
self.dimec.send(self.recipent, "latitude")
sleep(t_sleep)
# self.dimec2.sync()
self.dimec.send(self.recipent, "longitude")
sleep(t_sleep)
# self.dimec2.sync()
sleep(t_sleep)
def build_LMP(self):
self.dimec["LMP"] = np.array(self.market.LMP)
def send_LMP(self):
if not self.market.dime:
return
self.build_LMP()
sleep(0.05)
self.dimec.send(self.recipent, "LMP")
sleep(0.05)
# self.dimec2.sync()
# print(self.dimec2["LMP"])
@staticmethod
def finalize(self):
"""
Send ``DONE`` signal when simulation completes
:return: None
"""
if not self.market.dime:
return
self.dimec.broadcast_r(DONE=1)
self.dimec.close()
self.dimec2.close()