Exemple #1
0
def deviceRemove(deviceId):
    rows,isEof = database.db_select_device(id=deviceId)
    if len(rows)<=0:
        return
    accessKey = rows[0][8]
    row = database.db_select_accessKey(accessKey)
    if len(row) <= 0:# key is not alloced
        return error.ERR_CODE_OK_
    # accessDevices format: 'id1|id2|id3|...'
    accessDevices = row[0][3]
    if accessDevices is None:
        return error.ERR_CODE_OK_

    devices = accessDevices.split(':')
    try:
        devices.remove(str(deviceId))
    except:
        return error.ERR_CODE_OK_
    if len(devices) == 0:
        updDevices = None
    else:
        updDevices = str(devices[0])
        for item in devices[1:]:
            updDevices = updDevices + ":" + str(item)
    return database.db_update_accessKey(accessKey,updDevices)
Exemple #2
0
def deviceIsAccessed(deviceId,accessKey):
    row = database.db_select_accessKey(accessKey)
    if len(row) <= 0:# key is not alloced
        return False
    # accessDevices format: 'id1|id2|id3|...'
    accessDevices = row[0][3]
    if accessDevices is not None:
        devices = accessDevices.split(':')
        for item in devices:
            if deviceId==int(item):
                return False #already access
    return True
Exemple #3
0
def allocAccessKey(userId):
    #这个需要查询一下,规避随机生成的key值重复,虽然这种概率会非常小
    randomkey = ''
    loopCnt = 0  #for dead loop
    while True:
        loopCnt += 1
        if loopCnt>10:
            return None
        randomkey = __randomKey()
        if len(database.db_select_accessKey(randomkey))==0:
            break
    if error.ERR_CODE_OK_ == database.db_insert_accessKey(randomkey,userId):
        return randomkey
    else:
        return None
Exemple #4
0
def deviceAccess(deviceId, accessKey):
    row = database.db_select_accessKey(accessKey)
    if len(row) <= 0:# key is not alloced
        return error.ERR_CODE_PARAINVALID
    # accessDevices format: 'id1|id2|id3|...'
    accessDevices = row[0][3]
    if accessDevices is not None:
        devices = accessDevices.split(':')
        if len(devices)>=5:#one key can access max 5 devices.
            return error.ERR_CODE_ACCESS_MAX
        for item in devices:
            if deviceId==int(item):
                return error.ERR_CODE_OK_ #already access

    if accessDevices is None:
        updDevices = str(deviceId)
    else:
        updDevices = accessDevices+':'+str(deviceId)
    return database.db_update_accessKey(accessKey,updDevices)