sensorconfig.cpp 36.8 KB
Newer Older
Axel Auweter's avatar
Axel Auweter committed
1
2
3
4
5
6
7
8
9
/*
 * sensorconfig.cpp
 *
 *  Created on: May 19, 2015
 *      Author: Axel Auweter
 */

#include <cstring>
#include <iostream>
Axel Auweter's avatar
Axel Auweter committed
10
#include <algorithm>
11
12
13
#include <string>
#include <list>
#include <unordered_set>
Axel Auweter's avatar
Axel Auweter committed
14
15
16
17
18

#include "cassandra.h"

#include "sensorconfig_internal.h"
#include "dcdbglobals.h"
Axel Auweter's avatar
Axel Auweter committed
19
#include "dcdbendian.h"
20
#include "virtualsensor.h"
Axel Auweter's avatar
Axel Auweter committed
21

22
23
using namespace DCDB;

Axel Auweter's avatar
Axel Auweter committed
24
/*
25
 * PublicSensor functions.
Axel Auweter's avatar
Axel Auweter committed
26
 */
27
PublicSensor::PublicSensor()
Axel Auweter's avatar
Axel Auweter committed
28
29
{
  name = "";
30
  is_virtual = false;
Axel Auweter's avatar
Axel Auweter committed
31
32
33
  pattern = "";
  scaling_factor = 1.0;
  unit = "";
34
35
  integrable = false;
  expression = "";
36
  v_sensorid = "";
37
38
  t_zero = 0;
  frequency = 0;
Axel Auweter's avatar
Axel Auweter committed
39
40
}

41
PublicSensor::PublicSensor (const PublicSensor &copy)
Axel Auweter's avatar
Axel Auweter committed
42
43
{
  name = copy.name;
44
  is_virtual = copy.is_virtual;
Axel Auweter's avatar
Axel Auweter committed
45
46
47
  pattern = copy.pattern;
  scaling_factor = copy.scaling_factor;
  unit = copy.unit;
48
49
  integrable = copy.integrable;
  expression = copy.expression;
50
  v_sensorid = copy.v_sensorid;
51
52
  t_zero = copy.t_zero;
  frequency = copy.frequency;
Axel Auweter's avatar
Axel Auweter committed
53
54
55
56
57
58
59
60
61
62
63
}


/*
 * SensorConfig functions
 */
SCError SensorConfig::publishSensor(const char* publicName, const char* sensorPattern)
{
  return impl->publishSensor(publicName, sensorPattern);
}

64
SCError SensorConfig::publishVirtualSensor(const char* publicName, const char* vSensorExpression, const char* vSensorId, TimeStamp tZero, uint64_t frequency)
65
{
66
  return impl->publishVirtualSensor(publicName, vSensorExpression, vSensorId, tZero, frequency);
67
68
}

69
70
71
72
73
SCError SensorConfig::unPublishSensor(const char* publicName)
{
  return impl->unPublishSensor(publicName);
}

74
SCError SensorConfig::getPublicSensorNames(std::list<std::string>& publicSensors)
Axel Auweter's avatar
Axel Auweter committed
75
{
76
77
78
  return impl->getPublicSensorNames(publicSensors);
}

79
SCError SensorConfig::getPublicSensorsVerbose(std::list<PublicSensor>& publicSensors)
80
81
82
83
{
  return impl->getPublicSensorsVerbose(publicSensors);
}

84
SCError SensorConfig::getPublicSensorByName(PublicSensor& sensor, const char* publicName)
85
86
{
  return impl->getPublicSensorByName(sensor, publicName);
Axel Auweter's avatar
Axel Auweter committed
87
88
}

89
90
91
92
93
SCError SensorConfig::isVirtual(bool& isVirtual, std::string publicName)
{
  return impl->isVirtual(isVirtual, publicName);
}

Axel Auweter's avatar
Axel Auweter committed
94
95
96
97
98
99
100
101
102
103
SCError SensorConfig::getSensorPattern(std::string& pattern, std::string publicName)
{
  return impl->getSensorPattern(pattern, publicName);
}

SCError SensorConfig::getSensorListForPattern(std::list<SensorId>& sensorIds, std::string pattern)
{
  return impl->getSensorListForPattern(sensorIds, pattern);
}

