Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions pyalarm/alarm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import sys

from flask import Flask, request
app = Flask(__name__)

from datetime import datetime

import threading
import time
import subprocess

import json
import copy

# from threading import Thread as Zeka

params = {
'jacina': 0.4,
'vreme': 11*3600 + 8*60 + 25, # sekunde
'trajanje': 20, # sekunde
'tip': 'zvuk',
'sablon': 'beskonacno',
}

# from collections import namedtuple
# class Params(namedtuple('Params', [
# 'jacina',
# 'vreme',
# 'trajanje',
# 'tip',
# 'sablon',
# ])): pass
# config = Params(**params)
# print config
# config2 = Params(jacina=0.4, vreme=40105, trajanje=20, tip='zvuk', sablon='beskonacno')
# print config == config2
# print config._asdict()
# import json
# print json.dumps(config._asdict())
# sys.exit()


def calc_time():
h = int(params['vreme']/3600)
m = (params['vreme'] % 3600) / 60
s = params['vreme'] % 60
return (h, m, s)

@app.route("/")
def index():
h, m, s = calc_time()
return r'''
<pre>
jacina: %.0f%%
vreme : %02d:%02d:%02d
tip : %s
sablon: %s
</pre>
''' % (params['jacina']*100, h, m, s,
params['tip'], params['sablon'])

@app.route("/get_params")
def get_params():
d = copy.copy(params)
d['vreme_comps'] = list(calc_time())
return json.dumps(d)
# return r'''
# {"jacina": %r,
# "vreme": %r,
# "tip": %r,
# "sablon": %r
# }
# ''' % (params['jacina'], params['vreme'],
# params['tip'], params['sablon'])

@app.route("/set_params", methods=['POST'])
def set_params():
global params
d = request.get_json()
print type(d), d
params = d
return 'OK'

@app.route("/ui.html")
def ui_html(): return open('ui.html').read()

@app.route("/jquery-2.1.1.min.js")
def jquery_js(): return open('jquery-2.1.1.min.js').read()

# @app.route("/page2/<param>")
# def page2tttt(param):
# return 'ovo je test stranica 2: ' + param

def current_ts():
d = datetime.now()
t = d.time()
ts = t.hour*3600 + t.minute*60 + t.second
return ts

def alarm_should_be_on():
ts = current_ts()
return params['vreme'] <= ts < params['vreme'] + params['trajanje']

proc = None

def start_alarm():
global proc
proc = subprocess.Popen(['./play.sh'])

def stop_alarm():
global proc
proc.kill()
proc = None

def alarm_activator():
while True:
should = alarm_should_be_on()
active = proc != None

# print (should, active)

# if (should, active) == (False, False):
# pass
# elif (should, active) == (False, True):
# # stop
# elif (should, active) == (True, False):
# # start
# elif (should, active) == (True, True):
# pass

if not active and should:
start_alarm()
elif active and not should:
stop_alarm()

time.sleep(1.0)

if __name__ == "__main__":
#params['vreme'] = current_ts() + 10

# thr = threading.Thread(target=alarm_activator)
# thr.setDaemon(True)
# thr.start()

app.run(debug=True)
4 changes: 4 additions & 0 deletions pyalarm/jquery-2.1.1.min.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions pyalarm/play.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

while true; do
aplay -q /usr/share/sounds/alsa/Front_Center.wav
done

81 changes: 81 additions & 0 deletions pyalarm/ui.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<html>
<head>
<script src="/jquery-2.1.1.min.js"></script>
<style>
#jacina_div input { width: 50px }
#time_div input { width: 30px }
#trajanje_div input { width: 30px }
div { margin: 20px }
</style>
</head>
<body onload="on_load()">
<div id="jacina_div">
Jacina:
<input type="button" value="-" onclick="promeni_jacinu(-10)"></input>
<span id="jacina">nn%</span>
<input type="button" value="+" onclick="promeni_jacinu(+10)"></input>
</div>
<div id="time_div">
Vreme:
<input id="vreme_h" value="hh" maxlength="2" onchange="read_params()"></input>:<input id="vreme_m" value="mm" maxlength="2" onchange="read_params()"></input>:<input id="vreme_s" value="ss" maxlength="2" onchange="read_params()"></input>
</div>
<div id="trajanje_div">
Trajanje:
<input id="trajanje" value="nn" maxlength="2" onchange="read_params()"></input>m
</div>

<input type="button" value="STOP" onclick="stopiraj_alarm()" style="font-size: 40px"></input>

<script>


params = {};

function update_ui() {
$('#jacina').text(Math.round(params.jacina*100)+'%');
$('#vreme_h').attr('value', params.vreme_comps[0]);
$('#vreme_m').attr('value', params.vreme_comps[1]);
$('#vreme_s').attr('value', params.vreme_comps[2]);
$('#trajanje').attr('value', params.trajanje);
}

