Skip to content

Commit

Permalink
Feature/propagate platform connection (#74)
Browse files Browse the repository at this point in the history
* Added Platform Status Service and the message to send

* Integrated Platform Status Service

* Sending CONNECTED/OFFLINE for platform connection status

* Removed slash on topic

* Changer order of connection

* Bumped the version to v4.3.1

* Platform connection messages retained; Set as lastwill;

* Removed const qualifier in function argument; Log message publish;

Co-authored-by: nanavuletic <nenad.vuletic@wolkabout.com>
  • Loading branch information
LazarBozic94 and nanavuletic authored Dec 10, 2021
1 parent 0cdfce7 commit e738543
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 12 deletions.
3 changes: 3 additions & 0 deletions RELEASE_NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

WolkGateway bridges communication between WolkAbout IoT platform and multiple devices connected to it.

**Version 4.3.1**
- [IMPROVEMENT] - Implemented sending of `p2d/connection_status` message to the local broker, to announce platform connection state to the modules.

**Version 4.3.0**
- [IMPROVEMENT] - Created WolkDefault and WolkExternal that provide different interfaces for custom subdevice data providing.
- [IMPROVEMENT] - Introduced the FSFileRepository to provide info about FileManagement files from the File System.
Expand Down
13 changes: 3 additions & 10 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
wolkgateway (4.3.0) stable; urgency=medium
wolkgateway (4.3.1) stable; urgency=medium

* Created WolkDefault and WolkExternal that provide different interfaces for custom subdevice data providing.
* Introduced the FSFileRepository to provide info about FileManagement files from the File System.
* Disabled the Gateway Update message sending and mechanisms as they have been found unnecessary.
* Imported the newest WolkSDK-Cpp that introduces various fixes and improvements.
* Moved the version file when firmware installing to a different location. Also, finishing the install method with `true` now reports success.
* Introduced the custom data and device status protocols, that allow us to define custom MQTT topics/payloads for those messages.
* Added the FileListener interface for FileManagementService to allow input into the File Management process and allow for hooks.
* Made optimizations for the application to be built as a submodule.
* Implemented sending of `p2d/connection_status` message to the local broker, to announce platform connection state to the modules.

-- Wolkabout ELab <elab@wolkabout.com> Fri, 14 May 2021 00:00:00 +0100
-- Wolkabout ELab <elab@wolkabout.com> Wed, 08 Dec 2021 00:00:00 +0100
11 changes: 11 additions & 0 deletions src/Wolk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "service/data/DataService.h"
#include "service/data/GatewayDataService.h"
#include "service/status/DeviceStatusService.h"
#include "service/status/PlatformStatusService.h"

#include <memory>
#include <sstream>
Expand Down Expand Up @@ -363,6 +364,11 @@ void Wolk::notifyPlatformConnected()
{
m_keepAliveService->connected();
}

if (m_platformStatusService)
{
m_platformStatusService->sendPlatformConnectionStatusMessage(true);
}
}

void Wolk::notifyPlatformDisonnected()
Expand All @@ -379,6 +385,11 @@ void Wolk::notifyPlatformDisonnected()
{
m_keepAliveService->disconnected();
}

if (m_platformStatusService)
{
m_platformStatusService->sendPlatformConnectionStatusMessage(false);
}
}

void Wolk::connectToPlatform(bool firstTime)
Expand Down
2 changes: 2 additions & 0 deletions src/Wolk.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class JsonDownloadProtocol;
class KeepAliveService;
class PublishingService;
class Persistence;
class PlatformStatusService;
class RegistrationMessageRouter;
class RegistrationProtocol;
class StatusMessageRouter;
Expand Down Expand Up @@ -301,6 +302,7 @@ class Wolk
std::unique_ptr<KeepAliveService> m_keepAliveService;
std::unique_ptr<DeviceStatusService> m_deviceStatusService;
std::shared_ptr<StatusMessageRouter> m_statusMessageRouter;
std::shared_ptr<PlatformStatusService> m_platformStatusService;

