Exemple #1
0
        public static object GetParameterJsonValue(KeyValuePair <string, object> parameter)
        {
            var obj = parameter.Value;

            if (obj is JValue || obj is string)
            {
                return(obj);
            }

            var str = JsonConvert.SerializeObject(obj);

            return(C8oTranslator.StringToJson(str));
        }
Exemple #2
0
        async private Task <object> HandleRequest()
        {
            bool isFullSyncRequest = C8oFullSync.IsFullSyncRequest(parameters);

            if (isFullSyncRequest)
            {
                c8o.Log._Debug("Is FullSync request");

                var liveid = C8oUtils.GetParameterStringValue(parameters, C8o.FS_LIVE, false);
                if (liveid != null)
                {
                    var dbName = C8oUtils.GetParameterStringValue(parameters, C8o.ENGINE_PARAMETER_PROJECT, true).Substring(C8oFullSync.FULL_SYNC_PROJECT.Length);
                    c8o.AddLive(liveid, dbName, this);
                }

                // The result cannot be handled here because it can be different depending to the platform
                // But it can be useful bor debug
                try
                {
                    var fullSyncResult = await c8o.c8oFullSync.HandleFullSyncRequest(parameters, c8oResponseListener);

                    return(fullSyncResult);
                }
                catch (C8oException e)
                {
                    throw e;
                }
                catch (Exception e)
                {
                    throw new C8oException(C8oExceptionMessage.FullSyncRequestFail(), e);
                }
            }
            else
            {
                string responseType;
                if (c8oResponseListener == null || c8oResponseListener is C8oResponseXmlListener)
                {
                    responseType = C8o.RESPONSE_TYPE_XML;
                }
                else if (c8oResponseListener is C8oResponseJsonListener)
                {
                    responseType = C8o.RESPONSE_TYPE_JSON;
                }
                else
                {
                    return(new C8oException("wrong listener"));
                }

                //*** Local cache ***//

                string c8oCallRequestIdentifier = null;

                // Allows to enable or disable the local cache on a Convertigo requestable, default value is true
                C8oLocalCache localCache        = C8oUtils.GetParameterObjectValue(parameters, C8oLocalCache.PARAM, false) as C8oLocalCache;
                bool          localCacheEnabled = false;

                // If the engine parameter for local cache is specified
                if (localCache != null)
                {
                    // Removes local cache parameters and build the c8o call request identifier
                    parameters.Remove(C8oLocalCache.PARAM);

                    if (localCacheEnabled = localCache.enabled)
                    {
                        c8oCallRequestIdentifier = C8oUtils.IdentifyC8oCallRequest(parameters, responseType);

                        if (localCache.priority.IsAvailable(c8o))
                        {
                            try
                            {
                                C8oLocalCacheResponse localCacheResponse = await c8o.c8oFullSync.GetResponseFromLocalCache(c8oCallRequestIdentifier);

                                if (!localCacheResponse.Expired)
                                {
                                    if (responseType == C8o.RESPONSE_TYPE_XML)
                                    {
                                        return(C8oTranslator.StringToXml(localCacheResponse.Response));
                                    }
                                    else if (responseType == C8o.RESPONSE_TYPE_JSON)
                                    {
                                        return(C8oTranslator.StringToJson(localCacheResponse.Response));
                                    }
                                }
                            }
                            catch (C8oUnavailableLocalCacheException)
                            {
                                // no entry
                            }
                        }
                    }
                }

                //*** Get response ***//

                parameters[C8o.ENGINE_PARAMETER_DEVICE_UUID] = c8o.DeviceUUID;


                // Build the c8o call URL
                c8oCallUrl = c8o.Endpoint + "/." + responseType;

                HttpWebResponse httpResponse = null;
                Exception       exception    = null;
                try
                {
                    httpResponse = await c8o.httpInterface.HandleC8oCallRequest(c8oCallUrl, parameters);
                }
                catch (Exception e)
                {
                    exception = e;
                }

                if (exception != null)
                {
                    if (localCacheEnabled)
                    {
                        try
                        {
                            C8oLocalCacheResponse localCacheResponse = await c8o.c8oFullSync.GetResponseFromLocalCache(c8oCallRequestIdentifier);

                            if (!localCacheResponse.Expired)
                            {
                                if (responseType == C8o.RESPONSE_TYPE_XML)
                                {
                                    return(C8oTranslator.StringToXml(localCacheResponse.Response));
                                }
                                else if (responseType == C8o.RESPONSE_TYPE_JSON)
                                {
                                    return(C8oTranslator.StringToJson(localCacheResponse.Response));
                                }
                            }
                        }
                        catch (C8oUnavailableLocalCacheException)
                        {
                            // no entry
                        }
                    }
                    return(new C8oException(C8oExceptionMessage.handleC8oCallRequest(), exception));
                }

                var responseStream = httpResponse.GetResponseStream();

                object response;
                string responseString = null;
                if (c8oResponseListener is C8oResponseXmlListener)
                {
                    response = C8oTranslator.StreamToXml(responseStream);
                    if (localCacheEnabled)
                    {
                        responseString = C8oTranslator.XmlToString(response as XDocument);
                    }
                }
                else if (c8oResponseListener is C8oResponseJsonListener)
                {
                    responseString = C8oTranslator.StreamToString(responseStream);
                    response       = C8oTranslator.StringToJson(responseString);
                }
                else
                {
                    return(new C8oException("wrong listener"));
                }

                if (localCacheEnabled)
                {
                    // String responseString = C8oTranslator.StreamToString(responseStream);
                    long expirationDate = -1;
                    if (localCache.ttl > 0)
                    {
                        expirationDate = localCache.ttl + C8oUtils.GetUnixEpochTime(DateTime.Now);
                    }
                    var localCacheResponse = new C8oLocalCacheResponse(responseString, responseType, expirationDate);
                    await c8o.c8oFullSync.SaveResponseToLocalCache(c8oCallRequestIdentifier, localCacheResponse);
                }

                return(response);
            }
        }