yocto_gps.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_gps.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindGps(), the high-level API for Gps 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_gps.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__ "gps"
50 
51 YGps::YGps(const string& func): YFunction(func)
52 //--- (YGps initialization)
53  ,_isFixed(ISFIXED_INVALID)
54  ,_satCount(SATCOUNT_INVALID)
55  ,_coordSystem(COORDSYSTEM_INVALID)
56  ,_latitude(LATITUDE_INVALID)
57  ,_longitude(LONGITUDE_INVALID)
58  ,_dilution(DILUTION_INVALID)
59  ,_altitude(ALTITUDE_INVALID)
60  ,_groundSpeed(GROUNDSPEED_INVALID)
61  ,_direction(DIRECTION_INVALID)
62  ,_unixTime(UNIXTIME_INVALID)
63  ,_dateTime(DATETIME_INVALID)
64  ,_utcOffset(UTCOFFSET_INVALID)
65  ,_command(COMMAND_INVALID)
66  ,_valueCallbackGps(NULL)
67 //--- (end of YGps initialization)
68 {
69  _className="Gps";
70 }
71 
73 {
74 //--- (YGps cleanup)
75 //--- (end of YGps cleanup)
76 }
77 //--- (YGps implementation)
78 // static attributes
87 
89 {
90  if(json_val->has("isFixed")) {
91  _isFixed = (Y_ISFIXED_enum)json_val->getInt("isFixed");
92  }
93  if(json_val->has("satCount")) {
94  _satCount = json_val->getLong("satCount");
95  }
96  if(json_val->has("coordSystem")) {
97  _coordSystem = (Y_COORDSYSTEM_enum)json_val->getInt("coordSystem");
98  }
99  if(json_val->has("latitude")) {
100  _latitude = json_val->getString("latitude");
101  }
102  if(json_val->has("longitude")) {
103  _longitude = json_val->getString("longitude");
104  }
105  if(json_val->has("dilution")) {
106  _dilution = floor(json_val->getDouble("dilution") * 1000.0 / 65536.0 + 0.5) / 1000.0;
107  }
108  if(json_val->has("altitude")) {
109  _altitude = floor(json_val->getDouble("altitude") * 1000.0 / 65536.0 + 0.5) / 1000.0;
110  }
111  if(json_val->has("groundSpeed")) {
112  _groundSpeed = floor(json_val->getDouble("groundSpeed") * 1000.0 / 65536.0 + 0.5) / 1000.0;
113  }
114  if(json_val->has("direction")) {
115  _direction = floor(json_val->getDouble("direction") * 1000.0 / 65536.0 + 0.5) / 1000.0;
116  }
117  if(json_val->has("unixTime")) {
118  _unixTime = json_val->getLong("unixTime");
119  }
120  if(json_val->has("dateTime")) {
121  _dateTime = json_val->getString("dateTime");
122  }
123  if(json_val->has("utcOffset")) {
124  _utcOffset = json_val->getInt("utcOffset");
125  }
126  if(json_val->has("command")) {
127  _command = json_val->getString("command");
128  }
129  return YFunction::_parseAttr(json_val);
130 }
131 
132 
142 {
143  Y_ISFIXED_enum res;
145  try {
148  {
150  return YGps::ISFIXED_INVALID;
151  }
152  }
153  }
154  res = _isFixed;
155  } catch (std::exception) {
157  throw;
158  }
160  return res;
161 }
162 
171 {
172  s64 res = 0;
174  try {
177  {
179  return YGps::SATCOUNT_INVALID;
180  }
181  }
182  }
183  res = _satCount;
184  } catch (std::exception) {
186  throw;
187  }
189  return res;
190 }
191 
201 {
202  Y_COORDSYSTEM_enum res;
204  try {
207  {
210  }
211  }
212  }
213  res = _coordSystem;
214  } catch (std::exception) {
216  throw;
217  }
219  return res;
220 }
221 
233 {
234  string rest_val;
235  int res;
237  try {
238  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
239  res = _setAttr("coordSystem", rest_val);
240  } catch (std::exception) {
242  throw;
243  }
245  return res;
246 }
247 
255 string YGps::get_latitude(void)
256 {
257  string res;
259  try {
262  {
264  return YGps::LATITUDE_INVALID;
265  }
266  }
267  }
268  res = _latitude;
269  } catch (std::exception) {
271  throw;
272  }
274  return res;
275 }
276 
285 {
286  string res;
288  try {
291  {
294  }
295  }
296  }
297  res = _longitude;
298  } catch (std::exception) {
300  throw;
301  }
303  return res;
304 }
305 
315 double YGps::get_dilution(void)
316 {
317  double res = 0.0;
319  try {
322  {
324  return YGps::DILUTION_INVALID;
325  }
326  }
327  }
328  res = _dilution;
329  } catch (std::exception) {
331  throw;
332  }
334  return res;
335 }
336 
345 double YGps::get_altitude(void)
346 {
347  double res = 0.0;
349  try {
352  {
354  return YGps::ALTITUDE_INVALID;
355  }
356  }
357  }
358  res = _altitude;
359  } catch (std::exception) {
361  throw;
362  }
364  return res;
365 }
366 
375 {
376  double res = 0.0;
378  try {
381  {
384  }
385  }
386  }
387  res = _groundSpeed;
388  } catch (std::exception) {
390  throw;
391  }
393  return res;
394 }
395 
406 {
407  double res = 0.0;
409  try {
412  {
415  }
416  }
417  }
418  res = _direction;
419  } catch (std::exception) {
421  throw;
422  }
424  return res;
425 }
426 
437 {
438  s64 res = 0;
440  try {
443  {
445  return YGps::UNIXTIME_INVALID;
446  }
447  }
448  }
449  res = _unixTime;
450  } catch (std::exception) {
452  throw;
453  }
455  return res;
456 }
457 
465 string YGps::get_dateTime(void)
466 {
467  string res;
469  try {
472  {
474  return YGps::DATETIME_INVALID;
475  }
476  }
477  }
478  res = _dateTime;
479  } catch (std::exception) {
481  throw;
482  }
484  return res;
485 }
486 
495 {
496  int res = 0;
498  try {
501  {
504  }
505  }
506  }
507  res = _utcOffset;
508  } catch (std::exception) {
510  throw;
511  }
513  return res;
514 }
515 
527 int YGps::set_utcOffset(int newval)
528 {
529  string rest_val;
530  int res;
532  try {
533  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
534  res = _setAttr("utcOffset", rest_val);
535  } catch (std::exception) {
537  throw;
538  }
540  return res;
541 }
542 
543 string YGps::get_command(void)
544 {
545  string res;
547  try {
550  {
552  return YGps::COMMAND_INVALID;
553  }
554  }
555  }
556  res = _command;
557  } catch (std::exception) {
559  throw;
560  }
562  return res;
563 }
564 
565 int YGps::set_command(const string& newval)
566 {
567  string rest_val;
568  int res;
570  try {
571  rest_val = newval;
572  res = _setAttr("command", rest_val);
573  } catch (std::exception) {
575  throw;
576  }
578  return res;
579 }
580 
608 YGps* YGps::FindGps(string func)
609 {
610  YGps* obj = NULL;
611  int taken = 0;
612  if (YAPI::_apiInitialized) {
614  taken = 1;
615  }try {
616  obj = (YGps*) YFunction::_FindFromCache("Gps", func);
617  if (obj == NULL) {
618  obj = new YGps(func);
619  YFunction::_AddToCache("Gps", func, obj);
620  }
621  } catch (std::exception) {
623  throw;
624  }
626  return obj;
627 }
628 
641 {
642  string val;
643  if (callback != NULL) {
645  } else {
647  }
648  _valueCallbackGps = callback;
649  // Immediately invoke value callback with current value
650  if (callback != NULL && this->isOnline()) {
651  val = _advertisedValue;
652  if (!(val == "")) {
653  this->_invokeValueCallback(val);
654  }
655  }
656  return 0;
657 }
658 
659 int YGps::_invokeValueCallback(string value)
660 {
661  if (_valueCallbackGps != NULL) {
662  _valueCallbackGps(this, value);
663  } else {
665  }
666  return 0;
667 }
668 
670 {
671  string hwid;
672 
673  if(YISERR(_nextFunction(hwid)) || hwid=="") {
674  return NULL;
675  }
676  return YGps::FindGps(hwid);
677 }
678 
680 {
681  vector<YFUN_DESCR> v_fundescr;
682  YDEV_DESCR ydevice;
683  string serial, funcId, funcName, funcVal, errmsg;
684 
685  if(YISERR(YapiWrapper::getFunctionsByClass("Gps", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
686  v_fundescr.size() == 0 ||
687  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
688  return NULL;
689  }
690  return YGps::FindGps(serial+"."+funcId);
691 }
692 
693 //--- (end of YGps implementation)
694 
695 //--- (YGps functions)
696 //--- (end of YGps functions)
double _groundSpeed
Definition: yocto_gps.h:110
static const Y_ISFIXED_enum ISFIXED_INVALID
Definition: yocto_gps.h:134
static const string COMMAND_INVALID
Definition: yocto_gps.h:149
string _advertisedValue
Definition: yocto_api.h:1569
double getDouble(const string &key)
Definition: yocto_api.cpp:796
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
static const double DIRECTION_INVALID
Definition: yocto_gps.h:145
static const string DATETIME_INVALID
Definition: yocto_gps.h:147
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_gps.cpp:88
string _command
Definition: yocto_gps.h:115
static const string LATITUDE_INVALID
Definition: yocto_gps.h:140
virtual int _invokeValueCallback(string value)
Definition: yocto_gps.cpp:659
string get_longitude(void)
Definition: yocto_gps.cpp:284
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
Y_COORDSYSTEM_enum _coordSystem
Definition: yocto_gps.h:105
YGps(const string &func)
Definition: yocto_gps.cpp:51
double get_dilution(void)
Definition: yocto_gps.cpp:315
void(* YGpsValueCallback)(YGps *func, const string &functionValue)
Definition: yocto_gps.h:54
YGpsValueCallback _valueCallbackGps
Definition: yocto_gps.h:116
static YGps * FirstGps(void)
Definition: yocto_gps.cpp:679
static int DefaultCacheValidity
Definition: yocto_api.h:468
string getString(const string &key)
Definition: yocto_api.cpp:773
double _dilution
Definition: yocto_gps.h:108
Y_COORDSYSTEM_enum get_coordSystem(void)
Definition: yocto_gps.cpp:200
u64 _cacheExpiration
Definition: yocto_api.h:1571
int get_utcOffset(void)
Definition: yocto_gps.cpp:494
int set_utcOffset(int newval)
Definition: yocto_gps.cpp:527
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
string _longitude
Definition: yocto_gps.h:107
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 _satCount
Definition: yocto_gps.h:104
static const int UTCOFFSET_INVALID
Definition: yocto_gps.h:148
static const s64 SATCOUNT_INVALID
Definition: yocto_gps.h:135
s64 get_unixTime(void)
Definition: yocto_gps.cpp:436
static const string LONGITUDE_INVALID
Definition: yocto_gps.h:141
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
s64 get_satCount(void)
Definition: yocto_gps.cpp:170
double get_altitude(void)
Definition: yocto_gps.cpp:345
string get_command(void)
Definition: yocto_gps.cpp:543
Y_ISFIXED_enum
Definition: yocto_gps.h:57
Y_COORDSYSTEM_enum
Definition: yocto_gps.h:65
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
int getInt(const string &key)
Definition: yocto_api.cpp:779
double _altitude
Definition: yocto_gps.h:109
static u64 GetTickCount(void)
Definition: yocto_api.cpp:4755
Y_ISFIXED_enum get_isFixed(void)
Definition: yocto_gps.cpp:141
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
double _direction
Definition: yocto_gps.h:111
s64 _unixTime
Definition: yocto_gps.h:112
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
static const double DILUTION_INVALID
Definition: yocto_gps.h:142
Y_ISFIXED_enum _isFixed
Definition: yocto_gps.h:103
double get_direction(void)
Definition: yocto_gps.cpp:405
Definition: yocto_gps.h:95
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 double GROUNDSPEED_INVALID
Definition: yocto_gps.h:144
string _className
Definition: yocto_api.h:1558
s64 getLong(const string &key)
Definition: yocto_api.cpp:790
static const s64 UNIXTIME_INVALID
Definition: yocto_gps.h:146
YGps * nextGps(void)
Definition: yocto_gps.cpp:669
~YGps()
Definition: yocto_gps.cpp:72
string get_latitude(void)
Definition: yocto_gps.cpp:255
string get_dateTime(void)
Definition: yocto_gps.cpp:465
string _dateTime
Definition: yocto_gps.h:113
int set_coordSystem(Y_COORDSYSTEM_enum newval)
Definition: yocto_gps.cpp:232
static const double ALTITUDE_INVALID
Definition: yocto_gps.h:143
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
static const Y_COORDSYSTEM_enum COORDSYSTEM_INVALID
Definition: yocto_gps.h:139
static YGps * FindGps(string func)
Definition: yocto_gps.cpp:608
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
int set_command(const string &newval)
Definition: yocto_gps.cpp:565
bool has(const string &key)
Definition: yocto_api.cpp:735
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
string _latitude
Definition: yocto_gps.h:106
int _utcOffset
Definition: yocto_gps.h:114
virtual int registerValueCallback(YGpsValueCallback callback)
Definition: yocto_gps.cpp:640
double get_groundSpeed(void)
Definition: yocto_gps.cpp:374
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