Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport monitor #731

Merged
merged 15 commits into from
Oct 16, 2017
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions qcodes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from qcodes.loops import Loop, active_loop, active_data_set
from qcodes.measure import Measure
from qcodes.actions import Task, Wait, BreakIf
from qcodes.monitor.monitor import Monitor
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to drop this import to not make websockets a hard dependency of qcodes?


from qcodes.data.data_set import DataSet, new_data, load_data
from qcodes.data.location import FormatLocation
Expand Down
Empty file added qcodes/monitor/__init__.py
Empty file.
1 change: 1 addition & 0 deletions qcodes/monitor/dist/css/main.0963d6b9.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added qcodes/monitor/dist/favicon.ico
Binary file not shown.
1 change: 1 addition & 0 deletions qcodes/monitor/dist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="x-ua-compatible" content="ie=edge"><title>QCoDeS montior</title><link rel="shortcut icon" href="/favicon.ico"><link href="/css/main.0963d6b9.css" rel="stylesheet"></head><body><div id="root"></div><script type="text/javascript" src="/js/main.b221ee88.js"></script></body></html>
3 changes: 3 additions & 0 deletions qcodes/monitor/dist/js/main.b221ee88.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions qcodes/monitor/dist/webpack-assets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"main":{"js":"/js/main.b221ee88.js","css":"/css/main.0963d6b9.css"}}
219 changes: 219 additions & 0 deletions qcodes/monitor/monitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2017 unga <[email protected]>
#
# Distributed under terms of the MIT license.
"""
Monitor a set of parameter in a background thread
stream opuput over websocket
"""

import asyncio
import logging
import os
import time
import json
import http.server
import socketserver
import webbrowser

from threading import Thread
from typing import Dict
from concurrent.futures import Future
from concurrent.futures import CancelledError
import functools

import websockets

SERVER_PORT = 3000

log = logging.getLogger(__name__)


def _get_metadata(*parameters) -> Dict[float, list]:
"""
Return a dict that contains the parameter metadata grouped by the
instrument it belongs to.
"""
ts = time.time()
# group meta data by instrument if any
metas = {}
for parameter in parameters:
_meta = getattr(parameter, "_latest", None)
if _meta:
meta = _meta()
else:
raise ValueError("Input is not a parameter; Refusing to proceed")
# convert to string
meta['value'] = str(meta['value'])
if meta["ts"] is not None:
meta["ts"] = time.mktime(meta["ts"].timetuple())
meta["name"] = parameter.label or parameter.name
meta["unit"] = parameter.unit
# find the base instrument in case this is a channel parameter
baseinst = parameter._instrument
while hasattr(baseinst, '_parent'):
baseinst = baseinst._parent
accumulator = metas.get(str(baseinst), [])
accumulator.append(meta)
metas[str(baseinst)] = accumulator
parameters = []
for instrument in metas:
temp = {"instrument": instrument, "parameters": metas[instrument]}
parameters.append(temp)
state = {"ts": ts, "parameters": parameters}
return state


def _handler(parameters, interval: int):

async def serverFunc(websocket, path):
while True:
try:
try:
meta = _get_metadata(*parameters)
except ValueError as e:
log.exception(e)
break
log.debug("sending..")
try:
await websocket.send(json.dumps(meta))
# mute browser discconects
except websockets.exceptions.ConnectionClosed as e:
log.debug(e)
pass
await asyncio.sleep(interval)
except CancelledError:
break
log.debug("closing sever")

return serverFunc


class Monitor(Thread):
running = None
server = None

def __init__(self, *parameters, interval=1):
"""
Monitor qcodes parameters.

Args:
*parameters: Parameters to monitor
interval: How often one wants to refresh the values
"""
# let the thread start
time.sleep(0.01)
super().__init__()
self.loop = None
self._monitor(*parameters, interval=1)

def run(self):
"""
Start the event loop and run forever
"""
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
Monitor.running = self
self.loop.run_forever()

def stop(self):
"""
Shutdown the server, close the event loop and join the thread
"""
# this contains the server
# or any exception
server = self.future_restult.result()
# server.close()
self.loop.call_soon_threadsafe(server.close)
self.loop.call_soon_threadsafe(self.loop.stop)
self.join()
Monitor.running = None

@staticmethod
def show():
"""
Overwrite this method to show/raise your monitor GUI
F.ex.

::

import webbrowser
url = "localhost:3000"
# Open URL in new window, raising the window if possible.
webbrowser.open_new(url)

"""
webbrowser.open("http://localhost:{}".format(SERVER_PORT))

def _monitor(self, *parameters, interval=1):
handler = _handler(parameters, interval=interval)
# TODO (giulioungaretti) read from config
server = websockets.serve(handler, '127.0.0.1', 5678)

log.debug("Start monitoring thread")

if Monitor.running:
# stop the old server
log.debug("Stoppging and restarting server")
Monitor.running.stop()

self.start()

# let the thread start
time.sleep(0.01)

log.debug("Start monitoring server")
self._add_task(server)

def _create_task(self, future, coro):
task = self.loop.create_task(coro)
future.set_result(task)

def _add_task(self, coro):
future = Future()
self.task = coro
p = functools.partial(self._create_task, future, coro)
self.loop.call_soon_threadsafe(p)
# this stores the result of the future
self.future_restult = future.result()
self.future_restult.add_done_callback(_log_result)


def _log_result(future):
try:
future.result()
log.debug("Started server loop")
except:
log.exception("Could not start server loop")


class Server():

def __init__(self, port=3000):
self.port = port
self.handler = http.server.SimpleHTTPRequestHandler
self.httpd = socketserver.TCPServer(("", self.port), self.handler)
self.static_dir = os.path.join(os.path.dirname(__file__), 'dist')

def run(self):
os.chdir(self.static_dir)
log.debug("serving directory %s", self.static_dir)
log.info("Open broswer at http://localhost::{}".format(self.port))
self.httpd.serve_forever()

def stop(self):
self.httpd.shutdown()
self.join()


if __name__ == "__main__":
server = Server(SERVER_PORT)
print("Open broswer at http://localhost:{}".format(server.port))
try:
webbrowser.open("http://localhost:{}".format(server.port))
server.run()
except KeyboardInterrupt:
exit()
8 changes: 5 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ def readme():
# if we want to install without tests:
# packages=find_packages(exclude=["*.tests", "tests"]),
packages=find_packages(),
package_data={'qcodes': ['widgets/*.js', 'widgets/*.css', 'config/*.json']},
install_requires= [
package_data={'qcodes': ['monitor/dist/*', 'monitor/dist/js/*',
'monitor/dist/css/*', 'config/*.json']},
install_requires=[
'numpy>=1.10',
'pyvisa>=1.8',
'h5py>=2.6'
'h5py>=2.6',
'websockets>=3.2,<3.4'
],

test_suite='qcodes.tests',
Expand Down