yocto_power.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_power.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindPower(), the high-level API for Power 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_power.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__ "power"
50 
51 YPower::YPower(const string& func): YSensor(func)
52 //--- (YPower initialization)
53  ,_cosPhi(COSPHI_INVALID)
54  ,_meter(METER_INVALID)
55  ,_meterTimer(METERTIMER_INVALID)
56  ,_valueCallbackPower(NULL)
57  ,_timedReportCallbackPower(NULL)
58 //--- (end of YPower initialization)
59 {
60  _className="Power";
61 }
62 
64 {
65 //--- (YPower cleanup)
66 //--- (end of YPower cleanup)
67 }
68 //--- (YPower implementation)
69 // static attributes
72 
74 {
75  if(json_val->has("cosPhi")) {
76  _cosPhi = floor(json_val->getDouble("cosPhi") * 1000.0 / 65536.0 + 0.5) / 1000.0;
77  }
78  if(json_val->has("meter")) {
79  _meter = floor(json_val->getDouble("meter") * 1000.0 / 65536.0 + 0.5) / 1000.0;
80  }
81  if(json_val->has("meterTimer")) {
82  _meterTimer = json_val->getInt("meterTimer");
83  }
84  return YSensor::_parseAttr(json_val);
85 }
86 
87 
97 double YPower::get_cosPhi(void)
98 {
99  double res = 0.0;
101  try {
104  {
106  return YPower::COSPHI_INVALID;
107  }
108  }
109  }
110  res = _cosPhi;
111  } catch (std::exception) {
113  throw;
114  }
116  return res;
117 }
118 
119 int YPower::set_meter(double newval)
120 {
121  string rest_val;
122  int res;
124  try {
125  char buf[32]; sprintf(buf,"%d", (int)floor(newval * 65536.0 + 0.5)); rest_val = string(buf);
126  res = _setAttr("meter", rest_val);
127  } catch (std::exception) {
129  throw;
130  }
132  return res;
133 }
134 
144 double YPower::get_meter(void)
145 {
146  double res = 0.0;
148  try {
151  {
153  return YPower::METER_INVALID;
154  }
155  }
156  }
157  res = _meter;
158  } catch (std::exception) {
160  throw;
161  }
163  return res;
164 }
165 
174 {
175  int res = 0;
177  try {
180  {
183  }
184  }
185  }
186  res = _meterTimer;
187  } catch (std::exception) {
189  throw;
190  }
192  return res;
193 }
194 
223 {
224  YPower* obj = NULL;
225  int taken = 0;
226  if (YAPI::_apiInitialized) {
228  taken = 1;
229  }try {
230  obj = (YPower*) YFunction::_FindFromCache("Power", func);
231  if (obj == NULL) {
232  obj = new YPower(func);
233  YFunction::_AddToCache("Power", func, obj);
234  }
235  } catch (std::exception) {
237  throw;
238  }
240  return obj;
241 }
242 
255 {
256  string val;
257  if (callback != NULL) {
259  } else {
261  }
262  _valueCallbackPower = callback;
263  // Immediately invoke value callback with current value
264  if (callback != NULL && this->isOnline()) {
265  val = _advertisedValue;
266  if (!(val == "")) {
267  this->_invokeValueCallback(val);
268  }
269  }
270  return 0;
271 }
272 
274 {
275  if (_valueCallbackPower != NULL) {
276  _valueCallbackPower(this, value);
277  } else {
279  }
280  return 0;
281 }
282 
295 {
296  YSensor* sensor = NULL;
297  sensor = this;
298  if (callback != NULL) {
300  } else {
302  }
303  _timedReportCallbackPower = callback;
304  return 0;
305 }
306 
308 {
309  if (_timedReportCallbackPower != NULL) {
310  _timedReportCallbackPower(this, value);
311  } else {
313  }
314  return 0;
315 }
316 
324 int YPower::reset(void)
325 {
326  return this->set_meter(0);
327 }
328 
330 {
331  string hwid;
332 
333  if(YISERR(_nextFunction(hwid)) || hwid=="") {
334  return NULL;
335  }
336  return YPower::FindPower(hwid);
337 }
338 
340 {
341  vector<YFUN_DESCR> v_fundescr;
342  YDEV_DESCR ydevice;
343  string serial, funcId, funcName, funcVal, errmsg;
344 
345  if(YISERR(YapiWrapper::getFunctionsByClass("Power", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
346  v_fundescr.size() == 0 ||
347  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
348  return NULL;
349  }
350  return YPower::FindPower(serial+"."+funcId);
351 }
352 
353 //--- (end of YPower implementation)
354 
355 //--- (YPower functions)
356 //--- (end of YPower functions)
static const double COSPHI_INVALID
Definition: yocto_power.h:99
string _advertisedValue
Definition: yocto_api.h:1569
static YPower * FindPower(string func)
double getDouble(const string &key)
Definition: yocto_api.cpp:796
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
virtual int registerValueCallback(YPowerValueCallback callback)
YPower(const string &func)
Definition: yocto_power.cpp:51
virtual int reset(void)
static void _UpdateTimedReportCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3531
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
static const double METER_INVALID
Definition: yocto_power.h:100
static int DefaultCacheValidity
Definition: yocto_api.h:468
YPowerValueCallback _valueCallbackPower
Definition: yocto_power.h:82
u64 _cacheExpiration
Definition: yocto_api.h:1571
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
virtual int _invokeValueCallback(string value)
double _cosPhi
Definition: yocto_power.h:79
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
static const int METERTIMER_INVALID
Definition: yocto_power.h:101
static YPower * FirstPower(void)
double _meter
Definition: yocto_power.h:80
double get_cosPhi(void)
Definition: yocto_power.cpp:97
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
#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
virtual int _invokeTimedReportCallback(YMeasure value)
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
void(* YPowerTimedReportCallback)(YPower *func, YMeasure measure)
Definition: yocto_power.h:56
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
int get_meterTimer(void)
virtual int registerTimedReportCallback(YPowerTimedReportCallback callback)
YPower * nextPower(void)
string _className
Definition: yocto_api.h:1558
void(* YPowerValueCallback)(YPower *func, const string &functionValue)
Definition: yocto_power.h:54
int _meterTimer
Definition: yocto_power.h:81
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
double get_meter(void)
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_power.cpp:73
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
int set_meter(double newval)
bool has(const string &key)
Definition: yocto_api.cpp:735
YPowerTimedReportCallback _timedReportCallbackPower
Definition: yocto_power.h:83
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