我为解决这个问题所做的就是为它们连接的无线路由器上的按钮分配固定的 IP 地址(因为我可以在 DHCP 表中看到它们的 MAC 地址)。然后我重写了脚本以简单地检测对已分配按钮的 ipAddress 的成功 ping。我注意到按钮会在网络上保留一段时间(可能是 15-20 秒),所以我在检测之间增加了一点等待时间(有点像物理开关上的去抖过滤器)以确保相同的按钮按下两次都没被发现。这成功了。
首先,我找到了ping的纯 Python 实现(我将 lock、stock 和 bucket 复制到 Python 脚本中),然后我编写了以下代码:
#!/usr/bin/python
from scapy.all import *
import requests
import time
import os, sys
import subprocess
import re
import platform
import datetime
BUTTON_A = "<assigned IP address to Button A>"
BUTTON_B = "<assigned IP address to Button B>"
last_button_push_A = datetime.datetime(2013,12,30,23,59,59)
last_button_push_B = datetime.datetime(2005,12,30,1,1,1)
waittime_sec = 20
while (True):
try:
delta = (datetime.datetime.now() - last_button_push_A).seconds
print (delta > 20)
if (do_one(BUTTON_A, 1) is not None) and delta > waittime_sec:
last_button_push_A = datetime.datetime.now();
logfile.write("<Child 1> Completed a Chore: "+time.strftime("%Y-%m-%d %H:%M")+"\n")
logfile.flush()
play_sound_child_1()
delta = (datetime.datetime.now() - last_button_push_B).seconds
if (do_one(BUTTON_B, 1) is not None) and delta > waittime_sec:
last_button_push_B = datetime.datetime.now()
logfile.write("<child 2> Completed a Chore: "+time.strftime("%Y-%m-%d %H:%M")+"\n")
logfile.flush()
play_sound_child_2()
except BaseException as e:
logfile.write("EXCEPTION: {}\n".format(e))
logfile.write("Loop Rest at "+time.strftime("%Y-%m-%d %H:%M")+"\n")