Skip to content

Commit

Permalink
Plugin base implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
chollinger93 committed Dec 20, 2019
1 parent 91feab9 commit ba7f6a7
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 1 deletion.
2 changes: 1 addition & 1 deletion conf/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ IP=192.168.1.240
Port=5556

[Audio]
Streamer=none
Streamer=pygame
Path=../audio_files

[General]
Expand Down
11 changes: 11 additions & 0 deletions conf/plugins.d/audio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[ZmqSender]
IP=192.168.1.240
Port=5557

[ZmqReceiver]
IP=192.168.1.240
Port=5557

[Audio]
Streamer=pygame
Path=../audio_files
Empty file added plugin_base/__init__.py
Empty file.
61 changes: 61 additions & 0 deletions plugin_base/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import zmq


class ZmqBasePlugin:
def __init__(self, configuration):
self.configuration = configuration
self.recv_server = configuration['ZmqReceiver']['IP']
self.recv_port = configuration['ZmqReceiver']['Port']
self.send_server = configuration['ZmqSender']['IP']
self.send_port = configuration['ZmqSender']['Port']

def on_receive(self, *args):
print('on_receive is not implemented in {}'.format(
self.__class__.__name__))
pass

def send_ack(self, socket, *args):
print('send_ack is not implemented in {}'.format(self.__class__.__name__))
# Send acknowlede
socket.send(b'Ack')

def process(self, *args):
print('process is not implemented in {}'.format(self.__class__.__name__))
pass

def start_receiver(self, *args):
print('Starting receiver thread for ZMQ in {}...'.format(
self.__class__.__name__))
context = zmq.Context()
socket = context.socket(zmq.REP)
print('Binding to {}'.format(self.recv_server))
socket.bind('tcp://*:{}'.format(self.recv_port))
while True:
# Wait for next request from client
message = socket.recv()
self.on_receive(message)
self.process(message)
self.send_ack(socket)

def start_sender(self, *args):
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect('tcp://{}:{}'.format(self.send_server, self.send_port))
self.send(socket)
self.on_ack(socket)

def send(self, socket, *args):
print('send is not implemented in {}'.format(self.__class__.__name__))
msg = 'no_implemented'
print('Sending message {} to server {}:{}'.format(msg, self.send_server, self.send_port))
socket.send_string(msg)


def on_ack(self, socket, *args):
print('on_ack is not implemented in {}'.format(self.__class__.__name__))
# Send acknowlede
# Get the reply.
response = socket.recv()
print('Received response: {}'.format(response))


45 changes: 45 additions & 0 deletions plugin_base/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import sys
sys.path.append('..')
import time
import zmq
import multiprocessing as mp
import configparser
from plugins.audio import AudioPlugin

# TODO: this should be a dynamic loader
__allowed_plugins__ = {
'audio': AudioPlugin
}
plugins = ['audio']

def load_plugins():
# Load plugins
loaded_plugins = []
for plugin in plugins:
# Read config
conf = configparser.ConfigParser()
conf.read('../conf/plugins.d/{}.ini'.format(plugin))
# Check enabled
if plugin not in __allowed_plugins__:
raise NotImplementedError
else:
# TODO: enable port conflict scan
p = __allowed_plugins__[plugin](conf)
loaded_plugins.append(p)
return loaded_plugins


def start_receiver_plugins(loaded_plugins):
# Execution
procs = []
for plugin in loaded_plugins:
p = mp.Process(target=plugin.start_receiver)
# Set as daemon, so it gets killed alongside the parent
p.daemon = False
p.start()
procs.append(p)
return procs

def send_messages(loaded_plugins):
for se in loaded_plugins:
se.start_sender()
Empty file added plugins/__init__.py
Empty file.
30 changes: 30 additions & 0 deletions plugins/audio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import sys
sys.path.append('..')
from network.messages import Messages
from audio.player import play_sound
from plugin_base.base import *

class AudioPlugin(ZmqBasePlugin):
def __init__(self, configuration):
print(configuration['Audio'])
self.audio_path = configuration['Audio']['Path']
self.streamer = configuration['Audio']['Streamer']
ZmqBasePlugin.__init__(self, configuration)

def process(self, msg):
if msg.decode('ascii') == str(Messages.WARN.value):
print('Playing warning')
play_sound('{}/warning.mp3'.format(self.audio_path), self.streamer)
elif msg.decode('ascii') == str(Messages.MUSIC.value):
print('Playing Music')
play_sound('{}/music.mp3'.format(self.audio_path), self.streamer)
else:
print('Can\'t parse message!')

def send_ack(self, socket, *args):
# Send acknowlede
socket.send(b'Ack')

def send(self, socket, *args):
msg = Messages.WARN
socket.send_string(str(msg.value))

0 comments on commit ba7f6a7

Please sign in to comment.