Repository navigation
Expand file tree
/
Copy pathdriver.lua
More file actions
148 lines (116 loc) · 2.85 KB
/
Copy pathdriver.lua
File metadata and controls
148 lines (116 loc) · 2.85 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
do
State = "Closed";
PartiallyOpen = false;
FullyOpen = false;
RelayTimer = -1;
C4:AddVariable( "State", State, "STRING", true, false )
end
--
-- TODO - update the state based on external programming
--
function OnVariableChanged( varname )
if (varname == "State") then
-- TODO
end
end
--
-- Try to set the state based on current sensor states
-- todo: Could we query the sensor state more directly?
--
function UpdateImageState()
newState = "Closed";
if ( PartiallyOpen ) then newState = "Half" end
if ( FullyOpen ) then newState = "Open" end
UpdateState( newState );
end
function UpdateState( newState )
if ( State == newState ) then return end
State = newState;
print( "State Changed To " .. State )
-- Change the button icon
C4:SendToProxy(5001, "ICON_CHANGED", {icon = State})
-- Show the state on the driver dashboard
C4:UpdateProperty( 'Current State', State )
-- Update the variable
C4:SetVariable( 'State', State )
end
function ReceivedFromProxy (idBinding, strCommand, tParams)
--
-- Messages from the UI Button
--
if ( idBinding == 5001 ) then
if ( strCommand == "SELECT" ) then
print( "Closing Door Relay" )
--
-- Open the relay again in one second
--
RelayTimer = C4:AddTimer( 1, "SECONDS", false )
--
-- Close the relay
--
C4:SendToProxy (1, "OPEN", '')
C4:SendToProxy (1, "CLOSE", '')
--
-- If we were opening when we clicked again mark as closed
--
if ( State == "Opening" ) then
UpdateState( "Closed" )
end
--
-- If we started off closed, show the Opening state to give some immediate feedback
--
if ( State == "Closed") then
UpdateState( "Opening" )
end
--
-- If we started off open, show the Closing state to give some immediate feedback
--
if ( State == "Open") then
UpdateState( "Closing" )
end
return
end
end
--
-- Messages from the Relay - Ignored
--
if ( idBinding == 1 ) then
return;
end
--
-- Messages from Partially Opened sensor
--
if ( idBinding == 2 ) then
if ( strCommand == "OPENED") then PartiallyOpen = true end
if ( strCommand == "CLOSED") then PartiallyOpen = false end
UpdateImageState();
return;
end
--
-- Messages from Fully Opened sensor
--
if ( idBinding == 3 ) then
if ( strCommand == "OPENED") then FullyOpen = true end
if ( strCommand == "CLOSED") then FullyOpen = false end
UpdateImageState();
return;
end
--
-- Missed messages, lets just print them out
--
print ( idBinding .. ": " .. strCommand )
print ( "0 = " .. tParams[1] )
print ( "1 = " .. tParams[2] )
print ( "2 = " .. tParams[3] )
end
function OnTimerExpired(idTimer)
--
-- I don't know if this is thr right way to "press" relays
-- but this is what I came up with.
--
if ( idTimer == RelayTimer ) then
print( "Opening Door Relay" )
C4:SendToProxy (1, "OPEN", '')
RelayTimer = -1;
end
end