yocto_current.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_current.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindCurrent(), the high-level API for Current 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_current.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__ "current"
50 
51 YCurrent::YCurrent(const string& func): YSensor(func)
52 //--- (YCurrent initialization)
53  ,_enabled(ENABLED_INVALID)
54  ,_valueCallbackCurrent(NULL)
55  ,_timedReportCallbackCurrent(NULL)
56 //--- (end of YCurrent initialization)
57 {
58  _className="Current";
59 }
60 
62 {
63 //--- (YCurrent cleanup)
64 //--- (end of YCurrent cleanup)
65 }
66 //--- (YCurrent implementation)
67 // static attributes
68 
70 {
71  if(json_val->has("enabled")) {
72  _enabled = (Y_ENABLED_enum)json_val->getInt("enabled");
73  }
74  return YSensor::_parseAttr(json_val);
75 }
76 
77 
79 {
80  Y_ENABLED_enum res;
82  try {
85  {
88  }
89  }
90  }
91  res = _enabled;
92  } catch (std::exception) {
94  throw;
95  }
97  return res;
98 }
99 
101 {
102  string rest_val;
103  int res;
105  try {
106  rest_val = (newval>0 ? "1" : "0");
107  res = _setAttr("enabled", rest_val);
108  } catch (std::exception) {
110  throw;
111  }
113  return res;
114 }
115 
144 {
145  YCurrent* obj = NULL;
146  int taken = 0;
147  if (YAPI::_apiInitialized) {
149  taken = 1;
150  }try {
151  obj = (YCurrent*) YFunction::_FindFromCache("Current", func);
152  if (obj == NULL) {
153  obj = new YCurrent(func);
154  YFunction::_AddToCache("Current", func, obj);
155  }
156  } catch (std::exception) {
158  throw;
159  }
161  return obj;
162 }
163 
176 {
177  string val;
178  if (callback != NULL) {
180  } else {
182  }
183  _valueCallbackCurrent = callback;
184  // Immediately invoke value callback with current value
185  if (callback != NULL && this->isOnline()) {
186  val = _advertisedValue;
187  if (!(val == "")) {
188  this->_invokeValueCallback(val);
189  }
190  }
191  return 0;
192 }
193 
195 {
196  if (_valueCallbackCurrent != NULL) {
197  _valueCallbackCurrent(this, value);
198  } else {
200  }
201  return 0;
202 }
203 
216 {
217  YSensor* sensor = NULL;
218  sensor = this;
219  if (callback != NULL) {
221  } else {
223  }
224  _timedReportCallbackCurrent = callback;
225  return 0;
226 }
227 
229 {
230  if (_timedReportCallbackCurrent != NULL) {
231  _timedReportCallbackCurrent(this, value);
232  } else {
234  }
235  return 0;
236 }
237 
239 {
240  string hwid;
241 
242  if(YISERR(_nextFunction(hwid)) || hwid=="") {
243  return NULL;
244  }
245  return YCurrent::FindCurrent(hwid);
246 }
247 
249 {
250  vector<YFUN_DESCR> v_fundescr;
251  YDEV_DESCR ydevice;
252  string serial, funcId, funcName, funcVal, errmsg;
253 
254  if(YISERR(YapiWrapper::getFunctionsByClass("Current", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
255  v_fundescr.size() == 0 ||
256  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
257  return NULL;
258  }
259  return YCurrent::FindCurrent(serial+"."+funcId);
260 }
261 
262 //--- (end of YCurrent implementation)
263 
264 //--- (YCurrent functions)
265 //--- (end of YCurrent functions)
string _advertisedValue
Definition: yocto_api.h:1569
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
static YCurrent * FirstCurrent(void)
static void _UpdateTimedReportCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3531
Y_ENABLED_enum
Definition: yocto_current.h:59
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
int set_enabled(Y_ENABLED_enum newval)
Y_ENABLED_enum get_enabled(void)
static int DefaultCacheValidity
Definition: yocto_api.h:468
YCurrentTimedReportCallback _timedReportCallbackCurrent
Definition: yocto_current.h:85
YCurrent(const string &func)
u64 _cacheExpiration
Definition: yocto_api.h:1571
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
static const Y_ENABLED_enum ENABLED_INVALID
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
YCurrent * nextCurrent(void)
void(* YCurrentValueCallback)(YCurrent *func, const string &functionValue)
Definition: yocto_current.h:54
virtual int _invokeValueCallback(string value)
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
int getInt(const string &key)
Definition: yocto_api.cpp:779
virtual int _parseAttr(YJSONObject *json_val)
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
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
virtual int registerTimedReportCallback(YCurrentTimedReportCallback callback)
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 registerValueCallback(YCurrentValueCallback callback)
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
Y_ENABLED_enum _enabled
Definition: yocto_current.h:83
string _className
Definition: yocto_api.h:1558
YCurrentValueCallback _valueCallbackCurrent
Definition: yocto_current.h:84
void(* YCurrentTimedReportCallback)(YCurrent *func, YMeasure measure)
Definition: yocto_current.h:56
static YCurrent * FindCurrent(string func)
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
virtual int _invokeTimedReportCallback(YMeasure value)
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#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


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