Use a new interface for plugins.

Esse commit está contido em:
jfrey
2015-03-09 13:03:32 +01:00
commit 4ce3e3e938
9 arquivos alterados com 90 adições e 63 exclusões
+46
Ver Arquivo
@@ -0,0 +1,46 @@
"""
Extends Yapsy IPlugin interface to pass information about the board to plugins.
Fields of interest for plugins:
args: list of arguments passed to the plugins
sample_rate: actual sample rate of the board
eeg_channels: number of EEG
aux_channels: number of AUX channels
If needed, plugins that need to report an error can set self.is_activated to False during activate() call.
NB: because of how yapsy discovery system works, plugins must use the following syntax to inherit to use polymorphism (see http://yapsy.sourceforge.net/Advices.html):
import plugin_interface as plugintypes
class PluginExample(plugintypes.IPluginExtended):
...
"""
from yapsy.IPlugin import IPlugin
class IPluginExtended(IPlugin):
# args: passed by command line
def pre_activate(self, args, sample_rate=250, eeg_channels=8, aux_channels=3):
self.args = args
self.sample_rate = sample_rate
self.eeg_channels = eeg_channels
self.aux_channels = aux_channels
# by default we say that activation was okay -- inherited from IPlugin
self.is_activated = True
self.activate()
# tell outside world if init went good or bad
return self.is_activated
# inherited from IPlugin
def activate(self):
print "Plugin %s activated." % (self.__class__.__name__)
# inherited from IPlugin
def deactivate(self):
print "Plugin %s deactivated." % (self.__class__.__name__)
# plugins that require arguments should implement this method
def show_help(self):
print "I, %s, do not need any parameter." % (self.__class__.__name__)
+1 -1
Ver Arquivo
@@ -1,4 +1,4 @@
To create a new plugin, see print.py and print.yapsy-plugin for a minimal example.
To create a new plugin, see print.py and print.yapsy-plugin for a minimal example and plugin_interface.py for documentation about more advanced features.
Note: "__init__" will be automatically called when the main program loads, even if the plugin is not used, put computationally intensive instructions in activate() instead.
+8 -10
Ver Arquivo
@@ -2,10 +2,9 @@ import csv
import timeit
import datetime
import plugin_interface as plugintypes
from yapsy.IPlugin import IPlugin
class PluginCSVCollect(IPlugin):
class PluginCSVCollect(plugintypes.IPluginExtended):
def __init__(self, file_name="collect.csv", delim = ",", verbose=False):
now = datetime.datetime.now()
self.time_stamp = '%d-%d-%d_%d-%d-%d'%(now.year,now.month,now.day,now.hour,now.minute,now.second)
@@ -14,13 +13,13 @@ class PluginCSVCollect(IPlugin):
self.delim = delim
self.verbose = verbose
def activate(self, args):
if len(args) > 0:
if 'no_time' in args:
self.file_name = args[0]
def activate(self):
if len(self.args) > 0:
if 'no_time' in self.args:
self.file_name = self.args[0]
else:
self.file_name = args[0] + '_' + self.file_name;
if 'verbose' in args:
self.file_name = self.args[0] + '_' + self.file_name;
if 'verbose' in self.args:
self.verbose = True
self.file_name = self.file_name + '.csv'
@@ -28,7 +27,6 @@ class PluginCSVCollect(IPlugin):
#Open in append mode
with open(self.file_name, 'a') as f:
f.write('%'+self.time_stamp + '\n')
return True
def deactivate(self):
print "Closing, CSV saved to:", self.file_name
+3 -13
Ver Arquivo
@@ -1,18 +1,8 @@
import plugin_interface as plugintypes
from yapsy.IPlugin import IPlugin
class PluginPrint(IPlugin):
# args: passed by command line
def activate(self, args):
class PluginPrint(plugintypes.IPluginExtended):
def activate(self):
print "Print activated"
# tell outside world that init went good
return True
def deactivate(self):
print "Print Deactivated"
def show_help(self):
print "I do not need any parameter, just printing stuff."
# called with each new sample
def __call__(self, sample):
+5 -6
Ver Arquivo
@@ -2,7 +2,7 @@ import time
import timeit
from threading import Thread
from yapsy.IPlugin import IPlugin
import plugin_interface as plugintypes
# counter for sampling rate
nb_samples_out = -1
@@ -33,21 +33,20 @@ class Monitor(Thread):
self.nb_samples_out = nb_samples_out
time.sleep(self.polling_interval)
class PluginSampleRate(IPlugin):
class PluginSampleRate(plugintypes.IPluginExtended):
# update counters value
def __call__(self, sample):
global nb_samples_out
nb_samples_out = nb_samples_out + 1
# Instanciate "monitor" thread
def activate(self, args):
def activate(self):
monit = Monitor()
if len(args) > 0:
monit.polling_interval = float(args[0])
if len(self.args) > 0:
monit.polling_interval = float(self.args[0])
# daemonize thread to terminate it altogether with the main when time will come
monit.daemon = True
monit.start()
return True
def show_help(self):
print "Optional argument: polling_interval -- in seconds, default: 10."
+10 -12
Ver Arquivo
@@ -1,11 +1,11 @@
# requires pyosc
from OSC import OSCClient, OSCMessage
from yapsy.IPlugin import IPlugin
import plugin_interface as plugintypes
# Use OSC protocol to broadcast data (UDP layer), using "/openbci" stream. (NB. does not check numbers of channel as TCP server)
class StreamerOSC(IPlugin):
class StreamerOSC(plugintypes.IPluginExtended):
"""
Relay OpenBCI values to OSC clients
@@ -23,19 +23,17 @@ class StreamerOSC(IPlugin):
self.address = address
# From IPlugin
def activate(self, args):
if len(args) > 0:
self.ip = args[0]
if len(args) > 1:
self.port = args[1]
if len(args) > 2:
self.address = args[2]
def activate(self):
if len(self.args) > 0:
self.ip = self.args[0]
if len(self.args) > 1:
self.port = self.args[1]
if len(self.args) > 2:
self.address = self.args[2]
# init network
print "Selecting OSC streaming. IP: ", self.ip, ", port: ", self.port, ", address: ", self.address
self.client = OSCClient()
self.client.connect( (self.ip, self.port) )
return True
self.client.connect( (self.ip, self.port) )
# From IPlugin: close connections, send message to client
def deactivate(self):
+7 -9
Ver Arquivo
@@ -1,6 +1,6 @@
from threading import Thread
import socket, select, struct, time
from yapsy.IPlugin import IPlugin
import plugin_interface as plugintypes
# Simple TCP server to "broadcast" data to clients, handling deconnections. Binary format use network endianness (i.e., big-endian), float32
@@ -25,7 +25,7 @@ class MonitorStreamer(Thread):
time.sleep(1)
class StreamerTCPServer(IPlugin):
class StreamerTCPServer(plugintypes.IPluginExtended):
"""
Relay OpenBCI values to TCP clients
@@ -44,11 +44,11 @@ class StreamerTCPServer(IPlugin):
self.port = port
# From IPlugin
def activate(self, args):
if len(args) > 0:
self.ip = args[0]
if len(args) > 1:
self.port = args[1]
def activate(self):
if len(self.args) > 0:
self.ip = self.args[0]
if len(self.args) > 1:
self.port = self.args[1]
# init network
print "Selecting raw TCP streaming. IP: ", self.ip, ", port: ", self.port
@@ -59,8 +59,6 @@ class StreamerTCPServer(IPlugin):
self.monit.daemon = True
# launch monitor
self.monit.start()
return True
# the initialize method reads settings and outputs the first header
def initialize(self):
+9 -11
Ver Arquivo
@@ -11,8 +11,7 @@ import cPickle as pickle
import json
import socket
from yapsy.IPlugin import IPlugin
import plugin_interface as plugintypes
# class PluginPrint(IPlugin):
# # args: passed by command line
@@ -43,28 +42,27 @@ from yapsy.IPlugin import IPlugin
# # print sample_string
class UDPServer(IPlugin):
class UDPServer(plugintypes.IPluginExtended):
def __init__(self, ip='localhost', port=8888):
self.ip = ip
self.port = port
self.server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def activate(self, args):
def activate(self):
print "udp_server plugin"
print args
print self.args
if len(args) > 0:
self.ip = args[0]
if len(args) > 1:
self.port = int(args[1])
if len(self.args) > 0:
self.ip = self.args[0]
if len(self.args) > 1:
self.port = int(self.args[1])
# init network
print "Selecting raw UDP streaming. IP: ", self.ip, ", port: ", str(self.port)
self.server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print "Server started on port " + str(self.port)
return True
print "Server started on port " + str(self.port)
def __call__(self, sample):
self.send_data(json.dumps(sample.channel_data))
+1 -1
Ver Arquivo
@@ -86,7 +86,7 @@ if __name__ == '__main__':
print "Error: [", plug_name, "] not found or could not be loaded. Check name and requirements."
else:
print "\nActivating [", plug_name, "] plugin..."
if not plug.plugin_object.activate(plug_args):
if not plug.plugin_object.pre_activate(plug_args, sample_rate=250, eeg_channels=8, aux_channels=3):
print "Error while activating [", plug_name, "], check output for more info."
else:
print "Plugin [", plug_name, "] added to the list"