Esempio n. 1
0
	void Timer::Countup ()
	{
		if (m_control.b.countup)
		{
			--m_count;
			if (m_count == 0)
				TimeEvent();
		}
	}
Esempio n. 2
0
void SerialCommunicator::PerformCommunication(QIODevice * bt_socket_ptr, const QString & peer_name)
{
	unsigned int request_number = 0;
	bool socket_no_error = true;
	enum message_enum { OKAY_MSG = 0,  INIT_MSG = 1, DATA_MSG = 2, DUMP_MSG = 3, TIME_MSG = 4, TEST_MSG = 5, FINI_MSG = 6 };

	while(request_number < MAX_REQUESTS && socket_no_error)
	{
		//char command_str[MAX_COMMAND_LENGTH+1] = {'\0'};
		char command_str[1] = {0};

		// Beginning of communication first request
		if(request_number == 0)
		{
			bt_socket_ptr->putChar(OKAY_MSG);
		}

		// wait for request response upto TIMEOUT_MS
		//socket_no_error = ReceiveNChars(command_str, bt_socket_ptr,
		//		TIMEOUT_MS, MAX_COMMAND_LENGTH);
		socket_no_error = ReceiveNChars(command_str, bt_socket_ptr,
				TIMEOUT_MS, 1);


		// check for CASE A - slave has sent 'INIT' 
		//if(!strncmp(INIT_MSG_STR, command_str, MAX_COMMAND_LENGTH))
		if(command_str[0] == INIT_MSG)
		{
			//DEBUG
			std::cout << "INIT requested" << std::endl;
			// start scheduling handling for the requesting device
			std::future<std::unique_ptr<rendezvous_answer>> future_schedule =
				std::async(
					std::launch::async,
					&Scheduler<5>::ScheduleNextCollectionStart, 
					scheduler_, 
					peer_name);

			bt_socket_ptr->putChar(OKAY_MSG);

			const auto answer_unique_ptr( std::move(future_schedule.get()) );
			//TODO change to 5 * 60 seconds value after DEBUG
			answer_unique_ptr->interval_length_seconds = 300;
			bt_socket_ptr->write((char *) answer_unique_ptr.get(), 
					sizeof(rendezvous_answer));

			struct timestamp stamp;
			DatabaseManager::TimeConvertToDeviceTime(std::chrono::system_clock::now(),
					&stamp);
			bt_socket_ptr->write((char *) &stamp, sizeof(timestamp));
		}

		// check for CASE B1 - slave has sent 'DATA'
		//else if(!strncmp(DATA_MSG_STR, command_str, MAX_COMMAND_LENGTH))
		else if(command_str[0] == DATA_MSG)
		{
			std::cout << "DATA requested" << std::endl;
			std::future<std::unique_ptr<rendezvous_answer>> future_schedule =
				std::async(
					std::launch::async,
					&Scheduler<5>::ScheduleNextCollectionStart, 
					scheduler_, 
					peer_name);

			bt_socket_ptr->putChar(OKAY_MSG);

			//unsigned int counter = 0;
			//while(counter < 36)
			//{
			//	char c = 0xFF;
			//	bt_socket_ptr->waitForReadyRead(1000);
			//	bt_socket_ptr->getChar(&c);
			//	std::cout << "counter:" << counter <<" value: " << (unsigned int) c << std::endl;
			//	++counter;
			//}

			ReceiveAndStoreTemperatures(bt_socket_ptr, peer_name);

			auto answer_unique_ptr( move(future_schedule.get()) );
			//TODO change to 5 * 60 seconds value after DEBUG
			answer_unique_ptr->interval_length_seconds = 300;
			bt_socket_ptr->write((char *) answer_unique_ptr.get(), 
					sizeof(rendezvous_answer));
			bt_socket_ptr->waitForBytesWritten(500);
			break;
		}
		// check for CASE B2 - slave has sent 'DUMP'
		//else if(!strncmp(DUMP_MSG_STR, command_str, MAX_COMMAND_LENGTH))
		else if(command_str[0] == DUMP_MSG)
		{
			std::cout << "DUMP requested" << std::endl;
			bt_socket_ptr->putChar(OKAY_MSG);

			ReceiveAndStoreTemperatures(bt_socket_ptr, peer_name);

			//std::thread thread = std::thread(&DatabaseManager::PushRendezvousEvent,
			//		db_manager_ptr_, peer_name, 
			//		std::chrono::system_clock::now());
			//thread.detach();
			emit RendezvousEvent(peer_name, std::move(std::chrono::system_clock::now()));
			break;
		}
		// check for CASE C - slave has sent 'TIME'
		//else if(!strncmp(TIME_MSG_STR, command_str, MAX_COMMAND_LENGTH))
		else if(command_str[0] == TIME_MSG)
		{
			std::cout << "TIME requested" << std::endl;
			bt_socket_ptr->putChar(OKAY_MSG);

			struct timestamp stamp;
			DatabaseManager::TimeConvertToDeviceTime(std::chrono::system_clock::now(),
					&stamp);
			bt_socket_ptr->write((char *) &stamp, sizeof(timestamp));
			emit TimeEvent(peer_name, std::move(std::chrono::system_clock::now()));
		}
		// check for CASE D - slave has sent 'TEST'
		//else if(!strncmp(TEST_MSG_STR, command_str, MAX_COMMAND_LENGTH))
		else if(command_str[0] == TEST_MSG)
		{
			std::cout << "TEST requested" << std::endl;
			std::shared_ptr<struct timestamp> stamp_ptr(new timestamp);
			std::shared_ptr<struct temperature_reading> temperatures_ptr(new temperature_reading);

			ReceiveNChars((char *) stamp_ptr.get(), bt_socket_ptr, TIMEOUT_MS, sizeof(timestamp));
			ReceiveNChars((char *) temperatures_ptr.get(), bt_socket_ptr, TIMEOUT_MS, sizeof(temperature_reading));

			//auto db_manager_thread = std::thread(&DatabaseManager::HandleTestData, db_manager_ptr_,
			//		peer_name,
			//		std::move(stamp_ptr), std::move(temperatures_ptr));
			//db_manager_thread.detach();
			emit PushTestToDBManager(peer_name, stamp_ptr, temperatures_ptr);
		}
		//else if(!strncmp(FINI_MSG_STR, command_str, MAX_COMMAND_LENGTH))
		else if(command_str[0] == FINI_MSG)
		{
			std::cout << "FINI received" << std::endl;
			break;
		}
		// request command string not recognized - error CASE
		else
		{
			std::cout << "Error while parsing command" << std::endl;
			emit ErrorEvent(peer_name, std::chrono::system_clock::now(), 0);
			// TODO refine error handling in case the received command is not recognized 
			socket_no_error = false;
		}

		++request_number;
	}

	// shutdown communication
	// bt_socket_ptr->disconnectFromService();
	std::cout << "leaving serial handler" << std::endl;
	bt_socket_ptr->close();

}