Esempio n. 1
0
async def operation_bindDeviceSN(request):
    user_id = request['session'].get('user_id')
    user_auth = readUserAuth(user_id)
    deviceSN_list = request.form.get('deviceSN').split(',')
    print('待激活设备:', deviceSN_list)
    if user_auth in 'ABC':
        res = []
        # 判断设备所属公司和user_id所属公司是否一致
        taxPayer_user = mysqlGetUserTaxPayer(user_id)
        taxPayer_device = mysqlGetDeviceTaxPayer(deviceSN_list[0])
        if taxPayer_user != taxPayer_device:
            print('无操作权限')
            return response.json({'status': 'nopower'})
        # 验证用户是否可以操作设备
        for deviceSN in deviceSN_list:
            alias = mysqlGetDeviceAlias(deviceSN)
            status = judgeUserAuth(user_id, [alias])
            if status:
                switchDeviceStat(deviceSN, 'C1')
            else:
                res.append(alias)
        if res:
            print(res, '中的设备无权操作')
            return response.json({'status': 'warn', 'res': res})
        print('设备激活成功')
        return response.json({'status': 'success'})
    else:
        print('无操作权限')
        return response.json({'status': 'nopower'})
Esempio n. 2
0
async def operation_modifyFarmInfo(request):
    user_id = request['session'].get('user_id')
    tax_payer = mysqlGetUserTaxPayer(user_id)
    user_auth = readUserAuth(user_id)
    if user_auth != 'A':
        return response.json({'status': 'nopower'})
    old_farm_name = request.form.get('old_farm_name')
    new_farm_name = request.form.get('new_farm_name')
    print('old_farm_name:', old_farm_name, 'new_farm_name:', new_farm_name)
    # 将修改信息保存至mysql
    status = mysqlChangeFarmName(tax_payer, old_farm_name, new_farm_name)
    if not status:
        print('mysql修改失败')
        return response.json({'status': 'warn'})
    # 将修改信息保存至redis
    if not redis_ex.lrem(tax_payer, old_farm_name, 0):
        print('redis修改失败')
        return response.json({'status': 'warn'})
    pen_list = redis_ex.lrange(tax_payer + '/' + old_farm_name, 0, -1)
    redis_ex.lpush(tax_payer, new_farm_name)
    redis_ex.delete(tax_payer + '/' + old_farm_name)
    for i in pen_list:
        redis_ex.lpush(tax_payer + '/' + new_farm_name, i)
    print('猪场名修改成功')
    return response.json({'status': 'success'})
Esempio n. 3
0
async def operation_addmember(request):
    user_id = request['session'].get('user_id')  # 操作人员的user_id
    user_auth = readUserAuth(user_id)  # 操作人员的权限
    target_userID = request.form.get('add_userID')  # 待添加员工的user_id
    if user_auth in 'AB':
        add_userAuth = readUserAuth(target_userID)
        # 若待添加员工的权限为Z,方可添加
        if add_userAuth == 'Z':
            # 根据alias判断待赋予员工的权限
            target_alias_list = json.loads(
                request.form.get('alias'))  # 待绑定的alias列表
            target_userAuth_judge = judgeAuthFromAlias(
                target_alias_list[0])  # 根据alias判断的权限
            target_userAuth_real = request.form.get(
                'add_userAuth')  # 前端发来实际的权限
            print('判断的auth:', target_userAuth_judge, '实际的auth:',
                  target_userAuth_real)
            # 验证前端发来的权限合理
            # 验证待添加的alias属于操作人可管理的alias,
            if target_userAuth_judge <= target_userAuth_real and judgeUserAuth(
                    user_id, target_alias_list):
                # 获取用户纳税人识别号
                tax_payer = mysqlGetUserTaxPayer(user_id)
                # 将员工信息添加到对应猪场or猪舍下,并赋予对应的权限
                status = mysqlAddMember(target_userID, tax_payer,
                                        target_alias_list,
                                        target_userAuth_real)
                if not status:
                    return response.json({'status': 'warn'})
                return response.json({'status': 'success'})
    elif user_auth == 'P':
        tax_payer = request.form.get('tax_payer')
        print('userid:', user_id, '纳税人识别号:', tax_payer)
        status = mysqlAddMember(target_userID,
                                tax_payer,
                                alias_list=['/'],
                                user_auth='A')
        if not status:
            return response.json({'status': 'warn'})
        return response.json({'status': 'success'})
    else:
        return response.json({'status': 'nopower'})
