yocto_files.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * $Id: yocto_files.cpp 28753 2017-10-03 11:23:38Z seb $
4  *
5  * Implements yFindFiles(), the high-level API for Files 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_files.h"
43 #include "yapi/yjson.h"
44 #include "yapi/yapi.h"
45 #include <string.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #define __FILE_ID__ "files"
49 
50 
51 YFileRecord::YFileRecord(const string& json):
52 //--- (generated code: YFileRecord initialization)
53  _size(0)
54  ,_crc(0)
55 //--- (end of generated code: YFileRecord initialization)
56 {
58 
59  // Parse JSON data
60  j.src = json.c_str();
61  j.end = j.src + strlen(j.src);
62  j.st = YJSON_START;
64  return ;
65  }
66  while(yJsonParse(&j) == YJSON_PARSE_AVAIL && j.st == YJSON_PARSE_MEMBNAME) {
67  if (!strcmp(j.token, "name")) {
68  if (yJsonParse(&j) != YJSON_PARSE_AVAIL) {
69  return ;
70  }
71  _name = (string)j.token;
73  _name =(string)j.token;
74  }
75  } else if(!strcmp(j.token, "crc")) {
76  if (yJsonParse(&j) != YJSON_PARSE_AVAIL) {
77  return;
78  }
79  _crc = atoi(j.token);;
80  } else if(!strcmp(j.token, "size")) {
81  if (yJsonParse(&j) != YJSON_PARSE_AVAIL) {
82  return;
83  }
84  _size = atoi(j.token);;
85  } else {
86  yJsonSkip(&j, 1);
87  }
88  }
89 }
90 
91 //--- (generated code: YFileRecord implementation)
92 // static attributes
93 
94 
96 {
97  return _name;
98 }
99 
101 {
102  return _size;
103 }
104 
106 {
107  return _crc;
108 }
109 //--- (end of generated code: YFileRecord implementation)
110 
111 
112 
113 
114 
115 YFiles::YFiles(const string& func): YFunction(func)
116  //--- (generated code: YFiles initialization)
117  ,_filesCount(FILESCOUNT_INVALID)
118  ,_freeSpace(FREESPACE_INVALID)
119  ,_valueCallbackFiles(NULL)
120 //--- (end of generated code: YFiles initialization)
121 {
122  _className = "Files";
123 }
124 
126 {
127  //--- (generated code: YFiles cleanup)
128 //--- (end of generated code: YFiles cleanup)
129 }
130 
131 //--- (generated code: YFiles implementation)
132 // static attributes
133 
135 {
136  if(json_val->has("filesCount")) {
137  _filesCount = json_val->getInt("filesCount");
138  }
139  if(json_val->has("freeSpace")) {
140  _freeSpace = json_val->getInt("freeSpace");
141  }
142  return YFunction::_parseAttr(json_val);
143 }
144 
145 
154 {
155  int res = 0;
157  try {
160  {
163  }
164  }
165  }
166  res = _filesCount;
167  } catch (std::exception) {
169  throw;
170  }
172  return res;
173 }
174 
183 {
184  int res = 0;
186  try {
189  {
192  }
193  }
194  }
195  res = _freeSpace;
196  } catch (std::exception) {
198  throw;
199  }
201  return res;
202 }
203 
232 {
233  YFiles* obj = NULL;
234  int taken = 0;
235  if (YAPI::_apiInitialized) {
237  taken = 1;
238  }try {
239  obj = (YFiles*) YFunction::_FindFromCache("Files", func);
240  if (obj == NULL) {
241  obj = new YFiles(func);
242  YFunction::_AddToCache("Files", func, obj);
243  }
244  } catch (std::exception) {
246  throw;
247  }
249  return obj;
250 }
251 
264 {
265  string val;
266  if (callback != NULL) {
268  } else {
270  }
271  _valueCallbackFiles = callback;
272  // Immediately invoke value callback with current value
273  if (callback != NULL && this->isOnline()) {
274  val = _advertisedValue;
275  if (!(val == "")) {
276  this->_invokeValueCallback(val);
277  }
278  }
279  return 0;
280 }
281 
283 {
284  if (_valueCallbackFiles != NULL) {
285  _valueCallbackFiles(this, value);
286  } else {
288  }
289  return 0;
290 }
291 
292 string YFiles::sendCommand(string command)
293 {
294  string url;
295  url = YapiWrapper::ysprintf("files.json?a=%s",command.c_str());
296 
297  return this->_download(url);
298 }
299 
309 {
310  string json;
311  string res;
312  json = this->sendCommand("format");
313  res = this->_json_get_key(json, "res");
314  if (!(res == "ok")) {
315  _throw(YAPI_IO_ERROR,"format failed");
316  return YAPI_IO_ERROR;
317  }
318  return YAPI_SUCCESS;
319 }
320 
334 vector<YFileRecord> YFiles::get_list(string pattern)
335 {
336  string json;
337  vector<string> filelist;
338  vector<YFileRecord> res;
339  json = this->sendCommand(YapiWrapper::ysprintf("dir&f=%s",pattern.c_str()));
340  filelist = this->_json_get_array(json);
341  res.clear();
342  for (unsigned ii = 0; ii < filelist.size(); ii++) {
343  res.push_back(YFileRecord(filelist[ii]));
344  }
345  return res;
346 }
347 
357 bool YFiles::fileExist(string filename)
358 {
359  string json;
360  vector<string> filelist;
361  if ((int)(filename).length() == 0) {
362  return false;
363  }
364  json = this->sendCommand(YapiWrapper::ysprintf("dir&f=%s",filename.c_str()));
365  filelist = this->_json_get_array(json);
366  if ((int)filelist.size() > 0) {
367  return true;
368  }
369  return false;
370 }
371 
381 string YFiles::download(string pathname)
382 {
383  return this->_download(pathname);
384 }
385 
397 int YFiles::upload(string pathname,string content)
398 {
399  return this->_upload(pathname, content);
400 }
401 
416 int YFiles::remove(string pathname)
417 {
418  string json;
419  string res;
420  json = this->sendCommand(YapiWrapper::ysprintf("del&f=%s",pathname.c_str()));
421  res = this->_json_get_key(json, "res");
422  if (!(res == "ok")) {
423  _throw(YAPI_IO_ERROR,"unable to remove file");
424  return YAPI_IO_ERROR;
425  }
426  return YAPI_SUCCESS;
427 }
428 
430 {
431  string hwid;
432 
433  if(YISERR(_nextFunction(hwid)) || hwid=="") {
434  return NULL;
435  }
436  return YFiles::FindFiles(hwid);
437 }
438 
440 {
441  vector<YFUN_DESCR> v_fundescr;
442  YDEV_DESCR ydevice;
443  string serial, funcId, funcName, funcVal, errmsg;
444 
445  if(YISERR(YapiWrapper::getFunctionsByClass("Files", 0, v_fundescr, sizeof(YFUN_DESCR), errmsg)) ||
446  v_fundescr.size() == 0 ||
447  YISERR(YapiWrapper::getFunctionInfo(v_fundescr[0], ydevice, serial, funcId, funcName, funcVal, errmsg))) {
448  return NULL;
449  }
450  return YFiles::FindFiles(serial+"."+funcId);
451 }
452 
453 //--- (end of generated code: YFiles implementation)
454 
455 //--- (generated code: YFiles functions)
456 //--- (end of generated code: YFiles functions)
string _json_get_key(const string &json, const string &data)
Definition: yocto_api.cpp:2772
string _advertisedValue
Definition: yocto_api.h:1569
yCRITICAL_SECTION _this_cs
Definition: yocto_api.h:1563
int get_freeSpace(void)
YFiles * nextFiles(void)
YRETCODE _load_unsafe(int msValidity)
Definition: yocto_api.cpp:3327
YFileRecord(const string &json)
Definition: yocto_files.cpp:51
virtual bool fileExist(string filename)
char token[62]
Definition: yjson.h:88
void(* YFilesValueCallback)(YFiles *func, const string &functionValue)
Definition: yocto_files.h:52
YRETCODE _upload(const string &path, const string &content)
Definition: yocto_api.cpp:3098
static int DefaultCacheValidity
Definition: yocto_api.h:468
int _freeSpace
Definition: yocto_files.h:123
u64 _cacheExpiration
Definition: yocto_api.h:1571
void _throw(YRETCODE errType, string errMsg)
Definition: yocto_api.cpp:2656
yJsonState next
Definition: yjson.h:85
yJsonRetCode yJsonParse(yJsonStateMachine *j)
Definition: yjson.c:83
_FAR const char * src
Definition: yjson.h:82
void yLeaveCriticalSection(yCRITICAL_SECTION *cs)
Definition: ythread.c:672
int get_filesCount(void)
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 string download(string pathname)
string _name
Definition: yocto_files.h:75
void yJsonSkip(yJsonStateMachine *j, int nitems)
Definition: yjson.c:367
YFiles(const string &func)
int _filesCount
Definition: yocto_files.h:122
virtual int _parseAttr(YJSONObject *json_val)
Definition: yocto_api.cpp:2367
static YFiles * FindFiles(string func)
_FAR const char * end
Definition: yjson.h:83
virtual int remove(string pathname)
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
virtual int format_fs(void)
virtual int get_crc(void)
static const int FREESPACE_INVALID
Definition: yocto_files.h:144
YAPI_DEVICE YDEV_DESCR
Definition: yocto_api.h:229
static YFunction * _FindFromCache(const string &classname, const string &func)
Definition: yocto_api.cpp:2338
yJsonState st
Definition: yjson.h:84
vector< string > _json_get_array(const string &json)
Definition: yocto_api.cpp:2811
virtual string get_name(void)
Definition: yocto_files.cpp:95
static string ysprintf(const char *fmt,...)
Definition: yocto_api.cpp:4997
virtual vector< YFileRecord > get_list(string pattern)
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 _invokeValueCallback(string value)
static const int FILESCOUNT_INVALID
Definition: yocto_files.h:143
static int getFunctionsByClass(const string &class_str, YFUN_DESCR prevfundesc, vector< YFUN_DESCR > &buffer, int maxsize, string &errmsg)
Definition: yocto_api.cpp:4852
string _className
Definition: yocto_api.h:1558
virtual int get_size(void)
static YFiles * FirstFiles(void)
static bool _apiInitialized
Definition: yocto_api.h:475
YRETCODE _nextFunction(string &hwId)
Definition: yocto_api.cpp:2709
virtual int _parseAttr(YJSONObject *json_val)
static void _UpdateValueCallbackList(YFunction *func, bool add)
Definition: yocto_api.cpp:3509
#define YISERR(retcode)
Definition: ydef.h:394
virtual int upload(string pathname, string content)
virtual string sendCommand(string command)
bool has(const string &key)
Definition: yocto_api.cpp:735
virtual int registerValueCallback(YFilesValueCallback callback)
YFilesValueCallback _valueCallbackFiles
Definition: yocto_files.h:124
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