function read_params() {
var h = Number($('#vreme_h').val());
var m = Number($('#vreme_m').val());
var s = Number($('#vreme_s').val());
params.vreme = h*3600 + m*60 + s;
var t = Number($('#trajanje').val());
params.trajanje = t;
//console.log([h, m, s, t]);
send_params();
}

function on_load() {
$.getJSON("/get_params", function (d) {
// console.log(d);
params = d;
update_ui();
});
}

function promeni_jacinu(diff) {
params.jacina += diff/100;
if (params.jacina > 1.0) params.jacina = 1.0;
if (params.jacina < 0.0) params.jacina = 0.0;
update_ui();
send_params();
}

function send_params() {
$.ajax({
type: "POST",
url: '/set_params',
data: JSON.stringify(params),
contentType:"application/json; charset=utf-8"
});
}

</script>

</body>
</html>
Binary file added snimci za py alarm/01.mp3
Binary file not shown.
Binary file added snimci za py alarm/02.mp3
Binary file not shown.
Binary file added snimci za py alarm/04.mp3
Binary file not shown.
Binary file added snimci za py alarm/05.mp3
Binary file not shown.
Binary file added snimci za py alarm/06.mp3
Binary file not shown.
Binary file added snimci za py alarm/07.mp3
Binary file not shown.
Binary file added snimci za py alarm/08.mp3
Binary file not shown.
Binary file added snimci za py alarm/09.mp3
Binary file not shown.
Binary file added snimci za py alarm/10.mp3
Binary file not shown.
Binary file added snimci za py alarm/11.mp3
Binary file not shown.
2 changes: 1 addition & 1 deletion temperature-monitoring/openwrt/root/send-temps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cd /root
sleep 30

while true; do
wget -q 'http://stark-savannah-9456.herokuapp.com/add?temp='`digitemp_DS9097 -a | tail -n1 | cut -d' ' -f7`
wget -q -O - 'http://'`cat auth.txt`'@stark-savannah-9456.herokuapp.com/add?temp='`digitemp_DS9097 -a | tail -n1 | cut -d' ' -f7`
sleep 600
done

Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
"moat.middleware.MoatMiddleware",
)

ROOT_URLCONF = 'gettingstarted.urls'
Expand Down Expand Up @@ -102,4 +103,8 @@

STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
)

MOAT_ENABLED = True
MOAT_DEBUG_DISABLE_HTTPS = True
MOAT_ALWAYS_ALLOW_URLS = ['^/get.*$']
1 change: 1 addition & 0 deletions temperature-monitoring/tempmon-webapp/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ gunicorn==19.0.0
psycopg2==2.5.3
static3==0.5.1
wsgiref==0.1.2
git+https://github.com/arnaudlimbourg/django-moat#egg=django-moat
14 changes: 12 additions & 2 deletions workshop.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ def python_workshop(date):
'29.09.2014': 'Radili domaci wordcount.py, regularne izraze, poceli rekurzivne funkcije, puskice :)',
'06.10.2014': 'Radili OS module, igrali se sa fajlovima i direktorijumima',
'13.10.2014': 'Ponovili OS module, OOP, poceli obradjivati klase, objekte i metode',
'20.10.2014': 'Obnova gradiva, usli smo u sustinu, prakticirali sa pythonom pisali kod za idea_selection'
'20.10.2014': 'Obnova gradiva, usli smo u sustinu, prakticirali sa pythonom pisali kod za idea_selection',
'27.10.2014': 'Obnovili gradivo, testirali idea_selection program koji nam je odabrao prvi eko-elektro projekat: ocitavanje temperature u labu i njeno daljinsko menjanje',
'03.11.2014': 'Povezali tem. senzor na openwrt ruter, pisali i postavili python web aplikaciju na heroku sve komitovali na git kao i obicno :)',
'10.11.2014': 'Obnovili gradivo, Instalirali digitemp i drajvere za usb-serial, omogucili da ruter otvorene mreze podatke salje na heroku',
'17.11.2014': 'Debagovali kod, radili kalibraciju, rekurzija, dotakli lisp i haskel, plesali sa algoritmom neutralnog broja',
'24.11.2014': 'Obnova gradiva, flask, dekoratori, plan alarmnog sistema + poceli skripting, (jacina, vreme, tip, shablon)',
'01.12.2014': 'Implementacija alarma, muzicka python radionica',
'15.12.2014': 'Magic metode, OrderedDicts, Monoidi',
'12.01.2015': 'Nastavak rada na RPi py alarmu, python u harmoniji sa francuskim jezikom',
'19.01.2015': 'Py alarm - doradjivanje funkcionalnosti, heroku fix ciscenje baze - generalnno sredjivanje pred buru',
'26.01.2015': '',
}
if 0:
for k,v in d.items():
for k,v in d.items():
print (k,':', v)
if 1:
for k in d:
Expand Down