-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter.py
More file actions
37 lines (27 loc) · 949 Bytes
/
counter.py
File metadata and controls
37 lines (27 loc) · 949 Bytes
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
import threading
import time
class LightControl():
def __init__(self, maxTime):
self.state = True # 默认是开启的状态
self.lock = threading.Lock()
self.lastTime = time.time() # 最后一次有人出现
self.maxTime = maxTime # 最长没出现时间,超出一个时间就主动关闭, 单位 s
'''
1. 有人过来,就开启灯光,记录最后一次的出现时间
2. 长时间没人,就关闭
'''
def checkTime(self):
if time.time() - self.lastTime >= self.maxTime:
self.turnOff()
return self.state
else:
return self.state
def turnOff(self):
with self.lock:
self.state = False
def turnOn(self):
with self.lock:
self.state = True
self.lastTime = time.time() # 更新最后一次出现的时间
if __name__ == '__main__':
print(time.time())