''' global command functions 1.2 ''' import sys import os import signal import functools from shell.device_list import S_DEVICE from shell.ais_shell import GetListInformation, AISGetLibraryVersionStr, AisShellpGetProgramVersion, AISRestart, \ AISDestroy, AISOpen, AISClose, AISUpdateAndGetCount, wr_status, sys_get_timezone_info, AISGetTime, AISSetTime, \ log_get, whitelist_read, blacklist_read, get_io_state, lock_open, relay_toogle, AISGetVersion, ee_lock, ee_unlock from ais_http import AisHttpGetProgramVersion dev = S_DEVICE def GetDLLVersion(): return "AIS_GetDLLVersion() >> %s\n" % AISGetLibraryVersionStr() def GetProgramVersion(): return '{0}\n{1}\n'.format(AisHttpGetProgramVersion(), AisShellpGetProgramVersion()) def GetExitApp(): if sys.platform.startswith('linux'): os.system('pkill -9 python') os.kill(os.getpid(), signal.SIGINT) elif sys.platform.startswith('win'): os._exit(0) return '\nServer stopped !\nClose program !\n' def GetAISUpdateAndGetCount(): try: res, count = AISUpdateAndGetCount() return ' COUNT >> {0} {1}'.format(count, wr_status('', res)) except WindowsError: pass def GetAISGetTime(): return AISGetTime(dev)[0] def GetAISSetTime(): return '{0}\n{1}'.format(sys_get_timezone_info(), AISSetTime(dev)) def GetInformation(): return '{0}\n{1}\n{2}'.format(AISGetTime(dev)[0], sys_get_timezone_info(), AISGetVersion(dev)) action = {'q': GetListInformation, 'v': GetDLLVersion, 'V': GetProgramVersion, 'X': GetExitApp, 'R': functools.partial(AISRestart, dev), 'D': functools.partial(AISDestroy, dev), 'o': functools.partial(AISOpen, dev), 'c': functools.partial(AISClose, dev), 'd': GetAISUpdateAndGetCount, 't': GetAISGetTime, 'T': GetAISSetTime, 'l': functools.partial(log_get, dev), 'w': functools.partial(whitelist_read, dev), 'b': functools.partial(blacklist_read, dev), 'g': functools.partial(get_io_state, dev), 'G': functools.partial(lock_open, dev), 'y': functools.partial(relay_toogle, dev), 'f': functools.partial(AISGetVersion, dev), 'E': functools.partial(ee_lock, dev), 'e': functools.partial(ee_unlock, dev), 'i': GetInformation } def command(getfunction, ldev, **pq): global dev dev = ldev try: return action[getfunction].__call__() except KeyError: return 'WRONG COMMAND !!! \nYou must enter the correct command'