2015-01-13 19:12:19 +08:00
|
|
|
from collections import OrderedDict
|
2015-01-12 18:51:23 +08:00
|
|
|
import importlib
|
|
|
|
|
2015-01-17 19:38:20 +08:00
|
|
|
from artiq.protocols.sync_struct import Notifier
|
2015-02-12 00:54:54 +08:00
|
|
|
from artiq.protocols.pc_rpc import Client, BestEffortClient
|
2015-02-22 05:28:18 +08:00
|
|
|
from artiq.master.results import result_dict_to_hdf5
|
2015-02-04 18:37:57 +08:00
|
|
|
|
|
|
|
|
2015-01-12 18:51:23 +08:00
|
|
|
class ResultDB:
|
2015-01-13 19:12:19 +08:00
|
|
|
def __init__(self, realtime_results):
|
|
|
|
self.realtime_data = Notifier({x: [] for x in realtime_results})
|
|
|
|
self.data = Notifier(dict())
|
2015-01-12 18:51:23 +08:00
|
|
|
|
2015-01-29 23:48:39 +08:00
|
|
|
def _request(self, name):
|
2015-01-13 19:12:19 +08:00
|
|
|
try:
|
|
|
|
return self.realtime_data[name]
|
|
|
|
except KeyError:
|
|
|
|
try:
|
|
|
|
return self.data[name]
|
|
|
|
except KeyError:
|
|
|
|
self.data[name] = []
|
|
|
|
return self.data[name]
|
2015-01-12 18:51:23 +08:00
|
|
|
|
2015-01-29 23:48:39 +08:00
|
|
|
def request(self, name):
|
|
|
|
r = self._request(name)
|
|
|
|
r.kernel_attr_init = False
|
|
|
|
return r
|
|
|
|
|
2015-01-12 18:51:23 +08:00
|
|
|
def set(self, name, value):
|
2015-01-29 21:45:24 +08:00
|
|
|
if name in self.realtime_data.read:
|
2015-01-13 19:12:19 +08:00
|
|
|
self.realtime_data[name] = value
|
|
|
|
else:
|
|
|
|
self.data[name] = value
|
2015-01-12 18:51:23 +08:00
|
|
|
|
2015-02-04 18:37:57 +08:00
|
|
|
def write_hdf5(self, f):
|
2015-02-22 05:28:18 +08:00
|
|
|
result_dict_to_hdf5(f, self.realtime_data.read)
|
|
|
|
result_dict_to_hdf5(f, self.data.read)
|
2015-02-04 18:37:57 +08:00
|
|
|
|
2015-01-12 18:51:23 +08:00
|
|
|
|
|
|
|
def _create_device(desc, dbh):
|
2015-02-05 22:53:31 +08:00
|
|
|
ty = desc["type"]
|
|
|
|
if ty == "local":
|
|
|
|
module = importlib.import_module(desc["module"])
|
|
|
|
device_class = getattr(module, desc["class"])
|
|
|
|
return device_class(dbh, **desc["arguments"])
|
|
|
|
elif ty == "controller":
|
2015-02-12 00:54:54 +08:00
|
|
|
if desc["best_effort"]:
|
|
|
|
cl = BestEffortClient
|
|
|
|
else:
|
|
|
|
cl = Client
|
|
|
|
return cl(desc["host"], desc["port"], desc["target_name"])
|
2015-02-05 22:53:31 +08:00
|
|
|
else:
|
|
|
|
raise ValueError("Unsupported type in device DB: " + ty)
|
2015-01-12 18:51:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
class DBHub:
|
|
|
|
"""Connects device, parameter and result databases to experiment.
|
|
|
|
Handle device driver creation and destruction.
|
|
|
|
|
|
|
|
"""
|
|
|
|
def __init__(self, ddb, pdb, rdb):
|
|
|
|
self.ddb = ddb
|
|
|
|
self.active_devices = OrderedDict()
|
|
|
|
|
|
|
|
self.get_parameter = pdb.request
|
|
|
|
self.set_parameter = pdb.set
|
|
|
|
self.get_result = rdb.request
|
|
|
|
self.set_result = rdb.set
|
|
|
|
|
|
|
|
def get_device(self, name):
|
|
|
|
if name in self.active_devices:
|
|
|
|
return self.active_devices[name]
|
|
|
|
else:
|
|
|
|
desc = self.ddb.request(name)
|
|
|
|
while isinstance(desc, str):
|
|
|
|
# alias
|
|
|
|
desc = self.ddb.request(desc)
|
|
|
|
dev = _create_device(desc, self)
|
|
|
|
self.active_devices[name] = dev
|
|
|
|
return dev
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
"""Closes all active devices, in the opposite order as they were
|
|
|
|
requested.
|
|
|
|
|
|
|
|
Do not use the same ``DBHub`` again after calling
|
|
|
|
this function.
|
|
|
|
|
|
|
|
"""
|
|
|
|
for dev in reversed(list(self.active_devices.values())):
|
2015-02-12 00:54:54 +08:00
|
|
|
if isinstance(dev, (Client, BestEffortClient)):
|
2015-02-05 22:53:31 +08:00
|
|
|
dev.close_rpc()
|
|
|
|
elif hasattr(dev, "close"):
|
2015-01-12 18:51:23 +08:00
|
|
|
dev.close()
|