void IndoorLocalizationService::addTrackedDeviceCharacteristic() {

	buffer_ptr_t buffer = MasterBuffer::getInstance().getBuffer();

	_trackedDeviceCharac = new Characteristic<buffer_ptr_t>();
	addCharacteristic(_trackedDeviceCharac);

	_trackedDeviceCharac->setUUID(UUID(getUUID(), TRACKED_DEVICE_UUID));
	_trackedDeviceCharac->setName("Add tracked device");
	_trackedDeviceCharac->setWritable(true);
	_trackedDeviceCharac->setNotifies(false);
	_trackedDeviceCharac->onWrite([&](const buffer_ptr_t& value) -> void {
		TrackedDevice dev;
		dev.assign(_trackedDeviceCharac->getValue(), _trackedDeviceCharac->getValueLength());

		if (dev.getRSSI() > 0) {
			LOGi("Remove tracked device");
			dev.print();
			_trackedDeviceList->rem(dev.getAddress());
		} else {
			LOGi("Add tracked device");
			dev.print();
			_trackedDeviceList->add(dev.getAddress(), dev.getRSSI());
		}
		_trackedDeviceListCharac->setDataLength(_trackedDeviceList->getDataLength());
		_trackedDeviceListCharac->notify();

		LOGi("currently tracking devices:");
		_trackedDeviceList->print();

		// store in persistent memory
		writeTrackedDevices();
		savePersistentStorage();

		if (_trackedDeviceList->getSize() > 0) {
			startTracking();
		}
		else {
			stopTracking();
		}
	});

	_trackedDeviceCharac->setValue(buffer);
	_trackedDeviceCharac->setMaxLength(TRACKDEVICES_SERIALIZED_SIZE);
	_trackedDeviceCharac->setDataLength(0);

}
Example #2
0
void Tracker::handleTrackedDeviceCommand(buffer_ptr_t buffer, uint16_t size) {

	TrackedDevice dev;
	//TODO: should we check the result of assign() ?
	dev.assign(buffer, size);

	if (dev.getRSSI() > 0) {

#ifdef PRINT_TRACKER_VERBOSE
		LOGi("Remove tracked device");
#endif

#ifdef PRINT_DEBUG
		dev.print();
#endif

		Tracker::getInstance().removeTrackedDevice(dev);
	} else {

#ifdef PRINT_TRACKER_VERBOSE
		LOGi("Add tracked device");
#endif

#ifdef PRINT_DEBUG
		dev.print();
#endif

		Tracker::getInstance().addTrackedDevice(dev);
	}
	TrackedDeviceList* trackedDeviceList = Tracker::getInstance().getTrackedDevices();

#ifdef PRINT_DEBUG
	LOGi("currently tracking devices:");
	trackedDeviceList->print();
#endif

	if (trackedDeviceList->getSize() > 0) {
		Tracker::getInstance().startTracking();
	} else {
		Tracker::getInstance().stopTracking();
	}

}