提交 69852f39 authored 作者: blu's avatar blu

upload svc enhencement and bugfix

上级 ead11e58
/**
* @file evutils.cc
*
* @brief this is the main file impelents helpers
*
* @ingroup evcamera
*
* @author Bruce Lu
* Contact: lzbgt@icloud.com
*
*/
#include "evutils.hpp"
using namespace std;
using namespace jsoncons;
using namespace jsoncons::literals;
unsigned short crc16(const unsigned char* data_p, unsigned char length){
unsigned short crc16(const unsigned char* data_p, unsigned char length)
{
unsigned char x;
unsigned short crc = 0xFFFF;
while (length--){
while (length--) {
x = crc >> 8 ^ *data_p++;
x ^= x>>4;
crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x <<5)) ^ ((unsigned short)x);
......
......@@ -14,6 +14,7 @@ typedef struct ev_region_t {
typedef struct ev_module_config_t {
MqttHelper *pClient;
void **pPubCtx;
bool isRecordRunning;
struct module{
struct record{
int interval; // in seconds
......
/**
* @file main.cc
*
* @brief this is the main file impelents evcamera
*
* @ingroup evcamera
*
* @author Bruce Lu
* Contact: lzbgt@icloud.com
*
*/
#include <iostream>
#include <string>
#include <chrono>
......@@ -86,10 +98,10 @@ char *port = portArr;
//
OrderedList<int64_t> *gRecFilesList = nullptr;
namespace _upvariable_{
mutex mut;
condition_variable cv;
queue<upload_item_t> que;
namespace _upvariable_ {
mutex mut;
condition_variable cv;
queue<upload_item_t> que;
};
thread_upload_args_t gUploadArgs{&_upvariable_::mut, &_upvariable_::cv, &_upvariable_::que, &gJsonConfig, dev_sn, &gRecFilesList};
......@@ -380,7 +392,7 @@ string verify_config(json &data)
}
// check network connections
int socket = 0;
auto rc = raw_connect(uri.Host, uri.Port, &socket);
auto rc = raw_connect_nonblock(uri.Host, uri.Port, &socket);
if(rc < 0 || socket == 0) {
str = fmt::format("failed to connect to {}:{}, {}", k, value, data.to_string());
break;
......@@ -646,7 +658,7 @@ void handle_mqtt_req(MqttHelper *hlp, const void * const data, int len, string t
gUploadArgs.cv->notify_one();
}
string extra;
if(status > 0){
if(status > 0) {
extra = ", but it is scheduled in the future";
}
MqttMgr::report_response_args(gMqttClient, consts::pub_topic_response + rid, 0, string("request accepted") + extra, cmd, rid, data);
......@@ -851,6 +863,7 @@ int main(int argc, char *argv[])
}
//
gConfigSystem.isRecordRunning = false;
MaQueVideoEncodeCfg_s cfg;
init_stream_cfg(&cfg);
gConfigSystem.pPubCtx = &pPubCtx;
......@@ -902,9 +915,16 @@ int main(int argc, char *argv[])
thVideoRecord.detach();
}
while(gConfigSystem.isRecordRunning == false) {
spdlog::info("waiting for record svc to start");
this_thread::sleep_for(chrono::seconds(3));
}
spdlog::info("recording svc started, starting upload svc");
// start upload thread
thread thUpload = thread(upload_svc_entry, &gUploadArgs);
if(thUpload.joinable()){
if(thUpload.joinable()) {
thUpload.detach();
spdlog::info("started video upload service");
}
......
/**
* @file motion.cc
*
* @brief this is the main file impelents motion
*
* @ingroup evcamera
*
* @author Bruce Lu
* Contact: lzbgt@icloud.com
*
*/
extern "C" {
#include <maque_type.h>
#include <maque_osd.h>
......
/**
* @file motiondetect.hpp
*
* @brief this is the main file impelents motion detect state machine
*
* @ingroup evcamera
*
* @author Bruce Lu
* Contact: lzbgt@icloud.com
*
*/
#ifndef __MOTION_DETECT_HPP__
#define __MOTION_DETECT_HPP__
......@@ -122,8 +134,8 @@ namespace md{
};
const auto may_generate_event = [this]{
auto now = chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count();
if(timeStart != 0 && (now - timeStart) >= kMaxDuration *1000){
upload_item_t item{timeStart, now, 6, -1, now};
if(timeStart != 0 && ((now - timeStart) >= kMaxDuration *1000 /*|| this->is("none"_s)*/)){
upload_item_t item{timeStart, now, 7, -1, now};
{
lock_guard<mutex> lg(*gUploadArgs.mut);
gUploadArgs.que->push(item);
......@@ -136,6 +148,23 @@ namespace md{
return false;
};
const auto generate_event = [this]{
auto now = chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count();
if(timeStart != 0){
upload_item_t item{timeStart, now, 7, -1, now};
{
lock_guard<mutex> lg(*gUploadArgs.mut);
gUploadArgs.que->push(item);
gUploadArgs.cv->notify_one();
}
spdlog::info("event generated: tss {}, tse {}, type {}", timeStart, now, 7);
}else{
//spdlog::error("failed generate event: tss {}, tse {}, type {}", timeStart, now, 7);
}
timeStart = 0;
hasMotion = false;
};
using namespace sml;
return make_transition_table(
*"init"_s = "none"_s,
......@@ -165,7 +194,7 @@ namespace md{
// "post"_s + sml::on_entry<_> / may_generate_event,
//
"none"_s + sml::on_entry<_> / [this]{hasMotion = false,timeStart = 0;}
"none"_s + sml::on_entry<_> / generate_event
);
}
private:
......
/**
* @file ntp.cc
*
* @brief this is the main file impelents ntp update
*
* @ingroup evcamera
*
* @author Bruce Lu
* Contact: lzbgt@icloud.com
*
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
......
/**
* @file ptz.cc
*
* @brief this is the main file impelents motor control ptz
*
* @ingroup evcamera
*
* @author Bruce Lu
* Contact: lzbgt@icloud.com
*
*/
#include <libxmmaque_api.h>
#include <spdlog/spdlog.h>
#include <fmt/format.h>
......
/**
* @file raw_tcp.cc
*
* @brief this is the main file impelents raw tcp transfer
*
* @ingroup evcamera
*
* @author Bruce Lu
* Contact: lzbgt@icloud.com
*
*/
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
......@@ -9,7 +21,7 @@
using namespace std;
int raw_connect(std::string host, std::string port, int *socket_, int recv_timeout, int send_timeout)
int _raw_connect(std::string host, std::string port, int *socket_, int recv_timeout, int send_timeout)
{
int rv = 0;
struct addrinfo hints {};
......@@ -37,20 +49,22 @@ int raw_connect(std::string host, std::string port, int *socket_, int recv_timeo
}
struct timeval timeout;
if(recv_timeout != 0){
timeout.tv_sec = recv_timeout;
timeout.tv_usec = 0;
if (setsockopt (*socket_, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0) {
spdlog::error("setsockopt SO_RCVTIMEO failed");
return -1;
}
}
if(send_timeout != 0){
timeout.tv_sec = send_timeout;
if (setsockopt (*socket_, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0) {
spdlog::error("setsockopt SO_SNDTIMEO failed");
return -1;
}
}
rv = ::connect(*socket_, rp->ai_addr, rp->ai_addrlen);
if (rv == 0) {
......@@ -75,17 +89,29 @@ int raw_connect(std::string host, std::string port, int *socket_, int recv_timeo
}
string raw_send(int s, const void *buf, size_t len){
int raw_connect(std::string host, std::string port, int *socket_){
return _raw_connect(host, port, socket_, 0, 0);
}
int raw_connect_nonblock(std::string host, std::string port, int *socket_, int recv_timeout, int send_timeout){
return _raw_connect(host, port, socket_, recv_timeout, send_timeout);
}
string raw_send(int s, const void *buf, size_t len)
{
string rc;
ssize_t sent = 0;
ssize_t ret = 0;
int retries = 0;
while(len > 0) {
sent = ::send(s, buf, len, 0);
if(sent <= 0){
ret = ::send(s, (char*)buf+ sent, len, MSG_NOSIGNAL);
if(ret < 0) {
break;
}
len -= sent;
len -= ret;
sent += ret;
}
if(len > 0 || sent<=0){
if(len > 0 || ret < 0) {
rc = fmt::format("failed to send: {}", strerror(errno));
}
return rc;
......
......@@ -9,6 +9,7 @@
#include <mutex>
#include "common.h"
int raw_connect(std::string host, std::string port, int *socket_, int recv_timeout = 3, int send_timeout = 3);
int raw_connect(std::string host, std::string port, int *socket_);
int raw_connect_nonblock(std::string host, std::string port, int *socket_, int recv_timeout = 5, int send_timeout = 5);
std::string raw_send(int s, const void *buf, size_t len);
#endif
\ No newline at end of file
/**
* @file smart.cc
*
* @brief this is the main file impelents smart detection
*
* @ingroup evcamera
*
* @author Bruce Lu
* Contact: lzbgt@icloud.com
*
*/
#include "smart.h"
#include <spdlog/spdlog.h>
......@@ -209,9 +221,10 @@ void maq_smart_task_entry(ev_module_config_t *pArg)
int deltaMotionCnt = motionCnt_ - motionCntLast;
motionCntLast = motionCnt_;
if(deltaTimeMs == 0){
if(deltaTimeMs == 0) {
spdlog::warn("no video frames in last ~5 seconds");
}else{
}
else {
if(deltaMotionCnt >= motionCntThresh) {
hasMotion = true;
}
......
/**
* @file evpacket.h
*
* @brief this is the main file defines evpacket type
*
* @ingroup evcamera
*
* @author Bruce Lu
* Contact: lzbgt@icloud.com
*
*/
#ifndef __EVPACKET_H__
#define __EVPACKET_H__
#include <mutex>
......
/**
* mqtt_helper.hpp
* @file mqtt_helper.cc
*
* simplifies api for mqtt
* @brief this is the main file impelents mqtt helpers
*
* @ingroup evcamera
*
* @author Bruce Lu
* Contact: lzbgt@icloud.com
*
* Bruce.Lu @20200415
*/
#include <mqtt_helper.hpp>
......
/**
* @file videogateway.cc
*
* @brief this is the main file impelents video gateway
*
* @ingroup evcamera
*
* @author Bruce Lu
* Contact: lzbgt@icloud.com
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论