Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
Receive data information from client
Browse files Browse the repository at this point in the history
  • Loading branch information
mfkaptan committed Jul 4, 2013
1 parent 60c41a7 commit 0ac3d88
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 58 deletions.
54 changes: 42 additions & 12 deletions src/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,29 @@
WebSocketClientProtocol, \
connectWS


class Channel():
def __init__(self, name):
self.name = name
self.data = 0
self.quality = 0

def generate_data(self):
self.data = random.randrange(1024) * random.choice([-1,1]) + 1024 * 8
self.quality = random.randrange(200) * random.choice([-1,1]) + 900
return self.data, self.quality


class ChannelBlock():
def __init__(self, ch_type, ch_count, ch_names):
self.type = ch_type
self.count = ch_count
self.names = ch_names
self.channels = {}
for c in ch_names:
self.channels[c] = Channel(c)


class BroadcastClientProtocol(WebSocketClientProtocol):
"""
Simple client that connects to a WebSocket server, send a
Expand All @@ -25,26 +38,43 @@ def __init__(self):
'T7', 'P7', 'O1', 'O2',
'P8', 'T8', 'FC6', 'F4',
'F8', 'AF4']
self.channels = {}
for c in ch_names:
self.channels[c] = Channel(c)

def sendLevel(self):
self.client_channels = ChannelBlock("eeg", 14, ch_names)
self.about = 'type'

def sendType(self):
client_type = {'gtype': self.client_channels.type,
'gcount': self.client_channels.count,
'gnames': self.client_channels.names}
self.sendMessage(json.dumps({'about': 'config', 'client': client_type }))

if self.about == 'type':
reactor.callLater(0.2, self.sendType)
elif self.about == 'level':
self.sendLevel()

def sendLevel(self):
msg = list()
for c in self.channels:
data, qlt = self.channels[c].generate_data()
msg.append({'channel':self.channels[c].name,
for c in self.client_channels.names:
data, qlt = self.client_channels.channels[c].generate_data()
msg.append({'channel':self.client_channels.channels[c].name,
'signal': data,
'quality': qlt})

self.sendMessage(json.dumps({'channels': msg}))
reactor.callLater(0.2, self.sendLevel)
self.sendMessage(json.dumps({'about': 'channel', 'channels': msg}))
if self.about == 'level':
reactor.callLater(0.2, self.sendLevel)
elif self.about == 'type':
self.sendType()

def onOpen(self):
self.sendLevel()
self.sendType()

def onMessage(self, msg, binary):
pass
if msg == 'level':
self.about = 'level'
elif msg == 'type':
self.about = 'type'
#print "Got message: " + msg

if __name__ == '__main__':
Expand All @@ -57,4 +87,4 @@ def onMessage(self, msg, binary):
factory.protocol = BroadcastClientProtocol
connectWS(factory)

reactor.run()
reactor.run()
3 changes: 2 additions & 1 deletion src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
##
###############################################################################

import sys
import sys, json

from twisted.internet import reactor
from twisted.python import log
Expand Down Expand Up @@ -62,6 +62,7 @@ def unregister(self, client):
if client in self.clients:
print "unregistered client " + client.peerstr
self.clients.remove(client)
self.broadcast(json.dumps({'about': 'disconnect' }))

def broadcast(self, msg):
for c in self.clients:
Expand Down
118 changes: 73 additions & 45 deletions ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,14 @@

</tr>
</table>

</div>


<script>
/* Websocket */
var sock = null;
/* Log */
var ellog = null;
/* Default address */
var wsuri = "ws://localhost:9000";

function change_address()
Expand All @@ -93,12 +92,10 @@
{
wsuri = address.value;
}

if (sock != null)
{
sock.close();
}

draw();
}

Expand All @@ -115,72 +112,93 @@
log.style.display = "none";
document.getElementById("hidelog").innerHTML = "Show Log";
}

}

function draw()
{
var Channels;
var gArray = new Array();
var palette = new Rickshaw.Color.Palette( { scheme: 'classic9' } );

var graphtype;
var graphcount = 0;
var graphnames;

ellog = document.getElementById('log');

if ("WebSocket" in window)
{
sock = new WebSocket(wsuri);
}
else if ("MozWebSocket" in window)
{
sock = new MozWebSocket(wsuri);
}
}
else
{
log("Browser does not support WebSocket!");
window.location = "http://autobahn.ws/unsupportedbrowser";
}

/* Get data from the message and render graphs */
var getdata = function(channels)
{
var iv = setInterval( function()
{
for (i=0; i<graphcount; i++)
{
var data = { signal: channels.channels[i].signal, color:palette.color() };
gArray[i].series.addData(data);
gArray[i].render();
}
});
};

var Channels;
var gArray = new Array();
var palette = new Rickshaw.Color.Palette( { scheme: 'classic9' } );
var ch_names = ['AF3', 'F7', 'F3', 'FC5',
'T7', 'P7', 'O1', 'O2',
'P8', 'T8', 'FC6', 'F4',
'F8', 'AF4']
if (sock)
/* Get the config data (eeg etc.) */
var getconfig = function(config)
{
sock.onopen = function()
if (graphcount == 0)
{
log("Connected to " + wsuri);

document.getElementById("change").style.display = "none";
document.getElementById("wsuri").style.display = "none";
graphtype = config.client.gtype;
graphcount = config.client.gcount;
graphnames = config.client.gnames;

for(var i = 0; i < 14; i++)
for(var i=0; i<graphcount; i++)
{
var graph = new Rickshaw.Graph({
element: document.getElementById("chart"),
width: 1000,
height: 75,
renderer: 'line',
series: new Rickshaw.Series.FixedDuration([{ name: ch_names[i], color:palette.color() }], undefined,
series: new Rickshaw.Series.FixedDuration([{ name: graphnames[i], color:palette.color() }], undefined,
{
timeInterval: 1000,
maxDataPoints: 50,
})
});

graph.configure({strokeWidth:1});
var xTick = new Rickshaw.Graph.Axis.Time( { graph: graph } );
// var yTick = new Rickshaw.Graph.Axis.Y(
// {
// graph: graph,
// orientation: 'left',
// tickFormat: Rickshaw.Fixtures.Number.formatKMBT,
// element: document.getElementById('y_axis'),
// } );

var legend = new Rickshaw.Graph.Legend( {
element: document.querySelector('#legend'),
graph: graph
} );
graph.render()

/* Add graphs to gArray */
gArray.push(graph);
}
}
/* Send to device client that type information has received */
sock.send("level");
}
};

if (sock)
{
sock.onopen = function()
{
log("Connected to " + wsuri);

document.getElementById("change").style.display = "none";
document.getElementById("wsuri").style.display = "none";

/* Send type on open */
sock.send("type");
}

sock.onclose = function(e)
Expand All @@ -191,20 +209,30 @@
sock = null;
}

sock.onmessage = function(msg)
sock.onmessage = function(msg)
{
var text = msg.data;
Channels = eval ("(" + text + ")");

var iv = setInterval( function()
{
for (i=0 ; i<14; i++)
if (text != "level" && text != "type")
{
/* Convert to js object */
text = eval("(" + text + ")");

if(text.about == "config") /* Type information */
{
getconfig(text);
}
else if(text.about == "channel") /* Channel data */
{
getdata(text);
}
else if(text.about == "disconnect") /* Client disconnected */
{
var data = { signal: Channels.channels[i].signal, color:palette.color() };
gArray[i].series.addData(data);
gArray[i].render();
alert("Client disconnected");
location.reload(true);
sock.close();
sock = null;
}
});
}
}
}
}
Expand Down

1 comment on commit 0ac3d88

@mfkaptan
Copy link
Member Author

Choose a reason for hiding this comment

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

This commit will resolve the issue #3

Please sign in to comment.