所以我已经在同一个问题上工作了大约一年。我很快就让它在我的 mac 上运行,但在我的 PC 上运行时遇到了很多麻烦。我尝试了许多不同的方法。我有一个家庭自动化系统,当我或我的伴侣在家时(即我们的 iPhone 可以在家庭 WiFi 上检测到),它会打开暖气和热水(通过 arduino 和 RF 模块)。最后,我使用“nslookup”来查找 iPhone 的 IP 地址(以防 IP 地址因为它们是动态的而发生变化(但它们实际上从未在我的路由器上这样做))和“nmap”来检测 iPhone 是否开启网络。如果 iPhone 处于深度睡眠状态,'nmap' 并不总是能找到手机,所以在它说手机在家之前,我已经让它检查了 10 次。下面是我在 python 中的家庭自动化代码的一部分。我用过线程。
# Dictionary to store variables to reuse on program restart
v = {
'boilerControlCH' : 'HIH', # 'scheduled' or 'HIH' (Honey I'm Home)
'boilerControlHW' : 'scheduled',
'thermostatSetPoint' : 20.8,
'thermostatVariance' : 0.1,
'morningTime' : datetime(1970,1,1,6,0,0),
'nightTime' : datetime(1970,1,1,23,0,0),
'someOneHome' : False,
'guest' : False,
'minimumTemperatureOO' : False,
'minimumTemperature' : 4.0,
'iPhoneMark' : {'iPhoneHostname' : 'marks-iphone', 'home' : False},
'iPhoneJessica' : {'iPhoneHostname' :'jessicaesiphone', 'home' : False}
}
和
# Check if anyone at home
def occupancyStatus(person, Bol = False):
with lockOccupancyStatus:
someOneHome = False
if 'iPhone' in person:
v[person]['home'] = Bol
elif 'retest' in person:
pass
else:
v[person] = Bol
if v['guest'] == True:
someOneHome = True
for key in v:
if 'iPhone' in key:
if v[key]['home'] == True:
someOneHome = True
v['someOneHome'] = someOneHome
variablesToFile()
return
和主要代码
# iPhone home status threading code
class nmapClass(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global exitCounter
nmapThread()
msg.log('Exited nmapThread')
waitEvent.set()
waitEventAdjustable.set()
serialDataWaiting.set()
exitCounter += 1
def nmapThread():
iPhone = {}
maxCounts = 10
for phone in v:
if 'iPhone' in phone:
iPhone[phone] = {}
iPhone[phone]['hostname'] = v[phone]['iPhoneHostname']
iPhone[phone]['count'] = maxCounts
#msg.log(iPhone)
while exitFlag[0] == 0:
for phone in iPhone:
if iPhone[phone]['count'] > 0:
phoneFound = False
IPAddress = '0.0.0.0'
# Find iPhones IP address using its hostname
commandNsloolup = 'nslookup %s' %iPhone[phone]['hostname']
childNslookup = pexpect.popen_spawn.PopenSpawn(commandNsloolup, timeout = None)
output = childNslookup.readline()
while '\r\n' in output:
#msg.log(output)
if 'Name:' in output:
output = childNslookup.readline()
if 'Address:' in output:
tempStr = output
startPoint = tempStr.find('192')
tempStr = tempStr[startPoint:]
IPAddress = tempStr.replace('\r\n', '')
#msg.log(IPAddress)
output = childNslookup.readline()
if IPAddress == '0.0.0.0':
pass
#msg.error('Error finding IP address for %s' %iPhone[phone]['hostname'], GFI(CF()).lineno)
else:
#commandNmap = 'nmap -PR -sn %s' %IPAddress
#commandNmap = 'nmap -p 62078 -Pn %s' %IPAddress # -p specifies ports to try and access, -Pn removes pinging
commandNmap = 'nmap -p 62078 --max-rate 100 %s' %IPAddress
childNmap = pexpect.popen_spawn.PopenSpawn(commandNmap, timeout = None)
output = childNmap.readline()
while '\r\n' in output:
if 'Host is up' in output:
phoneFound = True
break
output = childNmap.readline()
#if phoneFound:
# break
if phoneFound:
iPhone[phone]['count'] = 0
if v[phone]['home'] == False:
msg.log('%s\'s iPhone has returned home' %phone)
occupancyStatus(phone, True)
waitEventAdjustable.set()
#else:
#msg.log('%s\'s iPhone still at home' %phone)
else:
iPhone[phone]['count'] -= 1
if v[phone]['home'] == True and iPhone[phone]['count'] == 0:
msg.log('%s\'s iPhone has left home' %phone)
occupancyStatus(phone, False)
waitEventAdjustable.set()
#else:
#msg.log('%s\'s iPhone still away from home' %phone)
elif iPhone[phone]['count'] < 0:
msg.error('Error with count variable in iPhone dictionary', GFI(CF()).lineno)
longWait = True
for phone in iPhone:
if iPhone[phone]['count'] > 0:
longWait = False
#msg.log('%s: %s' %(phone, iPhone[phone]['count']))
if longWait:
#msg.log('wait long')
# 600 = run every 10 minutes
waitEvent.wait(timeout=600)
for phone in iPhone:
iPhone[phone]['count'] = maxCounts
else:
#msg.log('wait short')
waitEvent.wait(timeout=60)
return
如果您将代码直接复制到自己的脚本中,则代码可能无法正常工作,因为缺少一些我没有复制的部分,以尝试保持简单易读,但希望上面的代码能让每个人都了解我是如何做到的做了事情。