-
Notifications
You must be signed in to change notification settings - Fork 319
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
WilliamHPNielsen
merged 15 commits into
microsoft:master
from
jenshnielsen:backport_monitor
Oct 16, 2017
Merged
Backport monitor #731
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
321d39d
Feature/monitor (#15)
giulioungaretti 851a45e
fix: Include static, add init
giulioungaretti f81fd2f
move server to onw scripts
giulioungaretti 7138f07
feat: make monitor server standalone
giulioungaretti 0184668
fix: Sleep before thread starts!
giulioungaretti 570fddb
fix: Update monitor GUI
giulioungaretti dc52646
Fix monitor for channel parameters (#61)
jenshnielsen 8e61060
The monitor does not work correctly with ws34
jenshnielsen ae8403c
make websockets a soft dependency
jenshnielsen 9f581e9
remove unused pass statment
jenshnielsen bc06cb4
add simple monitor readme
jenshnielsen f5e4b99
Merge branch 'master' into backport_monitor
jenshnielsen 2e559db
Merge branch 'master' into backport_monitor
WilliamHPNielsen e679d8b
Merge branch 'master' into backport_monitor
WilliamHPNielsen 4e79a02
Merge branch 'master' into backport_monitor
WilliamHPNielsen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"main":{"js":"/js/main.b221ee88.js","css":"/css/main.0963d6b9.css"}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?