std::unique_ptr<JsonDFUProtocol> m_firmwareUpdateProtocol;
std::unique_ptr<GatewayFirmwareUpdateProtocol> m_gatewayFirmwareUpdateProtocol;
Expand Down
6 changes: 5 additions & 1 deletion src/WolkBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include "service/status/DeviceStatusService.h"
#include "service/status/ExternalDeviceStatusService.h"
#include "service/status/InternalDeviceStatusService.h"
#include "service/status/PlatformStatusService.h"

#include <memory>
#include <stdexcept>
Expand Down Expand Up @@ -352,6 +353,8 @@ void WolkBuilder::setupWithInternalData(WolkDefault* wolk)
const std::string localMqttClientId = std::string("Gateway-").append(m_device.getKey());
wolk->m_deviceConnectivityService.reset(new MqttConnectivityService(
std::make_shared<PahoMqttClient>(), m_device.getKey(), m_device.getPassword(), m_gatewayHost, localMqttClientId));
wolk->m_deviceConnectivityService->setUncontrolledDisonnectMessage(
wolk->m_gatewayStatusProtocol->makePlatformConnectionStatusMessage(false), true);

// Create the publisher for the devices (local MQTT)
wolk->m_devicePublisher.reset(
Expand Down Expand Up @@ -396,7 +399,8 @@ void WolkBuilder::setupWithInternalData(WolkDefault* wolk)
m_device.getSubdeviceManagement().value() == SubdeviceManagement::GATEWAY ? wolk->m_deviceRepository.get() :
nullptr,
*wolk->m_platformPublisher, *wolk->m_devicePublisher, Wolk::KEEP_ALIVE_INTERVAL));

// Setup platform status service
wolk->m_platformStatusService.reset(new PlatformStatusService(*wolk->m_deviceConnectivityService, *wolk->m_gatewayStatusProtocol));
// Setup the data service
wolk->m_dataService = std::make_shared<InternalDataService>(
m_device.getKey(), *wolk->m_dataProtocol, *wolk->m_gatewayDataProtocol,
Expand Down
2 changes: 1 addition & 1 deletion src/WolkDefault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ WolkDefault::~WolkDefault() = default;

void WolkDefault::connect()
{
connectToPlatform(true);
connectToDevices(true);
connectToPlatform(true);
}

void WolkDefault::disconnect()
Expand Down
1 change: 1 addition & 0 deletions src/protocol/GatewayStatusProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class GatewayStatusProtocol : public GatewayProtocol
using GatewayProtocol::extractDeviceKeyFromChannel;

virtual std::unique_ptr<Message> makeDeviceStatusRequestMessage(const std::string& deviceKey) const = 0;
virtual std::unique_ptr<Message> makePlatformConnectionStatusMessage(const bool connected) const = 0;

virtual std::unique_ptr<DeviceStatus> makeDeviceStatusResponse(const Message& message) const = 0;
virtual std::unique_ptr<DeviceStatus> makeDeviceStatusUpdate(const Message& message) const = 0;
Expand Down
8 changes: 8 additions & 0 deletions src/protocol/json/JsonGatewayStatusProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const std::string JsonGatewayStatusProtocol::LAST_WILL_TOPIC_ROOT = "lastwill/";
const std::string JsonGatewayStatusProtocol::DEVICE_STATUS_RESPONSE_TOPIC_ROOT = "d2p/subdevice_status_response/";
const std::string JsonGatewayStatusProtocol::DEVICE_STATUS_UPDATE_TOPIC_ROOT = "d2p/subdevice_status_update/";
const std::string JsonGatewayStatusProtocol::DEVICE_STATUS_REQUEST_TOPIC_ROOT = "p2d/subdevice_status_request/";
const std::string JsonGatewayStatusProtocol::PLATFORM_CONNECTION_STATUS_ROOT = "p2d/connection_status";

const std::string JsonGatewayStatusProtocol::STATUS_RESPONSE_STATE_FIELD = "state";
const std::string JsonGatewayStatusProtocol::STATUS_RESPONSE_STATUS_CONNECTED = "CONNECTED";
Expand Down Expand Up @@ -87,6 +88,13 @@ std::unique_ptr<Message> JsonGatewayStatusProtocol::makeDeviceStatusRequestMessa
return std::unique_ptr<Message>(new Message("", topic));
}

