|
@@ -18,9 +18,14 @@ from time import gmtime, strftime
|
|
|
from tornado.escape import json_decode, json_encode, url_escape
|
|
|
from threading import Timer
|
|
|
import subprocess
|
|
|
+from epics import ca
|
|
|
|
|
|
-root = os.path.dirname(__file__)
|
|
|
+try:
|
|
|
+ from yaml import CLoader as Loader, CDumper as Dumper
|
|
|
+except ImportError:
|
|
|
+ from yaml import Loader, Dumper
|
|
|
|
|
|
+root = os.path.dirname(__file__)
|
|
|
|
|
|
|
|
|
def setup_custom_logger(name):
|
|
@@ -36,8 +41,10 @@ def setup_custom_logger(name):
|
|
|
logger.addHandler(screen_handler)
|
|
|
return logger
|
|
|
|
|
|
+
|
|
|
logger = setup_custom_logger('BORA')
|
|
|
|
|
|
+
|
|
|
class RepeatedTimer(object):
|
|
|
def __init__(self, interval, function, *args, **kwargs):
|
|
|
self._timer = None
|
|
@@ -68,26 +75,77 @@ class RepeatedTimer(object):
|
|
|
|
|
|
|
|
|
months = {
|
|
|
- 'Jan' : 1,
|
|
|
- 'Feb' : 2,
|
|
|
- 'Mar' : 3,
|
|
|
- 'Apr' : 4,
|
|
|
- 'May' : 5,
|
|
|
- 'Jun' : 6,
|
|
|
- 'Jul' : 7,
|
|
|
- 'Aug' : 8,
|
|
|
- 'Sep' : 9,
|
|
|
- 'Oct' : 10,
|
|
|
- 'Nov' : 11,
|
|
|
- 'Dec' : 12
|
|
|
+ 'Jan': 1,
|
|
|
+ 'Feb': 2,
|
|
|
+ 'Mar': 3,
|
|
|
+ 'Apr': 4,
|
|
|
+ 'May': 5,
|
|
|
+ 'Jun': 6,
|
|
|
+ 'Jul': 7,
|
|
|
+ 'Aug': 8,
|
|
|
+ 'Sep': 9,
|
|
|
+ 'Oct': 10,
|
|
|
+ 'Nov': 11,
|
|
|
+ 'Dec': 12
|
|
|
}
|
|
|
|
|
|
|
|
|
+def fetch_epics():
|
|
|
+ with open("varname.yaml", 'r') as stream:
|
|
|
+ try:
|
|
|
+ varname = yaml.load(stream, Loader=Loader)
|
|
|
+ except yaml.YAMLError as exc:
|
|
|
+ print(exc)
|
|
|
+
|
|
|
+ if varname is None:
|
|
|
+ logger.error("Error: Empty varname file.")
|
|
|
+ return
|
|
|
+
|
|
|
+ pvs = list(varname.keys())
|
|
|
+
|
|
|
+ pvdata = {}
|
|
|
+ pvchids = []
|
|
|
+ ca.use_initial_context()
|
|
|
+ # create, don't connect or create callbacks
|
|
|
+ for name in pvs:
|
|
|
+ chid = ca.create_channel(name, connect=False, auto_cb=False)
|
|
|
+ pvchids.append(chid)
|
|
|
+
|
|
|
+ # connect
|
|
|
+ for chid in pvchids:
|
|
|
+ ca.connect_channel(chid,)
|
|
|
+
|
|
|
+ ca.poll()
|
|
|
+
|
|
|
+ # request get, but do not wait for result
|
|
|
+ for chid in pvchids:
|
|
|
+ ca.get_with_metadata(chid, ftype=14, wait=False)
|
|
|
+
|
|
|
+ # now wait for get() to complete
|
|
|
+ ca.poll()
|
|
|
+ for chid in pvchids:
|
|
|
+ val = ca.get_complete_with_metadata(chid, ftype=14)
|
|
|
+ timestamp = calendar.timegm(
|
|
|
+ datetime.datetime.utcfromtimestamp(val['timestamp']).timetuple())
|
|
|
+ pvdata[ca.name(chid)] = {
|
|
|
+ 'timestamp': timestamp,
|
|
|
+ 'value': val['value']
|
|
|
+ }
|
|
|
+
|
|
|
+ pvdata['time'] = strftime("%Y-%m-%d %H:%M:%S")
|
|
|
+
|
|
|
+ with open("./bora/.tmp.yaml", 'w') as stream_tmp:
|
|
|
+ stream_tmp.write(yaml.dump(pvdata, default_flow_style=False))
|
|
|
+ src_file = os.getcwd() + "/bora/.tmp.yaml"
|
|
|
+ dst_file = os.getcwd() + "/bora/cache.yaml"
|
|
|
+ shutil.copy(src_file, dst_file)
|
|
|
+
|
|
|
+
|
|
|
def fetchDataADEI():
|
|
|
|
|
|
with open("varname.yaml", 'r') as stream:
|
|
|
try:
|
|
|
- varname = yaml.load(stream)
|
|
|
+ varname = yaml.load(stream, Loader=Loader)
|
|
|
except yaml.YAMLError as exc:
|
|
|
print(exc)
|
|
|
if varname is None:
|
|
@@ -100,7 +158,8 @@ def fetchDataADEI():
|
|
|
time_range = "3600,-1"
|
|
|
for param in varname:
|
|
|
dest = os.environ["BORA_ADEI_SERVER"] + 'services/getdata.php'
|
|
|
- url = dest + "?" + varname[param] + "&window=" + time_range + "&experiment=*-*&rt=full&cache=1"
|
|
|
+ url = dest + "?" + varname[param] + "&window=" + \
|
|
|
+ time_range + "&experiment=*-*&rt=full&cache=1"
|
|
|
data = requests.get(url,
|
|
|
auth=(os.environ["BORA_ADEI_USERNAME"],
|
|
|
os.environ["BORA_ADEI_PASSWORD"])).content
|
|
@@ -122,7 +181,8 @@ def fetchDataADEI():
|
|
|
time_buffer = first_value.split("-")
|
|
|
time_buffer[1] = str(months[time_buffer[1]])
|
|
|
first_value = "-".join(time_buffer)
|
|
|
- first_ts = calendar.timegm(datetime.datetime.strptime(first_value, "%d-%m-%y %H:%M:%S.%f").timetuple())
|
|
|
+ first_ts = calendar.timegm(datetime.datetime.strptime(
|
|
|
+ first_value, "%d-%m-%y %H:%M:%S.%f").timetuple())
|
|
|
except:
|
|
|
first_ts = ""
|
|
|
cache_data[param] = {'timestamp': first_ts, 'value': last_value}
|
|
@@ -146,7 +206,7 @@ class ListHandler(tornado.web.RequestHandler):
|
|
|
def get(self):
|
|
|
with open("./bora/cache.yaml", 'r') as stream:
|
|
|
try:
|
|
|
- response = yaml.load(stream)
|
|
|
+ response = yaml.load(stream, Loader=Loader)
|
|
|
except yaml.YAMLError as exc:
|
|
|
print(exc)
|
|
|
if response is None:
|
|
@@ -177,13 +237,13 @@ class DesignerHandler(tornado.web.RequestHandler):
|
|
|
print("In designer mode.")
|
|
|
with open("./bora/cache.yaml", 'r') as stream:
|
|
|
try:
|
|
|
- cache_data = yaml.load(stream)
|
|
|
+ cache_data = yaml.load(stream, Loader=Loader)
|
|
|
except yaml.YAMLError as exc:
|
|
|
print(exc)
|
|
|
|
|
|
with open("style.yaml", 'r') as stream:
|
|
|
try:
|
|
|
- style_data = yaml.load(stream)
|
|
|
+ style_data = yaml.load(stream, Loader=Loader)
|
|
|
except yaml.YAMLError as exc:
|
|
|
print(exc)
|
|
|
|
|
@@ -192,7 +252,8 @@ class DesignerHandler(tornado.web.RequestHandler):
|
|
|
else:
|
|
|
index_data = cache_data
|
|
|
|
|
|
- if index_data is not None: index_data = sorted(index_data)
|
|
|
+ if index_data is not None:
|
|
|
+ index_data = sorted(index_data)
|
|
|
|
|
|
data = {
|
|
|
"cache": cache_data,
|
|
@@ -238,23 +299,23 @@ class StatusHandler(tornado.web.RequestHandler):
|
|
|
print("In status mode.")
|
|
|
with open("style.yaml", 'r') as stream:
|
|
|
try:
|
|
|
- style_data = yaml.load(stream)
|
|
|
+ style_data = yaml.load(stream, Loader=Loader)
|
|
|
except yaml.YAMLError as exc:
|
|
|
print(exc)
|
|
|
|
|
|
with open("varname.yaml", 'r') as vstream:
|
|
|
try:
|
|
|
- varname_data = yaml.load(vstream)
|
|
|
+ varname_data = yaml.load(vstream, Loader=Loader)
|
|
|
except yaml.YAMLError as exc:
|
|
|
print(exc)
|
|
|
|
|
|
if not os.path.isfile("./bora/cache.yaml"):
|
|
|
print("BORA is loading data, please refresh the page again in a moment.")
|
|
|
- open("./bora/cache.yaml","w")
|
|
|
+ open("./bora/cache.yaml", "w")
|
|
|
|
|
|
with open("./bora/cache.yaml", 'r') as vstream:
|
|
|
try:
|
|
|
- cache_data = yaml.load(vstream)
|
|
|
+ cache_data = yaml.load(vstream, Loader=Loader)
|
|
|
except yaml.YAMLError as exc:
|
|
|
print(exc)
|
|
|
|
|
@@ -278,7 +339,7 @@ class UpdateHandler(tornado.web.RequestHandler):
|
|
|
rt.stop()
|
|
|
with open("varname.yaml", 'r') as stream:
|
|
|
try:
|
|
|
- cache_data = yaml.load(stream)
|
|
|
+ cache_data = yaml.load(stream, Loader=Loader)
|
|
|
except yaml.YAMLError as exc:
|
|
|
print(exc)
|
|
|
|
|
@@ -375,7 +436,7 @@ class AdeiKatrinHandler(tornado.web.RequestHandler):
|
|
|
data = requests.get(
|
|
|
url,
|
|
|
auth=(os.environ["BORA_ADEI_USERNAME"],
|
|
|
- os.environ["BORA_ADEI_PASSWORD"])
|
|
|
+ os.environ["BORA_ADEI_PASSWORD"])
|
|
|
)
|
|
|
cr = data.content
|
|
|
cr = cr.splitlines()
|
|
@@ -430,7 +491,7 @@ class AdeiKatrinHandler(tornado.web.RequestHandler):
|
|
|
# store in yaml file
|
|
|
with open("varname.yaml", 'r') as stream:
|
|
|
try:
|
|
|
- cache_data = yaml.load(stream)
|
|
|
+ cache_data = yaml.load(stream, Loader=Loader)
|
|
|
except yaml.YAMLError as exc:
|
|
|
print(exc)
|
|
|
|
|
@@ -458,10 +519,10 @@ class GetDataHandler(tornado.web.RequestHandler):
|
|
|
cache_data = None
|
|
|
if not os.path.isfile("./bora/cache.yaml"):
|
|
|
print("BORA is loading data, please refresh the page again in a moment.")
|
|
|
- open("./bora/cache.yaml","w")
|
|
|
+ open("./bora/cache.yaml", "w")
|
|
|
with open("./bora/cache.yaml", 'r') as stream:
|
|
|
try:
|
|
|
- cache_data = yaml.load(stream)
|
|
|
+ cache_data = yaml.load(stream, Loader=Loader)
|
|
|
except yaml.YAMLError as exc:
|
|
|
|
|
|
print(exc)
|
|
@@ -471,7 +532,7 @@ class GetDataHandler(tornado.web.RequestHandler):
|
|
|
|
|
|
|
|
|
print("Running...")
|
|
|
-rt = RepeatedTimer(int(os.environ["BORA_POLLING"]), fetchDataADEI)
|
|
|
+rt = RepeatedTimer(int(os.environ["BORA_POLLING"]), fetch_epics)
|
|
|
|
|
|
application = tornado.web.Application([
|
|
|
(r"/version/?", VersionHandler),
|