yocto_realtimeclock.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_realtimeclock.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindRealTimeClock(), the high-level API for RealTimeClock 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_realtimeclock.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__ "realtimeclock"
50 
51 YRealTimeClock::YRealTimeClock(const string& func): YFunction(func)
52 //--- (YRealTimeClock initialization)
53  ,_unixTime(UNIXTIME_INVALID)
54  ,_dateTime(DATETIME_INVALID)
55  ,_utcOffset(UTCOFFSET_INVALID)
56  ,_timeSet(TIMESET_INVALID)
57  ,_valueCallbackRealTimeClock(NULL)
58 //--- (end of YRealTimeClock initialization)
59 {
60  _className="RealTimeClock";
61 }
62 
64 {
65 //--- (YRealTimeClock cleanup)
66 //--- (end of YRealTimeClock cleanup)
67 }
68 //--- (YRealTimeClock implementation)
69 // static attributes
71 
73 {
74  if(json_val->has("unixTime")) {
75  _unixTime = json_val->getLong("unixTime");
76  }
77  if(json_val->has("dateTime")) {
78  _dateTime = json_val->getString("dateTime");
79  }
80  if(json_val->has("utcOffset")) {
81  _utcOffset = json_val->getInt("utcOffset");
82  }
83  if(json_val->has("timeSet")) {
84  _timeSet = (Y_TIMESET_enum)json_val->getInt("timeSet");
85  }
86  return YFunction::_parseAttr(json_val);
87 }
88 
89 
99 {
100  s64 res = 0;
102  try {
105  {
108  }
109  }
110  }
111  res = _unixTime;
112  } catch (std::exception) {
114  throw;
115  }
117  return res;
118 }
119 
130 {
131  string rest_val;
132  int res;
134  try {
135  char buf[32]; sprintf(buf, "%u", (u32)newval); rest_val = string(buf);
136  res = _setAttr("unixTime", rest_val);
137  } catch (std::exception) {
139  throw;
140  }
142  return res;
143 }
144 
153 {
154  string res;
156  try {
159  {
162  }
163  }
164  }
165  res = _dateTime;
166  } catch (std::exception) {
168  throw;
169  }
171  return res;
172 }
173 
182 {
183  int res = 0;
185  try {
188  {
191  }
192  }
193  }
194  res = _utcOffset;
195  } catch (std::exception) {
197  throw;
198  }
200  return res;
201 }
202 
214 {
215  string rest_val;
216  int res;
218  try {
219  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
220  res = _setAttr("utcOffset", rest_val);
221  } catch (std::exception) {
223  throw;
224  }
226  return res;
227 }
228 
238 {
239  Y_TIMESET_enum res;
241  try {
244  {
247  }
248  }
249  }
250  res = _timeSet;
251  } catch (std::exception) {
253  throw;
254  }
256  return res;
257 }
258 
287 {
288  YRealTimeClock* obj = NULL;
289  int taken = 0;
290  if (YAPI::_apiInitialized) {
292  taken = 1;
293  }try {
294  obj = (YRealTimeClock*) YFunction::_FindFromCache("RealTimeClock", func);
295  if (obj == NULL) {
296  obj = new YRealTimeClock(func);
297  YFunction::_AddToCache("RealTimeClock", func, obj);
298  }
299  } catch (std::exception) {
301  throw;
302  }
304  return obj;
305 }
306 
319 {
320  string val;
321  if (callback != NULL) {
323  } else {
325  }
326  _valueCallbackRealTimeClock = callback;
327  // Immediately invoke value callback with current value
328  if (callback != NULL && this->isOnline()) {
329  val = _advertisedValue;
330  if (!(val == "")) {
331  this->_invokeValueCallback(val);
332  }
333  }
334  return 0;
335 }
336 
338 {
339  if (_valueCallbackRealTimeClock != NULL) {
340  _valueCallbackRealTimeClock(this, value);
341  } else {
343  }
344  return 0;
345 }
346 
348 {
349  string hwid;
350 
351  if(YISERR(_nextFunction(hwid)) || hwid=="") {
352  return NULL;
353  }
355 }
356 
358 {
359  vector<YFUN_DESCR> v_fundescr;
360  YDEV_DESCR ydevice;
361  string serial, funcId, funcName, funcVal, errmsg;
362 
363  if(YISERR(YapiWrapper::getFunctionsByClass("RealTimeClock", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
364  v_fundescr.size() == 0 ||
365  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
366  return NULL;
367  }
368  return YRealTimeClock::FindRealTimeClock(serial+"."+funcId);
369 }
370 
371 //--- (end of YRealTimeClock implementation)
372 
373 //--- (YRealTimeClock functions)
374 //--- (end of YRealTimeClock functions)
string _advertisedValue
Definition: yocto_api.h:1569
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
Y_TIMESET_enum _timeSet
virtual int registerValueCallback(YRealTimeClockValueCallback callback)
static YRealTimeClock * FirstRealTimeClock(void)
int set_utcOffset(int newval)
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
virtual int _invokeValueCallback(string value)
Y_TIMESET_enum get_timeSet(void)
string get_dateTime(void)
static const string DATETIME_INVALID
static const s64 UNIXTIME_INVALID
static int DefaultCacheValidity
Definition: yocto_api.h:468
string getString(const string &key)
Definition: yocto_api.cpp:773
virtual int _parseAttr(YJSONObject *json_val)
u64 _cacheExpiration
Definition: yocto_api.h:1571
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
static const Y_TIMESET_enum TIMESET_INVALID
YRealTimeClock(const string &func)
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
int getInt(const string &key)
Definition: yocto_api.cpp:779
static YRealTimeClock * FindRealTimeClock(string func)
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 const int UTCOFFSET_INVALID
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
int set_unixTime(s64 newval)
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
Y_TIMESET_enum
string _className
Definition: yocto_api.h:1558
YRealTimeClockValueCallback _valueCallbackRealTimeClock
s64 getLong(const string &key)
Definition: yocto_api.cpp:790
YRealTimeClock * nextRealTimeClock(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
#define YISERR(retcode)
Definition: ydef.h:394
void(* YRealTimeClockValueCallback)(YRealTimeClock *func, const string &functionValue)
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:2563


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