int alarm_start_snapshot(WORKER_CTX *worker_ctx, ALARM_PRODUCER *producer, void *iot_info_p)
{
MEDIACENTER_CTX *mediacenter_ctx = NULL;
ALARM_CONSUMER *snapshot_consumer = NULL;
STORAGE_SESSION *storage_session = NULL;
DEV_INFO *dev_info = NULL;
time_t timestamp;
bool is_image_supplement = false, is_pushable = true, is_repeated_event = false;
time_t record_timestamp = -1;
IOT_INFO * iot_info = (IOT_INFO *) iot_info_p;
if (!worker_ctx || !producer) {
DBG_ERR("arg NULL\n");
return -1;
}
DBG_DBG("[alarm snapshot] ip=%s id=%d\n", producer->dev_ip, producer->count_id);
mediacenter_ctx = (MEDIACENTER_CTX*)worker_ctx->top_ctx;
dev_info = get_dev_info(mediacenter_ctx, producer->dev_id, producer->dev_ip);
if (!dev_info) {
DBG_ERR("find device failed\n");
return -1;
}
/* 已推送门铃文本消息,需补充带图门铃事件,其它侦测(防盗报警除外)图片修改为门铃类型 */
if (!dev_info->general_camera && RING_SNAPSHOT_NEEDED == dev_info->need_image_supplyment && EVENT_TYPE_ANTI_THEFT != producer->event_type) {
DBG_DBG("set producer->event_type(%d) to RING, this snapshot is a supplement\n", producer->event_type);
alarm_producer_set_event_type(producer, "RING");
is_image_supplement = true;
}
if (!iot_info) {
/* get iot info */
iot_info = iot_info_find(worker_ctx->iot_info_server, dev_info->dev_id, producer->count_id);
}
/* calculate timestamp */
if (producer->event_time) {
timestamp = producer->event_time;
} else {
timestamp = producer->count_id;
}
if (!dev_info->general_camera) {
timestamp -= common_get_time_diff();
if (timestamp < (time(NULL)-60*60)) {
// DBG_INFO("KC time error\n");
#ifndef TAPO_CAMERA
// FIXME: iot_info can only record event timestamp once
if (iot_info) {
DBG_INFO("Use record time\n");
timestamp = iot_info->event_timestamp;
}
else {
DBG_INFO("Use KH time\n");
timestamp = time(NULL);
}
#else
// DBG_INFO("Use KH time\n");
timestamp = time(NULL);
#endif
}
else {
DBG_DBG("KC time OK\n");
}
}
/* consumer */
#ifdef TAPO_CAMERA
if (dev_info->cloud_storage != TAPOCARE_ENABLE) {
if (dev_info->general_camera) {
if (EVENT_TYPE_DOORBELL_RING == producer->event_type) {
record_timestamp = dev_info->pRecord ? dev_info->pRecord->fileTimeUtcForRing : -1;
if (ring_event_info_filter(&dev_info->ring_info_list, record_timestamp, NULL)) {
ring_event_info_new(NULL, &dev_info->ring_info_list, timestamp, record_timestamp, producer->count_id, dev_info->cloud_storage);
}
}
DBG_DBG("general camera not push\n");
return 0;
}
// no tapocare, normal msg push only
is_pushable = is_alarm_need_notify(dev_info, producer->event_type, false);
if (EVENT_TYPE_DOORBELL_RING == producer->event_type) {
ring_alarm_handler_simple(dev_info, timestamp, producer->count_id);
}
#ifdef MULTILENS_CAMERA
if (alarm_normal_msg_push_request(dev_info, producer->event_type, is_pushable, 0, dev_info->lens_id))
#else
if (alarm_normal_msg_push_request(dev_info, producer->event_type, is_pushable, 0))
#endif
{
DBG_ERR("normal msg_push failed\n");
return -1;
}
return 0;
}
if (producer->content_len < 1) {
// no snapshot, will not trigger cloud connection
return 1;
}
#ifdef AI_ENHANCE
#ifdef MULTILENS_CAMERA
int i = 0;
if (dev_info->dev_lens_num > 1)
{
for(i = 0; i < dev_info->dev_lens_num ;i++) {
if(dev_info->multilens_info[i].lenschn_id == dev_info->lens_id) {
if (dev_info->multilens_info[i].ai_enhanced == 1) {
if (EVENT_TYPE_MOTION == producer->event_type) {
if (!(dev_info->multilens_info[i].cam_bitmap & CAM_MD_BITMAP)) {
DBG_WARN("dev[%s][%d] not enable MD, don't push snapshot\n", dev_info->dev_mac,i);
/* return 1 for save snapshot to sd card or disk */
return 1;
}
}
}
break;
}
}
}
else
#endif
{
if (dev_info->ai_enhanced == 1) {
if (EVENT_TYPE_MOTION == producer->event_type) {
if (!(dev_info->cam_bitmap & CAM_MD_BITMAP)) {
DBG_WARN("dev[%s] not enable MD, don't push snapshot\n", dev_info->dev_mac);
/* return 1 for save snapshot to sd card or disk */
return 1;
}
}
}
}
#endif
#endif
if (dev_info->alarm_snapshot_push_status) {
DBG_DBG("dev %s in alarm snapshot push status, save:%d\n", dev_info->dev_id, producer->event_type);
}
if (dev_info->general_camera) {
switch (dev_info->general_ring_status) {
case RING_SNAPSHOT_NEEDED:
/* ring call 周期内,第一个带图事件标记为RING */
if(producer->event_type != EVENT_TYPE_DOORBELL_RING) {
DBG_ERR("set producer event type to RING\n");
alarm_producer_set_event_type(producer, "RING");
}
break;
default:
break;
}
}
if (producer->event_type == EVENT_TYPE_PANORAMA) {
/* PANORAMA需在推送PANORAMA_START成功后推送 */
if (!dev_info->panorama_start_status) {
DBG_ERR("not sent PANORAMA_START yet\n");
return -1;
}
dev_info->panorama_start_status = 0;/* PANORAMA_START状态清除 */
}
if (!(snapshot_consumer = alarm_snapshot_consumer_no_socket_new(worker_ctx->alarm_server))) {
DBG_ERR("get snapshot_consumer failed\n");
return -1;
}
DBG_DBG("OOO: snapshot consumer: %p", snapshot_consumer);
MYDEBUG("Device Id = %s, snapshot consumer = %p\n", dev_info->dev_id, snapshot_consumer);
alarm_consumer_set_content_len(snapshot_consumer, producer->content_len);
alarm_consumer_set_dev_ip(snapshot_consumer, producer->dev_ip);
alarm_consumer_set_dev_id(snapshot_consumer, dev_info->dev_id);
alarm_consumer_set_dev_model(snapshot_consumer, dev_info->dev_model);
alarm_consumer_set_dev_alias(snapshot_consumer, dev_info->dev_alias);
alarm_consumer_set_count_id(snapshot_consumer, producer->count_id);
alarm_consumer_set_event_timestamp(snapshot_consumer, timestamp);
alarm_consumer_set_region(snapshot_consumer, &producer->region);
alarm_consumer_set_chunked(snapshot_consumer, 0);
alarm_consumer_set_event_type(snapshot_consumer, producer->event_type);
alarm_consumer_set_ts_encoded(snapshot_consumer, 0);
alarm_consumer_set_image_supplement_flag(snapshot_consumer, is_image_supplement);
// FIX: ring_timer_status need update
is_pushable = is_alarm_need_notify(dev_info, snapshot_consumer->event_type, snapshot_consumer->is_image_supplement);
alarm_consumer_set_silent_push_flag(snapshot_consumer, !is_pushable);
if (dev_info->general_camera) {
alarm_consumer_set_general_doorbell_params(snapshot_consumer, dev_info);
}
#ifdef MULTILENS_CAMERA
snapshot_consumer->lens_id = dev_info->lens_id;
#endif
/* set consumer type to support image/alert/summary */
DBG_NOTICE(" MULTILENS_TEST : lensid[%d] ready:%d, event_type:%d ",snapshot_consumer->lens_id, iot_info ? iot_info->ready:0, producer->event_type);
#ifdef MULTILENS_CAMERA
if (alarm_consumer_set_consumer_type(snapshot_consumer, iot_info, dev_info, producer->event_type, snapshot_consumer->lens_id) < 0)
#else
if (alarm_consumer_set_consumer_type(snapshot_consumer, iot_info, dev_info, producer->event_type) < 0)
#endif
{
DBG_ERR("invalid consumer type, with event (%d)\n", producer->event_type);
alarm_consumer_free(snapshot_consumer);
return -1;
}
#ifdef TAPO_CAMERA
/* request count of snapshot to cloud: int */
if (snapshot_consumer->consumer_type >= 0 && snapshot_consumer->consumer_type < CONSUMER_TYPE_MAX) {
dev_info->telemetry.snapshot[!!mediacenter_ctx->eth_up].request_count[snapshot_consumer->consumer_type]++;
}
#endif
if (dev_info->general_camera) {
/* related ID由WiFi camera发送 */
snprintf(snapshot_consumer->related_id, LEN_MAX_EVENT_ID, "%s", dev_info->ring_alarm_id);
if (snapshot_consumer->event_type == EVENT_TYPE_DOORBELL_RING && dev_info->general_ring_status == RING_SNAPSHOT_NEEDED) {
/* 开始发送RING事件后,Ring Call周期内后续事件需要静默 */
dev_info->general_ring_status = RING_SNAPSHOT_EXIST;
}
} else if (producer->event_type == EVENT_TYPE_DOORBELL_RING
|| dev_info->ring_timer_status != RING_EVENT_TIMER_IDLE) {
/* related ID由设备端自己构造并赋值 */
if (EVENT_TYPE_DOORBELL_RING == producer->event_type) {
is_repeated_event = fast_post_find_fuzzy(dev_info, producer->count_id);
if (false == is_repeated_event) {
/* subg门铃事件未到或丢失,由wifi门铃事件触发ring call状态更改 */
ring_alarm_handler_simple(dev_info, time(NULL), producer->count_id);
}
snapshot_consumer->is_repeated_event = is_repeated_event;
}
snprintf(snapshot_consumer->related_id, LEN_MAX_EVENT_ID, "%s", dev_info->ring_alarm_id);
}
DBG_DBG("snapshot consumer %p type: %d\n", snapshot_consumer, snapshot_consumer->consumer_type);
alarm_producer_add_consumer(producer, snapshot_consumer);
#ifdef ALARM_STORAGE
storage_delete_posted_session(worker_ctx->storage_server, dev_info->dev_id, STORAGE_SNAPSHOT);
/* storage */
storage_session = alarm_snapshot_storage(producer, dev_info->dev_id, worker_ctx->storage_server, timestamp);
/* combine */
if (storage_session) {
MYDEBUG("Device Id = %s, storage session = %p\n", dev_info->dev_id, storage_session);
storage_session_set_event_timestamp(storage_session, timestamp);
storage_session_set_region(storage_session, &producer->region);
storage_session_set_event_type(storage_session, producer->event_type);
storage_session->iot_consumer = snapshot_consumer;
}
#endif
/* iot */
#ifdef TAPO_CAMERA
/* do not retry to prevent users from receiving repeated notifications */
alarm_consumer_set_retry_flag(snapshot_consumer, false);
#endif
snapshot_consumer->storage_session = storage_session;
if (iot_info) {
iot_info->flag |= IOT_INFO_SNAPSHOT_FLAG;
#ifdef TAPO_CAMERA
if (iot_info->xtoken[0]) {
snprintf(snapshot_consumer->xtoken, LEN_MAX_TOKEN, "%s", iot_info->xtoken);
}
if (EVENT_TYPE_DOORBELL_RING == producer->event_type) {
iot_info->flag |= IOT_INFO_TRANSITION_FLAG;
}
/* refresh iot_info */
iot_info_set_dev_ip(iot_info, snapshot_consumer->dev_ip);
iot_info_set_dev_id(iot_info, snapshot_consumer->dev_id);
iot_info_set_count_id(iot_info, snapshot_consumer->count_id);
iot_info_set_event_timestamp(iot_info, timestamp);
#ifdef MULTILENS_CAMERA
iot_info->dev_lens_num = dev_info->dev_lens_num;
int lens_index = 0;
if(snapshot_consumer->lens_id == 2 && iot_info->dev_lens_num > 1) {
lens_index = 1;
}
iot_info->event_type_summary[lens_index] |= (1 << (producer->event_type + 4));
#ifdef EVENT_TYPE_INFOS
iot_info_record_alarm_event_infos(iot_info, 1 << (producer->event_type + 4), timestamp, lens_index);
#endif
#else /*MULTILENS_CAMERA*/
// iot_info->snapshot_consumer = snapshot_consumer;
iot_info->event_type_summary |= (1 << (producer->event_type + 4));
#ifdef EVENT_TYPE_INFOS
iot_info_record_alarm_event_infos(iot_info, 1 << (producer->event_type + 4), timestamp);
#endif
#endif /*MULTILENS_CAMERA*/
#else
iot_info->snapshot_consumer = snapshot_consumer;
#endif
}
else {
iot_info = iot_info_new(worker_ctx->iot_info_server);
if (!iot_info) {
DBG_ERR("iot_info_new falied\n");
alarm_consumer_free(snapshot_consumer);
return -1;
}
#ifdef MULTILENS_CAMERA
iot_info->dev_lens_num = dev_info->dev_lens_num;
#endif
iot_info->flag |= IOT_INFO_SNAPSHOT_FLAG;
iot_info_set_dev_ip(iot_info, snapshot_consumer->dev_ip);
iot_info_set_dev_id(iot_info, snapshot_consumer->dev_id);
iot_info_set_count_id(iot_info, snapshot_consumer->count_id);
iot_info_set_event_timestamp(iot_info, timestamp);
#ifdef TAPO_CAMERA
if (EVENT_TYPE_DOORBELL_RING == producer->event_type) {
iot_info->flag |= IOT_INFO_TRANSITION_FLAG;
}
#ifdef MULTILENS_CAMERA
int lens_index = 0;
if(snapshot_consumer->lens_id == 2 && iot_info->dev_lens_num > 1) {
lens_index = 1;
}
iot_info->event_type_summary[lens_index] |= (1 << (producer->event_type + 4));
#ifdef EVENT_TYPE_INFOS
iot_info_record_alarm_event_infos(iot_info, 1 << (producer->event_type + 4), timestamp, lens_index);
#endif
#else /*MULTILENS_CAMERA*/
iot_info->event_type_summary |= (1 << (producer->event_type + 4));
#ifdef EVENT_TYPE_INFOS
iot_info_record_alarm_event_infos(iot_info, 1 << (producer->event_type + 4), timestamp);
#endif
#endif /*MULTILENS_CAMERA*/
#endif
}
ALARM_CONSUMER_NODE *pnode = (ALARM_CONSUMER_NODE *)malloc(sizeof(ALARM_CONSUMER_NODE));
if (!pnode) {
DBG_ERR("malloc failed\n");
alarm_consumer_free(snapshot_consumer);
iot_info_free(iot_info);
return -1;
}
pnode->consumer = snapshot_consumer;
list_add_tail(&pnode->list, &iot_info->snapshot_consumer_list);
DBG_DBG("add consumer:%p into iot_info:%p list\n", snapshot_consumer, iot_info);
if (snapshot_consumer->consumer_type == CONSUMER_XTOKEN_SNAPSHOT) {
if (iot_info->ready == IOT_XTOKEN_READY) {
DBG_ERR("========critical========\n");
}
iot_info->ready = IOT_XTOKEN_WAITING;
}
iot_info->idle_for_post = false;
snapshot_consumer->iot_info = iot_info;
return 0;
}
解释代码
最新发布