Ejemplo n.º 1
0
async def device_publish_task(request_dict) -> TaskResult:
    """
    :param request_dict:
           required key: topic, prefixTopic,
                         payload, deviceID, tenantID, protocol
           optional key: streamID
    """
    request_dict['taskID'] = generate_uuid()  # emqx publish taskID
    device_uid = request_dict['deviceID']
    task_id = request_dict['taskID']
    if not request_dict.get('streamID'):
        request_dict['streamID'] = None
    # insert publish logs
    insert_sql = insert_publish_logs_sql.format(**request_dict)
    insert_status = await db.execute(sql=insert_sql)
    if not insert_status:
        message = f"insert {device_uid} publish logs errors!"
        return get_task_result(status=4, message=message)
    # get publish json
    request_dict['payload'] = json.loads(request_dict['payload'])
    publish_json_func = PROTOCOL_PUBLISH_JSON_FUNC.get(
        request_dict['protocol'])
    if not publish_json_func:
        message = f"{device_uid} publish not support this protocol"
        return get_task_result(status=4, message=message)
    publish_json = publish_json_func(request_dict)
    # emqx publish
    task_result = await _emqx_device_publish(publish_json, device_uid, task_id)
    return task_result
Ejemplo n.º 2
0
async def api_count_task() -> TaskResult:
    aggr_result = {}
    date_now = arrow.now(tz=project_config['TIMEZONE'])

    aggr_result['api_count_hour'] = await _hour_api_count()
    if date_now.hour == 0:
        aggr_result['api_count_day'] = await _day_api_count()
    if date_now.day == 1 and date_now.hour == 0:
        aggr_result['api_count_month'] = _month_api_count()
    # aggr_result: {'api_count_hour': True}
    aggr_status = aggr_result.values()
    # status 3:success 4:failure
    if aggr_status and all(aggr_status):
        task_result = get_task_result(
            status=3, message='Api count aggr success', result=aggr_result)
    else:
        task_result = get_task_result(
            status=4, message='Api count aggr failed', result=aggr_result)
    return task_result
Ejemplo n.º 3
0
async def emqx_bills_aggr_task() -> TaskResult:
    aggr_result = {}
    date_now = arrow.now(tz=project_config['TIMEZONE'])

    aggr_result['emqx_bills_hour'] = await _hour_emqx_bills_count()
    if date_now.hour == 0:
        aggr_result['emqx_bills_day'] = await _day_emqx_bills_count()
    if date_now.day == 1 and date_now.hour == 0:
        aggr_result['emqx_bills_month'] = await _month_emqx_bills_count()
    aggr_status = aggr_result.values()
    if aggr_status and all(aggr_status):
        task_result = get_task_result(
            status=3, message='Emqx bills aggr success', result=aggr_result
        )
    else:
        task_result = get_task_result(
            status=4, message='Emqx bills aggr failed', result=aggr_result
        )
    return task_result
Ejemplo n.º 4
0
def _emqx_client_publish(publish_json, created_publish_log):
    emqx_pub_url = f"{current_app.config['EMQX_API']}/mqtt/publish"
    with SyncHttp(auth=current_app.config['EMQX_AUTH']) as sync_http:
        response = sync_http.post(emqx_pub_url, json=publish_json)
    handled_response = handle_emqx_publish_response(response)
    base_result = {
        'deviceID': created_publish_log.deviceID,
        'taskID': created_publish_log.taskID
    }
    if handled_response['status'] == 3:
        task_result = get_task_result(
            status=3, message='Client publish success', result=base_result
        )
    else:
        error_message = handled_response.get('error') or 'Client publish failed'
        task_result = get_task_result(
            status=4, message=error_message, result=base_result
        )
        created_publish_log.publishStatus = 0
        created_publish_log.update()
    return task_result
Ejemplo n.º 5
0
async def device_events_aggr_task() -> TaskResult:
    aggr_result = {}
    aggr_device_events_hour = await db.execute(device_events_hour_aggr_sql)
    aggr_result['device_events_hour'] = aggr_device_events_hour

    date_now = arrow.now(tz=project_config['TIMEZONE'])
    if date_now.hour == 0:
        aggr_result['device_events_day'] = await db.execute(
            device_events_day_aggr_sql)
    if date_now.day == 1 and date_now.hour == 0:
        aggr_result['device_events_month'] = await db.execute(
            device_events_month_aggr_sql)
    aggr_status = aggr_result.values()
    if aggr_status and all(aggr_status):
        task_result = get_task_result(status=3,
                                      message='Device events aggr success',
                                      result=aggr_result)
    else:
        task_result = get_task_result(status=4,
                                      message='Device events aggr failed',
                                      result=aggr_result)
    return task_result
Ejemplo n.º 6
0
async def _emqx_device_publish(publish_json, device_uid, task_id):
    emqx_pub_url = project_config['EMQX_PUBLISH_URL']
    async with AsyncHttp(auth=project_config['EMQX_AUTH']) as async_http:
        response = await async_http.post_url(emqx_pub_url, json=publish_json)
        logger.info(response)
    handled_response = handle_emqx_publish_response(response)
    base_result = {'deviceID': device_uid, 'taskID': task_id}
    if handled_response['status'] == 3:
        task_result = get_task_result(status=3,
                                      message='Device publish success',
                                      task_id=task_id,
                                      result=base_result)
    else:
        error_message = handled_response.get(
            'error') or 'Device publish failed'
        task_result = get_task_result(status=4,
                                      message=error_message,
                                      task_id=task_id,
                                      result=base_result)
        update_sql = update_publish_logs_sql.format(taskID=task_id,
                                                    publishStatus=0)
        await db.execute(sql=update_sql)
    return task_result