yocto_humidity.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_humidity.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindHumidity(), the high-level API for Humidity 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_humidity.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__ "humidity"
50 
51 YHumidity::YHumidity(const string& func): YSensor(func)
52 //--- (YHumidity initialization)
53  ,_relHum(RELHUM_INVALID)
54  ,_absHum(ABSHUM_INVALID)
55  ,_valueCallbackHumidity(NULL)
56  ,_timedReportCallbackHumidity(NULL)
57 //--- (end of YHumidity initialization)
58 {
59  _className="Humidity";
60 }
61 
63 {
64 //--- (YHumidity cleanup)
65 //--- (end of YHumidity cleanup)
66 }
67 //--- (YHumidity implementation)
68 // static attributes
71 
73 {
74  if(json_val->has("relHum")) {
75  _relHum = floor(json_val->getDouble("relHum") * 1000.0 / 65536.0 + 0.5) / 1000.0;
76  }
77  if(json_val->has("absHum")) {
78  _absHum = floor(json_val->getDouble("absHum") * 1000.0 / 65536.0 + 0.5) / 1000.0;
79  }
80  return YSensor::_parseAttr(json_val);
81 }
82 
83 
99 int YHumidity::set_unit(const string& newval)
100 {
101  string rest_val;
102  int res;
104  try {
105  rest_val = newval;
106  res = _setAttr("unit", rest_val);
107  } catch (std::exception) {
109  throw;
110  }
112  return res;
113 }
114 
123 {
124  double res = 0.0;
126  try {
129  {
132  }
133  }
134  }
135  res = _relHum;
136  } catch (std::exception) {
138  throw;
139  }
141  return res;
142 }
143 
152 {
153  double res = 0.0;
155  try {
158  {
161  }
162  }
163  }
164  res = _absHum;
165  } catch (std::exception) {
167  throw;
168  }
170  return res;
171 }
172 
201 {
202  YHumidity* obj = NULL;
203  int taken = 0;
204  if (YAPI::_apiInitialized) {
206  taken = 1;
207  }try {
208  obj = (YHumidity*) YFunction::_FindFromCache("Humidity", func);
209  if (obj == NULL) {
210  obj = new YHumidity(func);
211  YFunction::_AddToCache("Humidity", func, obj);
212  }
213  } catch (std::exception) {
215  throw;
216  }
218  return obj;
219 }
220 
233 {
234  string val;
235  if (callback != NULL) {
237  } else {
239  }
240  _valueCallbackHumidity = callback;
241  // Immediately invoke value callback with current value
242  if (callback != NULL && this->isOnline()) {
243  val = _advertisedValue;
244  if (!(val == "")) {
245  this->_invokeValueCallback(val);
246  }
247  }
248  return 0;
249 }
250 
252 {
253  if (_valueCallbackHumidity != NULL) {
254  _valueCallbackHumidity(this, value);
255  } else {
257  }
258  return 0;
259 }
260 
273 {
274  YSensor* sensor = NULL;
275  sensor = this;
276  if (callback != NULL) {
278  } else {
280  }
281  _timedReportCallbackHumidity = callback;
282  return 0;
283 }
284 
286 {
287  if (_timedReportCallbackHumidity != NULL) {
288  _timedReportCallbackHumidity(this, value);
289  } else {
291  }
292  return 0;
293 }
294 
296 {
297  string hwid;
298 
299  if(YISERR(_nextFunction(hwid)) || hwid=="") {
300  return NULL;
301  }
302  return YHumidity::FindHumidity(hwid);
303 }
304 
306 {
307  vector<YFUN_DESCR> v_fundescr;
308  YDEV_DESCR ydevice;
309  string serial, funcId, funcName, funcVal, errmsg;
310 
311  if(YISERR(YapiWrapper::getFunctionsByClass("Humidity", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
312  v_fundescr.size() == 0 ||
313  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
314  return NULL;
315  }
316  return YHumidity::FindHumidity(serial+"."+funcId);
317 }
318 
319 //--- (end of YHumidity implementation)
320 
321 //--- (YHumidity functions)
322 //--- (end of YHumidity functions)
string _advertisedValue
Definition: yocto_api.h:1569
YHumidityValueCallback _valueCallbackHumidity
double getDouble(const string &key)
Definition: yocto_api.cpp:796
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
double _relHum
void(* YHumidityValueCallback)(YHumidity *func, const string &functionValue)
void(* YHumidityTimedReportCallback)(YHumidity *func, YMeasure measure)
static void _UpdateTimedReportCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3531
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
virtual int _invokeValueCallback(string value)
YHumidity(const string &func)
static int DefaultCacheValidity
Definition: yocto_api.h:468
static YHumidity * FindHumidity(string func)
virtual int _invokeTimedReportCallback(YMeasure value)
u64 _cacheExpiration
Definition: yocto_api.h:1571
double _absHum
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
virtual int registerTimedReportCallback(YHumidityTimedReportCallback callback)
static const double RELHUM_INVALID
YHumidity * nextHumidity(void)
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
static u64 GetTickCount(void)
Definition: yocto_api.cpp:4755
static YHumidity * FirstHumidity(void)
static void _AddToCache(const string &classname, const string &func, YFunction *obj)
Definition: yocto_api.cpp:2345
#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
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
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:7015
int set_unit(const string &newval)
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
double get_relHum(void)
string _className
Definition: yocto_api.h:1558
YHumidityTimedReportCallback _timedReportCallbackHumidity
double get_absHum(void)
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
virtual int _parseAttr(YJSONObject *json_val)
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
virtual int registerValueCallback(YHumidityValueCallback callback)
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
static const double ABSHUM_INVALID


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