Skip to content

Commit

Permalink
Added alarm
Browse files Browse the repository at this point in the history
  • Loading branch information
astagi committed Nov 6, 2012
1 parent 2e79b55 commit 6a9bc4c
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions synclock/synclock.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ def __init__(self, path = 'synclockalarm'):
self.getAlarm()

def setAlarm(self, h, m, on=True):
self.h = h
self.m = m
self.on = on
f = open(self.path, 'w')
f.write("%d:%d:%d" % (h, m, on))
f.close()
Expand All @@ -33,11 +30,20 @@ def getAlarm(self):
try:
f = open(self.path, 'r')
timestored = f.read()
(self.h, self.m, self.on) = timestored.split(":")
(h, m, on) = timestored.split(":")
f.close()
return (h, m, on)
except IOError:
self.setAlarm(0, 0, False)
return (self.h, self.m, self.on)
return (0, 0, False)

def equal(self, time_str):
(my_h, my_m) = time_str.split(":")
(h, m, on) = self.getAlarm()
if int(my_h) == h and int(my_m) == m:
return True
else:
return False

class StopThread(threading.Thread):

Expand Down Expand Up @@ -70,7 +76,7 @@ def elaborate(self):
port = server_sock.getsockname()[1]


advertise_service( server_sock, "SampleServer",
advertise_service( server_sock, "SynclockServer",
service_id = UUID,
service_classes = [ UUID, SERIAL_PORT_CLASS ],
profiles = [ SERIAL_PORT_PROFILE ] )
Expand All @@ -80,12 +86,8 @@ def elaborate(self):
client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info

#send h m and on to device
print self.ck.getAlarm()

#listen device
#get h, m and on

try:
while not self.stop:
data = client_sock.recv(3)
Expand All @@ -112,10 +114,25 @@ def elaborate(self):
try:
response = ntplib.NTPClient().request('europe.pool.ntp.org', version=3)
milltime = int(response.tx_time)
time_str = (datetime.fromtimestamp(milltime)).strftime('%H:%M')
if ck.equal(time_str):
PlayAlarmThread().start()
except ntplib.NTPException:
pass
time.sleep(1)

class PlayAlarmThread (StopThread):

def __init__(self):
StopThread.__init__(self)

def elaborate(self):
tone = Tone(4)
for i in range(5):
tone.play(Tone.NOTE_C4, 250)
Android.sleep(100)
self.stopMe()

class TemperatureThread (StopThread):

def __init__(self):
Expand Down

0 comments on commit 6a9bc4c

Please sign in to comment.