Esempio n. 4
0
async def operation_addDeviceSN(request):
    user_id = request['session'].get('user_id')
    user_auth = readUserAuth(user_id)
    deviceSN = request.form.get('deviceSN')
    device_status = readDeviceStatus(deviceSN)

    alias = request.form.get('alias')  # 设备将要绑定的地址'/黄陂/第1舍/第一栏'
    print('deviceSN:', deviceSN, 'alias:', alias)
    # 如果是卖家销售的权限
    if user_auth == 'M' and device_status == 'A':
        if redis_ex.get(deviceSN):
            return response.json({'status': 'deviceHasBinded'})  # 设备已被绑定
        else:
            redis_ex.set(name=deviceSN, value=user_id, ex=30)
            return response.json({'status': 'success'})
    # 如果是买家设备管理人的权限ABC
    elif user_auth in 'ABC' and device_status[0] == 'C':
        # 判断设备所属公司和user_id所属公司是否一致,
        # 判断设备将要绑定的alias是否属于该用户可控范围
        taxPayer_user = mysqlGetUserTaxPayer(user_id)
        taxPayer_device = mysqlGetDeviceTaxPayer(deviceSN)
        if taxPayer_user != taxPayer_device and not judgeUserAuth(
                user_id, [alias]):
            print('无操作权限')
            return response.json({'status': 'nopower'})
        # 验证设备是否已经绑定
        dev_status = mysqlGetDeviceAlias(deviceSN)
        if dev_status:
            print('设备已经绑定')
            return response.json({'status': 'deviceHasBinded'})
        # 判断同一公司下alias是否已存在
        status = mysqlGetStyInfo(taxPayer_user, [alias], ['alias'])
        # 若存在
        if status:
            print('alias:', alias, '已存在')
            return response.json({'status': 'aliasexist'})
        # 若满足,将绑定的信息保存至数据库
        status = mysqlAddDeviceSN(deviceSN, alias)
        if not status:
            print('绑定失败,写入数据库失败')
            return response.json({'status': 'warn'})
        print('绑定成功')
        # 绑定成功后,将猪场唯一标识hogpenID(纳税人识别号+猪场名)发送给下位机,不管下位机是否开机
        HogpenID = taxPayer_user + alias  # HogpenID:2314141242/黄陂/第1舍/第1栏
        publish.single("ComServer/Hogpen/" + deviceSN,
                       payload=HogpenID,
                       hostname=hostname,
                       port=port,
                       qos=1)
        return response.json({'status': 'success'})
    else:
        print('设备无法被绑定')
        return response.json({'status':
                              'cannotBind'})  #设备无法被绑定(可能是权限问题,或设备已被绑定)
