Skip to content

Commit

Permalink
so i can send local
Browse files Browse the repository at this point in the history
  • Loading branch information
Elijah Hudlow committed Feb 7, 2025
1 parent ac9738b commit 1dc1c63
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
23 changes: 21 additions & 2 deletions mrqart.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __repr__(self) -> str:
#: Websocket port used to send updates to browser
WS_PORT = 5000
#: HTTP port used to serve static/index.html
HTTP_PORT = 8080
HTTP_PORT = 9090

FOLLOW_FLAGS = aionotify.Flags.CLOSE_WRITE | aionotify.Flags.CREATE
#: list of all web socket connections to broadcast to
Expand Down Expand Up @@ -84,14 +84,19 @@ def __init__(self):
handlers = [
(r"/", HttpIndex),
# TODO(20250204): add GetState
# r"/state", GetState, # json state response
(r"/state", GetState)
]
settings = dict(
static_path=os.path.join(FILEDIR, "static"),
debug=True,
)
super().__init__(handlers, **settings)

class GetState(RequestHandler):
"""Return the current state as JSON"""

async def get(self):
self.write(json.dumps({k: repr(v) for k, v in STATE.items()}))

class HttpIndex(RequestHandler):
"""Handle index page request"""
Expand Down Expand Up @@ -151,16 +156,30 @@ async def monitor_dirs(watcher, dcm_checker):
await watcher.setup()
logging.debug("watching for new files")
while True:

# event = await asyncio.wait_for(watcher.get_event(), timeout=?)

event = await watcher.get_event()

# Refresh state every 60 seconds if no new event is found
if not event:
logging.info("Refreshing state...")
STATE.clear()
await asyncio.sleep(60) # 60 is the first attempt, we will see what works
continue

logging.debug("got event %s", event)
file = os.path.join(event.alias, event.name)


if os.path.isdir(file):
watcher.watch(path=file, flags=FOLLOW_FLAGS)
logging.info("%s is a dir! following with %d", file, FOLLOW_FLAGS)
continue
if event.flags == aionotify.Flags.CREATE:
logging.debug("file created but waiting for WRITE finish")
continue

# Event(flags=256, cookie=0, name='a', alias='/home/foranw/src/work/mrrc-hdr-qa/./sim')
if re.search("^MR.|.dcm$|.IMA$", event.name):

Expand Down
57 changes: 55 additions & 2 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,66 @@
//console.log("new message!", msg);
data = JSON.parse(msg.data)
console.log("new message data:", data);

if(data['type'] == 'new'){
add_new_series(data['content'])
}
// TODO(20250204): if 'update' but don't have state
// call to server and update UI

if(data['type'] == 'update') {
console.log("Received update message:", data);

// If state is empty, fetch it
if (document.getElementById("stations").innerHTML == "waiting for scanner") {
console.warn("State missing, fetching...");
fetchState();
}
}

}

// Fetch the current scanner state from /state and update UI
function fetchState() {
fetch("/state")
.then(response => response.json())
.then(data => {
console.log("Fetched state:", data);
updateUIFromState(data);
})
.catch(err => console.error("Error fetching state:", err));

}

// Update UI with the fetched state
function updateUIFromState(stateData) {
const stationList = document.getElementById("stations");
stationList.innerHTML = ""; // Clear previous state

for (const [station, session] of Object.entries(stateData)) {
let el = document.createElement("li");
el.textContent = `${station}: ${session}`;
stationList.appendChild(el);
}

}

// Periodically refresh state every 60 seconds
setInterval(fetchState, 60000);

// Fetch state on page load
window.onload = function() {
update_via_ws();
fetchState();
}

// Connects socket to main dispatcher `receivedMessage`
function update_via_ws() {
const host = "ws://" + location.hostname + ":5000/";
console.log("WebSocket connecting to:", host);
const ws = new WebSocket(host);
ws.addEventListener('message', receivedMessage);
}


// new dicom data into table
function mktable(data){
const keys = [
Expand Down

0 comments on commit 1dc1c63

Please sign in to comment.