1 # application-room405.tcl --
2 #
3 # SDS service to provide control of room 405 lights
4 #
5 # Copyright (c) 2000-2002 The Regents of the University of California.
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are met:
10 #
11 # A. Redistributions of source code must retain the above copyright notice,
12 # this list of conditions and the following disclaimer.
13 # B. Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
16 # C. Neither the names of the copyright holders nor the names of its
17 # contributors may be used to endorse or promote products derived from this
18 # software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
24 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 import RTPApplication
32 import CSdsService
33 import CServiceManager
34 import CService
35 import DpClient
36
37 #
38 # Code for CRnSwitcherApplication
39 #
40
41 Class CRoom405Application -superclass RTPApplication
42
43 CRoom405Application instproc InitArgs { options } {
44 # for the service discovery service
45 $options register_option -sa optServiceAddress
46 $options register_option -sp optServicePort
47 $options register_option -st optServiceTTL
48
49 # for the service discovery service
50 $options register_option -rp optComPort
51
52 # for the amx info
53 $options register_option -amxaddr optAMXAddress
54 $options register_option -amxport optAMXPort
55 }
56
57 CRoom405Application instproc InitResources { options } {
58 # for the service discovery service
59 $options add_default optServiceAddress "224.4.6.8"
60 $options add_default optServicePort "12344"
61 $options add_default optServiceTTL "16"
62
63 # for the service discovery service
64 $options add_default optComPort "11410"
65
66 # for the amx
67 $options add_default optAMXAddress "htsr.cs.berkeley.edu"
68 $options add_default optAMXPort "6901"
69 }
70
71 CRoom405Application instproc InitAMX { } {
72 $self instvar m_dpAMX
73 $self instvar m_szAMXState
74 set options [$self options]
75
76 set inetAMXAddr [$options get_option optAMXAddress]
77 set iAMXPort [$options get_option optAMXPort]
78
79 set m_dpAMX [new DpClient $inetAMXAddr $iAMXPort]
80 }
81
82 CRoom405Application instproc init { argv } {
83 $self next Room405
84
85 # set some member variables
86
87 # Initiailization of variables and resources.
88 set options [$self options]
89 $self InitArgs $options
90 $self InitResources $options
91 $options load_preferences "rtp rvc"
92 set argv [$options parse_args $argv]
93
94 # setup the amx stuff if we have to
95 $self InitAMX
96
97 # create the sds session object
98 set inetServiceAddr [$options get_option optServiceAddress]
99 set iServicePort [$options get_option optServicePort]
100 set iServiceTTL [$options get_option optServiceTTL]
101
102 $self instvar m_sdsService
103 set m_sdsService [new CSdsService $self $inetServiceAddr \
104 $iServicePort $iServicePort $iServiceTTL]
105
106 # setup the service objects
107 $self instvar m_serviceManager
108 $self instvar m_service
109
110 set iPort [$options get_option optComPort]
111 set m_serviceManager [new CServiceManager "ServiceApp" "Service" $iPort]
112 $m_serviceManager Attach $self
113
114 set m_service ""
115 }
116
117 ##############################################################################
118 #
119 # CRoom405Application instproc NewConnection { service } {
120 #
121 # Input:
122 # service - the new service that got connected
123 #
124 # Output:
125 # 1 - if handling this service
126 # 0 - otherwise
127 #
128 # Description:
129 # A call back function for the service manager. This function will be called
130 # when a new connection has just been noticed by the service manager
131 #
132 ##############################################################################
133 CRoom405Application instproc NewConnection { service } {
134 $self instvar m_service
135
136 # check that we don't already have a connection that we're using
137 if { $m_service != "" } {
138 $service CloseLink
139 return 1
140 }
141
142 set m_service $service
143
144 # Add the service calls
145 $m_service MapMessage "SYN_SERVICE_IMAGE" $self SynServiceImage
146 $m_service MapMessage "GET_UI_WINDOW" $self GetUIWindow
147 $m_service MapMessage "AMX_SWITCH" $self AMXSwitch
148 $m_service MapMessage "CLOSE_LINK" $self CloseService
149
150 return 1
151 }
152
153 ##############################################################################
154 #
155 # CRoom405Application instproc CloseService { service arguments }
156 #
157 # Input:
158 # service - the service object that called this function
159 # arguments - the arguments that are passed to this funct from remote caller
160 #
161 # Output:
162 # none
163 #
164 # Description:
165 # Need to close the service and allow other connections to be made
166 #
167 ##############################################################################
168 CRoom405Application instproc CloseService { service arguments } {
169 $self instvar m_service
170 set m_service ""
171
172 exit
173 }
174
175
176 ##############################################################################
177 #
178 # CRoom405Application public SynServiceImage { service arguments } {
179 #
180 # Input:
181 # service - the service that called this function
182 # arguments - the arguments associated with this call
183 #
184 # Output:
185 # none
186 #
187 # Description:
188 # This is called by the remote service client when it wants the image or text
189 # to display the service. The simple protocol goes as such:
190 # type type_dependent_arguments. The type could be text and it's argument
191 # the ascii of title. It could also be image and be a bitmap of the image
192 #
193 ##############################################################################
194 CRoom405Application public SynServiceImage { service arguments } {
195 $service Send "SYN_SERVICE_IMAGE" "text Room405"
196 }
197
198
199 ##############################################################################
200 #
201 # CRoom405Application public GetUIWindow { service arguments }
202 #
203 # Input:
204 # service - the service that called this function
205 # arguments - the arguments associated with this call
206 #
207 # Output:
208 # none
209 #
210 # Description:
211 # This is called by the remote service client when it wants the code
212 # to make the window. It should send back the code to build the window.
213 # There are two things that are assumed to be set by the client. One is
214 # winFrame the window to draw into. Another is service, which is the
215 # m_service object to send back to yourself.
216 #
217 ##############################################################################
218 CRoom405Application public GetUIWindow { service arguments } {
219 $self instvar m_szAMXState
220
221 set cmd ""
222
223 append cmd "regsub -all -- {\\\.} \$winFrame {_} __name \n"
224
225 # create the frame for the lights
226 append cmd "frame \$winFrame.lightFrame -relief raised \
227 -borderwidth 2 \n"
228 append cmd "pack \$winFrame.lightFrame -side top -fill both -expand 1 \n"
229
230 # the label for the light
231 append cmd "label \$winFrame.lightFrame.title -text \"Lights\" \n"
232 append cmd "pack \$winFrame.lightFrame.title -side top \n"
233
234 append cmd "global iLightState\$__name \n"
235 append cmd "set iLightState\$__name \"\" \n"
236
237 # create the radio buttons
238 set lszLightItem "{Full On} 9 Overlay 43 Slide 44 {Full Off} 55"
239 foreach {szLightName szLightVal} $lszLightItem {
240 append cmd "frame \$winFrame.lightFrame.$szLightVal \n"
241 append cmd "pack \$winFrame.lightFrame.$szLightVal \
242 -side top -fill x -expand 1\n"
243 append cmd "radiobutton \$winFrame.lightFrame.$szLightVal.radio \
244 -command \"\$service Send AMX_SWITCH \\\"5 $szLightVal\\\"\" \
245 -text \"$szLightName\" -value $szLightVal \
246 -variable iLightState\$__name \n"
247 append cmd "pack \$winFrame.lightFrame.$szLightVal.radio -side left \n"
248 }
249
250 # now put the close button in
251 append cmd "frame \$winFrame.command \n"
252 append cmd "pack \$winFrame.command -side top -fill x \n"
253
254 append cmd "button \$winFrame.command.closeButton -text Close \
255 -command \"destroy \$winFrame\" \n"
256 append cmd "pack \$winFrame.command.closeButton -side right \n"
257
258 append cmd "label \$winFrame.command.title -relief raised \
259 -text \"Room405\" \n"
260 append cmd "pack \$winFrame.command.title -side left -fill both -expand 1\n"
261
262
263 $service Send "GET_UI_WINDOW" $cmd
264 }
265
266
267 CRoom405Application public AMXSwitch { service target } {
268 $self instvar m_dpAMX
269
270 set device [lindex $target 0]
271 set mode [lindex $target 1]
272
273 switch -exact -- $device {
274 5 {
275 # lights device
276 if { [lsearch -exact {9 43 44 45 55} $mode] != -1 } {
277 $m_dpAMX do send-AMX-command 1 5 $mode
278 after 400
279 $m_dpAMX do send-AMX-command 2 5 $mode
280 }
281 }
282 }
283 }
284
285 ##############################################################################
286 #
287 # CRoom405Application public GetSdsServiceData { } {
288 #
289 # Input:
290 # none
291 #
292 # Output:
293 # the data that the will go out to the sds system
294 #
295 # Description:
296 # This is a callback function for the service discovery system. Need to
297 # return the data that will go out to the sds system. So far there are
298 # three fields with their values
299 #
300 ##############################################################################
301 CRoom405Application public GetSdsServiceData { } {
302 $self instvar m_lConfig
303
304 set options [$self options]
305 set iComPort [$options get_option optComPort]
306 set hostname [exec hostname]
307
308 set location "Berkeley Soda 405"
309 set type "405 Room Control"
310
311 set data [list "UPDATE:" [concat $hostname $iComPort] \
312 [list [list "LOCATION:" $location] \
313 [list "TYPE:" $type]]]
314
315 return $data
316 }
317
318
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.