def checkBt(): print "presenceBt Module (CTRL-C to exit)" sharedFunc.sPrint("Data: " + outFilePath) missingNo = 0 foundNo = 0 sleepAmt = 20 try: while True: startRulesProcessor = 0 sharedFunc.sPrint("\nType: presenceBt") sharedFunc.sPrint( "Time: " + time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())) sharedFunc.sPrint("Find: " + btStr + " (" + btMac + ")") try: result = bluetooth.lookup_name(btMac, timeout=3) except: sharedFunc.sPrint( "\n[*] Unable to scan. Trying again in 5 seconds.") time.sleep(5) else: resultInt = 0 if (result != None): if (foundNo >= 2 or checkInstantly): sharedFunc.sPrint("Result: Found") logger.info("Found") sleepAmt = 60 resultInt = 1 if (foundNo == 2 or checkInstantly): startRulesProcessor = 1 if (foundNo <= 19): sleepAmt = 30 foundNo += 1 else: sharedFunc.sPrint("Result: Possibly Found") logger.info("Possibly Found") sleepAmt = 5 resultInt = -1 missingNo = 0 foundNo += 1 else: if (missingNo >= 4): sharedFunc.sPrint("Result: Missing") logger.info("Missing") sleepAmt = 120 resultInt = 0 if (missingNo == 4): startRulesProcessor = 1 # if BT is missing, then set motion as (current time - 301 seconds) sharedFunc.valSave(str(time.time() - 301), motionFilePath) if (missingNo <= 19): sleepAmt = 30 missingNo += 1 else: sharedFunc.sPrint("Result: Possibly Missing") logger.info("Possibly Missing") sleepAmt = 5 resultInt = -1 foundNo = 0 missingNo += 1 if (resultInt != -1): sharedFunc.valSave(resultInt, outFilePath) if (startRulesProcessor == 1): # call main.py #runpy.run_path(sys.path[0]+"/main.py") rulesProcessor.checkAll({"presenceBt": resultInt}) startRulesProcessor = 0 if checkInstantly: break else: sharedFunc.sPrint("\n[*] Sleeping for " + str(sleepAmt) + " seconds") time.sleep(sleepAmt) except KeyboardInterrupt: print "\nQuit" return
#!/usr/bin/python # # mainLoop.py # # Loops main.py # Create by Jin # import sys import time import sharedFunc import rulesProcessor import switchRegulator switchRegulator.setParentName("rulesProcessor (Loop)") rulesProcessor.callerName = "rulesProcessor (Loop)" triggerLight_last = 'none' # silent detection if len(sys.argv) > 1 and sys.argv[1] == "-silent": sharedFunc.sPrint_silent = 1 try: while True: rulesProcessor.checkAll() sharedFunc.sPrint("\n[*] Sleeping for 120 seconds") time.sleep(120) except KeyboardInterrupt: sharedFunc.sPrint("\nQuit")
# Read PIR state Current_State = GPIO.input(GPIO_PIR) if Current_State == 1 and Previous_State == 0: # PIR is triggered sharedFunc.sPrint("Result: Motion Detected") logger.info("Motion Detected") # Record previous state Previous_State = 1 # Store data timeNow = str(time.time()) sharedFunc.valSave(timeNow, outFilePath) # call main.py #runpy.run_path(sys.path[0]+"/main.py") rulesProcessor.checkAll({"presenceMotion": 1}) # if fan is off, (move this part to rulesProcessor in the future) try: r = requests.get( "http://192.168.1.77/cgi-bin/json.cgi?get=state") except: pass else: if r.json()['state'] == "off": presenceBt.checkInstantly = True presenceBt.checkBt() # Wait a while sharedFunc.sPrint("\n[*] Sleeping for 20 seconds") time.sleep(20)
def sensorsMode(): setCaller("sensorsMode") rulesProcessor.checkAll() return 'Caller: sensorsMode'
def onResume(): setCaller("onResume") rulesProcessor.checkAll() return 'Caller: onResume'
def presencePc(): setCaller("presencePc") rulesProcessor.checkAll() return 'Caller: presencePc'
def roomSleep(): setCaller("roomSleep") rulesProcessor.checkAll() return 'Caller: roomSleep'