コード例 #1
0
def main():
    global evt_thread
    global conn_evt_thread

    try:
        from PIL import Image
    except:
        print "This program requires the pillow package."
        print 'Please run \'sudo pip install pillow\' and try again.'
        return

    try:
        from asciimatics.scene import Scene
    except:
        print "This program requires the asciimatics package."
        print 'Please run \'sudo pip install asciimatics\' and try again.'
        return

    # add for readline bug fix
    os.unsetenv('LINES')
    os.unsetenv('COLUMNS')

    # read config
    if not CONFIG.init_config(LOG):
        print 'read config fail...'
        return

    # set log
    LOG.set_default_log('sonawatched_err.log')

    # set history log
    history_log = USER_LOG()
    history_log.set_log('evt_history.log', CONFIG.get_cli_log_rotate(), int(CONFIG.get_cli_log_backup()))

    # set cli log
    cli_log = USER_LOG()
    cli_log.set_log('sonawatched_cli.log', CONFIG.get_cli_log_rotate(), int(CONFIG.get_cli_log_backup()))
    CLI.set_cli_log(cli_log, history_log)

    # set trace log
    trace_log = USER_LOG()
    trace_log.set_log('sonawatched_trace.log', CONFIG.get_trace_log_rotate(), int(CONFIG.get_trace_log_backup()))
    TRACE.set_trace_log(trace_log)

    # read log option
    LOG.set_log_config()

    # Start RESTful server for event
    evt = multiprocessing.Event()
    disconnect_evt = multiprocessing.Event()
    rest_evt = multiprocessing.Event()

    # create listen event thread
    evt_thread = threading.Thread(target=listen_evt, args=(evt,))
    evt_thread.daemon = False
    evt_thread.start()

    conn_evt_thread = threading.Thread(target=listen_disconnect_evt, args=(disconnect_evt, rest_evt))
    conn_evt_thread.daemon = False
    conn_evt_thread.start()

    try:
        # create rest server process
        child_pid = cli_rest.rest_server_start(evt, disconnect_evt, rest_evt, history_log)
    except:
        print 'Rest Server failed to start'
        print 'Processing shutdown...'
        LOG.exception_err_write()
        SYS.set_sys_thr_flag(False)
        conn_evt_thread.join()
        evt_thread.join()
        return

    # regi event
    if CLI.send_regi() == False:
        print 'Event registration failed'
        print 'Processing shutdown...'
        LOG.exception_err_write()
        SYS.set_sys_thr_flag(False)
        conn_evt_thread.join()
        evt_thread.join()
        return

    # get event list
    if CLI.get_event_list() == False:
        print 'Get Event List failed'
        print 'Processing shutdown...'
        LOG.exception_err_write()
        SYS.set_sys_thr_flag(False)
        conn_evt_thread.join()
        evt_thread.join()
        return

    # inquiry controller info
    try:
        res_code, sys_info = CLI.req_sys_info()
    except:
        print "Cannot connect rest server."
        print 'Processing shutdown...'
        SYS.set_sys_thr_flag(False)
        conn_evt_thread.join()
        evt_thread.join()
        return

    if res_code != 200:
        print "Rest server does not respond to the request."
        print "code = " + str(res_code)
        print 'Processing shutdown...'
        if not SYS.disconnect_type == 'disconnect':
            CLI.send_regi('unregi')
        SYS.set_sys_thr_flag(False)
        conn_evt_thread.join()
        evt_thread.join()
        return

    SYS.set_sys_info(sys_info)

    # set command list
    CLI.set_cmd_list()

    # set trace cond list
    TRACE.set_cnd_list()

    # set search list
    CLI.set_search_list()

    set_readline_opt()

    # system check
    check_system()

    # select input menu
    select_menu()

    # exit
    print 'Processing shutdown...'
    if not SYS.disconnect_type == 'disconnect':
        CLI.send_regi('unregi')

    SYS.set_sys_thr_flag(False)
    conn_evt_thread.join()
    evt_thread.join()

    return