// Get the whole REST API string for a device, from cache if possible internal virtual async Task <YJSONObject> requestAPI() { if (_cache_expiration > YAPI.GetTickCount()) { return(_cache_json); } string request; if (_cache_json == null) { request = "GET /api.json \r\n\r\n"; } else { string fw = _cache_json.getYJSONObject("module").getString("firmwareRelease"); request = "GET /api.json?fw=" + YAPIContext.imm_escapeAttr(fw) + " \r\n\r\n"; } string yreq = await requestHTTPSyncAsString(request, null); YJSONObject cache_json; try { cache_json = new YJSONObject(yreq); cache_json.parseWithRef(_cache_json); } catch (Exception ex) { _cache_json = null; throw new YAPI_Exception(YAPI.IO_ERROR, "Request failed, could not parse API (" + ex.Message + ")"); } this._cache_expiration = YAPI.GetTickCount() + YAPI.DefaultCacheValidity; this._cache_json = cache_json; return(cache_json); }
// Change the value of an attribute on a device, and update cache on the fly // Note: the function cache is a typed (parsed) cache, contrarily to the agnostic device cache protected internal virtual async Task <int> _setAttr(string attr, string newval) { if (newval == null) { throw new YAPI_Exception(YAPI.INVALID_ARGUMENT, "Undefined value to set for attribute " + attr); } string attrname = YAPIContext.imm_escapeAttr(attr); string extra = "/" + attrname + "?" + attrname + "=" + YAPIContext.imm_escapeAttr(newval) + "&."; await _devRequest(extra); if (_cacheExpiration != 0) { _cacheExpiration = YAPI.GetTickCount(); } return(YAPI.SUCCESS); }
protected string imm_escapeAttr(string changeval) { return(YAPIContext.imm_escapeAttr(changeval)); }