Skip to content

Commit

Permalink
First attempt to add an audio system with audio files and playlists. #42
Browse files Browse the repository at this point in the history
  • Loading branch information
theyosh committed Nov 5, 2017
1 parent f694f41 commit c8c468d
Show file tree
Hide file tree
Showing 16 changed files with 966 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ settings.cfg
history.db*
static/webcam/*.jpg
log/*.log*
audio/*
description.txt
weather.txt
Empty file added audio/.gitignore
Empty file.
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ apt-get -y autoremove
# Install required packages to get the terrarium software running
aptitude -y update
aptitude -y safe-upgrade
aptitude -y install libftdi1 screen python-imaging python-dateutil python-ow python-rpi.gpio python-psutil git subversion watchdog build-essential python-dev python-picamera python-opencv python-pip python-pigpio i2c-tools owfs ow-shell sqlite3
aptitude -y install libftdi1 screen python-imaging python-dateutil python-ow python-rpi.gpio python-psutil git subversion watchdog build-essential python-dev python-picamera python-opencv python-pip python-pigpio i2c-tools owfs ow-shell sqlite3 vlc-nox python-mediainfodll python-alsaaudio

# Basic config:
raspi-config
Expand Down
9 changes: 4 additions & 5 deletions start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ then
if (( SECONDS < RESTART_TIMEOUT )); then
# Restarted to soon... counting
RESTART_ATTEMPTS=$((RESTART_ATTEMPTS+1))
message "Restart counter: ${RESTART_ATTEMPTS}"
message "Restart counter: ${RESTART_ATTEMPTS}/${MAX_RESTARTS}"

if (( RESTART_ATTEMPTS > MAX_RESTARTS )); then
# To many errors. Rebooting
Expand All @@ -46,14 +46,13 @@ then
RESTART_ATTEMPTS=0
fi

# echo -n "$(date +"%Y-%m-%d %T,000") - WARNING - terrariumWrapper -
message "Restarting in ${RESTART_TIME} seconds after running for ${SECONDS} seconds. Press Ctrl+C now to terminate TerrariumPI."
for (( counter=1; counter<=${RESTART_TIME}; counter++ ))
for (( counter=${RESTART_TIME}; counter>0; counter-- ))
do
echo -n " ${counter},"
echo -n "${counter} "
sleep 1
done
echo " restart!"
echo "restart!"

# Restart PiGPIOd process....
sudo service pigpiod restart
Expand Down
9 changes: 9 additions & 0 deletions static/css/terrariumpi.css
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ div.row.door div.sidebar-widget i.fa {
-ms-filter: FlipV;
}

.dropzone {
min-height: auto !important;
}

table.dataTable .glyphicon-trash,
table.dataTable .glyphicon-play {
margin: 0.2em;
cursor: pointer;
}

/* Documentation */

Expand Down
101 changes: 99 additions & 2 deletions static/js/terrariumpi.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ function websocket_init(reconnect) {
case 'door_indicator':
update_door_indicator(data.data);
break;
case 'player_indicator':
update_player_indicator(data.data);
break;
case 'update_weather':
update_weather(data.data);
break;
Expand Down Expand Up @@ -218,6 +221,15 @@ function formatNumber(amount,minfrac,maxfrac) {
});
}

function formatBytes(bytes,decimals) {
if(bytes === 0) return '0 Bytes';
var k = 1024,
dm = decimals || 2,
sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
i = Math.floor(Math.log(bytes) / Math.log(k));
return formatNumber(parseFloat((bytes / Math.pow(k, i))),0,2) + ' ' + sizes[i];
}

