Пример #1
0
bool ADVBPatterns::AddPatternToFile(const AString& filename, const AString& pattern)
{
	const ADVBConfig& config = ADVBConfig::Get();
	AStdFile fp;
	bool done = false;
	bool added = false;

	if (fp.open(filename)) {
		AString line;

		while (line.ReadLn(fp) >= 0) {
			if (line == pattern) {
				done = true;
				break;
			}
		}

		fp.close();
	}

	if (!done) {
		if (fp.open(filename, "a")) {
			fp.printf("%s\n", pattern.str());
			fp.close();

			config.logit("Add pattern '%s' to file '%s'", pattern.str(), filename.str());

			added = true;
		}
	}

	return added;
}
Пример #2
0
/*--------------------------------------------------------------------------------*/
bool PostgresDatabase::CheckConnection(const AString& host)
{
	AString connstr;

	connstr.printf("postgresql://%s", host.str());

	bool success = (PQping(connstr) == PQPING_OK);
	debug("Checking connection to %s...%s\n", host.str(), success ? "success" : "**failure**");
	return success;
}
Пример #3
0
/*--------------------------------------------------------------------------------*/
bool PostgresDatabase::CreateDatabase(const AString& name)
{
	bool success = false;

	if ((success = RunSQL(AString::Formatify("create database %s", name.str()))) == false) {
		AString errmsg = GetErrorMessage();
		if (errmsg.PosNoCase("already exists") < 0) {
			debug("Failed to create database '%s': %s\n", name.str(), errmsg.str());
		}
		else success = true;
	}

	return success;
}
Пример #4
0
/*--------------------------------------------------------------------------------*/
bool PostgresDatabase::AddUser(const AString& username, const AString& password)
{
	bool success = false;

	if ((success = RunSQL(AString::Formatify("create user %s with password '%s'", username.str(), password.str()))) == false) {
		AString errmsg = GetErrorMessage();
		if (errmsg.PosNoCase("already exists") < 0) {
			debug("Failed to create user '%s': %s\n", username.str(), errmsg.str());
		}
		else success = true;
	}

	return success;
}
Пример #5
0
bool ADVBConfig::ReadReplacementsFile(std::vector<REPLACEMENT>& replacements, const AString& filename) const
{
	AStdFile fp;
	bool success = false;

	if (fp.open(filename)) {
		AString line;

		//printf("Reading replacements file '%s':", filename.str());

		while (line.ReadLn(fp) >= 0) {
			int p = 0;

			if ((line.Word(0)[0] != ';') && ((p = line.Pos("=")) >= 0)) {
				REPLACEMENT repl = {
					line.Left(p).Word(0).DeQuotify(),
					line.Mid(p + 1).Word(0).DeQuotify(),
				};

				//printf("Replacement: '%s' -> '%s'", repl.search.str(), repl.replace.str());

				replacements.push_back(repl);
			}
		}

		fp.close();

		success = true;
	}
	else logit("Failed to open replacements file '%s'", filename.str());

	return success;
}
Пример #6
0
int main(int argc, char *argv[])
{
	ASocketServer server;
	AStdSocket    socket(server);

	if (argc < 2) {
		fprintf(stderr, "cmdsender " VER_STRING "\n");
		fprintf(stderr, "Usage: cmdsender <host> [<port>]\n");
		exit(1);
	}
	
	if (socket.open("0.0.0.0",
					0,
					ASocketServer::Type_Datagram)) {
		uint_t port = 1722;

		if (argc >= 3) port = (uint_t)AString(argv[2]);
		
		if (socket.setdatagramdestination(argv[1], port)) {
			AString line;
			while (line.ReadLn(Stdin) >= 0) {
				if (socket.printf("%s\n", line.str()) <= 0) break;
			}
		}
		
		socket.close();
	}
	
	return 0;
}
Пример #7
0
bool TriggerServerCommand(const AString& cmd)
{
	const ADVBConfig& config = ADVBConfig::Get();
	AString host = config.GetServerHost();
	bool success = false;

	if (host.Valid()) {
		static ASocketServer server;
		static AStdSocket    socket(server);

		if (!socket.isopen()) {
			if (socket.open("0.0.0.0", 0, ASocketServer::Type_Datagram)) {
				uint_t port = config.GetServerPort();

				if (!socket.setdatagramdestination(host, port)) {
					config.printf("Failed to set %s:%u as UDP destination for server command triggers", host.str(), port);
				}
			}
			else config.printf("Failed to open UDP socket for server command triggers");
		}

		success = (socket.printf("%s\n", cmd.str()) > 0);
	}

	return success;
}
Пример #8
0
bool MoveFile(const AString& src, const AString& dst, bool binary)
{
	const ADVBConfig& config = ADVBConfig::Get();
	bool success = false;

	if (SameFile(src, dst)) {
		config.printf("File move: '%s' and '%s' are the same file", src.str(), dst.str());
		success = true;
	}
	else if	(rename(src, dst) == 0) success = true;
	else if (CopyFile(src, dst, binary)) {
		if (remove(src) == 0) success = true;
		else config.logit("Failed to remove '%s' after copy", src.str());
	}

	return success;
}
Пример #9
0
/*--------------------------------------------------------------------------------*/
bool PostgresDatabase::Open(const AString& host, const AString& username, const AString& password, const AString& database)
{
	bool success = false;

	if (!conn) {
		AString str;

		ClearResult();

		connstr.Delete();
		connstr.printf("postgresql://%s:%s@%s", username.str(), password.str(), host.str());

		if (CheckConnection()) {
			str = connstr;
			if (database.Valid()) str.printf("/%s", database.ToLower().str());

			success = (((conn = PQconnectdb(str.str())) != NULL) && (PQstatus(conn) == CONNECTION_OK));
			if (success) {
				if (database.Valid()) debug("Connected to database '%s' on %s\n", database.str(), host.str());
				isopen = true;
			}
			else {
				if (database.Valid()) debug("Failed to connect to database '%s' on %s: %s\n", database.str(), host.str(), GetErrorMessage().str());
				else				  debug("Failed to connect server %s: %s\n", host.str(), GetErrorMessage().str());

				Close();
			}
		}
		else debug("No connection to server!\n");
	}

	return success;
}
Пример #10
0
bool ChangeDirectory(const AString& dir)
{
	bool success = true;

#ifdef __LINUX__
	success = (chdir(dir) == 0);
#else
	success = ::SetCurrentDirectory(dir.str());
#endif

	return success;
}
Пример #11
0
/*--------------------------------------------------------------------------------*/
bool PostgresDatabase::RunSQL(const AString& sql)
{
	bool success = false;

	if (conn) {
		ClearResult();
		success = (((res = PQexec(conn, sql.str())) != NULL) && (PQresultStatus(res) == PGRES_COMMAND_OK));
		//debug("Running SQL '%s': %s\n", sql.str(), success ? "** success **" : GetErrorMessage().str());
	}

	return success;
}
Пример #12
0
bool CopyFile(const AString& src, const AString& dst, bool binary)
{
	const ADVBConfig& config = ADVBConfig::Get();
	AStdFile fp1, fp2;
	bool success = false;

	if (SameFile(src, dst)) {
		config.printf("File copy: '%s' and '%s' are the same file", src.str(), dst.str());
		success = true;
	}
	else if (fp1.open(src, binary ? "rb" : "r")) {
		if (fp2.open(dst, binary ? "wb" : "w")) {
			success = CopyFile(fp1, fp2);
			if (!success) config.logit("Failed to copy '%s' to '%s': transfer failed", src.str(), dst.str());
		}
		else config.logit("Failed to copy '%s' to '%s': failed to open '%s' for writing", src.str(), dst.str(), dst.str());
	}
	else config.logit("Failed to copy '%s' to '%s': failed to open '%s' for reading", src.str(), dst.str(), src.str());

	return success;
}
Пример #13
0
bool RunAndLogCommand(const AString& cmd)
{
	const ADVBConfig& config = ADVBConfig::Get();
	AString cmd1 = cmd;

	if (config.IsOutputDisabled()) cmd1.printf(" >>\"%s\" 2>&1", config.GetLogFile(ADateTime().GetDays()).str());
	else						   cmd1.printf(" 2>&1 | tee -a \"%s\"", config.GetLogFile(ADateTime().GetDays()).str());

	if (config.LogRemoteCommands()) {
		config.logit("Running '%s'", cmd1.str());
	}

	bool success = (system(cmd1) == 0);
	if (!success) config.logit("Command '%s' failed", cmd.str());

	return success;
}
Пример #14
0
bool CreateDirectory(const AString& dir)
{
	AString parentdir = dir.PathPart();
	FILE_INFO file;
	bool success = true;

	if (parentdir.Valid() && (parentdir != "/") && !::GetFileInfo(parentdir, &file)) success = CreateDirectory(parentdir);

	if (success && (dir.FilePart().Valid()) && !::GetFileInfo(dir, &file)) {
#ifdef __LINUX__
		success = (mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH) == 0);
#else
		success = ::CreateDirectoryA(dir.str(), NULL);
#endif
	}

	return success;
}
Пример #15
0
PostgresDatabase::PostgresQuery::PostgresQuery(PostgresDatabase *_db, const AString& query) : SQLQuery(),
																							  db(_db),
																							  conn(db->GetConnection()),
																							  res(NULL),
																							  nfields(0),
																							  nrows(0),
																							  row(0),
																							  success(false)
{
	ExecStatusType status;

	if (((res = PQexec(conn, query.str())) != NULL) &&
		(((status = PQresultStatus(res)) == PGRES_COMMAND_OK) ||
		 (status == PGRES_TUPLES_OK))) {
		nfields = PQnfields(res);
		nrows   = PQntuples(res);
		//debug("Query '%s' returns %u rows x %u columns\n", query.str(), nrows, nfields);
		success = true;
	}
	//else debug("Query '%s' failed: %s (%u)\n", query.str(), GetErrorMessage().str(), res ? (uint_t)PQresultStatus(res) : 0);
}
Пример #16
0
/*--------------------------------------------------------------------------------*/
bool PostgresDatabase::CreateTable(const AString& name, const AString& columns)
{
	AString sql;
	SQLQuery *query = NULL;
	uint_t i, n = columns.CountColumns();

	sql.printf("create table %s (", name.str());
	for (i = 0; i < n; i++) {
		AString column = columns.Column(i);
		if (i > 0) sql.printf(", ");
		sql.printf("%s %s", column.Word(0).str(), GetColumnType(column).str());
	}
	sql.printf(")");

	if ((query = RunQuery(sql)) != NULL) {
		bool success = query->GetResult();
		delete query;
		return success;
	}

	return false;
}
Пример #17
0
void ADVBConfig::vlogit(const char *fmt, va_list ap, bool show) const
{
	ADateTime dt;
	AString   filename = GetLogFile(dt.GetDays());
	AString   str;
	AStdFile  fp;

	str.vprintf(fmt, ap);

	if (fp.open(filename, "a")) {
		uint_t i, n = str.CountLines("\n", 0);

		for (i = 0; i < n; i++) {
			fp.printf("%s [%05u]: %s\n", dt.DateFormat("%Y-%M-%D %h:%m:%s.%S").str(), (uint_t)getpid(), str.Line(i, "\n", 0).str());
		}

		fp.close();
	}

	if (show && !webresponse) {
		Stdout->printf("%s\n", str.str());
	}
}
Пример #18
0
bool ADVBConfig::ExtractLogData(const ADateTime& start, const ADateTime& end, const AString& filename) const
{
	AStdFile dst;
	bool success = false;

	if (dst.open(filename, "w")) {
		ADateTime dt;
		uint32_t day1 = start.GetDays();
		uint32_t day2 = end.GetDays();
		uint32_t day;
		bool     valid = false;

		for (day = day1; day <= day2; day++) {
			AStdFile src;

			if (src.open(GetLogFile(day))) {
				AString line;

				while (line.ReadLn(src) >= 0) {
					valid |= dt.FromTimeStamp(line.Words(0, 2));

					if (valid) {
						if ((dt >= start) && (dt <= end)) dst.printf("%s\n", line.str());
						if (dt >= end) break;
					}
				}

				src.close();
			}
		}

		dst.close();
	}

	return success;
}
Пример #19
0
bool ADVBPatterns::UpdatePatternInFile(const AString& filename, const AString& pattern, const AString& newpattern)
{
	const ADVBConfig& config = ADVBConfig::Get();
	AString filename1 = filename + ".new";
	AStdFile ifp, ofp;
	bool changed = false, found = false;

	if (ifp.open(filename)) {
		if (ofp.open(filename1, "w")) {
			AString line;

			while (line.ReadLn(ifp) >= 0) {
				if (line == pattern) {
					if (newpattern.Valid()) ofp.printf("%s\n", newpattern.str());
					changed = found = true;

					if (newpattern.Valid()) {
						config.logit("Changed pattern '%s' to '%s' in file '%s'", pattern.str(), newpattern.str(), filename.str());
					}
					else {
						config.logit("Deleted pattern '%s' from file '%s'", pattern.str(), filename.str());
					}
				}
				else if (line.Words(0).Valid()) ofp.printf("%s\n", line.str());
				else changed = true;
			}
			ofp.close();
		}

		ifp.close();

		if (changed) {
			remove(filename);
			rename(filename1, filename);
		}
		else remove(filename1);
	}

	return found;
}
Пример #20
0
bool GetFileFromRecordingSlave(const AString& filename, const AString& localfilename)
{
	const ADVBConfig& config = ADVBConfig::Get();
	AString cmd;
	bool    success;

	remove(localfilename);
	//config.logit("'%s:%s' -> '%s'...", config.GetRecordingSlave().str(), filename.str(), localfilename.str());
	cmd.printf("scp -p -C -P %u %s %s:\"%s\" \"%s\"", config.GetRecordingSlavePort(), config.GetSCPArgs().str(), config.GetRecordingSlave().str(), filename.str(), localfilename.str());
	success = RunAndLogCommand(cmd);
	//config.logit("'%s:%s' -> '%s' %s", config.GetRecordingSlave().str(), filename.str(), localfilename.str(), success ? "success" : "failed");

	return success;
}
Пример #21
0
AString ADVBPatterns::ParsePattern(const AString& _line, PATTERN& pattern, const AString& user)
{
	const ADVBConfig& config = ADVBConfig::Get();
	ADataList& list   = pattern.list;
	AString&   errors = pattern.errors;
	AString    line   = config.ReplaceTerms(user, _line);
	TERM   *term;
	uint_t i;

	pattern.exclude    = false;
	pattern.enabled    = true;
	pattern.scorebased = false;
	pattern.pri        = 0;
	pattern.user       = user;
	pattern.pattern    = line;

	if (pattern.user.Valid()) {
		pattern.pri = (int)config.GetUserConfigItem(pattern.user, "pri");
	}

	list.DeleteList();
	list.SetDestructor(&__DeleteTerm);

	i = 0;
	while (IsWhiteSpace(line[i])) i++;
	if (line[i] == '#') {
		pattern.enabled = false;
		i++;
	}
	else if (line[i] == ';') {
		return errors;
	}

	while (IsWhiteSpace(line[i])) i++;

	if (line[i]) {
		while (line[i] && errors.Empty()) {
			if (!IsSymbolStart(line[i])) {
				errors.printf("Character '%c' (at %u) is not a legal field start character (term %u)", line[i], i, list.Count() + 1);
				break;
			}

			uint_t fieldstart = i++;
			while (IsSymbolChar(line[i])) i++;
			AString field = line.Mid(fieldstart, i - fieldstart).ToLower();

			while (IsWhiteSpace(line[i])) i++;

			if (field == "exclude") {
				pattern.exclude = true;
				continue;
			}

			const FIELD *fieldptr = (const FIELD *)ADVBProg::fieldhash.Read(field);
			if (!fieldptr) {
				uint_t nfields;
				const FIELD *fields = ADVBProg::GetFields(nfields);

				errors.printf("'%s' (at %u) is not a valid search field (term %u), valid search fields are: ", field.str(), fieldstart, list.Count() + 1);
				for (i = 0; i < nfields; i++) {
					const FIELD& field = fields[i];

					if (i) errors.printf(", ");
					errors.printf("'%s'", field.name);
				}
				break;
			}

			uint_t opstart = i;

			const char *str = line.str() + i;
			bool isassign = fieldptr->assignable;
			uint_t j;
			uint_t opindex = 0, opcode = Operator_EQ;
			for (j = 0; j < NUMBEROF(operators); j++) {
				if (((isassign == operators[j].assign) ||
					 (isassign && !operators[j].assign)) &&
					(operators[j].fieldtypes & (1U << fieldptr->type)) &&
					(strncmp(str, operators[j].str, operators[j].len) == 0)) {
					i      += operators[j].len;
					opindex = j;
					opcode  = operators[j].opcode;
					break;
				}
			}

			while (IsWhiteSpace(line[i])) i++;

			AString value;
			bool    implicitvalue = false;
			if (j == NUMBEROF(operators)) {
				if (!line[i] || IsSymbolStart(line[i])) {
					if (fieldptr->assignable) {
						switch (fieldptr->type) {
							case FieldType_string:
								break;

							case FieldType_date:
								value = "now";
								break;

							default:
								value = "1";
								break;
						}

						opcode = Operator_Assign;
					}
					else opcode = Operator_NE;

					for (j = 0; j < NUMBEROF(operators); j++) {
						if ((opcode == operators[j].opcode) &&
							((isassign == operators[j].assign) ||
							 (isassign && !operators[j].assign)) &&
							(operators[j].fieldtypes & (1U << fieldptr->type))) {
							opindex = j;
							break;
						}
					}

					implicitvalue = true;
				}
				else {
					errors.printf("Symbols at %u do not represent a legal operator (term %u), legal operators for the field '%s' are: ", opstart, list.Count() + 1, field.str());

					bool flag = false;
					for (j = 0; j < NUMBEROF(operators); j++) {
						if (((isassign == operators[j].assign) ||
							 (isassign && !operators[j].assign)) &&
							(operators[j].fieldtypes & (1U << fieldptr->type))) {
							if (flag) errors.printf(", ");
							errors.printf("'%s'", operators[j].str);
							flag = true;
						}
					}
					break;
				}
			}

			if (!implicitvalue) {
				char quote = 0;
				if (IsQuoteChar(line[i])) quote = line[i++];

				uint_t valuestart = i;
				while (line[i] && ((!quote && !IsWhiteSpace(line[i])) || (quote && (line[i] != quote)))) {
					if (line[i] == '\\') i++;
					i++;
				}

				value = line.Mid(valuestart, i - valuestart).DeEscapify();

				if (quote && (line[i] == quote)) i++;

				while (IsWhiteSpace(line[i])) i++;
			}

			bool orflag = false;
			if ((line.Mid(i, 2).ToLower() == "or") && IsWhiteSpace(line[i + 2])) {
				orflag = true;
				i += 2;

				while (IsWhiteSpace(line[i])) i++;
			}
			else if ((line[i] == '|') && IsWhiteSpace(line[i + 1])) {
				orflag = true;
				i += 1;

				while (IsWhiteSpace(line[i])) i++;
			}

			if ((term = new TERM) != NULL) {
				term->data.start   = fieldstart;
				term->data.length  = i - fieldstart;
				term->data.field   = fieldptr - ADVBProg::fields;
				term->data.opcode  = opcode;
				term->data.opindex = opindex;
				term->data.value   = value;
				term->data.orflag  = (orflag && !RANGE(opcode, Operator_First_Assignable, Operator_Last_Assignable));
				term->field    	   = fieldptr;
				term->datetype 	   = DateType_none;

				switch (term->field->type) {
					case FieldType_string:
#if DVBDATVERSION > 1
						if (fieldptr->offset == ADVBProg::GetTagsDataOffset()) {
							value = "|" + value + "|";
						}
#endif
						
						if ((opcode & ~Operator_Inverted) == Operator_Regex) {
							AString regexerrors;
							AString rvalue;

							rvalue = ParseRegex(value, regexerrors);
							if (regexerrors.Valid()) {
								errors.printf("Regex error in value '%s' (term %u): %s", value.str(), list.Count() + 1, regexerrors.str());
							}

							value = rvalue;
						}
						term->value.str = value.Steal();
						break;

					case FieldType_date: {
						ADateTime dt;
						uint_t specified;

						dt.StrToDate(value, ADateTime::Time_Relative_Local, &specified);

						//debug("Value '%s', specified %u\n", value.str(), specified);

						if (!specified) {
							errors.printf("Failed to parse date '%s' (term %u)", value.str(), list.Count() + 1);
							break;
						}
						else if (((specified == ADateTime::Specified_Day) && (stricmp(term->field->name, "on") == 0)) ||
								 (stricmp(term->field->name, "day") == 0)) {
							//debug("Date from '%s' is '%s' (week day only)\n", value.str(), dt.DateToStr().str());
							term->value.u64 = dt.GetWeekDay();
							term->datetype  = DateType_weekday;
						}
						else if (specified & ADateTime::Specified_Day) {
							specified |= ADateTime::Specified_Date;
						}

						if (term->datetype == DateType_none) {
							specified &= ADateTime::Specified_Date | ADateTime::Specified_Time;

							if (specified == (ADateTime::Specified_Date | ADateTime::Specified_Time)) {
								//debug("Date from '%s' is '%s' (full date and time)\n", value.str(), dt.DateToStr().str());
								term->value.u64 = (uint64_t)dt;
								term->datetype  = DateType_fulldate;
							}
							else if (specified == ADateTime::Specified_Date) {
								//debug("Date from '%s' is '%s' (date only)\n", value.str(), dt.DateToStr().str());
								term->value.u64 = dt.GetDays();
								term->datetype  = DateType_date;
							}
							else if (specified == ADateTime::Specified_Time) {
								//debug("Date from '%s' is '%s' (time only)\n", value.str(), dt.DateToStr().str());
								term->value.u64 = dt.GetMS();
								term->datetype  = DateType_time;
							}
							else {
								errors.printf("Unknown date specifier '%s' (term %u)", value.str(), list.Count() + 1);
							}
						}
						break;
					}

					case FieldType_span:
					case FieldType_age: {
						ADateTime dt;
						//ADateTime::EnableDebugStrToDate(true);
						term->value.u64 = (uint64_t)ADateTime(value, ADateTime::Time_Absolute);
						//ADateTime::EnableDebugStrToDate(false);
						break;
					}

					case FieldType_uint32_t:
					case FieldType_external_uint32_t:
						term->value.u32 = (uint32_t)value;
						break;

					case FieldType_sint32_t:
					case FieldType_external_sint32_t:
						term->value.s32 = (sint32_t)value;
						break;

					case FieldType_uint16_t:
						term->value.u16 = (uint16_t)value;
						break;

					case FieldType_sint16_t:
						term->value.s16 = (sint16_t)value;
						break;

					case FieldType_uint8_t:
						term->value.u8 = (uint8_t)(uint16_t)value;
						break;

					case FieldType_sint8_t:
						term->value.s8 = (sint8_t)(sint16_t)value;
						break;

					case FieldType_flag...FieldType_lastflag:
						term->value.u8 = ((uint32_t)value != 0);
						//debug("Setting test of flag to %u\n", (uint_t)term->value.u8);
						break;

					case FieldType_prog: {
						ADVBProg *prog;

						if ((prog = new ADVBProg) != NULL) {
							if (prog->Base64Decode(value)) {
								term->value.prog = prog;
							}
							else {
								errors.printf("Failed to decode base64 programme ('%s') for %s at %u (term %u)", value.str(), field.str(), fieldstart, list.Count() + 1);
								delete prog;
							}
						}
						else {
							errors.printf("Failed to allocate memory for base64 programme ('%s') for %s at %u (term %u)", value.str(), field.str(), fieldstart, list.Count() + 1);
						}
						break;
					}

					default:
						errors.printf("Unknown field '%s' type (%u) (term %u)", field.str(), (uint_t)term->field->type, list.Count() + 1);
						break;
				}

				//debug("term: field {name '%s', type %u, assignable %u, offset %u} type %u dateflags %u value '%s'\n", term->field->name, (uint_t)term->field->type, (uint_t)term->field->assignable, term->field->offset, (uint_t)term->data.opcode, (uint_t)term->dateflags, term->value.str);

				if (errors.Empty()) {
					pattern.scorebased |= (term->field->offset == ADVBProg::GetScoreDataOffset());
					list.Add((uptr_t)term);
				}
				else {
					__DeleteTerm((uptr_t)term, NULL);
					break;
				}
			}
		}
	}
Пример #22
0
void MotionDetector::ProcessImage(const ADateTime& dt, const AImage& image)
{
    AString datestr = dt.DateFormat("%Y-%M-%D %h:%m:%s.%S");

    images[imgindex] = image;

    AImage& image1 = images[(imgindex + images.size() - 1) % images.size()];
    AImage& image2 = images[imgindex];
    imgindex = (imgindex + 1) % images.size();

    if ((image1.GetRect() == image2.GetRect()) && (image2 != image1)) {
        const AImage::PIXEL *pix1 = image1.GetPixelData();
        const AImage::PIXEL *pix2 = image2.GetPixelData();
        const ARect& rect = image1.GetRect();
        std::vector<float> data;
        float *ptr, *p;
        double avg[3];
        uint_t x, y, w = rect.w, h = rect.h, w3 = w * 3, len = w * h, len3 = w3 * h;

        data.resize(len3);
        ptr = &data[0];

        memset(avg, 0, sizeof(avg));

        for (y = 0, p = ptr; y < h; y++) {
            double yavg[3];

            memset(yavg, 0, sizeof(yavg));

            for (x = 0; x < w; x++, p += 3, pix1++, pix2++) {
                p[0] 	 = ((float)pix1->r - (float)pix2->r) * redscale;
                p[1] 	 = ((float)pix1->g - (float)pix2->g) * grnscale;
                p[2] 	 = ((float)pix1->b - (float)pix2->b) * bluscale;

                yavg[0] += p[0];
                yavg[1] += p[1];
                yavg[2] += p[2];
            }

            yavg[0] /= (double)w;
            yavg[1] /= (double)w;
            yavg[2] /= (double)w;

            p -= 3 * w;

            for (x = 0; x < w; x++, p += 3) {
                p[0] -= yavg[0];
                avg[0] += p[0];
                p[1] -= yavg[1];
                avg[1] += p[1];
                p[2] -= yavg[2];
                avg[2] += p[2];
            }
        }

        avg[0] /= (double)len;
        avg[1] /= (double)len;
        avg[2] /= (double)len;

        float *p2;
        for (y = 0, p = ptr, p2 = ptr; y < h; y++) {
            for (x = 0; x < w; x++, p += 3, p2++) {
                p[0] -= avg[0];
                p[1] -= avg[1];
                p[2] -= avg[2];
                p2[0] = sqrt(p[0] * p[0] + p[1] * p[1] + p[2] * p[2]);
            }
        }

        double avg2 = 0.0, sd2 = 0.0;
        uint_t mx, my, cx = (matwid - 1) >> 1, cy = (mathgt - 1) >> 1;
        for (x = 0; x < w; x++) {
            for (y = 0; y < h; y++) {
                float val = 0.f;

                if (matwid && mathgt) {
                    for (my = 0; my < mathgt; my++) {
                        if (((y + my) >= cy) && ((y + my) < (h + cy))) {
                            for (mx = 0; mx < matwid; mx++) {
                                if (((x + mx) >= cx) && ((x + mx) < (h + cx))) {
                                    val += matrix[mx + my * matwid] * ptr[(x + mx - cx) + (y + my - cy) * w];
                                }
                            }
                        }
                    }

                    val *= matmul;
                }
                else val = ptr[x + y * w];

                ptr[w * h + x + y * w] = (float)val;
                avg2 += val;
                sd2  += val * val;
            }
        }

        avg2 /= (double)len;
        sd2   = sqrt(sd2 / (double)len - avg2 * avg2);

        Interpolate(diffavg, avg2, coeff);
        Interpolate(diffsd,  sd2,  coeff);

        stats.Set(AString("avg%").Arg(index), AString("%0.16e").Arg(diffavg));
        stats.Set(AString("sd%").Arg(index),  AString("%0.16e").Arg(diffsd));

        double diff = avgfactor * diffavg + sdfactor * diffsd, total = 0.0;
        for (x = 0; x < len; x++) {
            ptr[x] = MAX(ptr[w * h + x] - diff, 0.0);
            total += ptr[x];
        }

        total = total * 1000.0 / ((double)w * (double)h);

        if (verbose) {
            log.printf("%s[%u]: Level = %0.1lf (diff = %0.6lf)\n",
                       datestr.str(), index, total, diff);
        }

        stats.Set(AString("level%").Arg(index), AString("%0.4").Arg(total));

        if (total >= threshold) {
            static const TAG tags[] = {
                {AImage::TAG_JPEG_QUALITY, 95},
                {TAG_DONE, 0},
            };

            if (detimgdir.Valid()) {
                AImage img;
                if (img.Create(rect.w, rect.h)) {
                    const AImage::PIXEL *pixel1 = image1.GetPixelData();
                    const AImage::PIXEL *pixel2 = image2.GetPixelData();
                    AImage::PIXEL       *pixel  = img.GetPixelData();

                    for (x = 0; x < len; x++, pixel++, pixel1++, pixel2++) {
                        pixel->r = (uint8_t)LIMIT((double)MAX(pixel1->r, pixel2->r) * ptr[x] / 255.0, 0.0, 255.0);
                        pixel->g = (uint8_t)LIMIT((double)MAX(pixel1->g, pixel2->g) * ptr[x] / 255.0, 0.0, 255.0);
                        pixel->b = (uint8_t)LIMIT((double)MAX(pixel1->b, pixel2->b) * ptr[x] / 255.0, 0.0, 255.0);
                    }

                    AString filename = detimgdir.CatPath(dt.DateFormat(detimgfmt) + ".jpg");
                    CreateDirectory(filename.PathPart());
                    img.SaveJPEG(filename, tags);
                }
            }

            AString filename = imagedir.CatPath(dt.DateFormat(imagefmt) + ".jpg");
            CreateDirectory(filename.PathPart());
            log.printf("%s[%u]: Saving detection image in '%s'\n", datestr.str(), index, filename.str());
            image2.SaveJPEG(filename, tags);

            if (detcmd.Valid()) {
                AString cmd = detcmd.SearchAndReplace("{level}", AString("%0.4").Arg(total));
                if (system(cmd) != 0) {
                    log.printf("%s[%u]: Detection command '%s' failed\n", datestr.str(), index, cmd.str());
                }
            }
        }
        else if (nodetcmd.Valid()) {
            AString cmd = nodetcmd.SearchAndReplace("{level}", AString("%0.4").Arg(total));
            if (system(cmd) != 0) {
                log.printf("%s[%u]: No-detection command '%s' failed\n", datestr.str(), index, cmd.str());
            }
        }
    }
    //else debug("Images are different sizes or identical\n");
}
Пример #23
0
bool DecodeXML(AStructuredNode& root, const AString& str)
{
	AStructuredNode *pNode = &root;
	AKeyValuePair   *pAttr;
	ADataList       stack;
	uint_t p1 = 0, p2;
	
	while (IsXMLWhiteSpace(str[p1])) p1++;
	while (!HasQuit() && pNode && (str[p1] == '<')) {
		bool popnode = false;

		p1++;
		if (str[p1] == '/') {
			p1++;
			if ((pNode != &root) && ((p2 = FindXMLMarker(str, p1)) > p1)) {
				uint_t p3 = p1;
				
				if (IsSymbolStart(str[p1])) p1++;
				while (IsXMLNameChar(str[p1])) p1++;

				AString name = str.Mid(p3, p1 - p3);
				if (name == pNode->Key) {
					if (debug_decode) debug("Closing node '%s'\n", pNode->Key.str());
					popnode = true;
					p1 = p2 + 1;
				}
				else {
					debug("Unknown close object '%s' at %u\n", name.str(), p1);
					break;
				}
			}
			else {
				debug("Unknown close marker at %u\n", p1);
				break;
			}
		}
		else if (str.Mid(p1, 3) == "!--") {
			if ((p2 = str.Pos("-->", p1 + 3)) > p1) p1 = p2 + 2;
			else {
				debug("Unterminated comment marker at %u\n", p1);
				break;
			}
		}
		else if ((p2 = FindXMLMarker(str, p1)) > p1) {
			if (pNode) stack.Push((void *)pNode);
			
			if ((pNode = new AStructuredNode) != NULL) {
				uint_t p3 = p2;
				bool   complete = ((str[p3 - 1] == '/') || ((str[p1] == '?') && (str[p3 - 1] == '?')));
				
				if (complete) p3--;

				complete |= (str[p1] == '!');

				if ((str[p1] == '?') || (str[p1] == '!')) pNode->SetType(str[p1++]);
				
				while (IsXMLWhiteSpace(str[p1])) p1++;

				uint_t p4 = p1;
				if (IsSymbolStart(str[p1])) p1++;
				while (IsXMLNameChar(str[p1])) p1++;

				pNode->Key = str.Mid(p4, p1 - p4).DeHTMLify();
				if (debug_decode) debug("Created new node '%s' (%s)\n", pNode->Key.str(), complete ? "complete" : "open");

				while (IsXMLWhiteSpace(str[p1])) p1++;

				while (p1 < p3) {
					p4 = p1;
					if (IsSymbolStart(str[p1])) p1++;
					while (IsXMLNameChar(str[p1])) p1++;
					uint_t p5 = p1;
					
					while (IsXMLWhiteSpace(str[p1])) p1++;
					if (str[p1] == '=') p1++;
					while (IsXMLWhiteSpace(str[p1])) p1++;

					int quote = 0;
					if (IsQuoteChar(str[p1])) quote = str[p1++];

					uint_t p6 = p1;
					while (str[p1] && ((quote && (str[p1] != quote)) ||
									   (!quote && !IsXMLWhiteSpace(str[p1])))) p1++;
					uint_t p7 = p1;

					if (quote && (str[p1] == quote)) p1++;
					while (IsXMLWhiteSpace(str[p1])) p1++;

					if ((p5 > p4) && ((pAttr = new AKeyValuePair) != NULL)) {
						pAttr->Key   = str.Mid(p4, p5 - p4).DeHTMLify();
						pAttr->Value = str.Mid(p6, p7 - p6).DeHTMLify();
						pNode->AddAttribute(pAttr);
						if (debug_decode) debug("Added attribute '%s'='%s' to '%s'\n", pAttr->Key.str(), pAttr->Value.str(), pNode->Key.str());
					}
					else break;
				}
				
				AStructuredNode *pParent = (AStructuredNode *)stack.Last();
				if (pParent) pParent->AddChild(pNode);

				p2++;
				while (IsXMLWhiteSpace(str[p2])) p2++;

				p3 = p1 = p2;
				while (str[p2] && (str[p2] != '<')) {
					p2++;
					if (!IsXMLWhiteSpace(str[p2 - 1])) p3 = p2;
				}
				if (p3 > p1) {
					pNode->Value = str.Mid(p1, p3 - p1).DeHTMLify();
					if (debug_decode) debug("Set value of '%s' to '%s'\n", pNode->Key.str(), pNode->Value.str());
				}
				p1 = p2;

				popnode = complete;
			}
		}

		if (popnode) {
			if (stack.Count()) {
				pNode = (AStructuredNode *)stack.EndPop();
				if (debug_decode) debug("Back to node '%s'\n", pNode->Key.str());
			}
			else {
				debug("Stack empty at %u\n", p1);
				break;
			}
		}
		
		while (IsXMLWhiteSpace(str[p1])) p1++;
	}

	if (stack.Count()) debug("Unterminated XML entries at %u\n", p1);
	if (!pNode)		   debug("Extra XML termination at %u\n", p1);
	
	return (!str[p1] && (pNode == &root));
}
Пример #24
0
void ADVBPatterns::AssignValue(ADVBProg& prog, const FIELD& field, const VALUE& value, uint8_t termtype)
{
	uint8_t *ptr = prog.GetDataPtr(field.offset);

	switch (field.type) {
		case FieldType_string: {
			AString str;

			if (termtype == Operator_Concat) {
				uint16_t offset;

				memcpy(&offset, ptr, sizeof(offset));
				str = prog.GetString(offset);
			}

			if (termtype == Operator_Remove) {
				uint16_t offset;

				memcpy(&offset, ptr, sizeof(offset));
				str = prog.GetString(offset);
				str = str.SearchAndReplace(value.str, "");
			}
			else str += value.str;

			prog.SetString((const uint16_t *)ptr, str.str());
			break;
		}

		case FieldType_date:
		case FieldType_span:
		case FieldType_age:
			memcpy(ptr, &value.u64, sizeof(value.u64));
			break;

		case FieldType_uint32_t...FieldType_sint8_t: {
			sint64_t val1 = TermTypeToInt64s(ptr, field.type);
			sint64_t val2 = TermTypeToInt64s(&value.u64, field.type);
			sint64_t val;

			switch (termtype) {
				case Operator_Add:
					val = val1 + val2;
					break;

				case Operator_Subtract:
					val = val1 - val2;
					break;

				case Operator_Multiply:
					val = val1 * val2;
					break;

				case Operator_Divide:
					if (val2 != 0) val = val1 / val2;
					else		   val = val1;
					break;

				case Operator_Maximum:
					val = MAX(val1, val2);
					break;

				case Operator_Minimum:
					val = MIN(val1, val2);
					break;

				default:
					val = val2;
					break;
			}

			Int64sToTermType(ptr, val, field.type);

			// detect writing to dvbcard
			if (ptr == &prog.data->dvbcard) {
				//debug("DVB card specified as %u\n", (uint_t)data->dvbcard);
				prog.SetDVBCardSpecified();
			}
			break;
		}

		case FieldType_flag...FieldType_lastflag: {
			uint32_t flags, mask = 1UL << (field.type - FieldType_flag);

			memcpy(&flags, ptr, sizeof(flags));
			if (value.u8) flags |=  mask;
			else		  flags &= ~mask;
			memcpy(ptr, &flags, sizeof(flags));
			break;
		}
	}
}
Пример #25
0
/*--------------------------------------------------------------------------------*/
bool PostgresDatabase::GrantPrivileges(const AString& database, const AString& username)
{
	bool success = false;

	if ((success = RunSQL(AString::Formatify("grant all privileges on database %s to %s", database.str(), username.str()))) == false) {
		debug("Failed to grant privileges on database: %s\n", GetErrorMessage().str());
	}

	return success;
}
Пример #26
0
AString GetRemoteCommand(const AString& cmd, const AString& postcmd, bool compress, bool streamslave)
{
	const ADVBConfig& config = ADVBConfig::Get();
	AString host = streamslave ? config.GetStreamSlave()     : config.GetRecordingSlave();
	uint_t  port = streamslave ? config.GetStreamSlavePort() : config.GetRecordingSlavePort();
	AString cmd1;

	cmd1.printf("ssh %s %s -p %u %s \"%s\" %s", compress ? "-C" : "", config.GetSSHArgs().str(), port, host.str(), cmd.Escapify().str(), postcmd.str());

	return cmd1;
}
Пример #27
0
void AUDPServer::Send(const AString& data, const struct sockaddr *to)
{
    Send((const uint8_t *)data.str(), data.len(), to);
}
Пример #28
0
bool ADVBLock::GetLock(uint_t n)
{
	const ADVBConfig& config = ADVBConfig::Get();
	LOCK *lock = (LOCK *)lockhash.Read(name);
	bool success = false;

	(void)config;

	if (!lock) {
		if ((lock = new LOCK) != NULL) {
			lock->filename.printf("lockfile_%s.lock", name.str());
			lock->fd = -1;
			lock->refcount = 0;

			lockhash.Insert(name, (uptr_t)lock);
		}
	}

	if (lock) {
		if (!lock->refcount) {
			AString lockfile = GetFilename(lock);

			if ((lock->fd = open(lockfile, O_CREAT | O_RDWR, (S_IRUSR | S_IWUSR))) >= 0) {
				if (flock(lock->fd, LOCK_EX) >= 0) {
					//config.logit("Acquired lock '%s' (filename '%s')\n", name.str(), lockfile.str());
					lock->refcount += n;
					success = true;
				}
				else config.logit("Failed to lock file: %s\n", strerror(errno));
			}
			else config.logit("Failed to create lockfile '%s' (%s)!  All Hell could break loose!!\n", lockfile.str(), strerror(errno));
		}
	}
	else config.logit("Failed to create lock '%s'!\n", name.str());

	return success;
}
Пример #29
0
void MotionDetector::Configure()
{
    AString nstr = AString("%").Arg(index);

    log.printf("%s[%u]: Reading new settings...\n", ADateTime().DateFormat("%Y-%M-%D %h:%m:%s.%S").str(), index);

    stream.Close();
    stream.SetUsernameAndPassword(GetSetting("username", "admin"),
                                  GetSetting("password", "arsebark"));

    AString camera = GetSetting("camera", "");
    log.printf("%s[%u]: Connecting to %s...\n", ADateTime().DateFormat("%Y-%M-%D %h:%m:%s.%S").str(), index, camera.str());
    if (!stream.OpenHost(camera)) {
        log.printf("%s[%u]: Failed to connect to %s...\n", ADateTime().DateFormat("%Y-%M-%D %h:%m:%s.%S").str(), index, camera.str());
    }

    imagedir  = GetSetting("imagedir", "/media/cctv");
    imagefmt  = GetSetting("filename", "%Y-%M-%D/%h/Image-{camera}-%Y-%M-%D-%h-%m-%s-%S");
    detimgdir = GetSetting("detimagedir");
    detimgfmt = GetSetting("detfilename", "%Y-%M-%D/%h/detection/Image-{camera}-%Y-%M-%D-%h-%m-%s-%S");
    detcmd    = GetSetting("detcommand", "");
    nodetcmd  = GetSetting("nodetcommand", "");
    coeff  	  = (double)GetSetting("coeff", "1.0e-3");
    avgfactor = (double)GetSetting("avgfactor", "1.0");
    sdfactor  = (double)GetSetting("sdfactor", "2.0");
    redscale  = (double)GetSetting("rscale", "1.0");
    grnscale  = (double)GetSetting("gscale", "1.0");
    bluscale  = (double)GetSetting("bscale", "1.0");
    threshold = (double)GetSetting("threshold", "3000.0");
    verbose   = (uint_t)GetSetting("verbose", "0");

    matmul 	  = 1.f;
    matwid 	  = mathgt = 0;

    AString _matrix = GetSetting("matrix", "");
    if (_matrix.Valid()) {
        uint_t row, nrows = _matrix.CountLines(";");
        uint_t col, ncols = 1;
        int    p;

        if ((p = _matrix.Pos("*")) >= 0) {
            AString mul = _matrix.Mid(p + 1);

            _matrix = _matrix.Left(p);

            if ((p = mul.Pos("/")) >= 0) {
                matmul = (float)mul.Left(p) / (float)mul.Mid(p + 1);
            }
            else matmul = (float)mul;
        }
        else if ((p = _matrix.Pos("/")) >= 0) {
            AString mul = _matrix.Mid(p + 1);

            _matrix = _matrix.Left(p);

            matmul = 1.f / (float)mul;
        }

        for (row = 0; row < nrows; row++) {
            uint_t n = _matrix.Line(row, ";").CountLines(",");
            ncols = MAX(ncols, n);
        }

        nrows |= 1;
        ncols |= 1;

        matrix.resize(nrows * ncols);
        for (row = 0; row < nrows; row++) {
            AString line = _matrix.Line(row,";");

            for (col = 0; col < ncols; col++) matrix[col + row * ncols] = (float)line.Line(col, ",");
        }

        matwid = ncols;
        mathgt = nrows;

#if 0
        printf("Matrix is %u x %u:\n", matwid, mathgt);
        for (row = 0; row < nrows; row++) {
            for (col = 0; col < ncols; col++) printf("%8.3f", matrix[col + row * ncols]);
            printf("\n");
        }
        printf("Multiplier %0.6f\n", matmul);
#endif
    }
    else matrix.resize(0);
}