Esempio n. 5
0
async def operation_getFarmInfo(request, get_type):
    user_id = request['session'].get('user_id')
    tax_payer = mysqlGetUserTaxPayer(user_id)
    user_auth = readUserAuth(user_id)
    if get_type == 'farm':
        res = {}
        if user_auth == 'A':
            res['farm_list'] = redis_ex.lrange(tax_payer, 0, -1)
        else:
            alias_list = mysqlGetUserAlias(user_id)
            res['farm_list'] = list(
                set([alias.split('/')[1] for alias in alias_list]))
        res['status'] = 'success'
        print('猪场:', res['farm_list'])
        return response.json(res)
    elif get_type == 'pen':
        farm_name = request.form.get('farm_name')
        res = {}
        if user_auth in 'AB':
            res['pen_list'] = redis_ex.lrange(tax_payer + '/' + farm_name, 0,
                                              -1)
        else:
            alias_list = mysqlGetUserAlias(user_id)
            for alias in alias_list:
                if farm_name not in alias:
                    alias_list.pop(alias)
            res['pen_list'] = list(
                set([alias.split('/')[2] for alias in alias_list]))
        res['status'] = 'success'
        print('猪舍:', res['pen_list'])
        return response.json(res)
    elif get_type == 'sty':
        farm_name = request.form.get('farm_name')
        pen_name = request.form.get('pen_name')
        res = {}
        if user_auth in 'ABC':
            status = mysqlGetStyInfo(tax_payer,
                                     ['/' + farm_name + '/' + pen_name + '/'],
                                     ['deviceSN', 'alias'])
            res['device_list'] = [
                device + (readDeviceStatus(device[0]), ) for device in status
            ]
        else:
            alias_list = mysqlGetUserAlias(user_id)
            for alias in alias_list:
                if farm_name + '/' + pen_name not in alias:
                    alias_list.pop(alias)
            res['pen_list'] = list(
                set([alias.split('/')[3] for alias in alias_list]))
        res['status'] = 'success'
        print('猪栏:', res['device_list'])
        return response.json(res)
Esempio n. 6
0
async def operation_modifyMember(request, type):
    user_id = request['session'].get('user_id')  # 操作人员的user_id
    user_auth = readUserAuth(user_id)  # 操作人员的权限
    target_userID = request.form.get('target_userID')  # 待修改员工的user_id
    alias = request.form.get('alias')  # 待修改的alias
    # 验证操作用户权限为A或B
    # 验证待添加或删除的alias属于操作人可管理的alias,
    if user_auth in 'AB' and judgeUserAuth(user_id, alias):
        status = mysqlModifyUserAlias(type, target_userID, alias)
        if not status:
            return response.json({'status': 'warn'})
        return response.json({'status': 'success'})
    else:
        return response.json({'status': 'nopower'})
Esempio n. 7
0
async def operation_deletemember(request):
    user_id = request['session'].get('user_id')
    user_auth = readUserAuth(user_id)
    delete_userID = request.form.get('delete_userID')  # 将被删除的员工的user_id
    # 验证用户是否属于同一公司
    operation_tax_payer = mysqlGetUserTaxPayer(user_id)
    delete_tax_payer = mysqlGetUserTaxPayer(delete_userID)
    delete_alias_list = mysqlGetUserAlias(delete_userID)
    # 验证操作人有权限删除员工
    if operation_tax_payer == delete_tax_payer and user_auth in 'AB' and judgeUserAuth(
            user_id, delete_alias_list):
        # 删除该公司下的某一员工信息,并将用户的权限改为Z(不属于任何公司)
        status = mysqlDeleteUser(delete_userID)
        if not status:
            return response.json({'status': 'warn'})
        return response.json({'status': 'success'})
    else:
        return response.json({'status': 'nopower'})
Esempio n. 8
0
async def operation_deleteFarmInfo(request):
    user_id = request['session'].get('user_id')
    tax_payer = mysqlGetUserTaxPayer(user_id)
    alias = request.form.get('alias')
    alias_list = alias.split(',')
    user_auth = readUserAuth(user_id)
    len_of_alias = len(alias_list[0].split('/'))
    print('要删除的alias:', alias_list)
    # 删除多个猪场 [/黄陂/,/江夏/]
    if len_of_alias == 3 and user_auth in 'A':
        status = mysqlDeleteFarm(tax_payer, alias_list)
        if not status:
            print('猪场删除失败!')
            return response.json({'status': 'warn'})
        for i in alias_list:
            farm_name = i.split('/')[1]
            redis_ex.lrem(tax_payer, farm_name)
            redis_ex.delete(tax_payer + '/' + farm_name)
        print(farm_name, '猪场删除成功!')
        return response.json({'status': 'success'})
    # 删除猪舍 [/黄陂/第1舍/,/黄陂/第2舍/]
    elif len_of_alias == 4 and user_auth in 'AB' and alias_list[0][-1] == '/':
        status = mysqlDeleteFarm(tax_payer, alias_list)
        if not status:
            return response.json({'status': 'warn'})
        for i in alias_list:
            farm_name = i.split('/')[1]
            pen_name = i.split('/')[2]
            redis_ex.lrem(tax_payer + '/' + farm_name, pen_name)
            print(pen_name, '猪舍删除成功!')
        return response.json({'status': 'success'})
    # 删除猪栏 [/黄陂/第1舍/第1栏,/黄陂/第1舍/第2栏]
    elif len_of_alias == 4 and user_auth in 'AB' and alias_list[0][-1] != '/':
        status = mysqlDeleteFarm(tax_payer, alias_list)
        if not status:
            return response.json({'status': 'warn'})
        print(alias_list, '猪舍删除成功!')
        return response.json({'status': 'success'})
    else:
        print('无访问权限')
        return response.json({'status': 'nopower'})