std::unique_ptr<Message> JsonGatewayStatusProtocol::makePlatformConnectionStatusMessage(const bool connected) const
{
LOG(TRACE) << METHOD_INFO;

return std::unique_ptr<Message>(new Message(connected ? STATUS_RESPONSE_STATUS_CONNECTED : STATUS_RESPONSE_STATUS_OFFLINE, PLATFORM_CONNECTION_STATUS_ROOT));
}

std::unique_ptr<DeviceStatus> JsonGatewayStatusProtocol::makeDeviceStatusResponse(const Message& message) const
{
LOG(TRACE) << METHOD_INFO;
Expand Down
2 changes: 2 additions & 0 deletions src/protocol/json/JsonGatewayStatusProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class JsonGatewayStatusProtocol : public GatewayStatusProtocol

std::unique_ptr<DeviceStatus> makeDeviceStatusResponse(const Message& message) const override;
std::unique_ptr<DeviceStatus> makeDeviceStatusUpdate(const Message& message) const override;
std::unique_ptr<Message> makePlatformConnectionStatusMessage(const bool connected) const override;

bool isStatusResponseMessage(const Message& message) const override;
bool isStatusUpdateMessage(const Message& message) const override;
Expand All @@ -57,6 +58,7 @@ class JsonGatewayStatusProtocol : public GatewayStatusProtocol
static const std::string DEVICE_STATUS_RESPONSE_TOPIC_ROOT;
static const std::string DEVICE_STATUS_UPDATE_TOPIC_ROOT;
static const std::string DEVICE_STATUS_REQUEST_TOPIC_ROOT;
static const std::string PLATFORM_CONNECTION_STATUS_ROOT;
};
} // namespace wolkabout

Expand Down
50 changes: 50 additions & 0 deletions src/service/status/PlatformStatusService.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2021 WolkAbout Technology s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "PlatformStatusService.h"

#include "core/connectivity/ConnectivityService.h"
#include "core/model/Message.h"
#include "core/utilities/Logger.h"
#include "protocol/GatewayStatusProtocol.h"

namespace wolkabout
{

PlatformStatusService::PlatformStatusService(ConnectivityService& connectivityService, GatewayStatusProtocol& protocol)
: m_connectivityService(connectivityService), m_protocol(protocol)
{
}

void PlatformStatusService::sendPlatformConnectionStatusMessage(bool connected)
{
std::shared_ptr<Message> message = m_protocol.makePlatformConnectionStatusMessage(connected);
if(!message)
{
return;
}

if(!m_connectivityService.publish(message, true))
{
LOG(DEBUG) << "PlatformStatusService: Failed to send platform status message";
}
else
{
LOG(DEBUG) << "PlatformStatusService: Published platform status message";
}
}

} // namespace wolkabout
37 changes: 37 additions & 0 deletions src/service/status/PlatformStatusService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2021 WolkAbout Technology s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef PLATFORMSTATUSSERVICE_H
#define PLATFORMSTATUSSERVICE_H

namespace wolkabout
{
class ConnectivityService;
class GatewayStatusProtocol;

class PlatformStatusService
{
public:
PlatformStatusService(ConnectivityService& connectivityService, GatewayStatusProtocol& protocol);
void sendPlatformConnectionStatusMessage(bool connected);

private:
ConnectivityService& m_connectivityService;
GatewayStatusProtocol& m_protocol;
};

} // namespace wolkabout
#endif // PLATFORMSTATUSSERVICE_H

0 comments on commit e738543

Please sign in to comment.