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 |
||
| 738 | olof | 8 | import time |
| 736 | olof | 9 | |
| 10 | from CanPktHandlerBase import CanPktHandlerBase |
||
| 11 | from CanPktHandler1 import CanPktHandler1 |
||
| 12 | |||
| 13 | from NodeIfBase import NodeIfBase |
||
| 14 | from NodeIfCanStim import NodeIfCanStim |
||
| 15 | from NodeIfSerial import NodeIfSerial |
||
| 16 | |||
| 17 | from DaemonConfig import DaemonConfig |
||
| 18 | |||
| 19 | daemonApp = None |
||
| 20 | |||
| 21 | class CanDaemon(): |
||
| 22 | |||
| 23 | nodeIf = None |
||
| 738 | olof | 24 | ifNotifier = None |
| 25 | terminated = False |
||
| 736 | olof | 26 | |
| 738 | olof | 27 | class IfNotifier(): |
| 28 | |||
| 29 | UNDEFINED = 0 |
||
| 30 | TERMINATE = 1 |
||
| 31 | |||
| 32 | def notify(self, msg): |
||
| 33 | if msg == self.TERMINATE: |
||
| 34 | print 'Main terminate notify' |
||
| 35 | self.terminated = True |
||
| 36 | else: |
||
| 37 | print 'UNDEFINED NOTIFY' |
||
| 38 | |||
| 736 | olof | 39 | def __init__ (self): |
| 40 | pass |
||
| 41 | |||
| 42 | |||
| 43 | def usage(self): |
||
| 44 | pass |
||
| 45 | |||
| 46 | |||
| 47 | def helpCommands(self): |
||
| 48 | print 'quit, exit : shutdown CanDaemon' |
||
| 49 | print 'help : display help' |
||
| 50 | |||
| 51 | |||
| 52 | def exitHelper(self): |
||
| 737 | olof | 53 | if self.nodeIf is not None and self.nodeIf.running(): |
| 736 | olof | 54 | self.nodeIf.stop() |
| 738 | olof | 55 | |
| 736 | olof | 56 | |
| 57 | def run(self): |
||
| 58 | try: |
||
| 59 | demoPath = os.path.dirname(__file__) |
||
| 60 | os.chdir(demoPath) |
||
| 61 | except: |
||
| 62 | return |
||
| 63 | |||
| 737 | olof | 64 | print 'INIT: Start logging' |
| 736 | olof | 65 | log.basicConfig(level=log.DEBUG) |
| 66 | |||
| 737 | olof | 67 | print 'INIT: Get config options from command line / config file' |
| 736 | olof | 68 | cfg = DaemonConfig() |
| 69 | |||
| 70 | try: |
||
| 71 | opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="]) |
||
| 72 | except getopt.GetoptError: |
||
| 73 | print 'Invalid usage' |
||
| 74 | usage() |
||
| 75 | sys.exit(2) |
||
| 76 | # output = None |
||
| 77 | # verbose = False |
||
| 78 | for o, a in opts: |
||
| 79 | if o == "-v": |
||
| 80 | pass |
||
| 81 | # verbose = True |
||
| 82 | if o in ("-h", "--help"): |
||
| 83 | usage() |
||
| 84 | sys.exit() |
||
| 85 | if o in ("-o", "--output"): |
||
| 86 | pass |
||
| 87 | # output = a |
||
| 88 | |||
| 737 | olof | 89 | print 'INIT: Loading filters and spaces' |
| 738 | olof | 90 | # cfg.filterCfg.importFilter('DefaultFilter') |
| 91 | # cfg.stateSpaceCfg.importSpace('DefaultStateSpace') |
||
| 736 | olof | 92 | cfg.stateSpaceCfg.loadSpaces() |
| 93 | cfg.filterCfg.loadFilters() |
||
| 94 | cfg.setupFilterBindings() |
||
| 95 | cfg.setupFilterChain() |
||
| 737 | olof | 96 | |
| 97 | print 'INIT: Starting selected can interface' |
||
| 736 | olof | 98 | ''' nodeif thread launch ''' |
| 738 | olof | 99 | self.ifNotifier = self.IfNotifier() |
| 736 | olof | 100 | pktHandler = CanPktHandler1(cfg) |
| 101 | |||
| 738 | olof | 102 | ifConfig = NodeIfCanStim.DEFAULT_CONFIG |
| 103 | self.nodeIf = NodeIfCanStim(ifConfig, pktHandler, self.ifNotifier) |
||
| 104 | # ifConfig = NodeIfSerial.DEFAULT_CONFIG |
||
| 736 | olof | 105 | |
| 737 | olof | 106 | atexit.register(self.exitHelper) |
| 738 | olof | 107 | # sys.excepthook = self.exceptHelper |
| 108 | # self.nodeIf = NodeIfSerial(pktHandler, ifConfig) |
||
| 736 | olof | 109 | |
| 110 | if not self.nodeIf.start(): |
||
| 737 | olof | 111 | print 'FATAL: Failed to initialize node interface.' |
| 738 | olof | 112 | print 'This may indicate that the interface is unavailable, ' \ |
| 113 | 'or that you do not have permission to access it.' |
||
| 736 | olof | 114 | sys.exit(-1) |
| 115 | |||
| 116 | print 'Initialize/gather info on connected can nodes and load associated state' |
||
| 117 | print 'Start selected TCP and UDP server(s)' |
||
| 118 | # server threads launch |
||
| 119 | |||
| 120 | print 'CanDaemon v1 ready' |
||
| 121 | print 'Enter command or type \"help\" for a list of commands' |
||
| 738 | olof | 122 | |
| 123 | while not self.terminated: |
||
| 736 | olof | 124 | input = raw_input('> ').strip().lower() |
| 125 | if input == 'quit' or input == 'exit': |
||
| 738 | olof | 126 | self.terminated = True |
| 736 | olof | 127 | elif input == 'help': |
| 738 | olof | 128 | self.helpCommands() |
| 129 | else: |
||
| 130 | print 'Unknown command: ' + input |
||
| 736 | olof | 131 | |
| 132 | print 'Shutdown' |
||
| 738 | olof | 133 | if self.nodeIf.running(): |
| 134 | self.nodeIf.stop() |
||
| 135 | |||
| 736 | olof | 136 | cfg.stateSpaceCfg.saveSpaces() |
| 137 | cfg.filterCfg.saveFilters() |
||
| 738 | olof | 138 | |
| 139 | time.sleep(0.1) # wait for threads |
||
| 140 | sys.exit(0) # will except if there are threads remaining |
||
| 736 | olof | 141 | |
| 142 | #--------------------------------------------------------------------------- |
||
| 143 | |||
| 144 | def main(): |
||
| 145 | daemonApp = CanDaemon() |
||
| 737 | olof | 146 | # install_thread_excepthook() |
| 736 | olof | 147 | daemonApp.run() |
| 148 | |||
| 149 | #--------------------------------------------------------------------------- |
||
| 150 | |||
| 151 | if __name__ == '__main__': |
||
| 152 | __name__ = 'Main' |
||
| 153 | main() |