yocto_genericsensor.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_genericsensor.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindGenericSensor(), the high-level API for GenericSensor functions
6  *
7  * - - - - - - - - - License information: - - - - - - - - -
8  *
9  * Copyright (C) 2011 and beyond by Yoctopuce Sarl, Switzerland.
10  *
11  * Yoctopuce Sarl (hereafter Licensor) grants to you a perpetual
12  * non-exclusive license to use, modify, copy and integrate this
13  * file into your software for the sole purpose of interfacing
14  * with Yoctopuce products.
15  *
16  * You may reproduce and distribute copies of this file in
17  * source or object form, as long as the sole purpose of this
18  * code is to interface with Yoctopuce products. You must retain
19  * this notice in the distributed source file.
20  *
21  * You should refer to Yoctopuce General Terms and Conditions
22  * for additional information regarding your rights and
23  * obligations.
24  *
25  * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED 'AS IS' WITHOUT
26  * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
27  * WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS
28  * FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO
29  * EVENT SHALL LICENSOR BE LIABLE FOR ANY INCIDENTAL, SPECIAL,
30  * INDIRECT OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA,
31  * COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR
32  * SERVICES, ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT
33  * LIMITED TO ANY DEFENSE THEREOF), ANY CLAIMS FOR INDEMNITY OR
34  * CONTRIBUTION, OR OTHER SIMILAR COSTS, WHETHER ASSERTED ON THE
35  * BASIS OF CONTRACT, TORT (INCLUDING NEGLIGENCE), BREACH OF
36  * WARRANTY, OR OTHERWISE.
37  *
38  *********************************************************************/
39 
40 
41 #define _CRT_SECURE_NO_DEPRECATE //do not use windows secure crt
42 #include "yocto_genericsensor.h"
43 #include "yapi/yjson.h"
44 #include "yapi/yapi.h"
45 #include <string.h>
46 #include <stdio.h>
47 #include <math.h>
48 #include <stdlib.h>
49 #define __FILE_ID__ "genericsensor"
50 
51 YGenericSensor::YGenericSensor(const string& func): YSensor(func)
52 //--- (YGenericSensor initialization)
53  ,_signalValue(SIGNALVALUE_INVALID)
54  ,_signalUnit(SIGNALUNIT_INVALID)
55  ,_signalRange(SIGNALRANGE_INVALID)
56  ,_valueRange(VALUERANGE_INVALID)
57  ,_signalBias(SIGNALBIAS_INVALID)
58  ,_signalSampling(SIGNALSAMPLING_INVALID)
59  ,_valueCallbackGenericSensor(NULL)
60  ,_timedReportCallbackGenericSensor(NULL)
61 //--- (end of YGenericSensor initialization)
62 {
63  _className="GenericSensor";
64 }
65 
67 {
68 //--- (YGenericSensor cleanup)
69 //--- (end of YGenericSensor cleanup)
70 }
71 //--- (YGenericSensor implementation)
72 // static attributes
78 
80 {
81  if(json_val->has("signalValue")) {
82  _signalValue = floor(json_val->getDouble("signalValue") * 1000.0 / 65536.0 + 0.5) / 1000.0;
83  }
84  if(json_val->has("signalUnit")) {
85  _signalUnit = json_val->getString("signalUnit");
86  }
87  if(json_val->has("signalRange")) {
88  _signalRange = json_val->getString("signalRange");
89  }
90  if(json_val->has("valueRange")) {
91  _valueRange = json_val->getString("valueRange");
92  }
93  if(json_val->has("signalBias")) {
94  _signalBias = floor(json_val->getDouble("signalBias") * 1000.0 / 65536.0 + 0.5) / 1000.0;
95  }
96  if(json_val->has("signalSampling")) {
97  _signalSampling = (Y_SIGNALSAMPLING_enum)json_val->getInt("signalSampling");
98  }
99  return YSensor::_parseAttr(json_val);
100 }
101 
102 
114 int YGenericSensor::set_unit(const string& newval)
115 {
116  string rest_val;
117  int res;
119  try {
120  rest_val = newval;
121  res = _setAttr("unit", rest_val);
122  } catch (std::exception) {
124  throw;
125  }
127  return res;
128 }
129 
139 {
140  double res = 0.0;
142  try {
145  {
148  }
149  }
150  }
151  res = floor(_signalValue * 1000+0.5) / 1000;
152  } catch (std::exception) {
154  throw;
155  }
157  return res;
158 }
159 
168 {
169  string res;
171  try {
172  if (_cacheExpiration == 0) {
174  {
177  }
178  }
179  }
180  res = _signalUnit;
181  } catch (std::exception) {
183  throw;
184  }
186  return res;
187 }
188 
197 {
198  string res;
200  try {
203  {
206  }
207  }
208  }
209  res = _signalRange;
210  } catch (std::exception) {
212  throw;
213  }
215  return res;
216 }
217 
227 int YGenericSensor::set_signalRange(const string& newval)
228 {
229  string rest_val;
230  int res;
232  try {
233  rest_val = newval;
234  res = _setAttr("signalRange", rest_val);
235  } catch (std::exception) {
237  throw;
238  }
240  return res;
241 }
242 
251 {
252  string res;
254  try {
257  {
260  }
261  }
262  }
263  res = _valueRange;
264  } catch (std::exception) {
266  throw;
267  }
269  return res;
270 }
271 
282 int YGenericSensor::set_valueRange(const string& newval)
283 {
284  string rest_val;
285  int res;
287  try {
288  rest_val = newval;
289  res = _setAttr("valueRange", rest_val);
290  } catch (std::exception) {
292  throw;
293  }
295  return res;
296 }
297 
310 {
311  string rest_val;
312  int res;
314  try {
315  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
316  res = _setAttr("signalBias", rest_val);
317  } catch (std::exception) {
319  throw;
320  }
322  return res;
323 }
324 
335 {
336  double res = 0.0;
338  try {
341  {
344  }
345  }
346  }
347  res = _signalBias;
348  } catch (std::exception) {
350  throw;
351  }
353  return res;
354 }
355 
371 {
374  try {
377  {
380  }
381  }
382  }
383  res = _signalSampling;
384  } catch (std::exception) {
386  throw;
387  }
389  return res;
390 }
391 
409 {
410  string rest_val;
411  int res;
413  try {
414  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
415  res = _setAttr("signalSampling", rest_val);
416  } catch (std::exception) {
418  throw;
419  }
421  return res;
422 }
423 
452 {
453  YGenericSensor* obj = NULL;
454  int taken = 0;
455  if (YAPI::_apiInitialized) {
457  taken = 1;
458  }try {
459  obj = (YGenericSensor*) YFunction::_FindFromCache("GenericSensor", func);
460  if (obj == NULL) {
461  obj = new YGenericSensor(func);
462  YFunction::_AddToCache("GenericSensor", func, obj);
463  }
464  } catch (std::exception) {
466  throw;
467  }
469  return obj;
470 }
471 
484 {
485  string val;
486  if (callback != NULL) {
488  } else {
490  }
491  _valueCallbackGenericSensor = callback;
492  // Immediately invoke value callback with current value
493  if (callback != NULL && this->isOnline()) {
494  val = _advertisedValue;
495  if (!(val == "")) {
496  this->_invokeValueCallback(val);
497  }
498  }
499  return 0;
500 }
501 
503 {
504  if (_valueCallbackGenericSensor != NULL) {
505  _valueCallbackGenericSensor(this, value);
506  } else {
508  }
509  return 0;
510 }
511 
524 {
525  YSensor* sensor = NULL;
526  sensor = this;
527  if (callback != NULL) {
529  } else {
531  }
533  return 0;
534 }
535 
537 {
538  if (_timedReportCallbackGenericSensor != NULL) {
540  } else {
542  }
543  return 0;
544 }
545 
555 {
556  double currSignal = 0.0;
557  double currBias = 0.0;
558  currSignal = this->get_signalValue();
559  currBias = this->get_signalBias();
560  return this->set_signalBias(currSignal + currBias);
561 }
562 
564 {
565  string hwid;
566 
567  if(YISERR(_nextFunction(hwid)) || hwid=="") {
568  return NULL;
569  }
571 }
572 
574 {
575  vector<YFUN_DESCR> v_fundescr;
576  YDEV_DESCR ydevice;
577  string serial, funcId, funcName, funcVal, errmsg;
578 
579  if(YISERR(YapiWrapper::getFunctionsByClass("GenericSensor", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
580  v_fundescr.size() == 0 ||
581  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
582  return NULL;
583  }
584  return YGenericSensor::FindGenericSensor(serial+"."+funcId);
585 }
586 
587 //--- (end of YGenericSensor implementation)
588 
589 //--- (YGenericSensor functions)
590 //--- (end of YGenericSensor functions)
string get_signalRange(void)
virtual int registerTimedReportCallback(YGenericSensorTimedReportCallback callback)
string _advertisedValue
Definition: yocto_api.h:1569
double getDouble(const string &key)
Definition: yocto_api.cpp:796
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
int set_signalRange(const string &newval)
static void _UpdateTimedReportCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3531
void(* YGenericSensorValueCallback)(YGenericSensor *func, const string &functionValue)
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
virtual int zeroAdjust(void)
YGenericSensor(const string &func)
int set_valueRange(const string &newval)
double get_signalBias(void)
virtual int _parseAttr(YJSONObject *json_val)
static int DefaultCacheValidity
Definition: yocto_api.h:468
string getString(const string &key)
Definition: yocto_api.cpp:773
void(* YGenericSensorTimedReportCallback)(YGenericSensor *func, YMeasure measure)
u64 _cacheExpiration
Definition: yocto_api.h:1571
virtual int _invokeValueCallback(string value)
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
virtual int registerValueCallback(YGenericSensorValueCallback callback)
int set_signalBias(double newval)
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
int getInt(const string &key)
Definition: yocto_api.cpp:779
static u64 GetTickCount(void)
Definition: yocto_api.cpp:4755
static void _AddToCache(const string &classname, const string &func, YFunction *obj)
Definition: yocto_api.cpp:2345
static YGenericSensor * FirstGenericSensor(void)
#define YAPI_INVALID_DOUBLE
Definition: yocto_api.h:73
virtual int _invokeTimedReportCallback(YMeasure value)
Definition: yocto_api.cpp:7936
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:7645
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
Y_SIGNALSAMPLING_enum
static const string SIGNALRANGE_INVALID
void yEnterCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:647
static yCRITICAL_SECTION _global_cs
Definition: yocto_api.h:476
bool isOnline(void)
Definition: yocto_api.cpp:3291
static const double SIGNALBIAS_INVALID
Y_SIGNALSAMPLING_enum get_signalSampling(void)
YGenericSensor * nextGenericSensor(void)
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:7015
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
static const Y_SIGNALSAMPLING_enum SIGNALSAMPLING_INVALID
Y_SIGNALSAMPLING_enum _signalSampling
string get_signalUnit(void)
string _className
Definition: yocto_api.h:1558
static const string VALUERANGE_INVALID
double get_signalValue(void)
virtual int _invokeTimedReportCallback(YMeasure value)
string get_valueRange(void)
static YGenericSensor * FindGenericSensor(string func)
YGenericSensorTimedReportCallback _timedReportCallbackGenericSensor
static const string SIGNALUNIT_INVALID
int set_signalSampling(Y_SIGNALSAMPLING_enum newval)
int set_unit(const string &newval)
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
static const double SIGNALVALUE_INVALID
#define YISERR(retcode)
Definition: ydef.h:394
YGenericSensorValueCallback _valueCallbackGenericSensor
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968


yoctopuce_altimeter
Author(s): Anja Sheppard
autogenerated on Mon Jun 10 2019 15:49:11