Skip to content
commands.py 8.81 KiB
Newer Older
developer7's avatar
developer7 committed
'''
  global command functions
developer7's avatar
developer7 committed
  1.8
developer7's avatar
developer7 committed
'''

import sys
import os
import signal
developer7's avatar
developer7 committed
import functools
developer7's avatar
developer7 committed
import time
developer7's avatar
developer7 committed

developer7's avatar
developer7 committed
from inspect import getargspec
developer7's avatar
developer7 committed
from socket import gethostname, gethostbyname
from ctypes import c_uint64
developer7's avatar
developer7 committed
from shell.device_list import S_DEVICE
developer7's avatar
developer7 committed
from shell.ais_shell import GetListInformation, AISGetLibraryVersionStr, AisShellpGetProgramVersion, AISRestart, \
developer7's avatar
developer7 committed
    AISDestroy, AISOpen, AISClose, AISUpdateAndGetCount, wr_status, sys_get_timezone_info, AISGetTime, AISSetTime, \
developer7's avatar
developer7 committed
    log_get, whitelist_read, blacklist_read, get_io_state, lock_open, relay_toogle, AISGetVersion, ee_lock, ee_unlock,\
    log_by_index, log_by_time, get_unread_log_one, whitelist_write, blacklist_write, TestLights, PASS, \
    password_set_default, password_change, edit_device_list, MainLoop, fw_update, config_file_rd,\
    config_file_wr, DDEV_HND
developer7's avatar
developer7 committed

developer7's avatar
developer7 committed

developer7's avatar
developer7 committed
from constants import *
developer7's avatar
developer7 committed



def GetDLLVersion():
    return "AIS_GetDLLVersion() >> %s\n" % AISGetLibraryVersionStr()

developer7's avatar
developer7 committed

developer7's avatar
developer7 committed
def GetProgramVersion():
developer7's avatar
developer7 committed
    from ais_http import AisHttpGetProgramVersion
developer7's avatar
developer7 committed
    return '{0}\n{1}\n'.format(AisHttpGetProgramVersion(), AisShellpGetProgramVersion())

developer7's avatar
developer7 committed

developer7's avatar
developer7 committed
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'

developer7's avatar
developer7 committed

developer7's avatar
developer7 committed
def GetAISUpdateAndGetCount():
developer7's avatar
developer7 committed
    try:
developer7's avatar
developer7 committed
        res, count = AISUpdateAndGetCount()
        return ' COUNT >> {0} {1}'.format(count, wr_status('', res))
    except WindowsError:
        pass
developer7's avatar
developer7 committed

developer7's avatar
developer7 committed

def GetAISGetTime(devHnd):
    return AISGetTime(devHnd)[0]
developer7's avatar
developer7 committed

developer7's avatar
developer7 committed

def SetAISSetTime(devHnd):
    return '{0}\n{1}'.format(sys_get_timezone_info(), AISSetTime(devHnd))
developer7's avatar
developer7 committed

developer7's avatar
developer7 committed

def GetInformation(devHnd):
    return '{0}\n{1}\n{2}'.format(AISGetTime(devHnd)[0], sys_get_timezone_info(), AISGetVersion(devHnd))
developer7's avatar
developer7 committed


def GetLogByIndex(devHnd, **lpq):
developer7's avatar
developer7 committed
        start_index = ''.join(lpq[START_INDEX])
        end_index = ''.join(lpq[END_INDEX])
developer7's avatar
developer7 committed
        if len(start_index) == 0 or len(end_index) == 0:
            return 'You must enter values for START INDEX  and END INDEX !'
        else:
            return log_by_index(int(start_index), int(end_index), devHnd)
developer7's avatar
developer7 committed

developer7's avatar
developer7 committed

def GetLogByTime(devHnd, **lpq):
developer7's avatar
developer7 committed
        start_time = (''.join(lpq.get(START_TIME)))
        end_time = (''.join(lpq.get(END_TIME)))
        if len(start_time) == 0 or len(end_time) == 0:
            return 'You must enter values for START TIME  and END TIME !'
        else:
            return log_by_time(int(start_time), int(end_time), devHnd)
developer7's avatar
developer7 committed


