yocto_relay.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_relay.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindRelay(), the high-level API for Relay 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_relay.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__ "relay"
50 
51 YRelay::YRelay(const string& func): YFunction(func)
52 //--- (YRelay initialization)
53  ,_state(STATE_INVALID)
54  ,_stateAtPowerOn(STATEATPOWERON_INVALID)
55  ,_maxTimeOnStateA(MAXTIMEONSTATEA_INVALID)
56  ,_maxTimeOnStateB(MAXTIMEONSTATEB_INVALID)
57  ,_output(OUTPUT_INVALID)
58  ,_pulseTimer(PULSETIMER_INVALID)
59  ,_delayedPulseTimer(DELAYEDPULSETIMER_INVALID)
60  ,_countdown(COUNTDOWN_INVALID)
61  ,_valueCallbackRelay(NULL)
62 //--- (end of YRelay initialization)
63 {
64  _className="Relay";
65 }
66 
68 {
69 //--- (YRelay cleanup)
70 //--- (end of YRelay cleanup)
71 }
72 //--- (YRelay implementation)
73 // static attributes
75 
77 {
78  if(json_val->has("state")) {
79  _state = (Y_STATE_enum)json_val->getInt("state");
80  }
81  if(json_val->has("stateAtPowerOn")) {
82  _stateAtPowerOn = (Y_STATEATPOWERON_enum)json_val->getInt("stateAtPowerOn");
83  }
84  if(json_val->has("maxTimeOnStateA")) {
85  _maxTimeOnStateA = json_val->getLong("maxTimeOnStateA");
86  }
87  if(json_val->has("maxTimeOnStateB")) {
88  _maxTimeOnStateB = json_val->getLong("maxTimeOnStateB");
89  }
90  if(json_val->has("output")) {
91  _output = (Y_OUTPUT_enum)json_val->getInt("output");
92  }
93  if(json_val->has("pulseTimer")) {
94  _pulseTimer = json_val->getLong("pulseTimer");
95  }
96  if(json_val->has("delayedPulseTimer")) {
97  YJSONObject* subjson = json_val->getYJSONObject("delayedPulseTimer");
98  if (subjson->has("moving")) {
99  _delayedPulseTimer.moving = subjson->getInt("moving");
100  }
101  if (subjson->has("target")) {
102  _delayedPulseTimer.target = subjson->getInt("target");
103  }
104  if (subjson->has("ms")) {
105  _delayedPulseTimer.ms = subjson->getInt("ms");
106  }
107  }
108  if(json_val->has("countdown")) {
109  _countdown = json_val->getLong("countdown");
110  }
111  return YFunction::_parseAttr(json_val);
112 }
113 
114 
124 {
125  Y_STATE_enum res;
127  try {
130  {
132  return YRelay::STATE_INVALID;
133  }
134  }
135  }
136  res = _state;
137  } catch (std::exception) {
139  throw;
140  }
142  return res;
143 }
144 
156 {
157  string rest_val;
158  int res;
160  try {
161  rest_val = (newval>0 ? "1" : "0");
162  res = _setAttr("state", rest_val);
163  } catch (std::exception) {
165  throw;
166  }
168  return res;
169 }
170 
182 {
185  try {
188  {
191  }
192  }
193  }
194  res = _stateAtPowerOn;
195  } catch (std::exception) {
197  throw;
198  }
200  return res;
201 }
202 
215 {
216  string rest_val;
217  int res;
219  try {
220  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
221  res = _setAttr("stateAtPowerOn", rest_val);
222  } catch (std::exception) {
224  throw;
225  }
227  return res;
228 }
229 
239 {
240  s64 res = 0;
242  try {
245  {
248  }
249  }
250  }
251  res = _maxTimeOnStateA;
252  } catch (std::exception) {
254  throw;
255  }
257  return res;
258 }
259 
271 {
272  string rest_val;
273  int res;
275  try {
276  char buf[32]; sprintf(buf, "%u", (u32)newval); rest_val = string(buf);
277  res = _setAttr("maxTimeOnStateA", rest_val);
278  } catch (std::exception) {
280  throw;
281  }
283  return res;
284 }
285 
295 {
296  s64 res = 0;
298  try {
301  {
304  }
305  }
306  }
307  res = _maxTimeOnStateB;
308  } catch (std::exception) {
310  throw;
311  }
313  return res;
314 }
315 
327 {
328  string rest_val;
329  int res;
331  try {
332  char buf[32]; sprintf(buf, "%u", (u32)newval); rest_val = string(buf);
333  res = _setAttr("maxTimeOnStateB", rest_val);
334  } catch (std::exception) {
336  throw;
337  }
339  return res;
340 }
341 
351 {
352  Y_OUTPUT_enum res;
354  try {
357  {
359  return YRelay::OUTPUT_INVALID;
360  }
361  }
362  }
363  res = _output;
364  } catch (std::exception) {
366  throw;
367  }
369  return res;
370 }
371 
383 {
384  string rest_val;
385  int res;
387  try {
388  rest_val = (newval>0 ? "1" : "0");
389  res = _setAttr("output", rest_val);
390  } catch (std::exception) {
392  throw;
393  }
395  return res;
396 }
397 
409 {
410  s64 res = 0;
412  try {
415  {
418  }
419  }
420  }
421  res = _pulseTimer;
422  } catch (std::exception) {
424  throw;
425  }
427  return res;
428 }
429 
430 int YRelay::set_pulseTimer(s64 newval)
431 {
432  string rest_val;
433  int res;
435  try {
436  char buf[32]; sprintf(buf, "%u", (u32)newval); rest_val = string(buf);
437  res = _setAttr("pulseTimer", rest_val);
438  } catch (std::exception) {
440  throw;
441  }
443  return res;
444 }
445 
456 int YRelay::pulse(int ms_duration)
457 {
458  string rest_val;
459  char buf[32]; sprintf(buf, "%u", (u32)ms_duration); rest_val = string(buf);
460  return _setAttr("pulseTimer", rest_val);
461 }
462 
464 {
465  YDelayedPulse res;
467  try {
470  {
473  }
474  }
475  }
476  res = _delayedPulseTimer;
477  } catch (std::exception) {
479  throw;
480  }
482  return res;
483 }
484 
486 {
487  string rest_val;
488  int res;
490  try {
491  char buff[64]; sprintf(buff,"%d:%d",newval.target,newval.ms); rest_val = string(buff);
492  res = _setAttr("delayedPulseTimer", rest_val);
493  } catch (std::exception) {
495  throw;
496  }
498  return res;
499 }
500 
511 int YRelay::delayedPulse(int ms_delay,int ms_duration)
512 {
513  string rest_val;
514  char buff[64]; sprintf(buff,"%d:%d",ms_delay,ms_duration); rest_val = string(buff);
515  return _setAttr("delayedPulseTimer", rest_val);
516 }
517 
528 {
529  s64 res = 0;
531  try {
534  {
537  }
538  }
539  }
540  res = _countdown;
541  } catch (std::exception) {
543  throw;
544  }
546  return res;
547 }
548 
577 {
578  YRelay* obj = NULL;
579  int taken = 0;
580  if (YAPI::_apiInitialized) {
582  taken = 1;
583  }try {
584  obj = (YRelay*) YFunction::_FindFromCache("Relay", func);
585  if (obj == NULL) {
586  obj = new YRelay(func);
587  YFunction::_AddToCache("Relay", func, obj);
588  }
589  } catch (std::exception) {
591  throw;
592  }
594  return obj;
595 }
596 
609 {
610  string val;
611  if (callback != NULL) {
613  } else {
615  }
616  _valueCallbackRelay = callback;
617  // Immediately invoke value callback with current value
618  if (callback != NULL && this->isOnline()) {
619  val = _advertisedValue;
620  if (!(val == "")) {
621  this->_invokeValueCallback(val);
622  }
623  }
624  return 0;
625 }
626 
628 {
629  if (_valueCallbackRelay != NULL) {
630  _valueCallbackRelay(this, value);
631  } else {
633  }
634  return 0;
635 }
636 
638 {
639  string hwid;
640 
641  if(YISERR(_nextFunction(hwid)) || hwid=="") {
642  return NULL;
643  }
644  return YRelay::FindRelay(hwid);
645 }
646 
648 {
649  vector<YFUN_DESCR> v_fundescr;
650  YDEV_DESCR ydevice;
651  string serial, funcId, funcName, funcVal, errmsg;
652 
653  if(YISERR(YapiWrapper::getFunctionsByClass("Relay", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
654  v_fundescr.size() == 0 ||
655  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
656  return NULL;
657  }
658  return YRelay::FindRelay(serial+"."+funcId);
659 }
660 
661 //--- (end of YRelay implementation)
662 
663 //--- (YRelay functions)
664 //--- (end of YRelay functions)
string _advertisedValue
Definition: yocto_api.h:1569
int pulse(int ms_duration)
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
s64 _countdown
Definition: yocto_relay.h:130
int set_output(Y_OUTPUT_enum newval)
s64 get_maxTimeOnStateA(void)
void(* YRelayValueCallback)(YRelay *func, const string &functionValue)
Definition: yocto_relay.h:54
static YRelay * FirstRelay(void)
Y_STATEATPOWERON_enum get_stateAtPowerOn(void)
static const s64 MAXTIMEONSTATEB_INVALID
Definition: yocto_relay.h:155
static const s64 COUNTDOWN_INVALID
Definition: yocto_relay.h:161
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
Y_STATE_enum _state
Definition: yocto_relay.h:123
Y_STATEATPOWERON_enum _stateAtPowerOn
Definition: yocto_relay.h:124
s64 get_countdown(void)
int set_maxTimeOnStateA(s64 newval)
int set_state(Y_STATE_enum newval)
static int DefaultCacheValidity
Definition: yocto_api.h:468
s64 get_pulseTimer(void)
YJSONObject * getYJSONObject(const string &key)
Definition: yocto_api.cpp:744
u64 _cacheExpiration
Definition: yocto_api.h:1571
static YRelay * FindRelay(string func)
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
Y_OUTPUT_enum _output
Definition: yocto_relay.h:127
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
s64 _maxTimeOnStateA
Definition: yocto_relay.h:125
int set_delayedPulseTimer(YDelayedPulse newval)
virtual int _invokeValueCallback(string value)
YRelay * nextRelay(void)
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_relay.cpp:76
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
int getInt(const string &key)
Definition: yocto_api.cpp:779
static const s64 PULSETIMER_INVALID
Definition: yocto_relay.h:159
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 s64 MAXTIMEONSTATEA_INVALID
Definition: yocto_relay.h:154
YRelayValueCallback _valueCallbackRelay
Definition: yocto_relay.h:131
Y_OUTPUT_enum get_output(void)
static const Y_STATEATPOWERON_enum STATEATPOWERON_INVALID
Definition: yocto_relay.h:153
s64 _pulseTimer
Definition: yocto_relay.h:128
static const YDelayedPulse DELAYEDPULSETIMER_INVALID
Definition: yocto_relay.h:160
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
int set_maxTimeOnStateB(s64 newval)
Y_OUTPUT_enum
Definition: yocto_relay.h:74
void yEnterCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:647
static yCRITICAL_SECTION _global_cs
Definition: yocto_api.h:476
static const Y_STATE_enum STATE_INVALID
Definition: yocto_relay.h:149
bool isOnline(void)
Definition: yocto_api.cpp:3291
Y_STATE_enum get_state(void)
YDelayedPulse _delayedPulseTimer
Definition: yocto_relay.h:129
YRelay(const string &func)
Definition: yocto_relay.cpp:51
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
static const Y_OUTPUT_enum OUTPUT_INVALID
Definition: yocto_relay.h:158
Y_STATEATPOWERON_enum
Definition: yocto_relay.h:65
string _className
Definition: yocto_api.h:1558
s64 getLong(const string &key)
Definition: yocto_api.cpp:790
s64 _maxTimeOnStateB
Definition: yocto_relay.h:126
Y_STATE_enum
Definition: yocto_relay.h:57
s64 get_maxTimeOnStateB(void)
int set_stateAtPowerOn(Y_STATEATPOWERON_enum newval)
static bool _apiInitialized
Definition: yocto_api.h:475
YDelayedPulse get_delayedPulseTimer(void)
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
int delayedPulse(int ms_delay, int ms_duration)
virtual int registerValueCallback(YRelayValueCallback callback)
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
int set_pulseTimer(s64 newval)


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