104
SCError SensorConfig::getSensorListForPattern(std::list<SensorId>& sensorIds, std::string pattern, TimeStamp start, TimeStamp end)
Axel Auweter's avatar
Axel Auweter committed
105
106
107
108
{
  return impl->getSensorListForPattern(sensorIds, pattern, start, end);
}

109
110
111
112
113
114
115
116
117
118
119
120
121
122
SCError SensorConfig::setSensorScalingFactor(std::string publicName, double scalingFactor) {
  return impl->setSensorScalingFactor(publicName, scalingFactor);
}

SCError SensorConfig::setSensorUnit(std::string publicName, std::string unit)
{
  return impl->setSensorUnit(publicName, unit);
}

SCError SensorConfig::setSensorIntegrable(std::string publicName, bool integrable)
{
  return impl->setSensorIntegrable(publicName, integrable);
}

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
SCError SensorConfig::setVirtualSensorExpression(std::string publicName, std::string expression)
{
  return impl->setVirtualSensorExpression(publicName, expression);
}

SCError SensorConfig::setVirtualSensorTZero(std::string publicName, TimeStamp tZero)
{
  return impl->setVirtualSensorTZero(publicName, tZero);
}

SCError SensorConfig::setVirtualSensorFrequency(std::string publicName, uint64_t frequency)
{
  return impl->setVirtualSensorFrequency(publicName, frequency);
}

Axel Auweter's avatar
Axel Auweter committed
138

139
SensorConfig::SensorConfig(Connection* conn)
Axel Auweter's avatar
Axel Auweter committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
{
  /* Allocate impl object */
  impl = new SensorConfigImpl(conn);
}

SensorConfig::~SensorConfig()
{
  if (impl) {
      delete impl;
  }
}

/*
 * SensorConfigImpl protected functions
 */

/*
 * Validate the pattern for a Sensor to be published
 * Patterns may only consist of hex numbers and forward slashes
 * and at most one wildcard character (*). Also, the total number of
 * bits may not exceed 128.
 */
bool SensorConfigImpl::validateSensorPattern(const char* sensorPattern)
{
  unsigned int wildcards, bits;

  /* 128 bits in HEX is at most 32 characters. Considering forward-slash separators, 64 is a reasonable maxmimum string length */
  if (strlen(sensorPattern) > 64) {
      return false;
  }

  /* Iterate through the string and validate/count the characters */
  bits = 0;
  wildcards = 0;
  for (unsigned int c = 0; c < strlen(sensorPattern); c++) {
      switch (sensorPattern[c]) {

      /* Regular HEX digit */
      case '0':
      case '1':
      case '2':
      case '3':
      case '4':
      case '5':
      case '6':
      case '7':
      case '8':
      case '9':
      case 'a':
      case 'A':
      case 'b':
      case 'B':
      case 'c':
      case 'C':
      case 'd':
      case 'D':
      case 'e':
      case 'E':
      case 'f':
      case 'F':
        bits += 4;
        break;

      /* Wildcard */
      case '*':
        wildcards++;
        break;

      /* Forward slash */
      case '/':
        break;

      /* Everything else is not allowed */
      default:
        return false;
      }
  }

  /* More than one wildcard is not allowed */
  if (wildcards > 1)
    return false;

  /* In case of a wildcard, there should be some bits left for the wildcard to fill in */
  if (wildcards == 1 && bits >= 128)
    return false;

  /* Looks good */
  return true;
}

230
231
232
233
234
235
236
237
/*
 * Validate the public name of a sensor.
 * Sensor public names may not contain forward slash characters.
 */
bool SensorConfigImpl::validateSensorPublicName(std::string publicName)
{
  return (publicName.find("/") == std::string::npos);
}
Axel Auweter's avatar
Axel Auweter committed
238
239
240
241

/*
 * SensorConfigImpl public functions
 */
