Prechádzať zdrojové kódy

Add Folder check and Folder creation

JDierkse 2 rokov pred
rodič
commit
c26e48e43c

+ 78 - 5
Recorder/FoscamRecorder.cpp

@@ -1,10 +1,11 @@
 #include "FoscamRecorder.h"
-#include "Util.h"
+#include "Util/Util.h"
 #include <HttpClient.h>
 #include <JpegImageCollection.h>
 #include <Logging.h>
 #include <filesystem/path.h>
 #include <fstream>
+#include <iostream>
 #include <sstream>
 #include <thread>
 
@@ -26,7 +27,31 @@ std::string FoscamRecorder::Snapshot(ctpl::thread_pool* pThreadPool, const std::
 {
 	Logging::Log(Logging::Severity::Debug, "FoscamRecorder Snapshot");
 
-	std::string dateTimeString = GetDateTimeString();
+	std::string folderName = GetFolderName(m_settings);
+	if (!Util::FolderExists(folderName))
+	{
+		try
+		{
+			Util::CreateFolder(folderName);
+		}
+		catch (const std::exception& e)
+		{
+			std::cerr << "Exception caught" << std::endl;
+
+			std::stringstream ss;
+			ss << "Type : " << typeid(e).name() << std::endl;
+			ss << "ERROR: " << e.what() << std::endl;
+
+			Logging::Log(Logging::Severity::Error, ss.str());
+
+			return std::string();
+		}
+	}
+
+	//if (m_pRecorderMutex->IsRecording(m_settings.IpAddress))
+	//	return std::string();
+
+	std::string dateTimeString = Util::GetDateTimeString();
 	std::string fileName = GetFileName(m_settings, "jpg", dateTimeString);
 
 	Http::HttpClient* pHttpClient = m_pHttpClient;
@@ -40,7 +65,27 @@ std::string FoscamRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, const
 {
 	Logging::Log(Logging::Severity::Debug, "FoscamRecorder MultiSnapshot");
 
-	std::string dateTimeString = GetDateTimeString();
+	std::string folderName = GetFolderName(m_settings);
+	if (!Util::FolderExists(folderName))
+	{
+		try
+		{
+			Util::CreateFolder(folderName);
+		}
+		catch (const std::exception& e)
+		{
+			std::cerr << "Exception caught" << std::endl;
+
+			std::stringstream ss;
+			ss << "Type : " << typeid(e).name() << std::endl;
+			ss << "ERROR: " << e.what() << std::endl;
+
+			Logging::Log(Logging::Severity::Error, ss.str());
+
+			return std::string();
+		}
+	}
+	std::string dateTimeString = Util::GetDateTimeString();
 	std::string fileName = GetFileName(m_settings, "jpg", dateTimeString);
 
 	Http::HttpClient* pHttpClient = m_pHttpClient;
@@ -58,7 +103,28 @@ std::string FoscamRecorder::Video(ctpl::thread_pool* pThreadPool, const std::str
 	std::stringstream url;
 	url << "rtsp://" << m_settings.Username << ":" << m_settings.Password << "@" << m_settings.IpAddress << ":" << port << "/videoSub";
 
-	std::string dateTimeString = GetDateTimeString();
+	std::string folderName = GetFolderName(m_settings);
+	if (!Util::FolderExists(folderName))
+	{
+		try
+		{
+			Util::CreateFolder(folderName);
+		}
+		catch (const std::exception& e)
+		{
+			std::cerr << "Exception caught" << std::endl;
+
+			std::stringstream ss;
+			ss << "Type : " << typeid(e).name() << std::endl;
+			ss << "ERROR: " << e.what() << std::endl;
+
+			Logging::Log(Logging::Severity::Error, ss.str());
+
+			return std::string();
+		}
+	}
+
+	std::string dateTimeString = Util::GetDateTimeString();
 	std::string fileName = GetFileName(m_settings, "mp4", dateTimeString);
 
 	std::stringstream command;
@@ -71,11 +137,18 @@ std::string FoscamRecorder::Video(ctpl::thread_pool* pThreadPool, const std::str
 	return fileName;
 }
 
-std::string FoscamRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString)
+std::string FoscamRecorder::GetFolderName(const Settings& settings)
 {
 	filesystem::path fileLocation(settings.Path);
 	fileLocation = fileLocation / filesystem::path(settings.IpAddress);
 
+	return fileLocation.str();
+}
+
+std::string FoscamRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString)
+{
+	filesystem::path fileLocation(GetFolderName(settings));
+
 	std::string fileName = std::string(dateTimeString).append(".").append(extension);
 	filesystem::path fullFileName = fileLocation / filesystem::path(fileName);
 

+ 1 - 0
Recorder/FoscamRecorder.h

@@ -26,6 +26,7 @@ public:
 	virtual std::string Video(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg) override;
 
 private:
+	static std::string GetFolderName(const Settings& settings);
 	static std::string GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString);
 	static void Snapshots(Http::HttpClient* pHttpClient, const Settings& settings, std::string dateTimeString, int numberOfImages);
 	static Image::JpegImage Snapshot(Http::HttpClient* pHttpClient, const Settings& settings);

+ 77 - 6
Recorder/RTSPRecorder.cpp

@@ -1,7 +1,8 @@
 #include "RTSPRecorder.h"
-#include "Util.h"
+#include "Util/Util.h"
 #include <HttpClient.h>
 #include <Logging.h>
+#include <iostream>
 
 
 namespace CameraRecorder {
@@ -21,7 +22,28 @@ std::string RTSPRecorder::Snapshot(ctpl::thread_pool* pThreadPool, const std::st
 {
 	Logging::Log(Logging::Severity::Debug, "RTSPRecorder Snapshot");
 
-	std::string dateTimeString = GetDateTimeString();
+	std::string folderName = GetFolderName(m_settings);
+	if (!Util::FolderExists(folderName))
+	{
+		try
+		{
+			Util::CreateFolder(folderName);
+		}
+		catch (const std::exception& e)
+		{
+			std::cerr << "Exception caught" << std::endl;
+
+			std::stringstream ss;
+			ss << "Type : " << typeid(e).name() << std::endl;
+			ss << "ERROR: " << e.what() << std::endl;
+
+			Logging::Log(Logging::Severity::Error, ss.str());
+
+			return std::string();
+		}
+	}
+
+	std::string dateTimeString = Util::GetDateTimeString();
 	std::string fileName = GetFileName(m_settings, "jpg", dateTimeString, 0);
 
 	Http::HttpClient* pHttpClient = m_pHttpClient;
@@ -35,7 +57,28 @@ std::string RTSPRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, const st
 {
 	Logging::Log(Logging::Severity::Debug, "RTSPRecorder MultiSnapshot");
 
-	std::string dateTimeString = GetDateTimeString();
+	std::string folderName = GetFolderName(m_settings);
+	if (!Util::FolderExists(folderName))
+	{
+		try
+		{
+			Util::CreateFolder(folderName);
+		}
+		catch (const std::exception& e)
+		{
+			std::cerr << "Exception caught" << std::endl;
+
+			std::stringstream ss;
+			ss << "Type : " << typeid(e).name() << std::endl;
+			ss << "ERROR: " << e.what() << std::endl;
+
+			Logging::Log(Logging::Severity::Error, ss.str());
+
+			return std::string();
+		}
+	}
+
+	std::string dateTimeString = Util::GetDateTimeString();
 	std::string fileName = GetFileName(m_settings, "jpg", dateTimeString, 0);
 
 	Http::HttpClient* pHttpClient = m_pHttpClient;
@@ -52,7 +95,28 @@ std::string RTSPRecorder::Video(ctpl::thread_pool* pThreadPool, const std::strin
 	std::stringstream url;
 	url << "rtsp://" << m_settings.Username << ":" << m_settings.Password << "@" << m_settings.IpAddress << ":" << m_settings.Port << m_settings.URL;
 
-	std::string dateTimeString = GetDateTimeString();
+	std::string folderName = GetFolderName(m_settings);
+	if (!Util::FolderExists(folderName))
+	{
+		try
+		{
+			Util::CreateFolder(folderName);
+		}
+		catch (const std::exception& e)
+		{
+			std::cerr << "Exception caught" << std::endl;
+
+			std::stringstream ss;
+			ss << "Type : " << typeid(e).name() << std::endl;
+			ss << "ERROR: " << e.what() << std::endl;
+
+			Logging::Log(Logging::Severity::Error, ss.str());
+
+			return std::string();
+		}
+	}
+
+	std::string dateTimeString = Util::GetDateTimeString();
 	std::string fileName = GetFileName(m_settings, "mp4", dateTimeString, 0);
 
 	std::stringstream command;
@@ -65,12 +129,19 @@ std::string RTSPRecorder::Video(ctpl::thread_pool* pThreadPool, const std::strin
 	return fileName;
 }
 
-std::string RTSPRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString, int imageIndex)
+std::string RTSPRecorder::GetFolderName(const Settings& settings)
 {
 	filesystem::path fileLocation(settings.Path);
 	fileLocation = fileLocation / filesystem::path(settings.IpAddress);
 
-	std::string fileName = std::string(dateTimeString).append(".").append(extension);
+	return fileLocation.str();
+}
+
+std::string RTSPRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString, int imageIndex)
+{
+	filesystem::path fileLocation(GetFolderName(settings));
+
+	std::string fileName = std::string(dateTimeString).append("_").append(std::to_string(imageIndex)).append(".").append(extension);
 	filesystem::path fullFileName = fileLocation / filesystem::path(fileName);
 
 	return fullFileName.str();

+ 1 - 0
Recorder/RTSPRecorder.h

@@ -26,6 +26,7 @@ public:
 	virtual std::string Video(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg) override;
 
 private:
+	static std::string GetFolderName(const Settings& settings);
 	static std::string GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString, int imageIndex);
 	static void Snapshots(const std::string& ffmpeg, const Settings& settings, std::string dateTimeString, int numberOfImages);
 	static void Snapshot(const std::string& ffmpeg, const Settings& settings, const filesystem::path& fileName);

+ 0 - 25
Recorder/Util.cpp

@@ -1,25 +0,0 @@
-#include "Util.h"
-#include <array>
-#include <ctime>
-
-
-namespace CameraRecorder {
-namespace Recorder {
-
-std::string GetDateTimeString(bool omitSeparator)
-{
-	std::array<char, 17> buffer;
-	buffer.fill(0);
-	std::time_t t = std::time(nullptr);
-	std::tm* tm = std::localtime(&t);
-
-	if (omitSeparator)
-		strftime(buffer.data(), sizeof(buffer), "%Y%m%d%H%M%S", tm);
-	else
-		strftime(buffer.data(), sizeof(buffer), "%Y%m%d-%H%M%S", tm);
-
-	return buffer.data();
-}
-
-} // namespace Recorder
-} // namespace CameraRecorder

+ 0 - 15
Recorder/Util.h

@@ -1,15 +0,0 @@
-#ifndef RECORDER_UTIL_H
-#define RECORDER_UTIL_H
-
-#include <string>
-
-
-namespace CameraRecorder {
-namespace Recorder {
-
-std::string GetDateTimeString(bool omitSeparator = false);
-
-} // namespace Recorder
-} // namespace CameraRecorder
-
-#endif // RECORDER_UTIL_H

+ 76 - 5
Recorder/VStarCamRecorder.cpp

@@ -1,8 +1,9 @@
 #include "VStarCamRecorder.h"
-#include "Util.h"
+#include "Util/Util.h"
 #include <HttpClient.h>
 #include <Logging.h>
 #include <fstream>
+#include <iostream>
 #include <sstream>
 
 
@@ -23,7 +24,28 @@ std::string VStarCamRecorder::Snapshot(ctpl::thread_pool* pThreadPool, const std
 {
 	Logging::Log(Logging::Severity::Debug, "VStarCamRecorder Snapshot");
 
-	std::string dateTimeString = GetDateTimeString(true);
+	std::string folderName = GetFolderName(m_settings);
+	if (!Util::FolderExists(folderName))
+	{
+		try
+		{
+			Util::CreateFolder(folderName);
+		}
+		catch (const std::exception& e)
+		{
+			std::cerr << "Exception caught" << std::endl;
+
+			std::stringstream ss;
+			ss << "Type : " << typeid(e).name() << std::endl;
+			ss << "ERROR: " << e.what() << std::endl;
+
+			Logging::Log(Logging::Severity::Error, ss.str());
+
+			return std::string();
+		}
+	}
+
+	std::string dateTimeString = Util::GetDateTimeString(true);
 	std::string fileName = GetFileName(m_settings, "jpg", dateTimeString, 0);
 
 	Http::HttpClient* pHttpClient = m_pHttpClient;
@@ -37,7 +59,28 @@ std::string VStarCamRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, cons
 {
 	Logging::Log(Logging::Severity::Debug, "VStarCamRecorder MultiSnapshot");
 
-	std::string dateTimeString = GetDateTimeString(true);
+	std::string folderName = GetFolderName(m_settings);
+	if (!Util::FolderExists(folderName))
+	{
+		try
+		{
+			Util::CreateFolder(folderName);
+		}
+		catch (const std::exception& e)
+		{
+			std::cerr << "Exception caught" << std::endl;
+
+			std::stringstream ss;
+			ss << "Type : " << typeid(e).name() << std::endl;
+			ss << "ERROR: " << e.what() << std::endl;
+
+			Logging::Log(Logging::Severity::Error, ss.str());
+
+			return std::string();
+		}
+	}
+
+	std::string dateTimeString = Util::GetDateTimeString(true);
 	std::string fileName = GetFileName(m_settings, "jpg", dateTimeString, 0);
 
 	Http::HttpClient* pHttpClient = m_pHttpClient;
@@ -55,7 +98,28 @@ std::string VStarCamRecorder::Video(ctpl::thread_pool* pThreadPool, const std::s
 	std::stringstream url;
 	url << "rtsp://" << m_settings.Username << ":" << m_settings.Password << "@" << m_settings.IpAddress << ":" << port << "/udp/av0_0";
 
-	std::string dateTimeString = GetDateTimeString(true);
+	std::string folderName = GetFolderName(m_settings);
+	if (!Util::FolderExists(folderName))
+	{
+		try
+		{
+			Util::CreateFolder(folderName);
+		}
+		catch (const std::exception& e)
+		{
+			std::cerr << "Exception caught" << std::endl;
+
+			std::stringstream ss;
+			ss << "Type : " << typeid(e).name() << std::endl;
+			ss << "ERROR: " << e.what() << std::endl;
+
+			Logging::Log(Logging::Severity::Error, ss.str());
+
+			return std::string();
+		}
+	}
+
+	std::string dateTimeString = Util::GetDateTimeString(true);
 	std::string fileName = GetFileName(m_settings, "mp4", dateTimeString, 0);
 
 	std::stringstream command;
@@ -68,11 +132,18 @@ std::string VStarCamRecorder::Video(ctpl::thread_pool* pThreadPool, const std::s
 	return fileName;
 }
 
-std::string VStarCamRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString, int imageIndex)
+std::string VStarCamRecorder::GetFolderName(const Settings& settings)
 {
 	filesystem::path fileLocation(settings.Path);
 	fileLocation = fileLocation / filesystem::path(settings.IpAddress);
 
+	return fileLocation.str();
+}
+
+std::string VStarCamRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString, int imageIndex)
+{
+	filesystem::path fileLocation(GetFolderName(settings));
+
 	std::string fileName = std::string("VSTA000000XXXXX_0_").append(dateTimeString).append("_").append(std::to_string(imageIndex)).append(".").append(extension);
 	filesystem::path fullFileName = fileLocation / filesystem::path(fileName);
 

+ 1 - 0
Recorder/VStarCamRecorder.h

@@ -26,6 +26,7 @@ public:
 	virtual std::string Video(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg) override;
 
 private:
+	static std::string GetFolderName(const Settings& settings);
 	static std::string GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString, int imageIndex);
 	static void Snapshots(Http::HttpClient* pHttpClient, const Settings& settings, std::string dateTimeString, int numberOfImages);
 	static void Snapshot(Http::HttpClient* pHttpClient, const Settings& settings, const filesystem::path& fileName);

+ 76 - 5
Recorder/WatchBotRecorder.cpp

@@ -1,10 +1,11 @@
 #include "WatchBotRecorder.h"
-#include "Util.h"
+#include "Util/Util.h"
 #include <HttpClient.h>
 #include <JpegImageCollection.h>
 #include <Logging.h>
 #include <filesystem/path.h>
 #include <fstream>
+#include <iostream>
 #include <sstream>
 #include <vector>
 
@@ -26,7 +27,28 @@ std::string WatchBotRecorder::Snapshot(ctpl::thread_pool* pThreadPool, const std
 {
 	Logging::Log(Logging::Severity::Debug, "WatchBotRecorder Snapshot");
 
-	std::string dateTimeString = GetDateTimeString();
+	std::string folderName = GetFolderName(m_settings);
+	if (!Util::FolderExists(folderName))
+	{
+		try
+		{
+			Util::CreateFolder(folderName);
+		}
+		catch (const std::exception& e)
+		{
+			std::cerr << "Exception caught" << std::endl;
+
+			std::stringstream ss;
+			ss << "Type : " << typeid(e).name() << std::endl;
+			ss << "ERROR: " << e.what() << std::endl;
+
+			Logging::Log(Logging::Severity::Error, ss.str());
+
+			return std::string();
+		}
+	}
+
+	std::string dateTimeString = Util::GetDateTimeString();
 	std::string fileName = GetFileName(m_settings, "jpg", dateTimeString);
 
 	Http::HttpClient* pHttpClient = m_pHttpClient;
@@ -40,7 +62,28 @@ std::string WatchBotRecorder::MultiSnapshot(ctpl::thread_pool* pThreadPool, cons
 {
 	Logging::Log(Logging::Severity::Debug, "WatchBotRecorder MultiSnapshot");
 
-	std::string dateTimeString = GetDateTimeString();
+	std::string folderName = GetFolderName(m_settings);
+	if (!Util::FolderExists(folderName))
+	{
+		try
+		{
+			Util::CreateFolder(folderName);
+		}
+		catch (const std::exception& e)
+		{
+			std::cerr << "Exception caught" << std::endl;
+
+			std::stringstream ss;
+			ss << "Type : " << typeid(e).name() << std::endl;
+			ss << "ERROR: " << e.what() << std::endl;
+
+			Logging::Log(Logging::Severity::Error, ss.str());
+
+			return std::string();
+		}
+	}
+
+	std::string dateTimeString = Util::GetDateTimeString();
 	std::string fileName = GetFileName(m_settings, "jpg", dateTimeString);
 
 	Http::HttpClient* pHttpClient = m_pHttpClient;
@@ -57,7 +100,28 @@ std::string WatchBotRecorder::Video(ctpl::thread_pool* pThreadPool, const std::s
 	std::stringstream url;
 	url << "http://" << m_settings.IpAddress << ":" << m_settings.Port << "/videostream.asf?user=" << m_settings.Username << "&pwd=" << m_settings.Password;
 
-	std::string dateTimeString = GetDateTimeString();
+	std::string folderName = GetFolderName(m_settings);
+	if (!Util::FolderExists(folderName))
+	{
+		try
+		{
+			Util::CreateFolder(folderName);
+		}
+		catch (const std::exception& e)
+		{
+			std::cerr << "Exception caught" << std::endl;
+
+			std::stringstream ss;
+			ss << "Type : " << typeid(e).name() << std::endl;
+			ss << "ERROR: " << e.what() << std::endl;
+
+			Logging::Log(Logging::Severity::Error, ss.str());
+
+			return std::string();
+		}
+	}
+
+	std::string dateTimeString = Util::GetDateTimeString();
 	std::string fileName = GetFileName(m_settings, "mp4", dateTimeString);
 
 	std::stringstream command;
@@ -70,11 +134,18 @@ std::string WatchBotRecorder::Video(ctpl::thread_pool* pThreadPool, const std::s
 	return fileName;
 }
 
-std::string WatchBotRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString)
+std::string WatchBotRecorder::GetFolderName(const Settings& settings)
 {
 	filesystem::path fileLocation(settings.Path);
 	fileLocation = fileLocation / filesystem::path(settings.IpAddress);
 
+	return fileLocation.str();
+}
+
+std::string WatchBotRecorder::GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString)
+{
+	filesystem::path fileLocation(GetFolderName(settings));
+
 	std::string fileName = std::string(dateTimeString).append(".").append(extension);
 	filesystem::path fullFileName = fileLocation / filesystem::path(fileName);
 

+ 1 - 0
Recorder/WatchBotRecorder.h

@@ -26,6 +26,7 @@ public:
 	virtual std::string Video(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg) override;
 
 private:
+	static std::string GetFolderName(const Settings& settings);
 	static std::string GetFileName(const Settings& settings, const std::string& extension, const std::string& dateTimeString);
 	static void Snapshots(Http::HttpClient* pHttpClient, const Settings& settings, std::string dateTimeString, int numberOfImages);
 	static Image::JpegImage Snapshot(Http::HttpClient* pHttpClient, const Settings& settings);

+ 1 - 0
Util/Makefile

@@ -0,0 +1 @@
+../Makefile

+ 46 - 0
Util/Util.cpp

@@ -0,0 +1,46 @@
+#include "Util.h"
+#include <array>
+#include <ctime>
+#include <filesystem/path.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+
+namespace CameraRecorder {
+namespace Util {
+
+std::string GetDateTimeString(bool omitSeparator)
+{
+	std::array<char, 17> buffer;
+	buffer.fill(0);
+	std::time_t t = std::time(nullptr);
+	std::tm* tm = std::localtime(&t);
+
+	if (omitSeparator)
+		strftime(buffer.data(), sizeof(buffer), "%Y%m%d%H%M%S", tm);
+	else
+		strftime(buffer.data(), sizeof(buffer), "%Y%m%d-%H%M%S", tm);
+
+	return buffer.data();
+}
+
+bool FolderExists(const std::string& path)
+{
+	struct stat info;
+	if (stat(path.c_str(), &info) != 0)
+    	return false;
+	else if (info.st_mode & S_IFDIR)
+	    return true;
+	else
+	    return false;
+}
+
+void CreateFolder(const std::string& path)
+{
+	int status = mkdir(path.c_str(), 0777);
+	if ((status < 0) && (errno != EEXIST))
+		throw std::runtime_error("Can't create folder.");
+}
+
+} // namespace Util
+} // namespace CameraRecorder

+ 18 - 0
Util/Util.h

@@ -0,0 +1,18 @@
+#ifndef UTIL_UTIL_H
+#define UTIL_UTIL_H
+
+#include <string>
+
+
+namespace CameraRecorder {
+namespace Util {
+
+std::string GetDateTimeString(bool omitSeparator = false);
+
+bool FolderExists(const std::string& path);
+void CreateFolder(const std::string& path);
+
+} // namespace Util
+} // namespace CameraRecorder
+
+#endif // UTIL_UTIL_H