Commit 353f9e1d authored by Micha Mueller's avatar Micha Mueller
Browse files

BACnet plugin: Introduce new BACnetClient class to take care of network communication

parent 3e0a98e5
/*
* BACnetClient.cpp
*
* Created on: 20.04.2018
* Author: Micha Mueller
*/
#include "BACnetClient.h"
BACnetClient::BACnetClient() {
_strand = NULL;
//TODO setup datalink
}
BACnetClient::~BACnetClient() {
// TODO teardown datalink
}
uint64_t BACnetClient::readProperty(uint32_t deviceObjectInstance, uint32_t objectInstance, BACNET_OBJECT_TYPE objectType, BACNET_PROPERTY_ID objectIndex, int32_t objectIndex) {
//TODO
return 0;
}
void BACnetClient::initializeStrand(boost::asio::io_service& io) {
if (!_strand) {
_strand = new boost::asio::io_service::strand(io);
}
}
/*
* BACnetClient.h
*
* Created on: 20.04.2018
* Author: Micha Mueller
*/
#ifndef BACNETCLIENT_H_
#define BACNETCLIENT_H_
#include "../../Sensor.h" //only for logging
#include "bacenum.h"
#include <boost/asio.hpp>
class BACnetClient {
public:
BACnetClient();
virtual ~BACnetClient();
void initializeStrand(boost::asio::io_service& io);
boost::asio::io_service::strand* getStrand() const {
return _strand;
}
uint64_t readProperty(uint32_t deviceObjectInstance, uint32_t objectInstance = 0, BACNET_OBJECT_TYPE objectType = OBJECT_DEVICE, BACNET_PROPERTY_ID objectIndex = PROP_PRESENT_VALUE, int32_t objectIndex = BACNET_ARRAY_ALL);
private:
boost::asio::io_service::strand* _strand;
boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
};
#endif /* BACNETCLIENT_H_ */
......@@ -8,14 +8,14 @@
#include "BACnetConfigurator.h"
BACnetConfigurator::BACnetConfigurator() {
// TODO Auto-generated constructor stub
_bacClient = NULL;
}
BACnetConfigurator::~BACnetConfigurator() {
for(auto s : _sensors) {
delete s;
}
delete _bacClient;
}
std::vector<Sensor*>& BACnetConfigurator::readConfig(std::string cfgPath) {
......
......@@ -8,8 +8,11 @@
#ifndef BACNETCONFIGURATOR_H_
#define BACNETCONFIGURATOR_H_
#include "BACnetSensor.h"
#include "../../Configurator.h"
#include "BACnetSensor.h"
#include "BACnetClient.h"
#include <string>
#include <vector>
#include <map>
......@@ -38,6 +41,7 @@ private:
*/
void readSensor(BACnetSensor& sensor, boost::property_tree::iptree& config);
BACnetClient* _bacClient;
std::vector<Sensor*> _sensors;
sensorMap_t _templateSensors;
};
......
......@@ -14,6 +14,11 @@ extern volatile int keepRunning;
BACnetSensor::BACnetSensor(const std::string& name) :
Sensor(name) {
_deviceInstance = 0;
_objectInstance = 0;
_objectType = OBJECT_DEVICE;
_property = PROP_PRESENT_VALUE;
_objectIndex = BACNET_ARRAY_ALL;
_bacClient = NULL;
}
BACnetSensor::~BACnetSensor() {
......@@ -21,7 +26,20 @@ BACnetSensor::~BACnetSensor() {
}
void BACnetSensor::read() {
//TODO
reading_t reading;
reading.timestamp = getTimestamp();
try {
reading.value = _bacClient->readProperty(_deviceInstance);
_readingQueue->push(reading);
_latestValue.value = reading.value;
_latestValue.timestamp = reading.timestamp;
#ifdef DEBUG
LOG(debug) << _pdu->getHost() << "::" << _name << ": \"" << reading.value << "\"";
#endif
} catch (const std::exception& e) {
LOG(error) << _name << " could not read value: " << e.what();
}
}
void BACnetSensor::readAsync() {
......@@ -39,6 +57,13 @@ void BACnetSensor::startPolling(boost::asio::io_service& io) {
_readingQueue = new boost::lockfree::spsc_queue<reading_t>(1024);
}
_timer = new boost::asio::deadline_timer(io, boost::posix_time::seconds(0));
_timer->async_wait(std::bind(&BACnetSensor::readAsync, this));
if (_bacClient) {
_bacClient->initializeStrand(io);
_timer = new boost::asio::deadline_timer(io, boost::posix_time::seconds(0));
_timer->async_wait(std::bind(&BACnetSensor::readAsync, this));
} else {
LOG(error) << "No BACnetClient set for sensor " << _name << "! Cannot start polling.";
}
}
......@@ -9,6 +9,9 @@
#define BACNETSENSOR_H_
#include "../../Sensor.h"
#include "BACnetClient.h"
#include "bacenum.h"
class BACnetSensor: public Sensor {
......@@ -16,6 +19,7 @@ public:
BACnetSensor(const std::string& name);
virtual ~BACnetSensor();
/**** GETTERS + SETTERS ****/
uint32_t getDeviceInstance() const {
return _deviceInstance;
}
......@@ -24,6 +28,47 @@ public:
_deviceInstance = deviceInstance;
}
int32_t getObjectIndex() const {
return _objectIndex;
}
void setObjectIndex(int32_t objectIndex) {
_objectIndex = objectIndex;
}
uint32_t getObjectInstance() const {
return _objectInstance;
}
void setObjectInstance(uint32_t objectInstance) {
_objectInstance = objectInstance;
}
BACNET_OBJECT_TYPE getObjectType() const {
return _objectType;
}
void setObjectType(BACNET_OBJECT_TYPE objectType) {
_objectType = objectType;
}
BACNET_PROPERTY_ID getProperty() const {
return _property;
}
void setProperty(BACNET_PROPERTY_ID property) {
_property = property;
}
void setBACnetClient(BACnetClient* bacClient) {
_bacClient = bacClient;
}
BACnetClient* getBACnetClient() const {
return _bacClient;
}
/**** END GETTERS/SETTER ****/
void read();
void readAsync();
......@@ -32,6 +77,12 @@ public:
private:
uint32_t _deviceInstance;
uint32_t _objectInstance;
BACNET_OBJECT_TYPE _objectType;
BACNET_PROPERTY_ID _property;
int32_t _objectIndex;
BACnetClient* _bacClient;
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment