Esempio n. 1
0
def god_get_device():
    dbg('god_get_device')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        imei = data.get('imei')
        device_id = data.get('device_id')
        dbg(data)
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    god = dbapi.get_god(openluat_user_id=current_user.id)
    if not god:
        raise ApiError('ERROR_GOD_NOT_FOUND', error.ERROR_GOD_NOT_FOUND)

    role_bit = int(current_user.role[5])
    if role_bit not in (1, 2):
        raise ApiError('ERROR_AGENT_NO_PERMISSION',
                       error.ERROR_AGENT_NO_PERMISSION)

    device = None
    if imei:
        device = dbapi.get_device(imei=imei)
    elif device_id:
        device = dbapi.get_device(id=int(device_id))
    if not device:
        raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND)
    if role_bit == 1:
        god_agents = dbapi.get_god_agent(god_id=god.id)
        g = (god_agent.agent_id for god_agent in god_agents)
        if device.agent_id not in g:
            raise ApiError('ERROR_DEVICE_NOT_FOUND',
                           error.ERROR_DEVICE_NOT_FOUND)

    device_address = dbapi.get_device_address(id=device.address_id)
    address = ""
    if device_address:
        address = device_address.address
    agentname = ''
    agent = dbapi.get_agent(id=device.owner_agent_id)
    if agent:
        agentname = agent.name

    reply['data'] = {
        'id': device.id,
        'imei': device.imei,
        'address': address,
        'operator': agentname,
        'remark': device.remark
    }

    return make_response(jsonify(reply), status_code)
Esempio n. 2
0
def get_nopay_salesman_devices():
    dbg('get_nopay_salesman_devices')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    page = request.args.get('page', 1, type=int)
    psize = request.args.get('psize', 10, type=int)

    agent_id = current_user.agent_id
    agent = dbapi.get_agent(id=agent_id)
    if not agent:
        raise ApiError('ERROR_AGENT_NOT_FOUND', error.ERROR_AGENT_NOT_FOUND)
    count = 0
    data = []
    if agent.salesman == 0:
        count, devices = dbapi.get_device(agent_id=agent_id,
                                          salesman=0,
                                          page=page,
                                          psize=psize,
                                          nopay=1)
    elif agent.salesman == 1:
        count, devices = dbapi.get_device(agent_id=agent_id,
                                          salesman=1,
                                          page=page,
                                          psize=psize,
                                          nopay=1)
    if count:
        if devices:
            for device in devices:
                device_address = dbapi.get_device_address(id=device.address_id)
                address = ""
                if device_address:
                    address = device_address.address
                imei = device.imei
                nopay_count = dbapi.get_record_no_pay(imei=imei, count=True)
                info = {
                    'id': device.id,
                    'imei': device.imei,
                    'cat': device.cat,
                    'address': address,
                    'remark': device.remark,
                    'nopay_count': nopay_count
                }
                data.append(info)

    reply['data'] = {'count': count, 'devices': data}

    return make_response(jsonify(reply), status_code)
Esempio n. 3
0
def get_device_addresses():
    dbg('get_device_addresses')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    agent_id = current_user.agent_id
    device_addresses = dbapi.get_device_address(agent_id=agent_id)

    data = []
    if device_addresses:
        for address in device_addresses:
            info = {
                'id': address.id,
                'region': address.region,
                'address': address.address,
            }
            data.append(info)

    reply['data'] = data

    return make_response(jsonify(reply), status_code)
Esempio n. 4
0
def get_coupon_devices():
    dbg('get_coupon_devices')
    reply ,status_code = {'code': 0, 'msg': ''}, 200

    page  = request.args.get('page', 1, type=int)
    psize = request.args.get('psize', 10, type=int)
    address_id = request.args.get('address_id', 0, type=int)

    agent_id = current_user.agent_id
    count = 0
    data = []
    count, devices = dbapi.get_device(owner_agent_id=agent_id, 
        page=page, psize=psize, coupon=1, address_id=address_id)
    if count:
        if devices:
            for device in devices:
                device_address = dbapi.get_device_address(id=device.address_id)
                address = ""
                if device_address:
                    address = device_address.address
                device_coupons = dbapi.get_device_coupon(device_id=device.id)
                coupons = list(map(lambda x:x.coupon_id, device_coupons))
                info = {
                    'id': device.id,
                    'imei': device.imei,
                    'cat': device.cat,
                    'address': address,
                    'remark': device.remark,
                    'coupon': device.coupon,
                    'coupons': coupons
                }
                data.append(info)

    reply['data'] = {
        'count': count,
        'devices': data
    }

    return make_response(jsonify(reply), status_code)
Esempio n. 5
0
def get_nopay_devices():
    dbg('get_nopay_devices')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    page = request.args.get('page', 1, type=int)
    psize = request.args.get('psize', 10, type=int)

    agent_id = current_user.agent_id
    count = 0
    data = []
    count, devices = dbapi.get_device(agent_id=agent_id,
                                      page=page,
                                      psize=psize,
                                      nopay=1)
    if count:
        if devices:
            for device in devices:
                device_address = dbapi.get_device_address(id=device.address_id)
                address = ""
                if device_address:
                    address = device_address.address
                imei = device.imei
                nopay_count = dbapi.get_record_no_pay(imei=imei, count=True)
                info = {
                    'id': device.id,
                    'imei': device.imei,
                    'cat': device.cat,
                    'address': address,
                    'remark': device.remark,
                    'nopay_count': nopay_count
                }
                data.append(info)

    reply['data'] = {'count': count, 'devices': data}

    return make_response(jsonify(reply), status_code)
