Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added null api for not pushing readings to any consumer. #70

Merged
merged 1 commit into from
Dec 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.mysmartgrid
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ To generate the Unique Ids one can use the command uuidgen
General channel parameters:
===========================

"protocol" can be: "vz" to use the Volkzaehler API
"mysmartgrid" to use the MySmartGrid API
"api" can be "volkszaehler" to use the Volkzaehler API (default)
"mysmartgrid" to use the MySmartGrid API
"middleware" defines the specific server url
"uuid" defines the Unique ID of the sensor.

example:
"protocol" : "mysmartgrid",
"api" : "mysmartgrid",
"middleware" : "https://api.mysmartgrid.de:8443",
"uuid" : "e0c9dd9a-8f3c-4fc4-8a99-f2393ca60924",

Expand Down
10 changes: 4 additions & 6 deletions etc/vzlogger.conf
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,15 @@
"protocol": "sml", // see 'vzlogger -h' for list of available protocols
"host": "http://localhost/meinzaehler.dyndns.info:7331",
"channels": [{
"protocol": "vz", // volkszaehler.org (default)
"api": "volkszaehler", // default middleware api: volkszaehler.org
"uuid": "fde8f1d0-c5d0-11e0-856e-f9e4360ced10",
"middleware": "http://localhost/middleware.php",
"identifier": "power" // alias for '1-0:1.7.ff', see 'vzlogger -h' for list of available aliases
}, {
"protocol": "vz", // volkszaehler.org (default)
"uuid": "a8da012a-9eb4-49ed-b7f3-38c95142a90c",
"middleware": "http://localhost/middleware.php",
"identifier": "counter"
}, {
"protocol": "vz", // volkszaehler.org (default)
"uuid": "d5c6db0f-533e-498d-a85a-be972c104b48",
"middleware": "http://localhost/middleware.php",
"identifier": "1-0:1.8.0" // see 'vzlogger -v20' for an output with all available identifiers/OBIS ids
Expand Down Expand Up @@ -126,7 +124,7 @@
"interval": 2,
"resolution": 2000,
"channels": [{
"protocol": "mysmartgrid", // use MySmartgrid as middleware protocol
"api": "mysmartgrid", // use MySmartgrid as middleware api
"type": "sensor",
"uuid": "01234567-9abc-def0-1234-56789abcdefe",
"secretKey": "0123456789abcdef0123456789abcdef",
Expand All @@ -144,7 +142,7 @@
"interval": 2,
"resolution": 2000,
"channels": [{
"protocol": "mysmartgrid", // use MySmartgrid as middleware protocol
"api": "mysmartgrid", // use MySmartgrid as middleware api
"type": "sensor",
"uuid": "01234567-9abc-def0-1234-56789abcdefe",
"secretKey": "0123456789abcdef0123456789abcdef",
Expand All @@ -162,7 +160,7 @@
"interval": 2,
"resolution": 2000,
"channels": [{
"protocol": "mysmartgrid", // use MySmartgrid as middleware protocol
"api": "mysmartgrid", // use MySmartgrid as middleware api
"type": "sensor",
"uuid": "01234567-9abc-def0-1234-56789abcdefe",
"secretKey": "0123456789abcdef0123456789abcdef",
Expand Down
55 changes: 55 additions & 0 deletions include/api/Null.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/***********************************************************************/
/** @file Null.hpp
* Header file for null API calls
*
* @author Andreas Goetz
* @date 2012-03-15
* @email cpuidle@gmx.de
* @copyright Copyright (c) 2014, The volkszaehler.org project
* @package vzlogger
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
**/
/*---------------------------------------------------------------------*/

/*
* This file is part of volkzaehler.org
*
* volkzaehler.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* volkzaehler.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _Null_hpp_
#define _Null_hpp_

#include <ApiIF.hpp>
#include <Options.hpp>

namespace vz {
namespace api {

class Null : public ApiIF {
public:
typedef vz::shared_ptr<ApiIF> Ptr;

Null(Channel::Ptr ch, std::list<Option> options);
~Null();

void send();

void register_device();

}; // class Null

} // namespace api
} // namespace vz
#endif // _Null_hpp_
8 changes: 4 additions & 4 deletions include/api/Volkszaehler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ namespace vz {

Volkszaehler(Channel::Ptr ch, std::list<Option> options);
~Volkszaehler();

void send();

void register_device();

const std::string middleware() const { return _middleware; }

private:
std::string _middleware;

Expand All @@ -93,9 +93,9 @@ namespace vz {
// Volatil
std::list<Reading> _values;
uint64_t _last_timestamp; /**< remember last timestamp */

}; //class Volkszaehler

/**
* Reformat CURLs debugging output
*/
Expand Down
14 changes: 7 additions & 7 deletions src/Config_Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ void Config_Options::config_parse_channel(Json &jso, MeterMap &mapping)
else if (strcmp(key, "identifier") == 0 && type == json_type_string) {
id_str = json_object_get_string(value);
}
else if (strcmp(key, "protocol") == 0 && type == json_type_string) {
else if (strcmp(key, "api") == 0 && type == json_type_string) {
apiProtocol_str = json_object_get_string(value);
}
else { /* all other options will be passed to meter_init() */
Expand All @@ -254,11 +254,11 @@ void Config_Options::config_parse_channel(Json &jso, MeterMap &mapping)
print(log_error, "Invalid UUID: %s", NULL, uuid);
throw vz::VZException("Invalid UUID.");
}
// check if identifier is set. If not, use default
if (id_str == NULL ) {
print(log_error, "Identifier is not set. Set it to default value 'NilItentifier'.", NULL);
id_str = "NilItentifier";
}
// check if identifier is set. If not, use default
if (id_str == NULL ) {
print(log_error, "Identifier is not set. Set it to default value 'NilIdentifier'.", NULL);
id_str = "NilIdentifier";
}
//if (middleware == NULL) {
// print(log_error, "Missing middleware", NULL);
// throw vz::VZException("Missing middleware.");
Expand All @@ -279,7 +279,7 @@ void Config_Options::config_parse_channel(Json &jso, MeterMap &mapping)
}

Channel::Ptr ch(new Channel(options, apiProtocol_str, uuid, id));
print(log_info, "New channel initialized (uuid=...%s protocol=%s id=%s)", ch->name(),
print(log_info, "New channel initialized (uuid=...%s api=%s id=%s)", ch->name(),
uuid+30, apiProtocol_str, (id_str) ? id_str : "(none)");
mapping.push_back(ch);
}
Expand Down
18 changes: 12 additions & 6 deletions src/MeterMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <Config_Options.hpp>
#include <api/Volkszaehler.hpp>
#include <api/MySmartGrid.hpp>
#include <api/Null.hpp>

extern Config_Options options; /* global application options */

Expand Down Expand Up @@ -84,8 +85,8 @@ void MeterMap::start() {
}

bool MeterMap::stopped() {
if (_meter->isEnabled() && running() ) {
if (pthread_join(_thread, NULL) == 0 ) {
if (_meter->isEnabled() && running()) {
if (pthread_join(_thread, NULL) == 0) {
_thread_running = false;

// join channel-threads
Expand All @@ -100,7 +101,7 @@ bool MeterMap::stopped() {
}

void MeterMap::cancel() {
if (_meter->isEnabled() && running() ) {
if (_meter->isEnabled() && running()) {
for (iterator it = _channels.begin(); it != _channels.end(); it++) {
(*it)->cancel();
(*it)->join();
Expand All @@ -120,14 +121,19 @@ void MeterMap::registration() {
return;
}
for (iterator ch = _channels.begin(); ch != _channels.end(); ch++) {
// create configured api-interface
// create configured api interfaces
// NOTE: if additional APIs are introduced both threads.cpp and MeterMap.cpp need to be updated
vz::ApiIF::Ptr api;
if ((*ch)->apiProtocol() == "mysmartgrid") {
api = vz::ApiIF::Ptr(new vz::api::MySmartGrid(*ch, (*ch)->options()));
print(log_debug, "Using MSG-Api.", (*ch)->name());
print(log_debug, "Using MySmartGrid api.", (*ch)->name());
}
else if ((*ch)->apiProtocol() == "null") {
api = vz::ApiIF::Ptr(new vz::api::Null(*ch, (*ch)->options()));
print(log_debug, "Using null api- meter data available via local httpd if enabled.", (*ch)->name());
} else {
api = vz::ApiIF::Ptr(new vz::api::Volkszaehler(*ch, (*ch)->options()));
print(log_debug, "Using default api:", (*ch)->name());
print(log_debug, "Using default volkszaehler api.", (*ch)->name());
}

api->register_device();
Expand Down
2 changes: 1 addition & 1 deletion src/Reading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ size_t ChannelIdentifier::unparse(char *buffer, size_t n) {
}

size_t NilIdentifier::unparse(char *buffer, size_t n) {
return snprintf(buffer, n, "NilItentifier");
return snprintf(buffer, n, "NilIdentifier");
//buffer[0] = '\0';
//return strlen(buffer);
}
1 change: 1 addition & 0 deletions src/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
set(api_srcs
Volkszaehler.cpp
MySmartGrid.cpp
Null.cpp
CurlIF.cpp
CurlCallback.cpp
CurlResponse.cpp
Expand Down
64 changes: 64 additions & 0 deletions src/api/Null.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/***********************************************************************/
/** @file Null.cpp
* Header file for null API calls
*
* @author Andreas Goetz
* @date 2012-03-15
* @email cpuidle@gmx.de
* @copyright Copyright (c) 2014, The volkszaehler.org project
* @package vzlogger
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
**/
/*---------------------------------------------------------------------*/

/*
* This file is part of volkzaehler.org
*
* volkzaehler.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* volkzaehler.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
*/

#include "Config_Options.hpp"
#include <api/Null.hpp>

extern Config_Options options;

vz::api::Null::Null(
Channel::Ptr ch,
std::list<Option> pOptions
)
: ApiIF(ch)
{
}

vz::api::Null::~Null()
{
}

void vz::api::Null::send()
{
}

void vz::api::Null::register_device()
{
}


/*
* Local variables:
* tab-width: 2
* c-indent-level: 2
* c-basic-offset: 2
* project-name: vzlogger
* End:
*/
23 changes: 15 additions & 8 deletions src/threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <ApiIF.hpp>
#include <api/Volkszaehler.hpp>
#include <api/MySmartGrid.hpp>
#include <api/Null.hpp>

extern Config_Options options;

Expand Down Expand Up @@ -138,7 +139,7 @@ void * reading_thread(void *arg) {
char *dump = (char*)malloc(dump_len);

if (dump == NULL) {
print(log_error, "cannot allocate buffer", (*ch)->name());
print(log_error, "Cannot allocate buffer", (*ch)->name());
}

while (dump == NULL || (*ch)->dump(dump, dump_len) == NULL) {
Expand All @@ -162,11 +163,11 @@ void * reading_thread(void *arg) {
} catch (std::exception &e) {
std::stringstream oss;
oss << e.what();
print(log_error, "Reading-THREAD - reading Got an exception : %s", mtr->name(), e.what());
print(log_error, "Reading-THREAD - reading got an exception : %s", mtr->name(), e.what());
pthread_exit(0);
}

print(log_debug, "Stop reading.! ", mtr->name());
print(log_debug, "Stopped reading. ", mtr->name());
//pthread_cleanup_pop(1);

pthread_exit(0);
Expand All @@ -185,14 +186,20 @@ void * logging_thread(void *arg) {
print(log_debug, "Start logging thread for %s-api. Running as daemon: %s", ch->name(),
ch->apiProtocol().c_str(), options.daemon() ? "yes" : "no");

// create configured api-interface
// create configured api interfaces
// NOTE: if additional APIs are introduced both threads.cpp and MeterMap.cpp need to be updated
vz::ApiIF::Ptr api;
if (ch->apiProtocol() == "mysmartgrid") {
api = vz::ApiIF::Ptr(new vz::api::MySmartGrid(ch, ch->options()));
print(log_debug, "Using MSG-Api.", ch->name());
print(log_debug, "Using MySmartGrid api.", ch->name());
}
else if (ch->apiProtocol() == "null") {
api = vz::ApiIF::Ptr(new vz::api::Null(ch, ch->options()));
print(log_debug, "Using null api- meter data available via local httpd if enabled.", ch->name());
} else {
// default == volkszaehler
api = vz::ApiIF::Ptr(new vz::api::Volkszaehler(ch, ch->options()));
print(log_debug, "Using default api:", ch->name());
print(log_debug, "Using default volkszaehler api.", ch->name());
}

//pthread_cleanup_push(&logging_thread_cleanup, &api);
Expand All @@ -204,12 +211,12 @@ void * logging_thread(void *arg) {
api->send();
}
catch (std::exception &e) {
print(log_error, "logging thread failed due to: %s", ch->name(), e.what());
print(log_error, "Logging thread failed due to: %s", ch->name(), e.what());
}

} while (options.logging());

print(log_debug, "Stop logging.! (daemon=%d)", ch->name(), options.daemon());
print(log_debug, "Stopped logging. (daemon=%d)", ch->name(), options.daemon());
pthread_exit(0);
//pthread_cleanup_pop(1);

Expand Down