yocto_temperature.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_temperature.cpp 28748 2017-10-03 08:23:39Z seb $
4  *
5  * Implements yFindTemperature(), the high-level API for Temperature 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_temperature.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__ "temperature"
50 
51 YTemperature::YTemperature(const string& func): YSensor(func)
52 //--- (YTemperature initialization)
53  ,_sensorType(SENSORTYPE_INVALID)
54  ,_signalValue(SIGNALVALUE_INVALID)
55  ,_signalUnit(SIGNALUNIT_INVALID)
56  ,_command(COMMAND_INVALID)
57  ,_valueCallbackTemperature(NULL)
58  ,_timedReportCallbackTemperature(NULL)
59 //--- (end of YTemperature initialization)
60 {
61  _className="Temperature";
62 }
63 
65 {
66 //--- (YTemperature cleanup)
67 //--- (end of YTemperature cleanup)
68 }
69 //--- (YTemperature implementation)
70 // static attributes
74 
76 {
77  if(json_val->has("sensorType")) {
78  _sensorType = (Y_SENSORTYPE_enum)json_val->getInt("sensorType");
79  }
80  if(json_val->has("signalValue")) {
81  _signalValue = floor(json_val->getDouble("signalValue") * 1000.0 / 65536.0 + 0.5) / 1000.0;
82  }
83  if(json_val->has("signalUnit")) {
84  _signalUnit = json_val->getString("signalUnit");
85  }
86  if(json_val->has("command")) {
87  _command = json_val->getString("command");
88  }
89  return YSensor::_parseAttr(json_val);
90 }
91 
92 
110 int YTemperature::set_unit(const string& newval)
111 {
112  string rest_val;
113  int res;
115  try {
116  rest_val = newval;
117  res = _setAttr("unit", rest_val);
118  } catch (std::exception) {
120  throw;
121  }
123  return res;
124 }
125 
138 {
139  Y_SENSORTYPE_enum res;
141  try {
144  {
147  }
148  }
149  }
150  res = _sensorType;
151  } catch (std::exception) {
153  throw;
154  }
156  return res;
157 }
158 
177 {
178  string rest_val;
179  int res;
181  try {
182  char buf[32]; sprintf(buf, "%d", newval); rest_val = string(buf);
183  res = _setAttr("sensorType", rest_val);
184  } catch (std::exception) {
186  throw;
187  }
189  return res;
190 }
191 
201 {
202  double res = 0.0;
204  try {
207  {
210  }
211  }
212  }
213  res = floor(_signalValue * 1000+0.5) / 1000;
214  } catch (std::exception) {
216  throw;
217  }
219  return res;
220 }
221 
230 {
231  string res;
233  try {
234  if (_cacheExpiration == 0) {
236  {
239  }
240  }
241  }
242  res = _signalUnit;
243  } catch (std::exception) {
245  throw;
246  }
248  return res;
249 }
250 
252 {
253  string res;
255  try {
258  {
261  }
262  }
263  }
264  res = _command;
265  } catch (std::exception) {
267  throw;
268  }
270  return res;
271 }
272 
273 int YTemperature::set_command(const string& newval)
274 {
275  string rest_val;
276  int res;
278  try {
279  rest_val = newval;
280  res = _setAttr("command", rest_val);
281  } catch (std::exception) {
283  throw;
284  }
286  return res;
287 }
288 
317 {
318  YTemperature* obj = NULL;
319  int taken = 0;
320  if (YAPI::_apiInitialized) {
322  taken = 1;
323  }try {
324  obj = (YTemperature*) YFunction::_FindFromCache("Temperature", func);
325  if (obj == NULL) {
326  obj = new YTemperature(func);
327  YFunction::_AddToCache("Temperature", func, obj);
328  }
329  } catch (std::exception) {
331  throw;
332  }
334  return obj;
335 }
336 
349 {
350  string val;
351  if (callback != NULL) {
353  } else {
355  }
356  _valueCallbackTemperature = callback;
357  // Immediately invoke value callback with current value
358  if (callback != NULL && this->isOnline()) {
359  val = _advertisedValue;
360  if (!(val == "")) {
361  this->_invokeValueCallback(val);
362  }
363  }
364  return 0;
365 }
366 
368 {
369  if (_valueCallbackTemperature != NULL) {
370  _valueCallbackTemperature(this, value);
371  } else {
373  }
374  return 0;
375 }
376 
389 {
390  YSensor* sensor = NULL;
391  sensor = this;
392  if (callback != NULL) {
394  } else {
396  }
398  return 0;
399 }
400 
402 {
403  if (_timedReportCallbackTemperature != NULL) {
404  _timedReportCallbackTemperature(this, value);
405  } else {
407  }
408  return 0;
409 }
410 
424 int YTemperature::set_ntcParameters(double res25,double beta)
425 {
426  double t0 = 0.0;
427  double t1 = 0.0;
428  double res100 = 0.0;
429  vector<double> tempValues;
430  vector<double> resValues;
431  t0 = 25.0+275.15;
432  t1 = 100.0+275.15;
433  res100 = res25 * exp(beta*(1.0/t1 - 1.0/t0));
434  tempValues.clear();
435  resValues.clear();
436  tempValues.push_back(25.0);
437  resValues.push_back(res25);
438  tempValues.push_back(100.0);
439  resValues.push_back(res100);
440  return this->set_thermistorResponseTable(tempValues, resValues);
441 }
442 
459 int YTemperature::set_thermistorResponseTable(vector<double> tempValues,vector<double> resValues)
460 {
461  int siz = 0;
462  int res = 0;
463  int idx = 0;
464  int found = 0;
465  double prev = 0.0;
466  double curr = 0.0;
467  double currTemp = 0.0;
468  double idxres = 0.0;
469  siz = (int)tempValues.size();
470  if (!(siz >= 2)) {
471  _throw(YAPI_INVALID_ARGUMENT,"thermistor response table must have at least two points");
472  return YAPI_INVALID_ARGUMENT;
473  }
474  if (!(siz == (int)resValues.size())) {
475  _throw(YAPI_INVALID_ARGUMENT,"table sizes mismatch");
476  return YAPI_INVALID_ARGUMENT;
477  }
478 
479  res = this->set_command("Z");
480  if (!(res==YAPI_SUCCESS)) {
481  _throw(YAPI_IO_ERROR,"unable to reset thermistor parameters");
482  return YAPI_IO_ERROR;
483  }
484  // add records in growing resistance value
485  found = 1;
486  prev = 0.0;
487  while (found > 0) {
488  found = 0;
489  curr = 99999999.0;
490  currTemp = -999999.0;
491  idx = 0;
492  while (idx < siz) {
493  idxres = resValues[idx];
494  if ((idxres > prev) && (idxres < curr)) {
495  curr = idxres;
496  currTemp = tempValues[idx];
497  found = 1;
498  }
499  idx = idx + 1;
500  }
501  if (found > 0) {
502  res = this->set_command(YapiWrapper::ysprintf("m%d:%d", (int) floor(1000*curr+0.5),(int) floor(1000*currTemp+0.5)));
503  if (!(res==YAPI_SUCCESS)) {
504  _throw(YAPI_IO_ERROR,"unable to reset thermistor parameters");
505  return YAPI_IO_ERROR;
506  }
507  prev = curr;
508  }
509  }
510  return YAPI_SUCCESS;
511 }
512 
529 int YTemperature::loadThermistorResponseTable(vector<double>& tempValues,vector<double>& resValues)
530 {
531  string id;
532  string bin_json;
533  vector<string> paramlist;
534  vector<double> templist;
535  int siz = 0;
536  int idx = 0;
537  double temp = 0.0;
538  int found = 0;
539  double prev = 0.0;
540  double curr = 0.0;
541  double currRes = 0.0;
542  tempValues.clear();
543  resValues.clear();
544 
545  id = this->get_functionId();
546  id = (id).substr( 11, (int)(id).length() - 11);
547  bin_json = this->_download(YapiWrapper::ysprintf("extra.json?page=%s",id.c_str()));
548  paramlist = this->_json_get_array(bin_json);
549  // first convert all temperatures to float
550  siz = (((int)paramlist.size()) >> (1));
551  templist.clear();
552  idx = 0;
553  while (idx < siz) {
554  temp = atof((paramlist[2*idx+1]).c_str())/1000.0;
555  templist.push_back(temp);
556  idx = idx + 1;
557  }
558  // then add records in growing temperature value
559  tempValues.clear();
560  resValues.clear();
561  found = 1;
562  prev = -999999.0;
563  while (found > 0) {
564  found = 0;
565  curr = 999999.0;
566  currRes = -999999.0;
567  idx = 0;
568  while (idx < siz) {
569  temp = templist[idx];
570  if ((temp > prev) && (temp < curr)) {
571  curr = temp;
572  currRes = atof((paramlist[2*idx]).c_str())/1000.0;
573  found = 1;
574  }
575  idx = idx + 1;
576  }
577  if (found > 0) {
578  tempValues.push_back(curr);
579  resValues.push_back(currRes);
580  prev = curr;
581  }
582  }
583  return YAPI_SUCCESS;
584 }
585 
587 {
588  string hwid;
589 
590  if(YISERR(_nextFunction(hwid)) || hwid=="") {
591  return NULL;
592  }
593  return YTemperature::FindTemperature(hwid);
594 }
595 
597 {
598  vector<YFUN_DESCR> v_fundescr;
599  YDEV_DESCR ydevice;
600  string serial, funcId, funcName, funcVal, errmsg;
601 
602  if(YISERR(YapiWrapper::getFunctionsByClass("Temperature", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
603  v_fundescr.size() == 0 ||
604  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
605  return NULL;
606  }
607  return YTemperature::FindTemperature(serial+"."+funcId);
608 }
609 
610 //--- (end of YTemperature implementation)
611 
612 //--- (YTemperature functions)
613 //--- (end of YTemperature functions)
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 void _UpdateTimedReportCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3531
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
virtual int _invokeTimedReportCallback(YMeasure value)
virtual int set_thermistorResponseTable(vector< double > tempValues, vector< double > resValues)
virtual int registerTimedReportCallback(YTemperatureTimedReportCallback callback)
string get_signalUnit(void)
static int DefaultCacheValidity
Definition: yocto_api.h:468
Y_SENSORTYPE_enum
string getString(const string &key)
Definition: yocto_api.cpp:773
u64 _cacheExpiration
Definition: yocto_api.h:1571
void _throw(YRETCODE errType, string errMsg)
Definition: yocto_api.cpp:2656
int set_sensorType(Y_SENSORTYPE_enum newval)
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
virtual int loadThermistorResponseTable(vector< double > &tempValues, vector< double > &resValues)
string _download(const string &url)
Definition: yocto_api.cpp:3053
static YRETCODE getFunctionInfo(YFUN_DESCR fundesc, YDEV_DESCR &devdescr, string &serial, string &funcId, string &funcName, string &funcVal, string &errmsg)
Definition: yocto_api.cpp:4930
virtual int _parseAttr(YJSONObject *json_val)
YTemperature(const string &func)
YTemperatureTimedReportCallback _timedReportCallbackTemperature
int set_command(const string &newval)
#define YAPI_INVALID_STRING
Definition: yocto_api.h:69
string get_functionId(void)
Definition: yocto_api.cpp:3229
YAPI_FUNCTION YFUN_DESCR
Definition: yocto_api.h:230
int getInt(const string &key)
Definition: yocto_api.cpp:779
YTemperatureValueCallback _valueCallbackTemperature
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
YTemperature * nextTemperature(void)
double get_signalValue(void)
virtual int _invokeValueCallback(string value)
Definition: yocto_api.cpp:7645
static const double SIGNALVALUE_INVALID
static YTemperature * FindTemperature(string func)
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
void(* YTemperatureValueCallback)(YTemperature *func, const string &functionValue)
vector< string > _json_get_array(const string &json)
Definition: yocto_api.cpp:2811
Y_SENSORTYPE_enum _sensorType
static string ysprintf(const char *fmt,...)
Definition: yocto_api.cpp:4997
int set_unit(const string &newval)
virtual int _invokeValueCallback(string 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
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
virtual int set_ntcParameters(double res25, double beta)
virtual int registerValueCallback(YTemperatureValueCallback callback)
string _className
Definition: yocto_api.h:1558
Y_SENSORTYPE_enum get_sensorType(void)
static const string SIGNALUNIT_INVALID
static YTemperature * FirstTemperature(void)
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
string get_command(void)
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
static const Y_SENSORTYPE_enum SENSORTYPE_INVALID
bool has(const string &key)
Definition: yocto_api.cpp:735
void(* YTemperatureTimedReportCallback)(YTemperature *func, YMeasure measure)
YRETCODE _setAttr(string attrname, string newvalue)
Definition: yocto_api.cpp:2968
static const string COMMAND_INVALID


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