Esempio n. 6
0
def get_god_devices():
    dbg('get_god_devices')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        god_id = data['god_id']
        page = request.args.get('page', 1, type=int)
        psize = request.args.get('psize', 10, type=int)
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    role_bit = int(current_user.role[5])
    if role_bit != 2:
        raise ApiError('ERROR_AGENT_NO_PERMISSION',
                       error.ERROR_AGENT_NO_PERMISSION)

    count = 0
    data = []

    ct1, agents = dbapi.get_god_agent(god_id=god_id, page=1, psize=999)
    for agent in agents:
        agent_id = agent.id
        ct2, devices = dbapi.get_device(agent_id=agent_id,
                                        page=page,
                                        psize=psize)
        if ct2:
            count += ct2
            for device in devices:
                device_address = dbapi.get_device_address(id=device.address_id)
                address = ""
                if device_address:
                    address = device_address.address
                operator = ''
                operator_level = 0
                agent = dbapi.get_agent(id=device.owner_agent_id)
                if agent:
                    operator = agent.name
                    operator_level = agent.level
                imei = device.imei
                status = dbapi.get_cws_device_status(imei)
                online = -1
                if status:
                    online = status.get('online', -1)
                latest_report = dbapi.get_device_latestreport(imei)
                if not latest_report:
                    online = 0
                else:
                    if imei == '868575021151255':
                        dbg(latest_report['time'])
                    nowts = datetime.now().timestamp()
                    lrts = latest_report['time'].timestamp()
                    if nowts - lrts > 150:
                        if online == 1:
                            online = 0
                info = {
                    'id': device.id,
                    'imei': device.imei,
                    'cat': device.cat,
                    'address': address,
                    'use_state': 0,  # 0空闲,1使用
                    'comm_state': online,  # 0关机,1在线
                    'operator': operator,
                    'operator_level': operator_level,
                    'owner_agent_id': device.owner_agent_id,
                    'map_display': device.map_display,
                    'remark': device.remark,
                    'l4': device.l4,
                    'l3': device.l3,
                    'l2': device.l2,
                    'l1': device.l1
                }
                data.append(info)

    reply['data'] = {'count': count, 'devices': data}

    return make_response(jsonify(reply), status_code)
Esempio n. 7
0
def get_salesman_devices():
    dbg('get_salesman_devices')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    page = request.args.get('page', 1, type=int)
    psize = request.args.get('psize', 10, type=int)

    agent_id = current_user.agent_id
    agent = dbapi.get_agent(id=agent_id)
    if not agent:
        raise ApiError('ERROR_AGENT_NOT_FOUND', error.ERROR_AGENT_NOT_FOUND)
    count = 0
    data = []
    if agent.salesman == 0:
        count, devices = dbapi.get_device(agent_id=agent_id,
                                          salesman=0,
                                          page=page,
                                          psize=psize)
    elif agent.salesman == 1:
        count, devices = dbapi.get_device(agent_id=agent_id,
                                          salesman=1,
                                          page=page,
                                          psize=psize)
    if count:
        if devices:
            for device in devices:
                device_address = dbapi.get_device_address(id=device.address_id)
                address = ""
                if device_address:
                    address = device_address.address
                agent = dbapi.get_agent(id=device.salesman_agent_id)
                if agent:
                    operator = agent.name
                    operator_level = agent.slevel
                else:
                    operator = ''
                    operator_level = 0
                imei = device.imei
                status = dbapi.get_cws_device_status(imei)
                online = -1
                if status:
                    online = status.get('online', -1)
                latest_report = dbapi.get_device_latestreport(imei)
                if not latest_report:
                    online = 0
                    lng = '0'
                    lat = '0'
                    signal = 0
                else:
                    nowts = datetime.now().timestamp()
                    lrts = latest_report['time'].timestamp()
                    if nowts - lrts > 150:
                        if online == 1:
                            online = 0
                    lng = latest_report['lng']
                    lat = latest_report['lat']
                    signal = latest_report['signal']
                iccid = ''
                cws_device = dbapi.get_cws_device_iccid(imei)
                if cws_device:
                    iccid = cws_device['iccid']
                info = {
                    'id': device.id,
                    'imei': device.imei,
                    'cat': device.cat,
                    'address': address,
                    'use_state': 0,  # 0空闲,1使用
                    'comm_state': online,  # 0关机,1在线
                    'lng': lng,
                    'lat': lat,
                    'signal': signal,
                    'iccid': iccid,
                    'operator': operator,
                    'operator_level': operator_level,
                    'owner_agent_id': device.owner_agent_id,
                    'salesman_agent_id': device.salesman_agent_id,
                    'map_display': device.map_display,
                    'remark': device.remark,
                    'nopay': device.nopay,
                    'product_unit_price': device.product_unit_price,
                    'product_min_money': device.product_min_money,
                    'product_unit': device.product_unit,
                    'product_unit_pluse': device.product_unit_pluse,
                    'l4': device.l4,
                    'l3': device.l3,
                    'l2': device.l2,
                    'l1': device.l1,
                    'sl1': device.sl1,
                    'sl2': device.sl2,
                    'sl3': device.sl3,
                    'pulse_period': device.low,
                    'pulse_width': device.high
                }
                data.append(info)

    reply['data'] = {'count': count, 'devices': data}

    return make_response(jsonify(reply), status_code)
