// 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);
        }
        internal async Task <byte[]> GetResponse()
        {
            Task <byte[]> task      = _tcs.Task;
            ulong         now       = YAPI.GetTickCount();
            int           msTimeout = (int)(_timeout - now);
            Task          endedTask = await Task.WhenAny(task, Task.Delay(msTimeout));

            if (endedTask != task)
            {
                _asyncResult?.Invoke(_asyncContext, imm_RemoveHeader(_response), YAPI.TIMEOUT, "USB request " + _debug_name + " did not finished in time");
                throw new YAPI_Exception(YAPI.TIMEOUT, "USB request " + _debug_name + " did not finished in time");
            }

            byte[] response = task.Result;
            var    res      = imm_RemoveHeader(response);

            return(res);
        }
Beispiel #3
0
        // 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;

            try {
                yreq = await requestHTTPSyncAsString(request, null);
            } catch (YAPI_Exception) {
                await _hub.updateDeviceListAsync(true);

                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() + await YAPI.imm_GetYCtx().GetCacheValidity();

            this._cache_json = cache_json;
            return(cache_json);
        }
        public YRequest(byte[] requestBytes, YGenericHub.RequestAsyncResult asyncResult, object asyncContext, ulong maxTime)
        {
            _response      = new byte[1024];
            _response_size = 0;
            _isDone        = false;
            _debug_name    = YAPI.DefaultEncoding.GetString(requestBytes);
            int debug_pos = _debug_name.IndexOf('\r');

            if (debug_pos > 0)
            {
                _debug_name = _debug_name.Substring(0, debug_pos);
            }

            _asyncResult  = asyncResult;
            _asyncContext = asyncContext;
            _tm_start     = YAPI.GetTickCount();
            _timeout      = _tm_start + maxTime;
            _tcs          = new TaskCompletionSource <byte[]>();
        }
        internal override async Task <byte[]> devRequestSync(YDevice device, string req_first_line, byte[] req_head_and_body, uint mstimeout, YGenericHub.RequestProgress progress, object context)
        {
            ulong start = YAPI.GetTickCount();

            if (!_httpReqByDev.ContainsKey(device))
            {
                _httpReqByDev[device] = new YHTTPRequest(_hub, "Device " + device.SerialNumber);
            }

            YHTTPRequest req = _httpReqByDev[device];

            byte[] result = await req.RequestSync(req_first_line, req_head_and_body, mstimeout);

            ulong stop = YAPI.GetTickCount();

            //Debug.WriteLine(string.Format("SyncRes on {0} took {1}ms", device.SerialNumber, stop - start));

            return(result);
        }
        internal async Task EnsureLastRequestDone()
        {
            if (_currentReq != null)
            {
                ulong now     = YAPI.GetTickCount();
                ulong timeout = _startRequestTime + _requestTimeout;
                if (timeout <= now)
                {
                    throw new YAPI_Exception(YAPI.TIMEOUT, "Last Http request did not finished");
                }
                int  msTimeout = (int)(timeout - now);
                Task task      = await Task.WhenAny(_currentReq, Task.Delay(msTimeout));

                if (task != _currentReq)
                {
                    throw new YAPI_Exception(YAPI.TIMEOUT, "Last Http request did not finished");
                }
                _currentReq = null;
            }
        }
Beispiel #7
0
        internal void imm_AppendResponseData(byte[] data)
        {
            int length = _responseData.Length;

            if (_responseLen + data.Length > length)
            {
                byte[] tmp = new byte[length * 2];
                Array.Copy(_responseData, 0, tmp, 0, _responseLen);
                Array.Copy(data, 0, tmp, _responseLen, data.Length);
                _responseLen += data.Length;
                _responseData = tmp;
            }
            else
            {
                Array.Copy(data, 0, _responseData, _responseLen, data.Length);
                _responseLen += data.Length;
            }

            _tmIn = YAPI.GetTickCount();
        }
Beispiel #8
0
        internal override async Task updateDeviceListAsync(bool forceupdate)
        {
            ulong now = YAPI.GetTickCount();

            if (forceupdate)
            {
                _devListExpires = 0;
            }
            if (_devListExpires > now)
            {
                return;
            }

            await _ywatcher.ImediateEnum();

            List <YUSBDevice> devices = _ywatcher.imm_GetUsableDevices();
            //_yctx._Log(string.Format("{0} devices usable\n",devices.Count));
            Dictionary <string, List <YPEntry> > yellowPages = new Dictionary <string, List <YPEntry> >();
            List <WPEntry> whitePages = new List <WPEntry>();

            foreach (YUSBDevice d in devices)
            {
                whitePages.Add(d.imm_GetWhitesPagesEntry());
                d.imm_UpdateYellowPages(yellowPages);
            }
            // Reindex all devices from white pages
            for (int i = 0; i < whitePages.Count; i++)
            {
                _serialByYdx[i] = whitePages[i].SerialNumber;
            }
            await updateFromWpAndYp(whitePages, yellowPages);

            // reset device list cache timeout for this hub
            now             = YAPI.GetTickCount();
            _devListExpires = now + 500;
        }
 /**
  * <summary>
  * </summary>
  * <param name="func">
  *   functionid
  * </param>
  */
 protected YBluetoothLink(string func)
     : this(YAPI.imm_GetYCtx(), func)
 {
 }
