-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91feab9
commit ba7f6a7
Showing
7 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ IP=192.168.1.240 | |
Port=5556 | ||
|
||
[Audio] | ||
Streamer=none | ||
Streamer=pygame | ||
Path=../audio_files | ||
|
||
[General] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |