1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| #!/usr/bin/env python2.7
import sys
import os
import subprocess
import re
import signal
import MySQLdb
import threading
import time
##########################################################################
# class
##########################################################################
class watchSerial(threading.Thread):
''' thread init '''
def __init__(self, name = ''):
threading.Thread.__init__(self)
self.name = name
self._stopevent = threading.Event()
''' main process of the thread '''
def run(self):
while not self._stopevent.isSet():
conn = connectDB()
results = execSQL(conn,'SELECT * FROM evt WHERE type="Receive" ORDER BY tstamp')
for row in results:
idEvt = row["idEvt"]
action = row["action"]
hardName = row["hardName"]
idHard = row["idHard"]
hardKey = row["hardKey"]
serial = row["serial"]
cmd = row["cmd"]
print "[" + str(serial) + "] found evt " + action
print "[" + str(serial) + "] Deleting evt " + str(idEvt)
execSQL(conn,'DELETE FROM evt WHERE idEvt=' + str(idEvt))
closeDB(conn)
''' stop the thread '''
def stop(self):
self._stopevent.set()
class watchEvt(threading.Thread):
''' thread init '''
def __init__(self, name = ''):
threading.Thread.__init__(self)
self.name = name
self._stopevent = threading.Event()
''' main process of the thread '''
def run(self):
while not self._stopevent.isSet():
conn = connectDB()
results = execSQL(conn,'SELECT * FROM evt WHERE type="Send" OR type="Ring" OR type="Sys" ORDER BY tstamp')
for row in results:
idEvt = row["idEvt"]
action = row["action"]
hardName = row["hardName"]
idHard = row["idHard"]
hardKey = row["hardKey"]
serial = row["serial"]
cmd = row["cmd"]
if (action == "ring"):
print "[" + str(serial) + "] found evt " + action
res = execSQL(conn,'SELECT * FROM conf WHERE idConf=1')
for conf in res:
appPath = conf['appPath']
ringFile = conf['ringFile']
ring(appPath,ringFile,serial)
if (action == "sys"):
print "[" + str(serial) + "] found evt " + action
execSys(cmd,serial)
print "[" + str(serial) + "] Deleting evt " + str(idEvt)
execSQL(conn,'DELETE FROM evt WHERE idEvt=' + str(idEvt))
closeDB(conn)
''' self._stopevent.wait(1.0) '''
''' stop the thread '''
def stop(self):
self._stopevent.set()
##########################################################################
# functions
##########################################################################
''' execute a system command on host '''
def execSys(sysCmd,serial):
print "[" + str(serial) + "] Executing sys cmd: " + str(sysCmd)
os.system(sysCmd)
''' execute ring bell '''
def ring(appPath,ringFile,serial):
conf = appPath + "/config/mplayer.conf"
file = appPath + "/ringtones/" + ringFile;
ret = subprocess.Popen("ps -aef | grep mplayer | grep -v grep | awk '{print $2}'", shell=True, stdout=subprocess.PIPE)
ps = ret.stdout.read() . rstrip('\n')
ret.stdout.close()
val = re.search(r"(\d+)", ps)
if val:
print "[" + str(serial) + "] Already ringing ..."
else:
os.system("mplayer -include " + conf + " " + file + " &")
print "[" + str(serial) + "] Ringing"
''' connect to DB '''
def connectDB():
try:
conn = MySQLdb.connect(
host = "127.0.0.1",
user = "xxxx",
passwd = "xxxx",
db = "xxxx"
)
return conn
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit(1)
''' execute sql queries '''
def execSQL(conn,query):
try:
cursor = conn.cursor(MySQLdb.cursors.DictCursor)
cursor.execute(query)
rows = cursor.fetchall()
cursor.close()
return rows
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit(1)
''' disconnect from DB '''
def closeDB(conn):
conn.close()
##########################################################################
# main program
##########################################################################
if __name__=='__main__':
try:
watchEvtT = watchEvt('watchEvt')
watchSerialT = watchSerial('watchSerial')
watchEvtT.start()
watchSerialT.start()
except KeyboardInterrupt:
print "Ctrl-C pressed, exiting"
watchEvtT.stop()
watchSerialT.stop()
sys.exit() |
Partager