Skip to content
ais_http.py 43.5 KiB
Newer Older
developer8's avatar
developer8 committed
#!/usr/bin/env python

"""
@author: Vladan S
@version: 2.0.1.6 (build)  (lib:4.9.2)
developer8's avatar
developer8 committed
import MySQLdb as mdb

import os
import sys
import cgi
import threading
import time
import traceback
import requests
import urllib2,urllib

from platform import platform
from urlparse import urlparse, parse_qs

from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer

from ctypes import *
from socket import *
from constants import *
from dl_status import *
from ais_readers_list import *

import device_list

HND_LIST  = []    
devCount  = c_long()       
DEV_HND   = device_list .S_DEVICE()
log_t     = device_list .S_LOG()



def GetPlatformLib():       
            if sys.platform.startswith("win32"):
                return windll.LoadLibrary(os.getcwd() + LIB_PATH + WIN_PATH + LIB_WIN32)
            elif sys.platform.startswith("linux"):
                return cdll.LoadLibrary(os.getcwd() + LIB_PATH + LINUX_PATH + LIB_LINUX) #ARMHF_PATH + LIB_ARMHF (for BeagleBoneBlack)
            elif platform().lower().find('armv7l-with-debian') > -1:
                return cdll.LoadLibrary(os.getcwd() + LIB_PATH + LINUX_PATH + LIB_ARM) #ARM
developer8's avatar
developer8 committed
    
    
def AISGetVersion():
        
        hardware_type    = c_int() 
        firmware_version = c_int()
        dev              = DEV_HND
        DL_STATUS = mySO.AIS_GetVersion(dev.hnd,byref(hardware_type),byref(firmware_version))                   
        res = "AIS_GetVersion() hw = %d | fw = %d\n" % (hardware_type.value,firmware_version.value)    
developer8's avatar
developer8 committed
        return res
    
    
def AISUpdateAndGetCount():          
        return mySO.AIS_List_UpdateAndGetCount()
    

         
def AISGetTime():
        
        dev      = DEV_HND       
        currTime = c_uint64()        
        timezone = c_int()
        DST      = c_int()
        offset   = c_int()              
developer8's avatar
developer8 committed
        dev.status = mySO.AIS_GetTime(dev.hnd,byref(currTime),byref(timezone),byref(DST),byref(offset))                            
        if dev.status:
            res = wr_status("AIS_GetTime",dev.status)
            return res
        res = "AIS_GetTime()> {%d(%s):%s} = (tz= %d | dst= %d | offset= %d)  %d | %s\n" % (dev.status,hex(dev.status),E_ERROR_CODES[dev.status],timezone.value,DST.value,offset.value,currTime.value, time.ctime(currTime.value))                                                
developer8's avatar
developer8 committed
        return res
        
    
def AISSetTime():
    
    currTime = c_uint64
    timez    = c_int
    DST      = c_int
    offset   = c_int 
    dev      = DEV_HND
       
    currTime = int(time.time())
    # timez    = time.timezone
    # DST      = time.daylight
    # offset   = -3600
    
    timez    = mySO.sys_get_timezone()
    DST      = mySO.sys_get_daylight()
    offset   = mySO.sys_get_dstbias()
developer8's avatar
developer8 committed
    
    ais_set_time = mySO.AIS_SetTime
    ais_set_time.argtypes = (c_void_p,c_char_p,c_uint64,c_int,c_int,c_int)
    ais_set_time.restype = c_int
    result = ais_set_time(dev.hnd,PASS,currTime,timez,DST,offset)
    
    res    = "AIS_SetTime(pass:%s)> timezone=%d | DST=%d |offset=%d {%d(%s)%s}|%s\n" % \
             (PASS,timez,DST,offset,result,hex(result),E_ERROR_CODES[result],time.ctime(currTime))        
developer8's avatar
developer8 committed
    return res
      
    
def AISGetDevicesForCheck():                    
        myStr = mySO.AIS_List_GetDevicesForCheck           
        myStr.restype = c_char_p                                   
developer8's avatar
developer8 committed
        return myStr()
        
    
def AISEraseAllDevicesForCheck():
        mySO.AIS_List_EraseAllDevicesForCheck()
        
   
def AISAddDeviceForCheck(devType,devId):            
        return mySO.AIS_List_AddDeviceForCheck(devType,devId)
    
                                

def blacklist_write(black_list_write):    
    dev        = DEV_HND
    dev.status = mySO.AIS_Blacklist_Write(dev.hnd,PASS,black_list_write)
    res        = 'AIS_Blacklist_Write(pass:%s):b_list= %s > %s\n' %  (PASS,black_list_write,dl_status2str(dev.status))
    return res

def whitelist_write(white_list_write):       
    dev        = DEV_HND
    dev.status = mySO.AIS_Whitelist_Write(dev.hnd,PASS,white_list_write)
    res = '\nAIS_Whitelist_Write(pass:%s):w_list= %s > %s\n' %  (PASS,white_list_write,dl_status2str(dev.status))
    return res

def print_percent_hdr():
        i = c_int
        sys.stdout.write("%")
        for i in range(0,101):            
            sys.stdout.write(str(i % 10))
        sys.stdout.write("\n%=")
  
def AISOpen():        
        dev      = DEV_HND
        for hnd in HND_LIST:
            aop  = mySO.AIS_Open(hnd)                                           
developer8's avatar
developer8 committed
        dev.hnd  = HND_LIST[0]  #default           
        print "DEFAULT DEVICE:dev[%d] | hnd[0x%X] \n" % ((HND_LIST.index(dev.hnd) + 1),dev.hnd)
developer8's avatar
developer8 committed

developer8's avatar
developer8 committed
def dev_list():
    list_init = c_bool
    list_init = False
    if not list_init:
        ListDevices() #prepare device list
        list_init = True

    devCount = AISUpdateAndGetCount()
    res      = "AIS_List_UpdateAndGetCount()= [%d]\n" % devCount
    if devCount:
        GetListInformation()            
        AISOpen()                                            
    else:
        res = "NO DEVICE FOUND"
        print res
    return res 
 
developer8's avatar
developer8 committed

def http_request(path, post_attrib):
    try:
        req = urllib2.Request(path, post_attrib)
        req.add_header("Content-type", "application/x-www-form-urlencoded")
        page = urllib2.urlopen(req).read()
    except Exception as e:
        print e


def AIS_GetLog_Set():
    dev       = DEV_HND
    DL_STATUS = mySO.AIS_GetLog_Set(dev.hnd, PASS)
    res       = DL_STATUS,hex( DL_STATUS),E_ERROR_CODES[ DL_STATUS]
    return res     

        
             
# def GetInfoAndDeviceCount():            
        # print  AISUpdateAndGetCount()
developer8's avatar
developer8 committed

def ListDevices():
            
        deviceType = E_KNOWN_DEVICE_TYPES['DL_AIS_BASE_HD_SDK']        
developer8's avatar
developer8 committed
        AISEraseAllDevicesForCheck()        
        
        deviceId = 1        
        DL_STATUS =  AISAddDeviceForCheck(deviceType, deviceId) 
       
        deviceId = 3        
        DL_STATUS =  AISAddDeviceForCheck(deviceType, deviceId) 
        
Loading full blame...