|
|
import os
|
|
|
import json
|
|
|
import numpy as np
|
|
|
from datetime import datetime
|
|
|
|
|
|
from pymongo import MongoClient
|
|
|
|
|
|
from models import Experiment, Data
|
|
|
|
|
|
from channels.handler import AsgiHandler
|
|
|
from channels.auth import channel_session_user
|
|
|
from channels import Group
|
|
|
|
|
|
host = os.environ.get('HOST_MONGO', 'localhost')
|
|
|
CLIENT = MongoClient('{}:27017'.format(host))
|
|
|
DB = CLIENT['dbplots']
|
|
|
|
|
|
# Connected to websocket.connect
|
|
|
def ws_connect(message, code, plot):
|
|
|
# Accept the incoming connection
|
|
|
message.reply_channel.send({'accept': True})
|
|
|
# Add them to the chat group
|
|
|
Group('{}_{}'.format(code, plot)).add(message.reply_channel)
|
|
|
|
|
|
def ws_message(message, code, plot):
|
|
|
# Accept the incoming connection
|
|
|
|
|
|
dt = datetime.strptime(str(json.loads(message.content['text'])['date']), '%d/%m/%Y')
|
|
|
e = DB.experiment.find_one({'date': dt})
|
|
|
if e:
|
|
|
if plot == 'spc':
|
|
|
datas = DB.data.find({'experiment': e['_id']}, ['time', 'data']).sort('time', -1).limit(1)[0]
|
|
|
e['time'] = [datas['time']]
|
|
|
e['spc'] = datas['data']['spc']
|
|
|
e['rti'] = datas['data']['rti']
|
|
|
e['noise'] = datas['data']['noise']
|
|
|
else:
|
|
|
datas = DB.data.find({'experiment': e['_id']}, ['time', 'data']).sort('time', 1)
|
|
|
dum = [(d['time'], d['data'][plot]) for d in datas]
|
|
|
e['time'] = [d[0] for d in dum]
|
|
|
dum = [d[1] for d in dum]
|
|
|
e[plot] = map(list, zip(*dum))
|
|
|
e.pop('date', None)
|
|
|
e.pop('_id', None)
|
|
|
message.reply_channel.send({'text': json.dumps(e)})
|
|
|
else:
|
|
|
message.reply_channel.send({'text': json.dumps({'interval': 0})})
|
|
|
|
|
|
# Connected to websocket.disconnect
|
|
|
def ws_disconnect(message, code, plot):
|
|
|
Group('{}_{}'.format(code, plot)).discard(message.reply_channel)
|
|
|
|