This commit is contained in:
antifaz 2013-09-13 12:51:16 +02:00
parent a3086b4a63
commit a3b85772a4
2 changed files with 77 additions and 338 deletions

View file

@ -1,329 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title> TechREC </title>
<script src="jquery-1.9.1.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<script>
var txt_start = "Inizia";
var txt_stop = "Ferma";
var txt_download = "Scarica";
var srvaddr = "http://127.0.1.1:8000/";
var almostone = false;
var noplusbotton = true;
var maxrec = 0 ;
var rec_name_default = "";
/** Perform Ajax async loading **/
function async_load( destdiv, uri, postdata) {
postdata = postdata || '';
console.log("[ASYNC] " + destdiv + " <- " + uri)
console.log( postdata );
$(destdiv).html("\<div class=\"imageloader\"\> \<img src=\"img/ajax-loader.gif\" /\> \</div\>");
console.log("Tolta uri" + uri + "NOW -> http://127.0.1.1:8000/sampleJSON");
var request = $.ajax( { url: "http://127.0.1.1:8000/sampleJSON",
type:"POST",
data: postdata,
dataType: 'json',
success : function( msg ) {
$(".imageloader").remove();
resmsg = "["+jqXHR.status+"]";
alert("Ma ci sei ?");
$(destdiv).append( resmsg );
},
error: function(xhr) {
var errmsg = "";
console.log("Errore async ajax: " + xhr.status );
switch( xhr.status )
{
case 0: errmsg="[0] Network Problem."; break;
case 404: errmsg="[404] Requested page not found."; break;
case 500: errmsg="[500] Internal Server Error."; break;
default: errmsg="[ErrCode: "+xhr.status+"] Error uncatched!"; break;
}
console.log("ErrMSG" + errmsg);
$(".imageloader").remove();
//$(destdiv).html("Err: "+ errmsg);
alert("Err: "+ errmsg);
}
}
).always(function( jqXHR ) {
$(".imageloader").remove();
resmsg = "ALSWAST["+jqXHR.status+"]";
$(destdiv).append( resmsg );
}
).complete(
function (jqXHR, msgstatus) {
alert("Comlpete " +jqXHR + " - " + msgstatus);
}
);
}
function rs_button( code ) { return code; }
function rs_trxarea( code ) { return "recarea-"+code; }
function rs_trxname( code ) { return code+"_name"; }
function rs_buttonarea( code ) { return code+"_buttonarea"; }
function rs_inputstart( code ) { return code+"_startime"; }
function rs_inputend( code ) { return code+"_endtime"; }
function rs_formid(code) { return "form-"+code; }
function rs_id(code) { return code; }
function rs_log(code) { return code + "_log"; }
/** New record **/
function rec_new( ) {
console.log("Nuova");
var recid = "rec-"+maxrec;
maxrec += 1; // increment number of active record
console.log("AGGIUNTO AL MAIN CONTAINER");
$("#buttonscontainer").append( "<div id=\""+rs_trxarea(recid)+"\" class=\"recarea\"> </div>" );
$("#"+rs_trxarea(recid)).append( "<div id=\""+rs_buttonarea(recid)+"\" class=\"buttonarea\"> </div>" );
// POPULATE BUTTON AREA
// var str = "\<input type=\"image\" src=\"img/button.jpg\"id=\""+rs_button(recid)+ "\" name=\""+rs_button(recid)+"\" ";
var formid = rs_formid( recid );
var str = "<form id=\""+formid+"\">";
str = str + "\<input type=\"submit\" name=\""+rs_button(recid)+"\" id=\""+rs_button(recid)+"\" ";
str = str + " class=\"recbutton\" value=\"Inizia\" /\>";
str = str + "</form>";
$("#"+formid).attr("action","#") .attr("method","post") //set the form attributes
$("#"+rs_buttonarea(recid)).html( str );
// DELETE BUTTON (NO FIRST ELEMENT)
if ( recid > 0 ){
$("#"+rs_buttonarea(recid)).append( "\<div class=\"dellink\" \> <a href=\"#\"> cancella</a> \</div\>" );
}
// INSERT AND POPULATE BUTTON AREA
$("#"+rs_trxarea(recid)).append( "<div id=\""+rs_log(recid)+"\" class=\"logarea\">Nuova trasmissione </div>" );
// BINDING CLICK TO FUNCTION
// $("#"+rs_button(recid) ).click(function(){
$("#"+rs_button(recid) ).submit(function(){
rec_changestate( rs_button(recid) );
});
}
function download( eid ){
alert("DEntro download");
var trxnameobj = document.getElementById( rs_trxname(eid) );
var trxname = trxnameobj.value;
var starttime = $("#"+rs_inputstart(eid)).val();
var endtime = $("#"+rs_inputstart(eid)).val();
var jsondata = {"name":'"'+trxname+'"', "starttime":'"'+ starttime +'"', "endtime":'"'+ endtime +'"'};
async_load( destdiv, srvaddr, jsondata);
alert("FINE download");
}
function rec_changestate( eid ) {
console.log("[CS] change state for "+eid+"");
var recbutton = document.getElementById( eid );
var trxnameobj = document.getElementById( rs_trxname(eid) );
if (recbutton == null) {
alert("Impossibile trovare ID");
return;
}
console.log("[CS] "+eid+" == " +recbutton.value);
/***
Simple FSM - Finite State Machine
***/
console.log("RECBUTTON value :"+recbutton.value);
//console.log("TXT " + txt_star.toUpperCase() );
if ( recbutton.value.toUpperCase() == txt_start.toUpperCase() ) {
/*
In this case, user presses START
*/
var myDate = new Date();
var displayDate = (myDate.getFullYear()+1) + '/' + (myDate.getMonth()+1) + '/' + myDate.getDate();
displayDate = displayDate +' '+ myDate.getHours()+':'+myDate.getMinutes()+':'+myDate.getSeconds();
var str = "\<input type=\"text\" id=\""+rs_trxname(eid)+"\" placeholder=\"Nome trasmissione\" /\>";
str = str + "\<input type=\"hidden\" id=\""+rs_inputstart(eid)+"\" value=\""+displayDate+"\" /\>";
//$("#"+rs_buttonarea(eid)).append( str );
var formid = rs_formid( eid );
$("#"+formid).append(str)
console.log("[CS] Update log div (" + rs_log(eid) +")");
$("#"+rs_log(eid)).append("<br /\>Inizio: "+displayDate);
console.log("[CS] change text for "+eid);
recbutton.value = txt_stop;
console.log("[CS] change color for area "+eid);
$("#"+ rs_trxarea(eid) ).css("background-color","red");
almostone = true;
} else if ( recbutton.value.toUpperCase() == txt_stop.toUpperCase() ) {
/*
In this case, user presses STOP
*/
console.log("[CS] "+eid+" == " +txt_stop);
var myDate = new Date();
var displayDate = (myDate.getMonth()+1) + '/' + (myDate.getDate()) + '/' + myDate.getFullYear();
displayDate = displayDate +' '+ myDate.getHours()+':'+myDate.getMinutes()+':'+myDate.getSeconds();
var str = "\<input type=\"hidden\" id=\""+rs_inputend(eid)+"\" value=\""+displayDate+"\" /\>";
$("#"+rs_trxarea(eid)).append( str );
$("#"+rs_log(eid)).append("<br /\>Fine: "+displayDate);
recbutton.type = "submit";
recbutton.value = txt_download;
$("#"+eid).click(function(e){
e.preventDefault();
dataString = $("#JqAjaxForm").serialize();
$.ajax({
type: "POST",
url: "http://127.0.1.1:8000/sampleJSON",
data: {"value":"value"},
dataType: "json",
success: function(data) {
alert("QUI"+ data);
},
error: function (x) {
alert("error" + x.status);
}
});
});
$("#"+ rs_trxarea(eid)).css("background-color","green");
console.log("EID" + eid);
$("#"+ rs_trxarea(eid)).append( "<a href=\"#\" id=\"but-down-"+eid+"\" onclick=\"Javascript:download("+eid+");\") >SCARICA LINK</a>" );
/*$("#but-down-"+eid).click(function(){
var trxname = trxnameobj.value;
var starttime = $("#"+rs_inputstart(eid)).val();
var endtime = $("#"+rs_inputstart(eid)).val();
var jsondata = {"name":'"'+trxname+'"', "starttime":'"'+ starttime +'"', "endtime":'"'+ endtime +'"'};
async_load( destdiv, srvaddr, jsondata);
});*/
} else if ( recbutton.value.toUpperCase() == txt_download.toUpperCase() ) {
/* In this case, user presses DOWNLOAD
Save name and block the record. No operations are (more) allowed.
*/
console.log("[CS] "+eid+" == " +txt_download);
console.log("[CS] GetID"+ eid+ "_name");
var trxname = trxnameobj.value;
var starttime = $("#"+rs_inputstart(eid)).val();
var endtime = $("#"+rs_inputstart(eid)).val();
console.log("[CS] ID = "+eid+"_name ==> "+trxname ) ;
console.log("[CS] "+trxname+" != "+rec_name_default);
if (trxname == rec_name_default) {
trxname = prompt("Inserire il nome della registrazione per salvarla", "");
// check if it is empty or null
while ( ( $.trim(trxname) == "") || (trxname == null) ) {
trxname = prompt("Non posso registrare senza un nome!", "");
}
/* no possible !!! */
if ( $.trim(trxname) == "") {
alert("Sei riuscito/a a registrare senza dare un nome.. pensi di farla franca '??????"+
"techbl*c contro il lampo negli occhi della protoscimmia");
}
trxnameobj.setAttribute("value", trxname);
}
console.log( "Recarea "+rs_trxarea(eid) +" -> " + $("#"+rs_trxarea(eid)).innerHTML );
$("#"+rs_trxarea(eid)).css("background-color","#ccc");
// TODO: fare i controlli su starttime, endtime
var jsondata = {"name":'"'+trxname+'"', "starttime":'"'+ starttime +'"', "endtime":'"'+ endtime +'"'};
destdiv = "#"+rs_log(eid)
console.log( destdiv +" <- " + srvaddr + "\n" + jsondata );
async_load( destdiv, srvaddr, jsondata);
}
// if there is almost one button and the PLUSBOTTON has previously insered
if (almostone == true && noplusbotton == true) {
$("#addnewrecarea").prepend( "<a href=\"#\" id=\"newrecbutton\">+ nuova</a>" );
$("#newrecbutton").click(function(){
rec_new();
});
noplusbotton = false;
}
}
/* GESTIONE CAMBIO DI STATO */
$(document).ready(function(){
rec_new();
});
</script>
</head>
<body>
<!--
QUI ANDREBBE INSERITA UNA LOAD DEI DATI DAL DATABASE PER REDERE
L'INTERFACCIA COERENTE CON LA BASE DI DATI
-->
<div id="pagecontainer">
<!-- <form action="#" method="POST"> -->
<h1> techr*c </h1>
<h2> <a href="tempo.php"> macchina del tempo</a> </h2>
<div id="buttonscontainer"> </div>
<div id="addnewrecarea"> </div>
<!-- </form>-->
</div>
<div id="footer">
techbl*c
</div>
</body>
</html>