Axel Auweter's avatar
Axel Auweter committed
242
SCError SensorConfigImpl::publishSensor(std::string publicName, std::string sensorPattern)
Axel Auweter's avatar
Axel Auweter committed
243
244
{
  /* Check if the pattern matches the requirements */
Axel Auweter's avatar
Axel Auweter committed
245
  if (!validateSensorPattern(sensorPattern.c_str())) {
Axel Auweter's avatar
Axel Auweter committed
246
247
248
      return SC_INVALIDPATTERN;
  }

249
250
251
252
253
  /* Check if the publicName is valid */
  if (!validateSensorPublicName(publicName.c_str())) {
      return SC_INVALIDPUBLICNAME;
  }

Axel Auweter's avatar
Axel Auweter committed
254
255
256
257
258
259
260
261
262
263
  /* Check if the session is valid */
  if (!session) {
      return SC_INVALIDSESSION;
  }

  /* Insert the entry */
  CassError rc = CASS_OK;
  CassStatement* statement = nullptr;
  CassFuture* future = nullptr;
  const CassPrepared* prepared = nullptr;
264
  const char* query = "INSERT INTO " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " (name, pattern, virtual) VALUES (?,?, FALSE);";
Axel Auweter's avatar
Axel Auweter committed
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281

  future = cass_session_prepare(session, query);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
    connection->printError(future);
    cass_future_free(future);
    return SC_UNKNOWNERROR;
  } else {
    prepared = cass_future_get_prepared(future);
  }

  cass_future_free(future);

  statement = cass_prepared_bind(prepared);

Axel Auweter's avatar
Axel Auweter committed
282
283
  cass_statement_bind_string_by_name(statement, "name", publicName.c_str());
  cass_statement_bind_string_by_name(statement, "pattern", sensorPattern.c_str());
Axel Auweter's avatar
Axel Auweter committed
284
285
286
287
288
289
290

  future = cass_session_execute(session, statement);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
    connection->printError(future);
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305

    cass_prepared_free(prepared);
    cass_future_free(future);
    cass_statement_free(statement);

    return SC_UNKNOWNERROR;
  }

  cass_prepared_free(prepared);
  cass_future_free(future);
  cass_statement_free(statement);

  return SC_OK;
}

306
SCError SensorConfigImpl::publishVirtualSensor(std::string publicName, std::string vSensorExpression, std::string vSensorId, TimeStamp tZero, uint64_t frequency)
307
308
309
310
311
312
313
314
315
316
317
{
  /* Check if the publicName is valid */
  if (!validateSensorPublicName(publicName.c_str())) {
      return SC_INVALIDPUBLICNAME;
  }

  /* Check if the session is valid */
  if (!session) {
      return SC_INVALIDSESSION;
  }

318
319
  /* Validate vSensorExpression */
  try {
320
321
322
323
324
325
326
327
328
      VSensorExpression vsExp(connection, vSensorExpression);

      /* Check that it is not recursive-pointing to itself */
      std::unordered_set<std::string> inputSet;
      vsExp.getInputsRecursive(inputSet);
      std::unordered_set<std::string>::const_iterator found = inputSet.find(publicName);
      if (found != inputSet.end()) {
          return SC_EXPRESSIONSELFREF;
      }
329
330
331
332
333
  }
  catch (std::exception& e) {
      std::cout << e.what();
      return SC_INVALIDEXPRESSION;
  }
334
335
336
337
338
339

  /* Check if the vSensorId is valid */
  SensorId vSensor;
  if (!vSensor.mqttTopicConvert(vSensorId)) {
      return SC_INVALIDVSENSORID;
  }
340
341
342
343
344
345

  /* Insert the entry */
  CassError rc = CASS_OK;
  CassStatement* statement = nullptr;
  CassFuture* future = nullptr;
  const CassPrepared* prepared = nullptr;
346
  const char* query = "INSERT INTO " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " (name, expression, vsensorid, tzero, frequency, virtual) VALUES (?,?,?,?,?,TRUE);";
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365

  future = cass_session_prepare(session, query);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
    connection->printError(future);
    cass_future_free(future);
    return SC_UNKNOWNERROR;
  } else {
    prepared = cass_future_get_prepared(future);
  }

  cass_future_free(future);

  statement = cass_prepared_bind(prepared);

  cass_statement_bind_string_by_name(statement, "name", publicName.c_str());
  cass_statement_bind_string_by_name(statement, "expression", vSensorExpression.c_str());
366
  cass_statement_bind_string_by_name(statement, "vsensorid", vSensorId.c_str());
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
  cass_statement_bind_int64_by_name(statement, "tzero", tZero.getRaw());
  cass_statement_bind_int64_by_name(statement, "frequency", frequency);

  future = cass_session_execute(session, statement);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
    connection->printError(future);

    cass_prepared_free(prepared);
    cass_future_free(future);
    cass_statement_free(statement);

    return SC_UNKNOWNERROR;
  }

  cass_prepared_free(prepared);
  cass_future_free(future);
  cass_statement_free(statement);

  return SC_OK;
}


