add serial port reading
[scannr.git] / scannr.py
blob:a/scannr.py -> blob:b/scannr.py
import logging import logging
logging.basicConfig(level=logging.DEBUG, logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s\t%(levelname)s\t%(message)s') format='%(asctime)s\t%(levelname)s\t%(message)s')
   
import snd import snd
import time import time
from datetime import date from datetime import date
import threading import threading
from pydispatch import dispatcher from pydispatch import dispatcher
import wave import wave
import serial import serial
#python -m serial.tools.miniterm -p COM20 -e -b 115200 --cr #python -m serial.tools.miniterm -p COM20 -e -b 115200 --cr
  import psycopg2
  import csv
  import sys,os
   
   
  sys.path.insert(0, os.path.join(os.path.dirname(__file__) or '.', 'pynma'))
  import pynma
   
filename = "demo.wav" filename = "demo.wav"
MIN_LENGTH = 90000 MIN_LENGTH = 90000
   
def worker(filename):  
  def worker(filename, length):
   
"""thread worker function """thread worker function
http://www.doughellmann.com/PyMOTW/threading/ http://www.doughellmann.com/PyMOTW/threading/
https://github.com/uskr/pynma https://github.com/uskr/pynma
ffmpeg -i 2012-09-29-1348911268.34-demo.wav -ar 8000 -ab 4.75k test.3gp ffmpeg -i 2012-09-29-1348911268.34-demo.wav -ar 8000 -ab 4.75k test.3gp
http://stackoverflow.com/questions/2559746/getting-error-while-converting-wav-to-amr-using-ffmpeg http://stackoverflow.com/questions/2559746/getting-error-while-converting-wav-to-amr-using-ffmpeg
""" """
print 'Worker for '+filename print 'Worker for '+filename
ser.write("GLG\r") ser.write("GLG\r")
line = ser.readline() # read a '\n' terminated line line = ser.readline() # read a '\n' terminated line
print line print line
  reader = csv.reader([line])
  for row in reader:
  #GLG,40078,NFM,0,0,CanberraBlackMnt,AustralianCapita,SES Ops 1,1,0,NONE,NONE,NONE
  if (row[0] != 'GLG'):
  print "uh oh"
  if (row[1] != ''):
  tgid = row[1]
  #nma.push("scannr", "ping", filename, "http://www.google.com")
  tgname = row[7]
  sitename = row[5]
  """http://initd.org/psycopg/docs/usage.html"""
  cur = conn.cursor()
  cur.execute("INSERT INTO recordings (filename,tgid,tgname,sitename,length) VALUES (%s, %s,%s, %s, %s)",(filename,tgid,tgname,sitename, length))
  conn.commit()
  cur.close()
  else:
  print filename+" has no TGID"
   
return return
   
def filenameMaker(): def filenameMaker():
global filename global filename
filename = date.today().isoformat()+'-'+str(time.time())+'-demo.wav' filename = date.today().isoformat()+'-'+str(time.time())+'-demo.wav'
   
def record_to_async_file(): def record_to_async_file():
"Records from the microphone and outputs the resulting data to `path`" "Records from the microphone and outputs the resulting data to `path`"
sample_width, data = snd.record() sample_width, data = snd.record()
print str(len(data)) print str(len(data))
if len(data) > MIN_LENGTH: if len(data) > MIN_LENGTH:
data = snd.pack('<' + ('h'*len(data)), *data) data = snd.pack('<' + ('h'*len(data)), *data)
path = filename path = filename
dispatcher.send( signal='FILE_CREATED', sender=filename, filename=filename) dispatcher.send( signal='FILE_CREATED', sender=filename, filename=filename, length=len(data))
wf = wave.open(path, 'wb') wf = wave.open(path, 'wb')
wf.setnchannels(1) wf.setnchannels(1)
wf.setsampwidth(sample_width) wf.setsampwidth(sample_width)
wf.setframerate(snd.RATE) wf.setframerate(snd.RATE)
wf.writeframes(data) wf.writeframes(data)
wf.close() wf.close()
print("done - result "+str(len(data))+" frames written to "+path) print("done - result "+str(len(data))+" frames written to "+path)
   
dispatcher.connect( filenameMaker, signal='SND_STARTED', sender=dispatcher.Any ) dispatcher.connect( filenameMaker, signal='SND_STARTED', sender=dispatcher.Any )
dispatcher.connect( worker, signal='FILE_CREATED', sender=dispatcher.Any ) dispatcher.connect( worker, signal='FILE_CREATED', sender=dispatcher.Any )
   
  print "Opening serial port..."
ser = serial.Serial('COM20', 112500, timeout=1) ser = serial.Serial('COM20', 112500, timeout=1)
  print "Loading notifymyandroid..."
print "Scannr started..." nma = pynma.PyNMA( "a6f50f76119eda33befe4325b4b9e1dd25eef7bad2868e4f")
  print "Connecting database..."
  conn = psycopg2.connect("dbname=scannr user=postgres password=snmc")
  print "Scannr started."
while True: while True:
print "ready to record" print "ready to record again"
record_to_async_file() record_to_async_file()
ser.close() ser.close()