function process_form() {
$('form').each(function() {
$(this).on('submit', function() {
Expand Down Expand Up @@ -251,7 +263,7 @@ function process_form() {
function prepare_form_data(form) {
var formdata = [];
var form_type = form.attr('action').split('/').pop();
var re = /(sensor|switch|webcam|light|sprayer|heater|cooler|door|profile)(_\d+)?_(.*)/i;
var re = /(sensor|switch|webcam|light|sprayer|heater|cooler|door|profile|playlist)(_\d+)?_(.*)/i;
var matches = null;
var objectdata = {};
var prev_nr = -1;
Expand All @@ -277,6 +289,7 @@ function prepare_form_data(form) {
case 'environment':
case 'webcams':
case 'doors':
case 'audio':
if ((matches = re.exec(field_name)) !== null) {
if (matches.index === re.lastIndex) {
re.lastIndex++;
Expand All @@ -300,7 +313,7 @@ function prepare_form_data(form) {
objectdata = {};
prev_nr = current_nr;
}
if (matches[3] === 'on' || matches[3] === 'off') {
if (matches[3] === 'on' || matches[3] === 'off' || matches[3] === 'start' || matches[3] === 'stop') {
field_value = moment(field_value, 'LT').unix();
}
objectdata[matches[3]] = field_value;
Expand Down Expand Up @@ -496,6 +509,14 @@ function update_online_messages(online) {
add_notification_message('online_messages', title, message, icon, color);
}

function update_player_messages(data) {
var title = (data.running ? '{{_('Playing')}}' : '{{_('Stopped')}}');
var message = (data.running ? '{{_('Playlist')}}: ' + data.name + ' - ' + moment(data.start * 1000).format('LT') + '-' + moment(data.stop * 1000).format('LT'): '{{_('Not playing')}}');
var icon = (data.running ? 'fa-play-circle-o' : 'fa-play-circle-o');
var color = (data.running ? 'green' : 'red');
add_notification_message('player_messages', title, message, icon, color);
}

function add_notification_message(type, title, message, icon, color, date) {
var notification_date = new Date().getTime();
if (date != undefined) {
Expand Down Expand Up @@ -581,6 +602,18 @@ function door_closed() {
update_door_messages(false);
}

function update_player_indicator(data) {
var player_indicator = $('a#player_indicator');
if (data.running) {
player_indicator.find('span.running').show();
player_indicator.find('span.stopped').hide();
} else {
player_indicator.find('span.running').hide();
player_indicator.find('span.stopped').show();
}
update_player_messages(data);
}

function get_theme_color(color) {
if (color == 'orange') return '#f0ad4e';
return $('<div>').addClass(color).css('color');
Expand Down Expand Up @@ -1324,6 +1357,69 @@ function load_door_history() {
});
}

function load_player_status() {
$.getJSON('/api/audio/playing', function(player_data) {
update_player_indicator(player_data);
});
}

function delete_audio_file(audio_file_id, audio_file_name) {
if (confirm('Are you sure to delete the file: \'' + audio_file_name + '\' ?')) {
$.ajax({
url: "/api/audio/file/" + audio_file_id,
type: "DELETE",
dataType : "json",
}).done(function(response) {
new PNotify({
type: (response.ok ? 'success' : 'error'),
title: response.title,
text: response.message,
nonblock: {
nonblock: true
},
delay: 3000,
mouse_reset: false,
//addclass: 'dark',
styling: 'bootstrap3',
hide: true,
});
});
}
}

function add_audio_playlist() {
var form = $('.new-playlist-form');
if (!check_form_data(form)) return false;

add_audio_playlist_row('None',
form.find('input[name="playlist_[nr]_name"]').val(),
form.find('input[name="playlist_[nr]_start"]').val(),
form.find('input[name="playlist_[nr]_stop"]').val(),
form.find('input[name="playlist_[nr]_volume"]').val(),
form.find('select[name="playlist_[nr]_files"]').val());

$('.new-playlist-form').modal('hide');
}

function add_audio_playlist_row(id,name,start,stop,volume,files) {
var audio_playlist_row = $($('.modal-body div.row.playlist').parent().clone().html().replace(/\[nr\]/g, $('form div.row.playlist').length));
audio_playlist_row.find('.x_title').show().find('h2 small').text(name);
audio_playlist_row.find('span.select2.select2-container').remove();

audio_playlist_row.find('input, select').each(function(counter,item){
$(item).val(eval($(item).attr('name').replace(/playlist_[0-9]+_/g,'')));
});

audio_playlist_row.insertBefore('div.row.submit').show();
reload_reload_theme();

audio_playlist_row.find("select").select2({
placeholder: '{{_('Select an option')}}',
allowClear: false,
minimumResultsForSearch: Infinity
});
}

function capitalizeFirstLetter(string) {
return string[0].toUpperCase() + string.slice(1);
}
Expand Down Expand Up @@ -1485,6 +1581,7 @@ $(document).ready(function() {
}).appendTo("body");

load_door_history();
load_player_status();
load_page('dashboard.html');

setInterval(function() {
Expand Down
Loading

0 comments on commit c8c468d

Please sign in to comment.