392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
SCError SensorConfigImpl::unPublishSensor(std::string publicName)
{
  /* Check if the session is valid */
  if (!session) {
      return SC_INVALIDSESSION;
  }

  /* Remove the entry */
  CassError rc = CASS_OK;
  CassStatement* statement = nullptr;
  CassFuture* future = nullptr;
  const CassPrepared* prepared = nullptr;
  const char* query = "DELETE FROM " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " WHERE name = ? ;";

  future = cass_session_prepare(session, query);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
    connection->printError(future);
    cass_future_free(future);
    return SC_UNKNOWNERROR;
  } else {
    prepared = cass_future_get_prepared(future);
  }

  cass_future_free(future);

  statement = cass_prepared_bind(prepared);

  cass_statement_bind_string_by_name(statement, "name", publicName.c_str());

  future = cass_session_execute(session, statement);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
    connection->printError(future);
Axel Auweter's avatar
Axel Auweter committed
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444

    cass_prepared_free(prepared);
    cass_future_free(future);
    cass_statement_free(statement);

    return SC_UNKNOWNERROR;
  }

  cass_prepared_free(prepared);
  cass_future_free(future);
  cass_statement_free(statement);

  return SC_OK;
}

445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
SCError SensorConfigImpl::getPublicSensorNames(std::list<std::string>& publicSensors)
{
  /* Check if the session is valid */
  if (!session) {
      return SC_INVALIDSESSION;
  }

  /* Clear the list */
  publicSensors.clear();

  /* Fill the list with all public sensors */
  CassError rc = CASS_OK;
  CassStatement* statement = nullptr;
  CassFuture* future = nullptr;
  const char* query = "SELECT name FROM " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " ;";

  statement = cass_statement_new(query, 0);

  future = cass_session_execute(session, statement);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
     connection->printError(future);
     cass_future_free(future);
     cass_statement_free(statement);
     return SC_UNKNOWNERROR;
  } else {
      const CassResult* result = cass_future_get_result(future);
      CassIterator* iterator = cass_iterator_from_result(result);

      while (cass_iterator_next(iterator)) {
          const char* name;
          size_t      name_len;

          const CassRow* row = cass_iterator_get_row(iterator);

          if (cass_value_get_string(cass_row_get_column_by_name(row, "name"), &name, &name_len) != CASS_OK) {
              name = ""; name_len = 0;
          }

          publicSensors.push_back(std::string(name, name_len));
      }
      cass_result_free(result);
      cass_iterator_free(iterator);
  }

  cass_future_free(future);
  cass_statement_free(statement);
  return SC_OK;
}

