File: weather.py

package info (click to toggle)
python-omniorb 3.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 3,128 kB
  • ctags: 3,321
  • sloc: cpp: 13,969; python: 8,883; sh: 2,576; xml: 107; makefile: 95; ansic: 35
file content (230 lines) | stat: -rwxr-xr-x 8,606 bytes parent folder | download | duplicates (5)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python
#                           Package   : weather
# weather.py                Created on: 2000/01/06
#			    Author    : Duncan Grisby (dpg1)
#
#    Copyright (C) 2000 AT&T Laboratories Cambridge
#
#  This 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 2 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, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
#  02111-1307, USA.
#
# Description:
#   
#   Client to AT&T Labs Cambridge weather service

weatherIOR    = None
CORBAInterval = 1

import Tkinter
import sys, threading, time
import gauge

from omniORB import CORBA
import _GlobalIDL

immediateState  = None
cumulativeState = None

class CORBAThread(threading.Thread):
    def __init__(self, argv, gui):
        global weatherIOR, immediateState, cumulativeState

        self.gui = gui

        threading.Thread.__init__(self)
        self.setDaemon(1)

        self.orb = CORBA.ORB_init(argv, CORBA.ORB_ID)

        self.verbose = 0
        for arg in argv[1:]:
            if arg == "-v":
                self.verbose = 1
            else:
                weatherIOR = arg

        if weatherIOR is None:
            print "You must specify an IOR for the weather server."
            sys.exit(1)

        self.wio = self.orb.string_to_object(weatherIOR)

        print "Acquiring initial weather state..."
        immediateState  = self.wio.immediate()
        cumulativeState = self.wio.cumulative()
        print "...initial state acquired."

        self.gui.update()

    def run(self):
        global cumulativeState, immediateState

        print "CORBA thread started"

        while 1:
            time.sleep(CORBAInterval)
            if self.verbose: print "CORBA calls...",
            sys.stdout.flush()

            self.gui.startCORBA()
            before = time.time()
            immediateState  = self.wio.immediate()
            cumulativeState = self.wio.cumulative()
            after = time.time()
            self.gui.endCORBA()

            if self.verbose:
                print "done in", int((after-before) * 1000000), "microseconds"

            self.gui.update()
            self.gui.callTime(int((after-before)*1000))


class GUI:

    def __init__(self):

        self.root   = Tkinter.Tk()
        self.root.title("AT&T Rooftop Weather")
        self.canvas = Tkinter.Canvas(self.root, width=800, height=500,
                                     background="blue")
        self.canvas.pack()

        self.root.protocol("WM_DELETE_WINDOW", self.root.quit)

        # Make gauges
        self.windDir = gauge.Compass(self.canvas, 100, 100,
                                     title        = "Wind\ndirection",
                                     handcolour   = "green")

        self.wind    = gauge.Gauge(self.canvas, 250, 100,
                                   title        = "Wind speed",
                                   min          = 0,
                                   max          = 100,
                                   unitspertick = 5,
                                   bigticks     = 25,
                                   digitformat  = "%d knots",
                                   handcolour   = "orange")

        self.windAvg = gauge.Gauge(self.canvas, 400, 100,
                                   title        = "Wind speed\n(10 min avg.)",
                                   min          = 0,
                                   max          = 100,
                                   unitspertick = 5,
                                   bigticks     = 25,
                                   digitformat  = "%.1f knots",
                                   handcolour   = "orange")

        self.temp    = gauge.Gauge(self.canvas, 550, 100,
                                   title        = "Temperature",
                                   min          = -10,
                                   max          = 35,
                                   scalevalues  = [-10,0,10,20,30],
                                   unitspertick = 1,
                                   bigticks     = 5,
                                   digitformat  = "%.1f C",
                                   handcolour   = "red")

        self.dewpt   = gauge.Gauge(self.canvas, 700, 100,
                                   title        = "Dew point",
                                   min          = -10,
                                   max          = 35,
                                   scalevalues  = [-10,0,10,20,30],
                                   unitspertick = 1,
                                   bigticks     = 5,
                                   digitformat  = "%.1f C",
                                   handcolour   = "cyan")

        self.press   = gauge.Gauge(self.canvas, 175, 250,
                                   title        = "Pressure\nQNH",
                                   min          = 950,
                                   max          = 1050,
                                   unitspertick = 5,
                                   digitformat  = "%d mB",
                                   handcolour   = "purple")

        self.humid   = gauge.Gauge(self.canvas, 325, 250,
                                   title        = "Humidity",
                                   min          = 0,
                                   max          = 100,
                                   unitspertick = 5,
                                   bigticks     = 25,
                                   digitformat  = "%d %%",
                                   handcolour   = "green")

        self.sun     = gauge.Gauge(self.canvas, 475, 250,
                                   title        = "Sun since\nmidnight",
                                   min          = 0,
                                   max          = 24,
                                   unitspertick = 1,
                                   bigticks     = 6,
                                   digitformat  = "%.2f hours",
                                   handcolour   = "yellow")

        self.rain    = gauge.Gauge(self.canvas, 625, 250,
                                   title        = "Rain since\nmidnight",
                                   min          = 0,
                                   max          = 100,
                                   unitspertick = 5,
                                   bigticks     = 25,
                                   digitformat  = "%.1f mm",
                                   handcolour   = "blue")

        self.clock   = gauge.Clock(self.canvas, 400, 400)

        # CORBA run indicator:
        self.canvas.create_oval(85,385, 115,415, fill="red", outline="black",
                                width=3, tags="corba")
        self.canvas.create_text(120,400, text="CORBA call in progress",
                                anchor="w", fill="white")

        self.canvas.create_text(550,400,
                                text="CORBA invocation time: --- ms",
                                anchor="w", fill="white", tags="call")



    def update(self):
        self.windDir.set(immediateState.windDirection)
        self.wind.   set(immediateState.windSpeed)
        self.windAvg.set(immediateState.rollingWindSpeed)
        self.temp.   set(immediateState.temperature)
        self.dewpt.  set(immediateState.dewpoint)
        self.press.  set(immediateState.pressure)
        self.humid.  set(immediateState.humidity)
        self.sun.    set(cumulativeState.sunshine)
        self.rain.   set(cumulativeState.rainfall)
        self.clock.  set(cumulativeState.end)

    def startCORBA(self):
        self.canvas.itemconfigure("corba", fill="green")

    def endCORBA(self):
        time.sleep(0.1)
        self.canvas.itemconfigure("corba", fill="dark green")

    def callTime(self, ct):
        self.canvas.itemconfigure("call",
                                  text="CORBA invocation time: %d ms" % ct)

    def go(self):
        self.root.mainloop()
        self.root.destroy()


gui = GUI()
ct  = CORBAThread(sys.argv, gui)
ct.start()
gui.go()