Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 736 | olof | 1 | #!/bin/env python |
| 2 | |||
| 3 | import os |
||
| 4 | import sys |
||
| 5 | import logging as log |
||
| 6 | import getopt |
||
| 7 | import atexit |
||
| 8 | |||
| 9 | from CanPktHandlerBase import CanPktHandlerBase |
||
| 10 | from CanPktHandler1 import CanPktHandler1 |
||
| 11 | |||
| 12 | from NodeIfBase import NodeIfBase |
||
| 13 | from NodeIfCanStim import NodeIfCanStim |
||
| 14 | from NodeIfSerial import NodeIfSerial |
||
| 15 | |||
| 16 | from DaemonConfig import DaemonConfig |
||
| 17 | |||
| 18 | daemonApp = None |
||
| 19 | |||
| 20 | class CanDaemon(): |
||
| 21 | |||
| 22 | nodeIf = None |
||
| 23 | |||
| 24 | def __init__ (self): |
||
| 25 | pass |
||
| 26 | |||
| 27 | |||
| 28 | def usage(self): |
||
| 29 | pass |
||
| 30 | |||
| 31 | |||
| 32 | def helpCommands(self): |
||
| 33 | print 'quit, exit : shutdown CanDaemon' |
||
| 34 | print 'help : display help' |
||
| 35 | |||
| 36 | |||
| 37 | def exitHelper(self): |
||
| 737 | olof | 38 | if self.nodeIf is not None and self.nodeIf.running(): |
| 736 | olof | 39 | self.nodeIf.stop() |
| 40 | |||
| 41 | |||
| 42 | def exceptHelper(self, type, value, tb): |
||
| 737 | olof | 43 | import traceback |
| 44 | traceback.print_exc() |
||
| 45 | print 'Exception in main program, shutting down threads' |
||
| 736 | olof | 46 | self.exitHelper() |
| 47 | |||
| 737 | olof | 48 | |
| 736 | olof | 49 | def run(self): |
| 50 | try: |
||
| 51 | demoPath = os.path.dirname(__file__) |
||
| 52 | os.chdir(demoPath) |
||
| 53 | except: |
||
| 54 | return |
||
| 55 | |||
| 737 | olof | 56 | print 'INIT: Start logging' |
| 736 | olof | 57 | log.basicConfig(level=log.DEBUG) |
| 58 | |||
| 737 | olof | 59 | print 'INIT: Get config options from command line / config file' |
| 736 | olof | 60 | cfg = DaemonConfig() |
| 61 | |||
| 62 | try: |
||
| 63 | opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="]) |
||
| 64 | except getopt.GetoptError: |
||
| 65 | print 'Invalid usage' |
||
| 66 | usage() |
||
| 67 | sys.exit(2) |
||
| 68 | # output = None |
||
| 69 | # verbose = False |
||
| 70 | for o, a in opts: |
||
| 71 | if o == "-v": |
||
| 72 | pass |
||
| 73 | # verbose = True |
||
| 74 | if o in ("-h", "--help"): |
||
| 75 | usage() |
||
| 76 | sys.exit() |
||
| 77 | if o in ("-o", "--output"): |
||
| 78 | pass |
||
| 79 | # output = a |
||
| 80 | |||
| 737 | olof | 81 | print 'INIT: Loading filters and spaces' |
| 736 | olof | 82 | # cfg.filterCfg.importFilter('DefaultFilter') |
| 83 | # cfg.stateSpaceCfg.importSpace('DefaultStateSpace') |
||
| 84 | cfg.stateSpaceCfg.loadSpaces() |
||
| 85 | cfg.filterCfg.loadFilters() |
||
| 86 | cfg.setupFilterBindings() |
||
| 87 | cfg.setupFilterChain() |
||
| 737 | olof | 88 | |
| 89 | print 'INIT: Starting selected can interface' |
||
| 736 | olof | 90 | ''' nodeif thread launch ''' |
| 91 | pktHandler = CanPktHandler1(cfg) |
||
| 92 | |||
| 93 | # cfg = NodeIfCanStim.DEFAULT_CONFIG |
||
| 94 | # nodeif = NodeIfCanStim(cfg, pkthandler) |
||
| 95 | ifConfig = NodeIfSerial.DEFAULT_CONFIG |
||
| 96 | |||
| 737 | olof | 97 | atexit.register(self.exitHelper) |
| 98 | sys.excepthook = self.exceptHelper |
||
| 99 | |||
| 100 | self.nodeIf = NodeIfSerial(pktHandler, ifConfig) |
||
| 736 | olof | 101 | |
| 102 | if not self.nodeIf.start(): |
||
| 737 | olof | 103 | print 'FATAL: Failed to initialize node interface.' |
| 104 | print 'This may indicate that the interface is busy or that you don\'t have ' \ |
||
| 105 | 'permission to access it.' |
||
| 736 | olof | 106 | sys.exit(-1) |
| 107 | |||
| 108 | print 'Initialize/gather info on connected can nodes and load associated state' |
||
| 109 | print 'Start selected TCP and UDP server(s)' |
||
| 110 | # server threads launch |
||
| 111 | |||
| 112 | print 'CanDaemon v1 ready' |
||
| 113 | print 'Enter command or type \"help\" for a list of commands' |
||
| 114 | terminated = False |
||
| 115 | while not terminated: |
||
| 116 | input = raw_input('> ').strip().lower() |
||
| 117 | if input == 'quit' or input == 'exit': |
||
| 118 | terminated = True |
||
| 119 | elif input == 'help': |
||
| 120 | helpCommands() |
||
| 121 | |||
| 122 | print 'Shutdown' |
||
| 123 | self.nodeIf.stop() |
||
| 124 | cfg.stateSpaceCfg.saveSpaces() |
||
| 125 | cfg.filterCfg.saveFilters() |
||
| 126 | |||
| 127 | #--------------------------------------------------------------------------- |
||
| 128 | |||
| 737 | olof | 129 | #def install_thread_excepthook(): |
| 130 | # """Workaround for sys.excepthook thread bug |
||
| 131 | # (https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1230540&group_id=5470). |
||
| 132 | # Call once from __main__ before creating any threads. |
||
| 133 | # If using psyco, call psyco.cannotcompile(threading.Thread.run) |
||
| 134 | # since this replaces a new-style class method. |
||
| 135 | # """ |
||
| 136 | # import sys, threading |
||
| 137 | # run_old = threading.Thread.run |
||
| 138 | # def run(*args, **kwargs): |
||
| 139 | # print 'WORKAROUND RUN' |
||
| 140 | # try: |
||
| 141 | # run_old(*args, **kwargs) |
||
| 142 | # except (KeyboardInterrupt, SystemExit): |
||
| 143 | # raise |
||
| 144 | # except: |
||
| 145 | # sys.excepthook(*sys.exc_info()) |
||
| 146 | # threading.Thread.run = run |
||
| 147 | |||
| 148 | #--------------------------------------------------------------------------- |
||
| 149 | |||
| 736 | olof | 150 | def main(): |
| 151 | daemonApp = CanDaemon() |
||
| 737 | olof | 152 | # install_thread_excepthook() |
| 736 | olof | 153 | daemonApp.run() |
| 154 | |||
| 155 | #--------------------------------------------------------------------------- |
||
| 156 | |||
| 157 | if __name__ == '__main__': |
||
| 158 | __name__ = 'Main' |
||
| 159 | main() |