497
SCError SensorConfigImpl::getPublicSensorsVerbose(std::list<PublicSensor>& publicSensors)
Axel Auweter's avatar
Axel Auweter committed
498
499
500
501
502
503
{
  /* Check if the session is valid */
  if (!session) {
      return SC_INVALIDSESSION;
  }

Axel Auweter's avatar
Axel Auweter committed
504
505
506
  /* Clear the list */
  publicSensors.clear();

Axel Auweter's avatar
Axel Auweter committed
507
508
509
510
  /* Fill the list with all public sensors */
  CassError rc = CASS_OK;
  CassStatement* statement = nullptr;
  CassFuture* future = nullptr;
Axel Auweter's avatar
Axel Auweter committed
511
  const char* query = "SELECT * FROM " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " ;";
Axel Auweter's avatar
Axel Auweter committed
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530

  statement = cass_statement_new(query, 0);

  future = cass_session_execute(session, statement);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
     connection->printError(future);
     cass_future_free(future);
     cass_statement_free(statement);
     return SC_UNKNOWNERROR;
  } else {
      const CassResult* result = cass_future_get_result(future);
      CassIterator* iterator = cass_iterator_from_result(result);

      while (cass_iterator_next(iterator)) {
          const char* name;
          size_t      name_len;
531
          cass_bool_t is_virtual;
Axel Auweter's avatar
Axel Auweter committed
532
533
534
535
536
          const char* pattern;
          size_t      pattern_len;
          double      scaling_factor;
          const char* unit;
          size_t      unit_len;
537
          cass_bool_t integrable;
538
539
          const char* expression;
          size_t      expression_len;
540
541
          const char* vsensorid;
          size_t      vsensorid_len;
542
543
          int64_t     tzero;
          int64_t     frequency;
544
          PublicSensor sensor;
Axel Auweter's avatar
Axel Auweter committed
545
546
547
548
549
550

          const CassRow* row = cass_iterator_get_row(iterator);

          if (cass_value_get_string(cass_row_get_column_by_name(row, "name"), &name, &name_len) != CASS_OK) {
              name = ""; name_len = 0;
          }
551
552
553
          if (cass_value_get_bool(cass_row_get_column_by_name(row, "virtual"), &is_virtual) != CASS_OK) {
              is_virtual = cass_false;
          }
Axel Auweter's avatar
Axel Auweter committed
554
555
556
557
558
559
560
561
562
          if (cass_value_get_string(cass_row_get_column_by_name(row, "pattern"), &pattern, &pattern_len) != CASS_OK) {
              pattern = ""; pattern_len = 0;
          }
          if (cass_value_get_double(cass_row_get_column_by_name(row, "scaling_factor"), &scaling_factor) != CASS_OK) {
              scaling_factor = 1.0;
          }
          if (cass_value_get_string(cass_row_get_column_by_name(row, "unit"), &unit, &unit_len) != CASS_OK) {
              unit = ""; unit_len = 0;
          }
563
564
565
          if (cass_value_get_bool(cass_row_get_column_by_name(row, "integrable"), &integrable) != CASS_OK) {
              integrable = cass_false;
          }
566
567
568
          if (cass_value_get_string(cass_row_get_column_by_name(row, "expression"), &expression, &expression_len) != CASS_OK) {
              expression = ""; expression_len = 0;
          }
569
570
571
          if (cass_value_get_string(cass_row_get_column_by_name(row, "vsensorid"), &vsensorid, &vsensorid_len) != CASS_OK) {
              vsensorid = ""; vsensorid_len = 0;
          }
572
573
574
575
576
577
          if (cass_value_get_int64(cass_row_get_column_by_name(row, "tzero"), &tzero) != CASS_OK) {
              tzero = 0;
          }
          if (cass_value_get_int64(cass_row_get_column_by_name(row, "frequency"), &frequency) != CASS_OK) {
              frequency = 0;
          }
Axel Auweter's avatar
Axel Auweter committed
578
579

          sensor.name = std::string(name, name_len);
580
          sensor.is_virtual = is_virtual == cass_true ? true : false;
Axel Auweter's avatar
Axel Auweter committed
581
582
583
          sensor.pattern = std::string(pattern, pattern_len);
          sensor.scaling_factor = scaling_factor;
          sensor.unit = std::string(unit, unit_len);
584
          sensor.integrable = integrable == cass_true ? true: false;
585
          sensor.expression = std::string(expression, expression_len);
586
          sensor.v_sensorid = std::string(vsensorid, vsensorid_len);
587
588
          sensor.t_zero = tzero;
          sensor.frequency = frequency;
Axel Auweter's avatar
Axel Auweter committed
589
590
591
592
593
594
595
596
597
598
599
600

          publicSensors.push_back(sensor);
      }
      cass_result_free(result);
      cass_iterator_free(iterator);
  }

  cass_future_free(future);
  cass_statement_free(statement);
  return SC_OK;
}