def GetUnreadLog(devHnd, **lpq):
developer7's avatar
developer7 committed
    dev = S_DEVICE
    dev.hnd = devHnd
developer7's avatar
developer7 committed
    get_unread_log = int(''.join(lpq.get(UNREAD_LOG)))
developer7's avatar
developer7 committed
    return get_unread_log_one(get_unread_log, dev)
def WhiteListWrite(devHnd, **lpq):
developer7's avatar
developer7 committed
        white_list_write = ''.join(lpq.get(WHITE_LIST_WRITE))
        if len(white_list_write) == 0:
            return 'You must enter values for write white list !'
        else:
            return whitelist_write(white_list_write, devHnd)
developer7's avatar
developer7 committed

developer7's avatar
developer7 committed

def BlackListWrite(devHnd, **lpq):
developer7's avatar
developer7 committed
        black_list_write = ''.join(lpq.get(BLACK_LIST_WRITE))
        if len(black_list_write) == 0:
            return 'You must enter values for write black list !'
        else:
            return blacklist_write(black_list_write, devHnd)
def GetTestLights(devHnd, **lpq):
developer7's avatar
developer7 committed
        lights_choise = ''.join(lpq[LIGHTS])
        return TestLights(lights_choise, devHnd)
developer7's avatar
developer7 committed

def SetAppPassword(**lpq):
developer7's avatar
developer7 committed
    global PASS
    mess = ["Actual application password is :{0}\n" .format(PASS),
            "Patch - new pass = default pass\n"]
    set_def_pass = ''.join(lpq.get(DEFAULT_APP_PASS))
    if len(set_def_pass) == 0:
        set_def_pass = PASS
        return ''.join((mess[0], mess[1], password_set_default(set_def_pass)))
    else:
        PASS = set_def_pass
    return ''.join((mess[0], password_set_default(set_def_pass)))

developer7's avatar
developer7 committed

def SetDevicePassword(devHnd, **lpq):
developer7's avatar
developer7 committed
    global PASS
    new_pass = ''.join(lpq.get(NEW_PASS))
    mess = ["Old password is actual application password :{0}\n".format(PASS),
            "New password for units ( and application ) :{0}\n".format(new_pass),
            "Patch - new pass = default pass\n",
            "Try set new password for units= {0}\n" .format(new_pass)]
    if len(new_pass) == 0:
        new_pass = PASS
        return ''.join((mess[0], mess[2], password_change(new_pass, devHnd)))
developer7's avatar
developer7 committed
    else:
        return ''.join((mess[0], mess[1], password_change(new_pass, devHnd)))
developer7's avatar
developer7 committed

def SetEditListDevices(**lpq):
developer7's avatar
developer7 committed
    if lpq.get(DEVICE_TYPE):
        device_type = ''.join(lpq.get(DEVICE_TYPE))
    if lpq.get(DEVICE_ID):
        device_id = ''.join(lpq.get(DEVICE_ID))
    edit_list_choise = ''.join(lpq[EDIT_LIST])
developer7's avatar
developer7 committed
    if edit_list_choise == AVAILABLE_DEVICES:
        return edit_device_list(1)
    elif edit_list_choise == ACTUAL_LIST:
        return edit_device_list(2)
    elif edit_list_choise == CLEAR_LIST:
        return edit_device_list(3)
    elif edit_list_choise == ADD_DEVICE:
        if len(device_type) == 0 or len(device_id) == 0:
            return "You must enter values in the relevant fields !"
        else:
            return edit_device_list(4, "AIS_List_AddDeviceForCheck", int(device_type), int(device_id))
    elif edit_list_choise == ERASE_DEVICE:
            return edit_device_list(5, "AIS_List_EraseDeviceForCheck", int(device_type), int(device_id))
    else:
        return ''

developer7's avatar
developer7 committed

def GetReadRTE(**lpq):
developer7's avatar
developer7 committed
    rt = ''.join(lpq.get(RTE))
    dev = S_DEVICE
developer7's avatar
developer7 committed
    if rt.strip() == '':
        return 'Must enter value for seconds !'
    else:
        seconds = int(rt)
        rt = ''
        stop_time = c_uint64()
        stop_time = time.time() + seconds
        mess = ["RTE for {0} sec \n" .format(seconds),
                "End RTE listen"]
        while time.ctime(time.time()) < time.ctime(stop_time):
            for hnd in DDEV_HND.keys():