View file

@ -33,13 +33,6 @@ DONE = 4
PAGESIZE = 10
# Job in thread
class RecJob():
def __init__(self, rec):
print "Estraggo %s Start:%s, End:%s" % (rec.name, rec.starttime, rec.endtime)
def run(self):
pass
class Rec(Base):
@ -163,14 +156,89 @@ def printall( queryres ):
for record in queryres:
print "R: %s" % record
# Job in thread
class RecJob():
def __init__(self, rec):
print "Estraggo %s Start:%s, End:%s" % (rec.name, rec.starttime, rec.endtime)
self.fdir = "/rec/ror/"
self.fnameformat = "ror-%Y-%m-%d-%H-00-00.mp3"
self.name = rec.name
self.starttime = rec.starttime
self.endtime = rec.endtime
def run(self):
if type(self.starttime) != type(datetime.datetime.now()):
logging.info("Starttime format error")
return
if type(self.endtime) != type(datetime.datetime.now()):
logging.info("Endtime format error")
return
if self.starttime >= self.endtime:
logging.info("Starttime > Endtime (%s > %s)" % (self.starttime,self.endtime) )
return
"""print self.starttime
print self.starttime + datetime.timedelta(minutes=10)
print self.starttime.year
print self._truncate(self.starttime)
self.starttimetrunc = self._truncate(self.starttime)
print self.starttime - self.starttimetrunc
print "preleva da file "
print "Opt: %s => %s" % ( self.starttime, self.endtime )
print self._get_recfile(self.starttime)
"""
start = self.starttime
end = self.endtime
app = self.starttime
while True:
print
print "**** From file %s take:" % ( self._get_recfile(start) )
nexth = self._truncate(start) + datetime.timedelta(minutes=60)
if start > self._truncate(start):
print "FROM: %s for %s seconds" % (start - self._truncate(start), nexth - start )
if end < self._truncate(nexth):
print "FROM: %s for %s seconds" % (0, end - self._truncate(start) )
else:
print "FROM: %s for 0 to 60." % (self._get_recfile(start))
if nexth >= end:
print "FINITO"
print "Start ", start, " end: ", end
break;
start = nexth
def _truncate(self, mytime):
return datetime.datetime(mytime.year,mytime.month,mytime.day,mytime.hour)
def _get_recfile(self, mytime):
return "%s/%s" % (self.fdir,mytime.strftime(self.fnameformat))
def __repr__(self):
return "%s: %s (%s) => %s (%s)" % ( self.name, self.starttime, type(self.starttime) ,self.endtime, type(self.endtime))
"""
TEST
"""
if __name__ == "__main__":
db = RecDB()
_mytime = datetime.datetime(2014,05,24,15,12,17)
_endtime = datetime.datetime(2014,05,24,17,45,17)
a = Rec(name="Mimmo1", starttime=_mytime, endtime=_endtime)
j = RecJob( a )
# print (j)
j.run()
sys.exit("End test job")
_mytime = datetime.datetime(2014,05,24,15,16,17)
a = Rec(name="Mimmo1", starttime=_mytime, endtime=None)
# a = Rec(name="Mimmo1", starttime=_mytime, endtime=None)
print "Aggiunto", db.add( a )
printall( db.get_all(page_size=5,page=0) )