601
SCError SensorConfigImpl::getPublicSensorByName(PublicSensor& sensor, const char* publicName)
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
{
  /* Check if the session is valid */
  if (!session) {
      return SC_INVALIDSESSION;
  }

  /* Fill the list with all public sensors */
  CassError rc = CASS_OK;
  CassStatement* statement = nullptr;
  CassFuture* future = nullptr;
  const CassPrepared* prepared = nullptr;
  const char* query = "SELECT * FROM " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " WHERE name = ?;";

  future = cass_session_prepare(session, query);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
      connection->printError(future);
      cass_future_free(future);
      return SC_UNKNOWNERROR;
  } else {
      prepared = cass_future_get_prepared(future);
  }
  cass_future_free(future);

  statement = cass_prepared_bind(prepared);
  cass_statement_bind_string_by_name(statement, "name", publicName);

  future = cass_session_execute(session, statement);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
     connection->printError(future);
     cass_future_free(future);
     cass_statement_free(statement);
     return SC_UNKNOWNERROR;
  } else {
      const CassResult* result = cass_future_get_result(future);
      CassIterator* iterator = cass_iterator_from_result(result);

      if (cass_iterator_next(iterator)) {
          const char* name;
          size_t      name_len;
647
          cass_bool_t is_virtual;
648
649
650
651
652
653
          const char* pattern;
          size_t      pattern_len;
          double      scaling_factor;
          const char* unit;
          size_t      unit_len;
          cass_bool_t integrable;
654
655
          const char* expression;
          size_t      expression_len;
656
657
          const char* vsensorid;
          size_t      vsensorid_len;
658
659
          int64_t     tzero;
          int64_t     frequency;
660
661
662
663
664
665

          const CassRow* row = cass_iterator_get_row(iterator);

          if (cass_value_get_string(cass_row_get_column_by_name(row, "name"), &name, &name_len) != CASS_OK) {
              name = ""; name_len = 0;
          }
666
667
668
          if (cass_value_get_bool(cass_row_get_column_by_name(row, "virtual"), &is_virtual) != CASS_OK) {
              is_virtual = cass_false;
          }
669
670
671
672
673
674
675
676
677
678
679
680
          if (cass_value_get_string(cass_row_get_column_by_name(row, "pattern"), &pattern, &pattern_len) != CASS_OK) {
              pattern = ""; pattern_len = 0;
          }
          if (cass_value_get_double(cass_row_get_column_by_name(row, "scaling_factor"), &scaling_factor) != CASS_OK) {
              scaling_factor = 1.0;
          }
          if (cass_value_get_string(cass_row_get_column_by_name(row, "unit"), &unit, &unit_len) != CASS_OK) {
              unit = ""; unit_len = 0;
          }
          if (cass_value_get_bool(cass_row_get_column_by_name(row, "integrable"), &integrable) != CASS_OK) {
              integrable = cass_false;
          }
681
682
683
          if (cass_value_get_string(cass_row_get_column_by_name(row, "expression"), &expression, &expression_len) != CASS_OK) {
              expression = ""; expression_len = 0;
          }
684
685
686
          if (cass_value_get_string(cass_row_get_column_by_name(row, "vsensorid"), &vsensorid, &vsensorid_len) != CASS_OK) {
              vsensorid = ""; vsensorid_len = 0;
          }
687
688
689
690
691
692
          if (cass_value_get_int64(cass_row_get_column_by_name(row, "tzero"), &tzero) != CASS_OK) {
              tzero = 0;
          }
          if (cass_value_get_int64(cass_row_get_column_by_name(row, "frequency"), &frequency) != CASS_OK) {
              frequency = 0;
          }
693
694

          sensor.name = std::string(name, name_len);
695
          sensor.is_virtual = is_virtual == cass_true ? true : false;
696
697
698
699
          sensor.pattern = std::string(pattern, pattern_len);
          sensor.scaling_factor = scaling_factor;
          sensor.unit = std::string(unit, unit_len);
          sensor.integrable = integrable == cass_true ? true: false;
700
          sensor.expression = std::string(expression, expression_len);
701
          sensor.v_sensorid = std::string(vsensorid, vsensorid_len);
702
703
704
          sensor.t_zero = tzero;
          sensor.frequency = frequency;

705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
      }
      else {
          cass_result_free(result);
          cass_iterator_free(iterator);
          cass_future_free(future);
          cass_statement_free(statement);

          return SC_UNKNOWNSENSOR;
      }
      cass_result_free(result);
      cass_iterator_free(iterator);
  }

  cass_future_free(future);
  cass_statement_free(statement);
  return SC_OK;
}

