Skip to content

Commit

Permalink
Refact service layer for a separation of concern
Browse files Browse the repository at this point in the history
  • Loading branch information
stefets committed Feb 19, 2023
1 parent d1a5926 commit 32338ac
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 187 deletions.
49 changes: 22 additions & 27 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from werkzeug.exceptions import HTTPException

from blueprint.views import frontend
from services.dings import OscContext
from services.live import LiveContext


app = Flask(__name__)
Expand All @@ -37,15 +37,15 @@
thread = None
thread_lock = Lock()

''' Mididings OSC context '''
dings_context = None
''' Mididings and OSC context '''
live_context = None
if not app.debug or os.environ.get("WERKZEUG_RUN_MAIN") == "true":
dings_context = OscContext(
live_context = LiveContext(
configuration["osc_server"])


""" Bluebrint(s) """
app.config['dings_context'] = dings_context
app.config['live_context'] = live_context.scene_context
app.register_blueprint(frontend)


Expand Down Expand Up @@ -117,15 +117,9 @@ def api_restart():


def mididings_context_update():
dings_context.dirty = False
socketio.emit('mididings_context_update', {
'current_scene': dings_context.current_scene,
'current_subscene': dings_context.current_subscene,
'scenes': dings_context.scenes,
"scene_name" : dings_context.scene_name,
"subscene_name" : dings_context.subscene_name,
"has_subscene" : dings_context.has_subscene
})
live_context.set_dirty(False)
socketio.emit('mididings_context_update',
live_context.scene_context.payload)


@socketio.event
Expand Down Expand Up @@ -192,58 +186,59 @@ def sio_quit():
socketio.emit("on_terminate")



''' Mididings '''


def osc_observer_thread():
while True:
if dings_context.dirty:
if live_context.is_dirty():
mididings_context_update()
socketio.emit("on_start") if dings_context.running else socketio.emit("on_exit")
socketio.emit("on_start") if live_context.is_running(
) else socketio.emit("on_exit")
socketio.sleep(0.125)


''' API calls '''


def _quit():
dings_context.quit()
live_context.quit()


def _panic():
dings_context.panic()
live_context.panic()


def _query():
dings_context.query()
live_context.query()


def _restart():
dings_context.restart()
live_context.restart()


def _next_subscene():
dings_context.next_subscene()
live_context.next_subscene()


def _next_scene():
dings_context.next_scene()
live_context.next_scene()


def _prev_subscene():
dings_context.prev_subscene()
live_context.prev_subscene()


def _prev_scene():
dings_context.prev_scene()
live_context.prev_scene()


def _switch_scene(id):
dings_context.switch_scene(id)
live_context.switch_scene(id)


def _switch_subscene(id):
dings_context.switch_subscene(id)
live_context.switch_subscene(id)


'''
Expand Down
133 changes: 69 additions & 64 deletions src/blueprint/templates/ui.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ <h3>
<input class="btn btn-block btn-info" type="button" id="prev_scene" value="Previous Scene">
</div>
<div class="col-md-3">
<input class="btn btn-block btn-primary" type="button" id="prev_subscene" value="Previous Sub-Scene">
<input class="btn btn-block btn-primary" type="button" id="prev_subscene"
value="Previous Sub-Scene">
</div>
<div class="col-md-3">
<input class="btn btn-block btn-primary" type="button" id="next_subscene" value="Next Sub-Scene">
Expand All @@ -99,151 +100,155 @@ <h3>

{% endblock body %} {% block websockets %}

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.4.0/socket.io.js" integrity="sha512-nYuHvSAhY5lFZ4ixSViOwsEKFvlxHMU2NHts1ILuJgOS6ptUmAGt/0i5czIgMOahKZ6JN84YFDA+mCdky7dD8A==" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.4.0/socket.io.js"
integrity="sha512-nYuHvSAhY5lFZ4ixSViOwsEKFvlxHMU2NHts1ILuJgOS6ptUmAGt/0i5czIgMOahKZ6JN84YFDA+mCdky7dD8A=="
crossorigin="anonymous"></script>

<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$(document).ready(function () {

var socket = io()

socket.on('connect', function() {
socket.on('connect', function () {
socket.emit('sio_get_mididings_context');
$('#websockets').html('<div class="bg-success">ONLINE</div>');
$('#mididings').html('<div class="bg-success">RUNNING</div>');
});

socket.on('on_exit', function() {
socket.on('on_exit', function () {
$('#mididings').html('<div class="bg-danger">STOPPED</div>');
});

socket.on('on_start', function() {
socket.on('on_start', function () {
$('#mididings').html('<div class="bg-success">RUNNING</div>');
});

socket.on('on_terminate', function() {
socket.on('on_terminate', function () {
socket.disconnect();
$('#websockets').html('<div class="bg-danger">DISCONNECTED</div>');
$('#mididings').html('<div class="bg-danger">STOPPED</div>');
});

socket.on('mididings_context_update', function(ld) {

$('#current_scene').text(ld.current_scene);
$('#current_scene_name').text(ld.scene_name);

$('#current_subscene').text(ld.current_subscene);
$('#current_subscene_name').text(ld.subscene_name);

socket.on('mididings_context_update', function (context) {
//
// Redraw the content section
$('#scenes').text('');
$('#subscenes').text('');

var html = ""
var cpt_s = 0;
//
var count = 0;
var content = "";
var subscenes = [];

// Scenes
for (let scene_id in ld.scenes) {
for (let index in context.items) {

if (cpt_s % 2 == 0)
html += "<div class='row mt-1 mb-1'>";
var scene = context.items[index];

if (count % 2 == 0)
content += "<div class='row mt-1 mb-1'>";

// Button style
var button_class = "'btn btn-block switch_scene ";
if (parseInt(scene_id) == ld.current_scene)
if (scene.current)
{
$('#current_scene').text(scene.id);
$('#current_scene_name').text(scene.name);
button_class += "btn-light'";
subscenes = scene.subscenes;
}
else
button_class += "btn-dark'";

html += "<div class='col-md-6'>";
html += "<input class=" + button_class + " type='button' name='" + scene_id + "' value='" + ld.scenes[scene_id][0] + "'>"
html += "</div>";
content += "<div class='col-md-6'>";
content += "<input class=" + button_class + " type='button' name='" + scene.id.toString() + "' value='" + scene.name + "'>";
content += "</div>";

cpt_s++;
count++;

if (cpt_s % 2 == 0)
html += "</div>";
if (count % 2 == 0)
content += "</div>";
}
$("#scenes").append(html);
$('#scenes').text("");
$("#scenes").append(content);


// Subscenes
if (ld.has_subscene) {

cpt_s = 0;
html = "";
count = 0;
content = "";

for (let subscene_index in ld.scenes[ld.current_scene][1]) {
$('#current_subscene_name').text("...");
for (let index in subscenes) {

if (cpt_s % 4 == 0)
html += "<div class='row mt-1 mb-1'>";
var scene = subscenes[index];

var subscene_id = parseInt(subscene_index) + 1;
if (count % 4 == 0)
content += "<div class='row mt-1 mb-1'>";

// Button style
var button_class = "'btn btn-block switch_subscene ";
if (parseInt(subscene_id) == ld.current_subscene)
button_class += "btn-light'";
else
button_class += "btn-secondary'";

html += "<div class='col-md-3'>";
html += "<input class=" + button_class + " type='button' name='" + subscene_id.toString() + "' value='" + ld.scenes[ld.current_scene][1][subscene_index] + "'>"
html += "</div>";
// Button style
var button_class = "'btn btn-block switch_subscene ";
if (scene.current) {
button_class += "btn-light'";
$('#current_subscene').text(scene.id);
$('#current_subscene_name').text(scene.name);
}
else
button_class += "btn-secondary'";

cpt_s++;
content += "<div class='col-md-3'>";
content += "<input class=" + button_class + " type='button' name='" + scene.id.toString() + "' value='" + scene.name + "'>";
content += "</div>";

if (cpt_s % 4 == 0)
html += "</div>";
}
$('#subscenes').append(html);
count++;

if (count % 4 == 0)
content += "</div>";
}
$('#subscenes').text("");
$('#subscenes').append(content);

});

// Scene navigation delegate
$("#scenes").delegate("input.switch_scene", "click", function(event) {
$("#scenes").delegate("input.switch_scene", "click", function (event) {
return emit('sio_switch_scene', {
id: $(this).attr('name')
});
});

// Sub-Scene navigation delegate
$("#subscenes").delegate("input.switch_subscene", "click", function(event) {
$("#subscenes").delegate("input.switch_subscene", "click", function (event) {
return emit('sio_switch_subscene', {
id: $(this).attr('name')
});
});

$('#next_scene').click(function(event) {
$('#next_scene').click(function (event) {
return emit('sio_next_scene');
});

$('#prev_scene').click(function(event) {
$('#prev_scene').click(function (event) {
return emit('sio_prev_scene');
});

$('#next_subscene').click(function(event) {
$('#next_subscene').click(function (event) {
return emit('sio_next_subscene');
});

$('#prev_subscene').click(function(event) {
$('#prev_subscene').click(function (event) {
return emit('sio_prev_subscene');
});

$('#restart').click(function(event) {
$('#restart').click(function (event) {
return emit('sio_restart');
});

$('#panic').click(function(event) {
$('#panic').click(function (event) {
return emit('sio_panic');
});

$('#query').click(function(event) {
$('#query').click(function (event) {
return emit('sio_query');
});

$('#quit').click(function(event) {
$('#quit').click(function (event) {
return emit('sio_quit');
});

Expand Down
2 changes: 1 addition & 1 deletion src/blueprint/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

@frontend.route("/")
def index():
context = current_app.config['dings_context']
context = current_app.config['live_context']
return render_template('ui.html') if context.scenes else render_template('no_ui.html')
Loading

0 comments on commit 32338ac

Please sign in to comment.