Esempio n. 8
0
def search_device():
    dbg('search_device')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        imei = data.get('imei')
        device_id = data.get('device_id')
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    device = None
    if imei:
        device = dbapi.get_device(imei=imei)
    elif device_id:
        device = dbapi.get_device(id=device_id)
    if not device:
        raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND)

    has_permission = 0
    cur_agent = current_user.cur_agent
    if cur_agent.salesman == 0:
        dds = dbapi.get_device_distribution(device_id=device.id)
        for dd in dds:
            if dd.to_agent == current_user.agent_id:
                has_permission = 1
                break
    elif cur_agent.salesman == 1:
        items = dbapi.get_device_distribution_salesman(device_id=device.id)
        for item in items:
            if item.to_agent == current_user.agent_id:
                has_permission = 1
                break

    if not has_permission:
        raise ApiError('ERROR_AGENT_NO_PERMISSION',
                       error.ERROR_AGENT_NO_PERMISSION)

    device_address = dbapi.get_device_address(id=device.address_id)
    address = ""
    if device_address:
        address = device_address.address
    agent = dbapi.get_agent(id=device.owner_agent_id)
    imei = device.imei
    status = dbapi.get_cws_device_status(imei)
    online = -1
    if status:
        online = status.get('online', -1)
    latest_report = dbapi.get_device_latestreport(imei)
    if not latest_report:
        online = 0
        lng = '0'
        lat = '0'
        signal = 0
    else:
        nowts = datetime.now().timestamp()
        lrts = latest_report['time'].timestamp()
        if nowts - lrts > 150:
            if online == 1:
                online = 0
        lng = latest_report['lng']
        lat = latest_report['lat']
        signal = latest_report['signal']
    iccid = ''
    cws_device = dbapi.get_cws_device_iccid(imei)
    if cws_device:
        iccid = cws_device['iccid']

    info = {
        'id': device.id,
        'imei': device.imei,
        'cat': device.cat,
        'address': address,
        'use_state': 0,  # 0空闲,1使用
        'comm_state': online,  # 0关机,1在线
        'lng': lng,
        'lat': lat,
        'signal': signal,
        'iccid': iccid,
        'operator': agent.name,
        'operator_level': agent.level,
        'owner_agent_id': device.owner_agent_id,
        'map_display': device.map_display,
        'remark': device.remark,
        'nopay': device.nopay,
        'coupon': device.coupon,
        'product_unit_price': device.product_unit_price,
        'product_min_money': device.product_min_money,
        'product_unit': device.product_unit,
        'product_unit_pluse': device.product_unit_pluse,
        'l4': device.l4,
        'l3': device.l3,
        'l2': device.l2,
        'l1': device.l1,
        'sl3': device.sl3,
        'sl2': device.sl2,
        'sl1': device.sl1,
        'pulse_period': device.low,
        'pulse_width': device.high
    }

    reply['data'] = info

    return make_response(jsonify(reply), status_code)
Esempio n. 9
0
def search_nopay_device():
    dbg('search_nopay_device')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        imei = data.get('imei')
        device_id = data.get('device_id')
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    device = None
    if imei:
        device = dbapi.get_device(imei=imei)
    elif device_id:
        device = dbapi.get_device(id=device_id)
    if not device:
        raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND)

    if device.nopay == 0:
        raise ApiError('', error.ERROR_DEVICE_NOT_SUP_NOPAY)

    has_permission = 0
    cur_agent = current_user.cur_agent
    if cur_agent.salesman == 0:
        dds = dbapi.get_device_distribution(device_id=device.id)
        for dd in dds:
            if dd.to_agent == current_user.agent_id:
                has_permission = 1
                break
    elif cur_agent.salesman == 1:
        items = dbapi.get_device_distribution_salesman(device_id=device.id)
        for item in items:
            if item.to_agent == current_user.agent_id:
                has_permission = 1
                break

    if not has_permission:
        raise ApiError('ERROR_AGENT_NO_PERMISSION',
                       error.ERROR_AGENT_NO_PERMISSION)

    device_address = dbapi.get_device_address(id=device.address_id)
    address = ""
    if device_address:
        address = device_address.address

    nopay_count = dbapi.get_record_no_pay(imei=imei, count=True)

    info = {
        'id': device.id,
        'imei': device.imei,
        'cat': device.cat,
        'address': address,
        'remark': device.remark,
        'nopay_count': nopay_count
    }

    reply['data'] = info

    return make_response(jsonify(reply), status_code)