723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
SCError SensorConfigImpl::isVirtual(bool& isVirtual, std::string publicName)
{
  /* Check if the session is valid */
  if (!session) {
      return SC_INVALIDSESSION;
  }

  /* Read the virtual field from the database */
  CassError rc = CASS_OK;
  CassStatement* statement = nullptr;
  CassFuture* future = nullptr;
  const CassPrepared* prepared = nullptr;
  const char* query = "SELECT virtual FROM " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " WHERE name = ? ;";

  future = cass_session_prepare(session, query);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
      connection->printError(future);
      cass_future_free(future);
      return SC_UNKNOWNERROR;
  } else {
      prepared = cass_future_get_prepared(future);
  }
  cass_future_free(future);

  statement = cass_prepared_bind(prepared);
  cass_statement_bind_string_by_name(statement, "name", publicName.c_str());

  future = cass_session_execute(session, statement);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
      connection->printError(future);
  } else {
      const CassResult* result = cass_future_get_result(future);
      CassIterator* iterator = cass_iterator_from_result(result);

      if (cass_iterator_next(iterator)) {
          cass_bool_t isVirtual_;
          const CassRow* row = cass_iterator_get_row(iterator);
          cass_value_get_bool(cass_row_get_column_by_name(row, "virtual"), &isVirtual_);
          isVirtual = isVirtual_ ? true : false;
      }
      else {
          return SC_UNKNOWNSENSOR;
      }

      cass_result_free(result);
      cass_iterator_free(iterator);
  }

  cass_future_free(future);
  cass_statement_free(statement);
  cass_prepared_free(prepared);
  return SC_OK;
}

Axel Auweter's avatar
Axel Auweter committed
783
784
785
786
787
788
789
SCError SensorConfigImpl::getSensorPattern(std::string& pattern, std::string publicName)
{
  /* Check if the session is valid */
  if (!session) {
      return SC_INVALIDSESSION;
  }

790
791
792
793
794
795
796
797
798
799
  /* Ensure that the public sensor is not virtual */
  bool virt;
  SCError err = isVirtual(virt, publicName);
  if (err != SC_OK) {
      return err;
  }
  if (virt) {
      return SC_WRONGTYPE;
  }

Axel Auweter's avatar
Axel Auweter committed
800
801
802
803
804
805
806
807
808
809
810
811
812
  /* Read the Pattern string from the database */
  CassError rc = CASS_OK;
  CassStatement* statement = nullptr;
  CassFuture* future = nullptr;
  const CassPrepared* prepared = nullptr;
  const char* query = "SELECT pattern FROM " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " WHERE name = ? ;";

  future = cass_session_prepare(session, query);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
      connection->printError(future);
813
814
      cass_future_free(future);
      return SC_UNKNOWNERROR;
Axel Auweter's avatar
Axel Auweter committed
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
  } else {
      prepared = cass_future_get_prepared(future);
  }
  cass_future_free(future);

  statement = cass_prepared_bind(prepared);
  cass_statement_bind_string_by_name(statement, "name", publicName.c_str());

  future = cass_session_execute(session, statement);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
      connection->printError(future);
  } else {
      const CassResult* result = cass_future_get_result(future);
      CassIterator* iterator = cass_iterator_from_result(result);

      if (cass_iterator_next(iterator)) {
          const char* pattern_cstr;
          size_t      pattern_len;
          const CassRow* row = cass_iterator_get_row(iterator);
          cass_value_get_string(cass_row_get_column_by_name(row, "pattern"), &pattern_cstr, &pattern_len);
          pattern = std::string(pattern_cstr, pattern_len);
      }
      else {
          return SC_UNKNOWNSENSOR;
      }

      cass_result_free(result);
      cass_iterator_free(iterator);
  }

  cass_future_free(future);
  cass_statement_free(statement);
  cass_prepared_free(prepared);
  return SC_OK;
}

SCError SensorConfigImpl::getSensorListForPattern(std::list<SensorId>& sensorIds, std::string pattern)
{
  /* Tiny hack to call the long version of this function */
857
858
  TimeStamp start((uint64_t)0x0);
  TimeStamp end((uint64_t)0x260DD31906D70000);
Axel Auweter's avatar
Axel Auweter committed
859
860
861
  return getSensorListForPattern(sensorIds, pattern, start, end);
}