developer7's avatar
developer7 committed
                dev.hnd = hnd
                r, rte = MainLoop(dev)
                if rte:
                    rt += rte
            time.sleep(.5)
        return ''.join((mess[0], rt, mess[1]))


def FirmwareUpdate(devHnd, **lpq):
developer7's avatar
developer7 committed
    try:
        if lpq.get('fw_file'):
            fw_file_name = ''.join(lpq.get('fw_file_name'))
            with open(fw_file_name, 'wb') as out:
                out.write(''.join(lpq['fw_file']))
            out.close()
            os.remove(fw_file_name)
            return fw_update(devHnd, fw_name=fw_file_name)
developer7's avatar
developer7 committed
    except Exception as exc:
        return "ERROR: {0}" .format(exc)


def ReadConfigFromDevToFile(**lpq):
developer7's avatar
developer7 committed
    try:
        if lpq.get(CONFIG_FILE_READ):
            return 'DEPRECATED AIS_Config_Send(), AIS_Config_Read()'
            conf_file_rd = ''.join(lpq.get(CONFIG_FILE_READ))
            return config_file_rd(fname=conf_file_rd)
            ip = gethostbyname(gethostname())

            with open(conf_file_rd + '.config', 'wb') as out:
                http_request(ip + ":" + str(HTTP_SERVER_PORT), out.write(conf_file_rd + '.config'))
    except Exception as exc:
        return "ERROR: {0}".format(exc)

developer7's avatar
developer7 committed

def ReadConfigFromFileToDev(**lpq):
developer7's avatar
developer7 committed
    try:
        if lpq.get(CONFIG_FILE_WR_NAME):
            return 'DEPRECATED AIS_Config_Send(), AIS_Config_Read()'
            confFileNameWR = ''.join(lpq.get(CONFIG_FILE_WR_NAME))
            confFileWR = ''.join(lpq.get(CONFIG_FILE_WRITE))
            with open(confFileNameWR, 'wb') as out:
                out.write(confFileWR)
            return config_file_wr(fname=confFileNameWR)
        else:
            return 'NO FILE'
    except Exception as exc:
        return "ERROR: {0}".format(exc)


developer7's avatar
developer7 committed
action = {'q': GetListInformation,
          'v': GetDLLVersion,
developer7's avatar
developer7 committed
          'V': GetProgramVersion,
developer7's avatar
developer7 committed
          'X': GetExitApp,
          'R': AISRestart,
          'D': AISDestroy,
          'o': AISOpen,
          'c': AISClose,
developer7's avatar
developer7 committed
          'd': GetAISUpdateAndGetCount,
          't': GetAISGetTime,
developer7's avatar
developer7 committed
          'T': SetAISSetTime,
          'w': whitelist_read,
          'b': blacklist_read,
          'g': get_io_state,
          'G': lock_open,
          'y': relay_toogle,
          'f': AISGetVersion,
          'E': ee_lock,
          'e': ee_unlock,
developer7's avatar
developer7 committed
          'i': GetInformation,
          'l': log_get,
developer7's avatar
developer7 committed
          'n': GetLogByIndex,
          'N': GetLogByTime,
          'u': GetUnreadLog,
          'W': WhiteListWrite,
          'B': BlackListWrite,
          'L': GetTestLights,
          'P': SetAppPassword,
          'p': SetDevicePassword,
          'Q': SetEditListDevices,
          'r': GetReadRTE,
          'F': FirmwareUpdate,
          's': ReadConfigFromDevToFile,
          'S': ReadConfigFromFileToDev
developer7's avatar
developer7 committed
          }


def command(getfunction, ldev, **pq):
developer7's avatar
developer7 committed
    try:
        r = getargspec(action[getfunction])
        if r[0].__len__() == 0:
developer7's avatar
developer7 committed
            return action[getfunction].__call__()
developer7's avatar
developer7 committed
        elif r[0] and r[2]:
            return action[getfunction].__call__(ldev.hnd, **pq)
developer7's avatar
developer7 committed
        else:
            return action[getfunction].__call__(ldev.hnd)
    except KeyError:
        return False

developer7's avatar
developer7 committed