Extend ListCameras to return more ifno

In particular, this returns all the extra configuration data that will be
necessary to actually instantiate streams from the database rather than the
soon-to-be-removed configuration file.
This commit is contained in:
Scott Lamb 2016-01-31 23:27:52 -08:00
parent 1bd5c8aafe
commit 8ee1ab1c7b
3 changed files with 51 additions and 10 deletions

View File

@ -67,12 +67,20 @@ class MoonfireDbTest : public testing::Test {
DatabaseContext ctx(&db_); DatabaseContext ctx(&db_);
auto run = ctx.UseOnce( auto run = ctx.UseOnce(
R"( R"(
insert into camera (uuid, short_name, retain_bytes) insert into camera (uuid, short_name, host, username, password,
values (:uuid, :short_name, :retain_bytes); main_rtsp_path, sub_rtsp_path, retain_bytes)
values (:uuid, :short_name, :host, :username, :password,
:main_rtsp_path, :sub_rtsp_path, :retain_bytes);
)"); )");
run.BindBlob(":uuid", uuid.binary_view()); run.BindBlob(":uuid", uuid.binary_view());
run.BindText(":short_name", short_name); run.BindText(":short_name", short_name);
run.BindText(":host", "test-camera");
run.BindText(":username", "foo");
run.BindText(":password", "bar");
run.BindText(":main_rtsp_path", "/main");
run.BindText(":sub_rtsp_path", "/sub");
run.BindInt64(":retain_bytes", 42); run.BindInt64(":retain_bytes", 42);
CHECK_EQ(SQLITE_DONE, run.Step()) << run.error_message();
if (run.Step() != SQLITE_DONE) { if (run.Step() != SQLITE_DONE) {
ADD_FAILURE() << run.error_message(); ADD_FAILURE() << run.error_message();
return -1; return -1;
@ -85,6 +93,12 @@ class MoonfireDbTest : public testing::Test {
mdb_->ListCameras([&](const ListCamerasRow &row) { mdb_->ListCameras([&](const ListCamerasRow &row) {
++rows; ++rows;
EXPECT_EQ(camera_uuid, row.uuid); EXPECT_EQ(camera_uuid, row.uuid);
EXPECT_EQ("test-camera", row.host);
EXPECT_EQ("foo", row.username);
EXPECT_EQ("bar", row.password);
EXPECT_EQ("/main", row.main_rtsp_path);
EXPECT_EQ("/sub", row.sub_rtsp_path);
EXPECT_EQ(42, row.retain_bytes);
EXPECT_EQ(-1, row.min_start_time_90k); EXPECT_EQ(-1, row.min_start_time_90k);
EXPECT_EQ(-1, row.max_end_time_90k); EXPECT_EQ(-1, row.max_end_time_90k);
EXPECT_EQ(0, row.total_duration_90k); EXPECT_EQ(0, row.total_duration_90k);

View File

@ -59,6 +59,11 @@ bool MoonfireDatabase::Init(Database *db, std::string *error_message) {
camera.uuid, camera.uuid,
camera.short_name, camera.short_name,
camera.description, camera.description,
camera.host,
camera.username,
camera.password,
camera.main_rtsp_path,
camera.sub_rtsp_path,
camera.retain_bytes, camera.retain_bytes,
min(recording.start_time_90k), min(recording.start_time_90k),
max(recording.start_time_90k + recording.duration_90k), max(recording.start_time_90k + recording.duration_90k),
@ -80,21 +85,26 @@ bool MoonfireDatabase::Init(Database *db, std::string *error_message) {
Uuid uuid; Uuid uuid;
if (!uuid.ParseBinary(list_cameras_run.ColumnBlob(1))) { if (!uuid.ParseBinary(list_cameras_run.ColumnBlob(1))) {
*error_message = *error_message =
StrCat("bad uuid ", ToHex(list_cameras_run.ColumnBlob(2)), StrCat("bad uuid ", ToHex(list_cameras_run.ColumnBlob(1)),
" for camera id ", data.id); " for camera id ", data.id);
return false; return false;
} }
data.short_name = list_cameras_run.ColumnText(2).as_string(); data.short_name = list_cameras_run.ColumnText(2).as_string();
data.description = list_cameras_run.ColumnText(3).as_string(); data.description = list_cameras_run.ColumnText(3).as_string();
data.retain_bytes = list_cameras_run.ColumnInt64(4); data.host = list_cameras_run.ColumnText(4).as_string();
data.min_start_time_90k = list_cameras_run.ColumnType(5) == SQLITE_NULL data.username = list_cameras_run.ColumnText(5).as_string();
data.password = list_cameras_run.ColumnText(6).as_string();
data.main_rtsp_path = list_cameras_run.ColumnText(7).as_string();
data.sub_rtsp_path = list_cameras_run.ColumnText(8).as_string();
data.retain_bytes = list_cameras_run.ColumnInt64(9);
data.min_start_time_90k = list_cameras_run.ColumnType(10) == SQLITE_NULL
? -1 ? -1
: list_cameras_run.ColumnInt64(5); : list_cameras_run.ColumnInt64(10);
data.max_end_time_90k = list_cameras_run.ColumnType(6) == SQLITE_NULL data.max_end_time_90k = list_cameras_run.ColumnType(11) == SQLITE_NULL
? -1 ? -1
: list_cameras_run.ColumnInt64(6); : list_cameras_run.ColumnInt64(11);
data.total_duration_90k = list_cameras_run.ColumnInt64(7); data.total_duration_90k = list_cameras_run.ColumnInt64(12);
data.total_sample_file_bytes = list_cameras_run.ColumnInt64(8); data.total_sample_file_bytes = list_cameras_run.ColumnInt64(13);
auto ret = cameras_by_uuid_.insert(std::make_pair(uuid, data)); auto ret = cameras_by_uuid_.insert(std::make_pair(uuid, data));
if (!ret.second) { if (!ret.second) {
@ -307,9 +317,15 @@ void MoonfireDatabase::ListCameras(
DatabaseContext ctx(db_); DatabaseContext ctx(db_);
ListCamerasRow row; ListCamerasRow row;
for (const auto &entry : cameras_by_uuid_) { for (const auto &entry : cameras_by_uuid_) {
row.id = entry.second.id;
row.uuid = entry.first; row.uuid = entry.first;
row.short_name = entry.second.short_name; row.short_name = entry.second.short_name;
row.description = entry.second.description; row.description = entry.second.description;
row.host = entry.second.host;
row.username = entry.second.username;
row.password = entry.second.password;
row.main_rtsp_path = entry.second.main_rtsp_path;
row.sub_rtsp_path = entry.second.sub_rtsp_path;
row.retain_bytes = entry.second.retain_bytes; row.retain_bytes = entry.second.retain_bytes;
row.min_start_time_90k = entry.second.min_start_time_90k; row.min_start_time_90k = entry.second.min_start_time_90k;
row.max_end_time_90k = entry.second.max_end_time_90k; row.max_end_time_90k = entry.second.max_end_time_90k;

View File

@ -75,9 +75,15 @@ namespace moonfire_nvr {
// For use with MoonfireDatabase::ListCameras. // For use with MoonfireDatabase::ListCameras.
struct ListCamerasRow { struct ListCamerasRow {
int64_t id = -1;
Uuid uuid; Uuid uuid;
std::string short_name; std::string short_name;
std::string description; std::string description;
std::string host;
std::string username;
std::string password;
std::string main_rtsp_path;
std::string sub_rtsp_path;
int64_t retain_bytes = -1; int64_t retain_bytes = -1;
// Aggregates summarizing completed recordings. // Aggregates summarizing completed recordings.
@ -201,6 +207,11 @@ class MoonfireDatabase {
int64_t id = -1; int64_t id = -1;
std::string short_name; std::string short_name;
std::string description; std::string description;
std::string host;
std::string username;
std::string password;
std::string main_rtsp_path;
std::string sub_rtsp_path;
int64_t retain_bytes = -1; int64_t retain_bytes = -1;
// Aggregates of all recordings associated with the camera. // Aggregates of all recordings associated with the camera.