提交 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" #include "evutils.hpp"
using namespace std; using namespace std;
using namespace jsoncons; using namespace jsoncons;
using namespace jsoncons::literals; 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 char x;
unsigned short crc = 0xFFFF; unsigned short crc = 0xFFFF;
while (length--){ while (length--) {
x = crc >> 8 ^ *data_p++; x = crc >> 8 ^ *data_p++;
x ^= x>>4; x ^= x>>4;
crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x <<5)) ^ ((unsigned short)x); crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x <<5)) ^ ((unsigned short)x);
......
...@@ -14,6 +14,7 @@ typedef struct ev_region_t { ...@@ -14,6 +14,7 @@ typedef struct ev_region_t {
typedef struct ev_module_config_t { typedef struct ev_module_config_t {
MqttHelper *pClient; MqttHelper *pClient;
void **pPubCtx; void **pPubCtx;
bool isRecordRunning;
struct module{ struct module{
struct record{ struct record{
int interval; // in seconds 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 <iostream>
#include <string> #include <string>
#include <chrono> #include <chrono>
...@@ -86,10 +98,10 @@ char *port = portArr; ...@@ -86,10 +98,10 @@ char *port = portArr;
// //
OrderedList<int64_t> *gRecFilesList = nullptr; OrderedList<int64_t> *gRecFilesList = nullptr;
namespace _upvariable_{ namespace _upvariable_ {
mutex mut; mutex mut;
condition_variable cv; condition_variable cv;
queue<upload_item_t> que; queue<upload_item_t> que;
}; };
thread_upload_args_t gUploadArgs{&_upvariable_::mut, &_upvariable_::cv, &_upvariable_::que, &gJsonConfig, dev_sn, &gRecFilesList}; thread_upload_args_t gUploadArgs{&_upvariable_::mut, &_upvariable_::cv, &_upvariable_::que, &gJsonConfig, dev_sn, &gRecFilesList};
...@@ -380,7 +392,7 @@ string verify_config(json &data) ...@@ -380,7 +392,7 @@ string verify_config(json &data)
} }
// check network connections // check network connections
int socket = 0; 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) { if(rc < 0 || socket == 0) {
str = fmt::format("failed to connect to {}:{}, {}", k, value, data.to_string()); str = fmt::format("failed to connect to {}:{}, {}", k, value, data.to_string());
break; break;
...@@ -646,7 +658,7 @@ void handle_mqtt_req(MqttHelper *hlp, const void * const data, int len, string t ...@@ -646,7 +658,7 @@ void handle_mqtt_req(MqttHelper *hlp, const void * const data, int len, string t
gUploadArgs.cv->notify_one(); gUploadArgs.cv->notify_one();
} }
string extra; string extra;
if(status > 0){ if(status > 0) {
extra = ", but it is scheduled in the future"; 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); 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[]) ...@@ -851,6 +863,7 @@ int main(int argc, char *argv[])
} }
// //
gConfigSystem.isRecordRunning = false;
MaQueVideoEncodeCfg_s cfg; MaQueVideoEncodeCfg_s cfg;
init_stream_cfg(&cfg); init_stream_cfg(&cfg);
gConfigSystem.pPubCtx = &pPubCtx; gConfigSystem.pPubCtx = &pPubCtx;
...@@ -902,9 +915,16 @@ int main(int argc, char *argv[]) ...@@ -902,9 +915,16 @@ int main(int argc, char *argv[])
thVideoRecord.detach(); 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 // start upload thread
thread thUpload = thread(upload_svc_entry, &gUploadArgs); thread thUpload = thread(upload_svc_entry, &gUploadArgs);
if(thUpload.joinable()){ if(thUpload.joinable()) {
thUpload.detach(); thUpload.detach();
spdlog::info("started video upload service"); 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" { extern "C" {
#include <maque_type.h> #include <maque_type.h>
#include <maque_osd.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__ #ifndef __MOTION_DETECT_HPP__
#define __MOTION_DETECT_HPP__ #define __MOTION_DETECT_HPP__
...@@ -122,8 +134,8 @@ namespace md{ ...@@ -122,8 +134,8 @@ namespace md{
}; };
const auto may_generate_event = [this]{ const auto may_generate_event = [this]{
auto now = chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count(); auto now = chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count();
if(timeStart != 0 && (now - timeStart) >= kMaxDuration *1000){ if(timeStart != 0 && ((now - timeStart) >= kMaxDuration *1000 /*|| this->is("none"_s)*/)){
upload_item_t item{timeStart, now, 6, -1, now}; upload_item_t item{timeStart, now, 7, -1, now};
{ {
lock_guard<mutex> lg(*gUploadArgs.mut); lock_guard<mutex> lg(*gUploadArgs.mut);
gUploadArgs.que->push(item); gUploadArgs.que->push(item);
...@@ -135,6 +147,23 @@ namespace md{ ...@@ -135,6 +147,23 @@ namespace md{
return false; 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; using namespace sml;
return make_transition_table( return make_transition_table(
...@@ -165,7 +194,7 @@ namespace md{ ...@@ -165,7 +194,7 @@ namespace md{
// "post"_s + sml::on_entry<_> / may_generate_event, // "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: 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 <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.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 <libxmmaque_api.h>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <fmt/format.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/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/time.h> #include <sys/time.h>
...@@ -9,7 +21,7 @@ ...@@ -9,7 +21,7 @@
using namespace std; 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; int rv = 0;
struct addrinfo hints {}; struct addrinfo hints {};
...@@ -37,19 +49,21 @@ int raw_connect(std::string host, std::string port, int *socket_, int recv_timeo ...@@ -37,19 +49,21 @@ int raw_connect(std::string host, std::string port, int *socket_, int recv_timeo
} }
struct timeval timeout; struct timeval timeout;
timeout.tv_sec = recv_timeout; if(recv_timeout != 0){
timeout.tv_usec = 0; timeout.tv_sec = recv_timeout;
timeout.tv_usec = 0;
if (setsockopt (*socket_, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0) { if (setsockopt (*socket_, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0) {
spdlog::error("setsockopt SO_RCVTIMEO failed"); spdlog::error("setsockopt SO_RCVTIMEO failed");
return -1; return -1;
}
} }
timeout.tv_sec = send_timeout; if(send_timeout != 0){
timeout.tv_sec = send_timeout;
if (setsockopt (*socket_, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0) { if (setsockopt (*socket_, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0) {
spdlog::error("setsockopt SO_SNDTIMEO failed"); spdlog::error("setsockopt SO_SNDTIMEO failed");
return -1; return -1;
}
} }
rv = ::connect(*socket_, rp->ai_addr, rp->ai_addrlen); rv = ::connect(*socket_, rp->ai_addr, rp->ai_addrlen);
...@@ -75,17 +89,29 @@ int raw_connect(std::string host, std::string port, int *socket_, int recv_timeo ...@@ -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; string rc;
ssize_t sent = 0; ssize_t sent = 0;
ssize_t ret = 0;
int retries = 0;
while(len > 0) { while(len > 0) {
sent = ::send(s, buf, len, 0); ret = ::send(s, (char*)buf+ sent, len, MSG_NOSIGNAL);
if(sent <= 0){ if(ret < 0) {
break; 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)); rc = fmt::format("failed to send: {}", strerror(errno));
} }
return rc; return rc;
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <mutex> #include <mutex>
#include "common.h" #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); std::string raw_send(int s, const void *buf, size_t len);
#endif #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 "smart.h"
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
...@@ -209,9 +221,10 @@ void maq_smart_task_entry(ev_module_config_t *pArg) ...@@ -209,9 +221,10 @@ void maq_smart_task_entry(ev_module_config_t *pArg)
int deltaMotionCnt = motionCnt_ - motionCntLast; int deltaMotionCnt = motionCnt_ - motionCntLast;
motionCntLast = motionCnt_; motionCntLast = motionCnt_;
if(deltaTimeMs == 0){ if(deltaTimeMs == 0) {
spdlog::warn("no video frames in last ~5 seconds"); spdlog::warn("no video frames in last ~5 seconds");
}else{ }
else {
if(deltaMotionCnt >= motionCntThresh) { if(deltaMotionCnt >= motionCntThresh) {
hasMotion = true; 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__ #ifndef __EVPACKET_H__
#define __EVPACKET_H__ #define __EVPACKET_H__
#include <mutex> #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> #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 <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论