Esempio n. 9
0
async def operation_BuildFarm(request, build_type):
    # 从数据库查找操作人权限,并验证是否为老板
    user_id = request['session'].get('user_id')
    tax_payer = mysqlGetUserTaxPayer(user_id)
    user_auth = readUserAuth(user_id)
    if build_type == 'farm' and user_auth == 'A':
        farm_name = request.form.get('farm_name')
        farmName_list = redis_ex.lrange(tax_payer, 0, -1)
        # 若猪场名存在
        if farm_name in farmName_list:
            print(farm_name, '猪场已存在!')
            return response.json({'status': 'existed'})
        # 将猪场信息保存至redis
        redis_ex.lpush(tax_payer, farm_name)
        print(farm_name, '猪场新建成功!')
        return response.json({'status': 'success'})
    elif build_type == 'pen' and user_auth in 'AB':
        farm_name = request.form.get('farm_name')
        pen_name = request.form.get('pen_name')
        pen_name_list = pen_name.split(',')
        pen_name_list = list(set(pen_name_list))
        exsit_pen = []
        for i in pen_name_list:
            if i in redis_ex.lrange(tax_payer + '/' + farm_name, 0, -1):
                exsit_pen.append(i)
            else:
                redis_ex.rpush(tax_payer + '/' + farm_name, i)
        if exsit_pen != []:
            print('猪舍:', exsit_pen, '已存在')
            return response.json({'status': 'existed', 'exsit_pen': exsit_pen})
        else:
            print('猪舍:', pen_name_list, '新建成功')
            return response.json({'status': 'success'})
    else:
        print('无访问权限')
        return response.json({'status': 'nopower'})
Esempio n. 10
0
async def operation_unbindDeviceSN(request):
    user_id = request['session'].get('user_id')
    user_auth = readUserAuth(user_id)
    deviceSN_list = request.form.get('deviceSN').split(',')
    # 如果是卖家销售人员,且设备归属于操作人
    # if user_auth == 'M' and redis_ex.get(deviceSN_list) == user_id:
    #     # 解绑设备
    #     redis_ex.delete(deviceSN_list)
    #     return response.json({'status': 'success'})
    print('待取消激活的设备:', deviceSN_list)
    # 如果是买家设备管理人员ABC
    if user_auth in 'ABC':
        res = []
        # 判断设备所属公司和user_id所属公司是否一致
        taxPayer_user = mysqlGetUserTaxPayer(user_id)
        taxPayer_device = mysqlGetDeviceTaxPayer(deviceSN_list[0])
        if taxPayer_user != taxPayer_device:
            print('无操作权限')
            return response.json({'status': 'nopower'})
        # 验证用户是否可以操作设备
        for deviceSN in deviceSN_list:
            alias = mysqlGetDeviceAlias(deviceSN)
            status = judgeUserAuth(user_id, [alias])
            if status:
                switchDeviceStat(deviceSN, 'C0')
            else:
                res.append(alias)
        # 若存在不可操作的设备,将其alias返回给前端
        if res:
            print(res, '中的设备无权操作')
            return response.json({'sttaus': 'warn', 'res': res})
        print('取消激活成功')
        return response.json({"status": 'success'})
    else:
        print('无操作权限')
        return response.json({'status': 'nopower'})