Beispiel #10
0
 /**
  * <summary>
  * </summary>
  * <param name="func">
  *   functionid
  * </param>
  */
 protected YPwmOutput(string func)
     : this(YAPI.imm_GetYCtx(), func)
 {
 }
Beispiel #11
0
 /**
  * <summary>
  * </summary>
  * <param name="func">
  *   functionid
  * </param>
  */
 protected YMotor(string func)
     : this(YAPI.imm_GetYCtx(), func)
 {
 }
Beispiel #12
0
 /**
  * <summary>
  * </summary>
  * <param name="func">
  *   functionid
  * </param>
  */
 protected YDigitalIO(string func)
     : this(YAPI.imm_GetYCtx(), func)
 {
 }
Beispiel #13
0
 /**
  * <summary>
  * </summary>
  * <param name="func">
  *   functionid
  * </param>
  */
 protected YCurrent(string func)
     : this(YAPI.imm_GetYCtx(), func)
 {
 }
 /**
  * <summary>
  * </summary>
  * <param name="func">
  *   functionid
  * </param>
  */
 protected YGroundSpeed(string func)
     : this(YAPI.imm_GetYCtx(), func)
 {
 }
Beispiel #15
0
 /**
  * <summary>
  * </summary>
  * <param name="func">
  *   functionid
  * </param>
  */
 protected YAudioOut(string func)
     : this(YAPI.imm_GetYCtx(), func)
 {
 }
Beispiel #16
0
 /**
  * <summary>
  * </summary>
  * <param name="func">
  *   functionid
  * </param>
  */
 protected YRangeFinder(string func)
     : this(YAPI.imm_GetYCtx(), func)
 {
 }
Beispiel #17
0
 /**
  * <summary>
  * </summary>
  * <param name="func">
  *   functionid
  * </param>
  */
 protected YPressure(string func)
     : this(YAPI.imm_GetYCtx(), func)
 {
 }
 /**
  * <summary>
  * </summary>
  * <param name="func">
  *   functionid
  * </param>
  */
 protected YGenericSensor(string func)
     : this(YAPI.imm_GetYCtx(), func)
 {
 }
Beispiel #19
0
 /**
  * <summary>
  * </summary>
  * <param name="func">
  *   functionid
  * </param>
  */
 protected YArithmeticSensor(string func)
     : this(YAPI.imm_GetYCtx(), func)
 {
 }
Beispiel #20
0
 /**
  * <summary>
  * </summary>
  * <param name="func">
  *   functionid
  * </param>
  */
 protected YWatchdog(string func)
     : this(YAPI.imm_GetYCtx(), func)
 {
 }
 /**
  * <summary>
  * </summary>
  * <param name="func">
  *   functionid
  * </param>
  */
 protected YColorLed(string func)
     : this(YAPI.imm_GetYCtx(), func)
 {
 }
 /**
  * <summary>
  * </summary>
  * <param name="func">
  *   functionid
  * </param>
  */
 protected YMultiSensController(string func)
     : this(YAPI.imm_GetYCtx(), func)
 {
 }
 /**
  * <summary>
  * </summary>
  * <param name="func">
  *   functionid
  * </param>
  */
 protected YSegmentedDisplay(string func)
     : this(YAPI.imm_GetYCtx(), func)
 {
 }
Beispiel #24
0
 public YFunction(string func) : this(YAPI.imm_GetYCtx(), func, "Function")
 {
 }
Beispiel #25
0
 /**
  * <summary>
  * </summary>
  * <param name="func">
  *   functionid
  * </param>
  */
 protected YAltitude(string func)
     : this(YAPI.imm_GetYCtx(), func)
 {
 }
Beispiel #26
0
        protected internal static YFunction _FindFromCache(string className, string func)
        {
            YAPIContext ctx = YAPI.imm_GetYCtx();

            return(ctx._yHash.imm_getFunction(className, func));
        }
Beispiel #27
0
 /**
  * <summary>
  * </summary>
  * <param name="func">
  *   functionid
  * </param>
  */
 protected YTvoc(string func)
     : this(YAPI.imm_GetYCtx(), func)
 {
 }
Beispiel #28
0
 internal virtual async void _parse(YJSONObject json, ulong msValidity)
 {
     _cacheExpiration = YAPI.GetTickCount() + msValidity;
     imm_parseAttr(json);
     await _parserHelper();
 }
Beispiel #29
0
 /**
  * <summary>
  * </summary>
  * <param name="func">
  *   functionid
  * </param>
  */
 protected YWakeUpMonitor(string func)
     : this(YAPI.imm_GetYCtx(), func)
 {
 }
 /**
  * <summary>
  * </summary>
  * <param name="func">
  *   functionid
  * </param>
  */
 protected YFiles(string func) : this(YAPI.imm_GetYCtx(), func)
 {
 }