yocto_carbondioxide.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_carbondioxide.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindCarbonDioxide(), the high-level API for CarbonDioxide 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_carbondioxide.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__ "carbondioxide"
50 
51 YCarbonDioxide::YCarbonDioxide(const string& func): YSensor(func)
52 //--- (YCarbonDioxide initialization)
53  ,_abcPeriod(ABCPERIOD_INVALID)
54  ,_command(COMMAND_INVALID)
55  ,_valueCallbackCarbonDioxide(NULL)
56  ,_timedReportCallbackCarbonDioxide(NULL)
57 //--- (end of YCarbonDioxide initialization)
58 {
59  _className="CarbonDioxide";
60 }
61 
63 {
64 //--- (YCarbonDioxide cleanup)
65 //--- (end of YCarbonDioxide cleanup)
66 }
67 //--- (YCarbonDioxide implementation)
68 // static attributes
70 
72 {
73  if(json_val->has("abcPeriod")) {
74  _abcPeriod = json_val->getInt("abcPeriod");
75  }
76  if(json_val->has("command")) {
77  _command = json_val->getString("command");
78  }
79  return YSensor::_parseAttr(json_val);
80 }
81 
82 
92 {
93  int res = 0;
95  try {
98  {
101  }
102  }
103  }
104  res = _abcPeriod;
105  } catch (std::exception) {
107  throw;
108  }
110  return res;
111 }
112 
127 {
128  string rest_val;
129  int res;
131  try {
132  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
133  res = _setAttr("abcPeriod", rest_val);
134  } catch (std::exception) {
136  throw;
137  }
139  return res;
140 }
141 
143 {
144  string res;
146  try {
149  {
152  }
153  }
154  }
155  res = _command;
156  } catch (std::exception) {
158  throw;
159  }
161  return res;
162 }
163 
164 int YCarbonDioxide::set_command(const string& newval)
165 {
166  string rest_val;
167  int res;
169  try {
170  rest_val = newval;
171  res = _setAttr("command", rest_val);
172  } catch (std::exception) {
174  throw;
175  }
177  return res;
178 }
179 
208 {
209  YCarbonDioxide* obj = NULL;
210  int taken = 0;
211  if (YAPI::_apiInitialized) {
213  taken = 1;
214  }try {
215  obj = (YCarbonDioxide*) YFunction::_FindFromCache("CarbonDioxide", func);
216  if (obj == NULL) {
217  obj = new YCarbonDioxide(func);
218  YFunction::_AddToCache("CarbonDioxide", func, obj);
219  }
220  } catch (std::exception) {
222  throw;
223  }
225  return obj;
226 }
227 
240 {
241  string val;
242  if (callback != NULL) {
244  } else {
246  }
247  _valueCallbackCarbonDioxide = callback;
248  // Immediately invoke value callback with current value
249  if (callback != NULL && this->isOnline()) {
250  val = _advertisedValue;
251  if (!(val == "")) {
252  this->_invokeValueCallback(val);
253  }
254  }
255  return 0;
256 }
257 
259 {
260  if (_valueCallbackCarbonDioxide != NULL) {
261  _valueCallbackCarbonDioxide(this, value);
262  } else {
264  }
265  return 0;
266 }
267 
280 {
281  YSensor* sensor = NULL;
282  sensor = this;
283  if (callback != NULL) {
285  } else {
287  }
289  return 0;
290 }
291 
293 {
294  if (_timedReportCallbackCarbonDioxide != NULL) {
296  } else {
298  }
299  return 0;
300 }
301 
317 {
318  return this->set_command("BC");
319 }
320 
322 {
323  return this->triggerBaselineCalibration();
324 }
325 
343 {
344  return this->set_command("ZC");
345 }
346 
348 {
349  return this->triggerZeroCalibration();
350 }
351 
353 {
354  string hwid;
355 
356  if(YISERR(_nextFunction(hwid)) || hwid=="") {
357  return NULL;
358  }
360 }
361 
363 {
364  vector<YFUN_DESCR> v_fundescr;
365  YDEV_DESCR ydevice;
366  string serial, funcId, funcName, funcVal, errmsg;
367 
368  if(YISERR(YapiWrapper::getFunctionsByClass("CarbonDioxide", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
369  v_fundescr.size() == 0 ||
370  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
371  return NULL;
372  }
373  return YCarbonDioxide::FindCarbonDioxide(serial+"."+funcId);
374 }
375 
376 //--- (end of YCarbonDioxide implementation)
377 
378 //--- (YCarbonDioxide functions)
379 //--- (end of YCarbonDioxide functions)
string _advertisedValue
Definition: yocto_api.h:1569
YCarbonDioxide(const string &func)
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
static void _UpdateTimedReportCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3531
virtual int _invokeValueCallback(string value)
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
virtual int _parseAttr(YJSONObject *json_val)
int set_command(const string &newval)
static YCarbonDioxide * FindCarbonDioxide(string func)
static int DefaultCacheValidity
Definition: yocto_api.h:468
string getString(const string &key)
Definition: yocto_api.cpp:773
u64 _cacheExpiration
Definition: yocto_api.h:1571
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
virtual int _invokeTimedReportCallback(YMeasure value)
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
YCarbonDioxide * nextCarbonDioxide(void)
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
int getInt(const string &key)
Definition: yocto_api.cpp:779
virtual int triggerZeroCalibration(void)
static u64 GetTickCount(void)
Definition: yocto_api.cpp:4755
string get_command(void)
static void _AddToCache(const string &classname, const string &func, YFunction *obj)
Definition: yocto_api.cpp:2345
int set_abcPeriod(int newval)
virtual int _invokeTimedReportCallback(YMeasure value)
Definition: yocto_api.cpp:7936
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:7645
virtual int triggetBaselineCalibration(void)
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
virtual int registerTimedReportCallback(YCarbonDioxideTimedReportCallback callback)
virtual int registerValueCallback(YCarbonDioxideValueCallback callback)
static const int ABCPERIOD_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
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:7015
static const string COMMAND_INVALID
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
static YCarbonDioxide * FirstCarbonDioxide(void)
string _className
Definition: yocto_api.h:1558
void(* YCarbonDioxideValueCallback)(YCarbonDioxide *func, const string &functionValue)
virtual int triggerBaselineCalibration(void)
virtual int triggetZeroCalibration(void)
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
YCarbonDioxideValueCallback _valueCallbackCarbonDioxide
#define YISERR(retcode)
Definition: ydef.h:394
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
YCarbonDioxideTimedReportCallback _timedReportCallbackCarbonDioxide
void(* YCarbonDioxideTimedReportCallback)(YCarbonDioxide *func, YMeasure measure)


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