862
SCError SensorConfigImpl::getSensorListForPattern(std::list<SensorId>& sensorIds, std::string pattern, TimeStamp start, TimeStamp end)
Axel Auweter's avatar
Axel Auweter committed
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
{
  /* Clear the list of sensorIds */
  sensorIds.clear();

  /* Strip all slashes from publishedSensorName */
  pattern.erase(std::remove(pattern.begin(), pattern.end(), '/'), pattern.end());

  /* Calculate lower and upper boundaries for the expansion of the pattern */
  std::string low  = pattern;
  std::string high = pattern;
  if (pattern.find("*") != std::string::npos) {
      low.replace(pattern.find("*"), 1, 33-pattern.length(), '0');
      high.replace(pattern.find("*"), 1, 33-pattern.length(), 'F');
  }

  SensorId lowId, highId;
  if (!lowId.mqttTopicConvert(low)) {
      return SC_INVALIDPATTERN;
  }
  if (!highId.mqttTopicConvert(high)) {
      return SC_INVALIDPATTERN;
  }
  lowId.setRsvd(start.getWeekstamp());
  highId.setRsvd(end.getWeekstamp());

  low = lowId.serialize();
  high = highId.serialize();

//  std::cout << "Lower boundary for sensor scan: " << std::hex << std::setfill('0') << std::setw(16) << lowId.raw[0] << " " << std::hex << std::setfill('0') << std::setw(16) << lowId.raw[1] << std::endl;
//  std::cout << "Upper boundary for sensor scan: " << std::hex << std::setfill('0') << std::setw(16) << highId.raw[0] << " " << std::hex << std::setfill('0') << std::setw(16) << highId.raw[1] << std::endl;

  /* Query the database to see which raw sensors actually exist in the interval between low and high */
  CassError rc = CASS_OK;
  CassStatement* statement = nullptr;
  CassFuture* future = nullptr;
  const CassPrepared* prepared = nullptr;
  const char* query = "SELECT DISTINCT sid FROM " KEYSPACE_NAME "." CF_SENSORDATA " WHERE TOKEN(sid) >= ? and TOKEN(sid) <= ?;";

  future = cass_session_prepare(session, query);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
    connection->printError(future);
    return SC_UNKNOWNERROR;
  }

  prepared = cass_future_get_prepared(future);
  cass_future_free(future);

  statement = cass_prepared_bind(prepared);

  cass_statement_bind_bytes(statement, 0, (const cass_byte_t*)(low.c_str()), 16);
  cass_statement_bind_bytes(statement, 1, (const cass_byte_t*)(high.c_str()), 16);

  future = cass_session_execute(session, statement);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
      connection->printError(future);
      return SC_UNKNOWNERROR;
  }

  const CassResult* result = cass_future_get_result(future);
  cass_future_free(future);

  CassIterator* iterator = cass_iterator_from_result(result);
  while (cass_iterator_next(iterator)) {
      const CassRow* row = cass_iterator_get_row(iterator);
      const cass_byte_t* res;
      size_t       res_len;
      cass_value_get_bytes(cass_row_get_column_by_name(row, "sid"), &res, &res_len);

      uint64_t raw[2];
      raw[0] = Endian::BEToHost(((uint64_t*)res)[0]);
      raw[1] = Endian::BEToHost(((uint64_t*)res)[1]);

      SensorId sensor;
      sensor.setRaw(raw);

      /* Check if the sensorId matches the pattern and append to result */
      if (sensor.patternMatch(pattern)) {
946
947
948
949
          /* Only append if within the weekstamp window */
          if ((sensor.getRsvd() >= start.getWeekstamp()) && (sensor.getRsvd() <= end.getWeekstamp())) {
              sensorIds.push_back(sensor);
          }
Axel Auweter's avatar
Axel Auweter committed
950
951
952
953
954
955
956
957
958
959
      }
  }
  cass_result_free(result);
  cass_iterator_free(iterator);
  cass_statement_free(statement);
  cass_prepared_free(prepared);

  return SC_OK;
}

960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
SCError SensorConfigImpl::setSensorScalingFactor(std::string publicName, double scalingFactor)
{
  SCError error = SC_UNKNOWNERROR;

  /* Check the sensorconfig whether the given public sensor has the integrable flag set to true */
  CassError rc = CASS_OK;
  CassStatement* statement = nullptr;
  CassFuture* future = nullptr;
  const CassPrepared* prepared = nullptr;
  const char* query = "UPDATE " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " SET scaling_factor = ? WHERE name = ? ;";

  future = cass_session_prepare(session, query);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
    connection->printError(future);
    return SC_UNKNOWNERROR;
  }

  prepared = cass_future_get_prepared(future);
  cass_future_free(future);

  statement = cass_prepared_bind(prepared);

  cass_statement_bind_double(statement, 0, scalingFactor);
  cass_statement_bind_string(statement, 1, publicName.c_str());

  future = cass_session_execute(session, statement);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
      connection->printError(future);
      error = SC_UNKNOWNERROR;
  }
  else {
      error = SC_OK;
  }

  cass_statement_free(statement);