-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshutter.py
More file actions
165 lines (142 loc) · 7.47 KB
/
shutter.py
File metadata and controls
165 lines (142 loc) · 7.47 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# -*- coding: utf-8 -*-
import threading
import time
import traceback
# windowblindserver
# A python server for Raspberry Pi to control window blinds equipped with Conrad RSL actuators
# Copyright (C) 2015 Thoralt Franz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# =============================================================================================================
# =============================================================================================================
class Shutter:
# ---------------------------------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------------------------------
def __init__(self, address, name, closingTime):
self.address = address
self.name = name
self.closingTime = closingTime
self.isMoving = False
self.position = 100
self.targetPosition = 100
self.direction = None
self.commandManager = None
self.queue = []
# ---------------------------------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------------------------------
def move_to_position(self, targetPosition):
# if currently moving, add next target position to queue, will be processed when current movement ends
if self.isMoving:
print 'Shutter.moveToPosition(): Currently moving, adding new targetPosition to queue.'
self.queue.append(targetPosition)
return
# check if we need to move at all
if self.position == targetPosition:
print 'Shutter.moveToPosition(): Current position = target position -> no action'
return True
self.isMoving = True
self.targetPosition = targetPosition
# calculate direction and moving time
if self.targetPosition >= 100:
# always move full time up to sync position
t = self.closingTime
self.direction = 'up'
print 'moveToPosition(): Target position = 100 -> performing full up cycle'
elif self.targetPosition <=0:
# always move full time down to sync position
t = self.closingTime
self.direction = 'dn'
print 'moveToPosition(): Target position = 0 -> performing full down cycle'
else:
if self.targetPosition > self.position:
self.direction = 'up'
t = (self.targetPosition - self.position)/100 * self.closingTime
else:
self.direction = 'dn'
t = (self.position - self.targetPosition)/100 * self.closingTime
print 'moveToPosition(): Current position=%d, target position=%d, direction=\'%s\', time=%.3f seconds' \
% (self.position, self.targetPosition, self.direction, t)
self.delayed_stop(t)
return self.commandManager.send_cmd(self.address + '-' + self.direction)
# ---------------------------------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------------------------------
def delayed_stop(self, delay):
t = DelayedStopThread(self, delay)
t.start()
# =============================================================================================================
# =============================================================================================================
class DelayedStopThread(threading.Thread):
def __init__(self, device, delay):
print 'delayedExecutionThread.init(%s:\'%s\', %.3f)' % (device.address, device.name, delay)
self.device = device
self.delay = delay
threading.Thread.__init__(self)
def run(self):
try:
d = self.device
# actuator is moving, now wait until we reach target position
print 'delayedExecutionThread.run() start, waiting %.3f seconds...' % self.delay
time.sleep(self.delay)
d.position = d.targetPosition
print '%s reached position %d' % (d.address, d.targetPosition)
# queue contains commands?
while len(d.queue) > 0:
# get next target position
d.targetPosition = d.queue.pop(0)
print '%s next position in queue: %d, items left: %d' % (d.address, d.targetPosition, len(d.queue))
# depending on new target position either continue to move or
# change direction
if d.direction == 'up' and d.targetPosition > d.position:
# can continue to move up
t = (d.targetPosition - d.position)/100 * d.closingTime
print '%s can continue to move up, waiting %.3f seconds' % (d.address, t)
time.sleep(t)
d.position = d.targetPosition
print '%s reached position %d' % (d.address, d.targetPosition)
elif d.direction == 'dn' and d.targetPosition < d.position:
# can continue to move down
t = (d.position - d.targetPosition)/100 * d.closingTime
print '%s can continue to move down, waiting %.3f seconds' % (d.address, t)
time.sleep(t)
d.position = d.targetPosition
print '%s reached position %d' % (d.address, d.targetPosition)
else:
# direction changed for next queue element
print '%s needs to change direction, stopping and starting.' % d.address
if d.direction == 'up':
cmd = d.address + '-dn'
d.direction = 'dn'
t = (d.position - d.targetPosition)/100 * d.closingTime
else:
cmd = d.address + '-up'
d.direction = 'up'
t = (d.targetPosition - d.position)/100 * d.closingTime
# first command to stop
d.commandManager.send_cmd(cmd)
# same command to start again in opposite direction
d.commandManager.send_cmd(cmd)
print '%s changed direction, waiting %.3f seconds' % (d.address, t)
time.sleep(t)
d.position = d.targetPosition
print '%s reached position %d' % (d.address, d.targetPosition)
# queue is now empty -> issue stop command
if d.direction == 'up':
cmd = d.address + '-dn'
else:
cmd = d.address + '-up'
d.commandManager.send_cmd(cmd)
d.isMoving = False
except Exception as e:
print 'Exception during delayedExecutionThread'
traceback.print_exc()