-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoLayoutPowerOff.py
More file actions
70 lines (54 loc) · 2.39 KB
/
AutoLayoutPowerOff.py
File metadata and controls
70 lines (54 loc) · 2.39 KB
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
# This script turns the off power to the layout after
# a specified time has passed with no activity. ('Activity'
# means a change in state of a sensor used as a block
# occupancy detector.) It's a sort of idle timeout for
# track power. It is intended to be run from the startup
# menu.
#
# The timeout is set at the top, in minutes.
#
# Author: Steve Rawlinson, 2016
# Part of the JMRI distribution
import time
import jmri
# Edit this:
timeOutInMinutes = 30
debug = False
class AutoLayoutPowerOff(jmri.jmrit.automat.AbstractAutomaton):
def init(self):
# set the timeout
self.timeout = timeOutInMinutes
# get the list of sensors in use as block occupancy detectors
self.blocklist = blocks.getNamedBeanList()
self.blockOccupancySensors = []
for b in self.blocklist:
s = b.getSensor()
if s is not None:
self.blockOccupancySensors.append(s)
def handle(self):
if debug:
print "AutoLayoutPowerOff: starting up"
# get the current power state
self.powerState = powermanager.getPower()
if debug:
if self.powerState != jmri.PowerManager.ON:
print "AutoLayoutPowerOff: Layout power is off, sleeping until it comes on"
# If the power is off, do nothing, checking every minute
while powermanager.getPower() != jmri.PowerManager.ON:
time.sleep(60)
if debug:
print "AutoLayoutPowerOff: Layout Power is on"
while True: # we break out after the timeout
if debug:
print "AutoLayoutPowerOff: monitoring activity on", len(self.blockOccupancySensors), "sensors; timeout: ", str(timeOutInMinutes), "minutes"
startTime = time.time()
self.waitChange(self.blockOccupancySensors, self.timeout * 60 * 1000)
stopTime = time.time()
if stopTime - startTime > self.timeout * 60:
print "AutoLayoutPowerOff: timeout waiting for activity, turning layout power OFF"
powermanager.setPower(jmri.PowerManager.OFF)
return True # start again
else:
if debug:
print "AutoLayoutPowerOff: activity detected at", stopTime
AutoLayoutPowerOff().start()