forked from fledge-iot/fledge-south-random
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.cpp
81 lines (76 loc) · 1.6 KB
/
random.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* Fledge south service plugin
*
* Copyright (c) 2018 Dianomic Systems
*
* Released under the Apache 2.0 Licence
*
* Author: Massimiliano Pinto
*/
#include <random.h>
#include <reading.h>
#include <plugin_api.h>
#include <logger.h>
/**
* Constructor for the random "sensor"
*/
Random::Random()
{
srand(time(0));
m_lastValue = rand() % 100;
}
/**
* Destructor for the random "sensor"
*/
Random::~Random()
{
}
/**
* Take a reading from the random "sensor"
*/
Reading Random::takeReading()
{
m_lastValue += ((rand() % 100) > 50 ? 1 : -1) *
((rand() % 100) / 20);
DatapointValue value(m_lastValue);
return Reading(m_asset_name,new Datapoint("random", value));
}
/**
* Setpoint write operation
*/
bool Random::write(const std::string& name, const std::string value)
{
Logger::getLogger()->error("Random plugin does not support write operations");
return false;
}
/**
* SetPoint operation. We support reseeding the random number generator
*/
bool Random::operation(const std::string& operation, int count, PLUGIN_PARAMETER **params)
{
if (operation.compare("seed") == 0)
{
if (count)
{
if (! params[0]->name.compare("seed"))
{
long seed = strtol(params[0]->value.c_str(), NULL, 10);
srand(seed);
}
else
{
Logger::getLogger()->error("Unsupported parameter %s for control operation %s",
params[0]->name.c_str(), operation.c_str());
return false;
}
}
else
{
srand(time(0));
}
Logger::getLogger()->info("Reseeded random number generator");
return true;
}
Logger::getLogger()->error("Unrecognised operation %s", operation.c_str());
return false;
}