Rev 747 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 747 | Rev 749 | ||
|---|---|---|---|
| Line 4... | Line 4... | ||
| 4 | import sys |
4 | import sys |
| 5 | import logging as log |
5 | import logging as log |
| 6 | import getopt |
6 | import getopt |
| 7 | import atexit |
7 | import atexit |
| 8 | import time |
8 | import time |
| 9 | - | ||
| 10 | from CanPktHandlerBase import CanPktHandlerBase |
- | |
| 11 | from CanPktHandler1 import CanPktHandler1 |
- | |
| 12 | 9 | ||
| 13 | from NodeIfBase import NodeIfBase |
10 | from NodeIfBase import NodeIfBase |
| 14 | from NodeIfCanStim import NodeIfCanStim |
11 | from NodeIfCanStim import NodeIfCanStim |
| 15 | from NodeIfSerial import NodeIfSerial |
12 | from NodeIfSerial import NodeIfSerial |
| 16 | from NodeIfTCP import NodeIfTCP |
13 | from NodeIfTCP import NodeIfTCP |
| Line 20... | Line 17... | ||
| 20 | daemonApp = None |
17 | daemonApp = None |
| 21 | 18 | ||
| 22 | class CanDaemon(): |
19 | class CanDaemon(): |
| 23 | 20 | ||
| 24 | cfg = None |
21 | cfg = None |
| 25 | nodeIf = None |
- | |
| 26 | ifNotifier = None |
22 | ifNotifier = None |
| 27 | terminated = False |
23 | terminated = False |
| 28 | 24 | ||
| 29 | command_list = [] |
25 | command_list = [] |
| 30 | command_map = {} |
26 | command_map = {} |
| Line 120... | Line 116... | ||
| 120 | def notify(self, msg): |
116 | def notify(self, msg): |
| 121 | if msg == self.TERMINATE: |
117 | if msg == self.TERMINATE: |
| 122 | print 'Main terminate notify' |
118 | print 'Main terminate notify' |
| 123 | else: |
119 | else: |
| 124 | print 'UNDEFINED NOTIFY' |
120 | print 'UNDEFINED NOTIFY' |
| 125 | 121 | ||
| 126 | 122 | ||
| 127 | def __rightAdjust(self, refstr, str): |
123 | def __rightAdjust(self, refstr, str): |
| 128 | spcstr = '' |
124 | spcstr = '' |
| 129 | endpos = 25-len(refstr) |
125 | endpos = 25-len(refstr) |
| 130 | for i in range(1,endpos): |
126 | for i in range(1,endpos): |
| 131 | if i == endpos-5: |
127 | if i == endpos-5: |
| 132 | spcstr += ':' |
128 | spcstr += ':' |
| 133 | else: |
129 | else: |
| 134 | spcstr += ' ' |
130 | spcstr += ' ' |
| 135 | return (spcstr+str) |
131 | return (spcstr+str) |
| 136 | 132 | ||
| 137 | def helpCmd(self, *args): |
133 | def helpCmd(self, *args): |
| 138 | try: |
134 | try: |
| 139 | args = args[0] |
135 | args = args[0] |
| 140 | except: |
136 | except: |
| 141 | args = [] |
137 | args = [] |
| 142 | if len(args) < 1: |
138 | if len(args) < 1: |
| 143 | quitStr = 'quit, exit' |
139 | quitStr = 'quit, exit' |
| 144 | print quitStr, self.__rightAdjust(quitStr, 'shutdown pyCanDaemon') |
140 | print quitStr, self.__rightAdjust(quitStr, 'shutdown pyCanDaemon') |
| 145 | for cmd in self.command_list: |
141 | for cmd in self.command_list: |
| 146 | cmdstr = cmd[self.CMDLIST_NAME] + ' ' + cmd[self.CMDLIST_PARAMINFO] |
142 | cmdstr = cmd[self.CMDLIST_NAME] + ' ' + cmd[self.CMDLIST_PARAMINFO] |
| Line 154... | Line 150... | ||
| 154 | print 'no help for ' + subCmd |
150 | print 'no help for ' + subCmd |
| 155 | else: |
151 | else: |
| 156 | print 'usage: help [topic]' |
152 | print 'usage: help [topic]' |
| 157 | 153 | ||
| 158 | def filterCmd(self, *args): |
154 | def filterCmd(self, *args): |
| 159 | try: |
155 | try: |
| 160 | args = args[0] |
156 | args = args[0] |
| 161 | except: |
157 | except: |
| 162 | args = [] |
158 | args = [] |
| 163 | log.debug('filterCmd ' + str(args)) |
159 | log.debug('filterCmd ' + str(args)) |
| 164 | if len(args) < 2: |
160 | if len(args) < 2: |
| 165 | print 'Invalid arguments\n' + self.filterHelp |
161 | print 'Invalid arguments\n' + self.filterHelp |
| 166 | return |
162 | return |
| 167 | cmd = args[0] |
163 | cmd = args[0] |
| 168 | filterName = args[1] |
164 | filterName = args[1] |
| 169 | # FIXME: verify |
165 | # FIXME: verify |
| 170 | if cmd == 'add': |
166 | if cmd == 'add': |
| 171 | print 'Importing filter: ' + filterName |
167 | print 'Importing filter: ' + filterName |
| 172 | self.cfg.filterCfg.importFilter(filterName) |
168 | self.cfg.filterCfg.importFilter(filterName) |
| 173 | elif cmd == 'rem' or cmd == 'remove': |
169 | elif cmd == 'rem' or cmd == 'remove': |
| 174 | print 'Deleting filter: ' + filterName |
170 | print 'Deleting filter: ' + filterName |
| 175 | # FIXME: actually delete |
171 | # FIXME: actually delete |
| 176 | raise 'Not implemented' |
172 | raise 'Not implemented' |
| 177 | 173 | ||
| 178 | def stateCmd(self, *args): |
174 | def stateCmd(self, *args): |
| 179 | try: |
175 | try: |
| 180 | args = args[0] |
176 | args = args[0] |
| 181 | except: |
177 | except: |
| 182 | args = [] |
178 | args = [] |
| Line 194... | Line 190... | ||
| 194 | print 'Deleting state space: ' + spaceName |
190 | print 'Deleting state space: ' + spaceName |
| 195 | # FIXME: actually delete statespace |
191 | # FIXME: actually delete statespace |
| 196 | raise 'Not implemented' |
192 | raise 'Not implemented' |
| 197 | 193 | ||
| 198 | def ifAddCmd(self, *args): |
194 | def ifAddCmd(self, *args): |
| 199 | raise 'not implemented' |
195 | raise 'not implemented' |
| 200 | 196 | ||
| 201 | def ifRemCmd(self, *args): |
197 | def ifRemCmd(self, *args): |
| 202 | raise 'not implemented' |
198 | raise 'not implemented' |
| 203 | 199 | ||
| 204 | def ifUpCmd(self, *args): |
200 | def ifUpCmd(self, *args): |
| 205 | raise 'not implemented' |
201 | raise 'not implemented' |
| Line 252... | Line 248... | ||
| 252 | self.command_list[self.command_map[cmd[0]]][self.CMDLIST_HANDLER](cmd[1:]) |
248 | self.command_list[self.command_map[cmd[0]]][self.CMDLIST_HANDLER](cmd[1:]) |
| 253 | else: |
249 | else: |
| 254 | print 'unknown command: ' + cmd[0] |
250 | print 'unknown command: ' + cmd[0] |
| 255 | 251 | ||
| 256 | def exitHelper(self): |
252 | def exitHelper(self): |
| - | 253 | pass |
|
| 257 |
|
254 | # if self.nodeIf is not None and self.nodeIf.running(): |
| 258 |
|
255 | # self.nodeIf.stop() |
| 259 | 256 | ||
| 260 | def exceptHelper(self, type, value, tb): |
257 | def exceptHelper(self, type, value, tb): |
| 261 | import traceback |
258 | import traceback |
| 262 | traceback.print_exception(type, value, tb) |
259 | traceback.print_exception(type, value, tb) |
| 263 | print '\nException in main program, shutting down threads' |
260 | print '\nException in main program, shutting down threads' |
| 264 | self.terminated = True |
261 | self.terminated = True |
| 265 | self.exitHelper() |
262 | self.exitHelper() |
| 266 | # sys.exit(-1) |
- | |
| - | 263 | ||
| 267 | 264 | ||
| 268 | def run(self): |
265 | def run(self): |
| 269 | try: |
266 | try: |
| 270 | myPath = os.path.dirname(__file__) |
267 | myPath = os.path.dirname(__file__) |
| 271 | os.chdir(myPath) |
268 | os.chdir(myPath) |
| Line 296... | Line 293... | ||
| 296 | pass |
293 | pass |
| 297 | # output = a |
294 | # output = a |
| 298 | 295 | ||
| 299 | """load config defaults, or last config if exists""" |
296 | """load config defaults, or last config if exists""" |
| 300 | """run startup.cfg""" |
297 | """run startup.cfg""" |
| 301 | - | ||
| 302 |
|
298 | print 'INIT: Loading daemon configuration' |
| 303 | # self.cfg.filterCfg.importFilter('DefaultFilter') |
- | |
| 304 | # self.cfg.stateSpaceCfg.importSpace('DefaultStateSpace') |
- | |
| 305 | # self.cfg.stateSpaceCfg.loadSpaces() |
- | |
| 306 | # self.cfg.filterCfg.loadFilters() |
- | |
| 307 |
|
299 | self.cfg = DaemonConfig() |
| 308 |
|
300 | self.cfg.load() |
| 309 | 301 | ||
| 310 | # print 'INIT: Starting selected can interface' |
- | |
| 311 | # ''' nodeif thread launch ''' |
- | |
| 312 | self.ifNotifier = self.IfNotifier(self) |
302 | self.ifNotifier = self.IfNotifier(self) |
| 313 |
|
303 | for nodeIf in self.cfg.nodeInterfaces: |
| 314 | # |
- | |
| 315 | # ifConfig = NodeIfCanStim.DEFAULT_CONFIG |
- | |
| 316 |
|
304 | nodeIf.setIfNotifier(self.ifNotifier) |
| 317 | # # ifConfig = NodeIfSerial.DEFAULT_CONFIG |
- | |
| 318 | ifConfig = NodeIfTCP.DEFAULT_CONFIG |
- | |
| 319 |
|
305 | |
| 320 | atexit.register(self.exitHelper) |
306 | atexit.register(self.exitHelper) |
| 321 | sys.excepthook = self.exceptHelper |
307 | sys.excepthook = self.exceptHelper |
| - | 308 | ||
| 322 |
|
309 | print 'INIT: Starting configured node interfaces' |
| 323 |
|
310 | for nodeIf in self.cfg.nodeInterfaces: |
| 324 | # |
- | |
| 325 | if |
311 | if not nodeIf.start(): |
| 326 | print 'FATAL: Failed to initialize node interface.' |
312 | print 'FATAL: Failed to initialize node interface.' |
| 327 | print 'This may indicate that the interface is unavailable, ' \ |
313 | print 'This may indicate that the interface is unavailable, ' \ |
| 328 | 'or that you do not have permission to access it.' |
314 | 'or that you do not have permission to access it.' |
| 329 | sys.exit(-1) |
315 | sys.exit(-1) |
| 330 | 316 | ||
| 331 |
|
317 | print 'INIT: Initialize/gather info on connected can nodes and load associated state' |
| 332 |
|
318 | print 'INIT: Starting configured servers' |
| 333 | # server threads launch |
- | |
| 334 | 319 | ||
| 335 | print 'CanDaemon v1 ready' |
320 | print 'CanDaemon v1 ready' |
| 336 | print 'Enter command or type \"help\" for a list of commands' |
321 | print 'Enter command or type \"help\" for a list of commands' |
| 337 | while not self.terminated: |
322 | while not self.terminated: |
| 338 | input = raw_input('> ') |
323 | input = raw_input('> ') |
| 339 | self.parseCommand(input) |
324 | self.parseCommand(input) |
| 340 | 325 | ||
| 341 | print 'Shutdown' |
326 | print 'Shutdown' |
| 342 |
|
327 | self.cfg.save() |
| - | 328 | ||
| 343 |
|
329 | for nodeIf in self.cfg.nodeInterfaces: |
| 344 |
|
330 | if self.nodeIf.running(): |
| 345 | - | ||
| 346 |
|
331 | print 'Stopping If thread' |
| 347 |
|
332 | self.nodeIf.stop() |
| 348 | 333 | ||
| 349 | print 'Waiting 0.1..' |
334 | print 'Waiting 0.1..' |
| 350 | time.sleep(0.1) # wait for threads |
335 | time.sleep(0.1) # wait for threads |
| 351 | print 'Terminating' |
336 | print 'Terminating' |
| 352 | sys.exit(0) # will except if there are threads remaining |
337 | sys.exit(0) # will except if there are threads remaining |