예제 #1
0
 private static void CreateKPIIndexes()
 {
     // We could wait here for engines, but then other KPIs will not be written in meantime
     // await TheBaseEngine.WaitForEnginesStartedAsync();
     KPIIndexes = new cdeConcurrentDictionary <string, int>();
     lock (KPIIndexes)
     {
         Array indexes = Enum.GetValues(typeof(eKPINames));
         foreach (var index in indexes)
         {
             KPIIndexes.TryAdd(Enum.GetName(typeof(eKPINames), index), (int)index);
         }
         List <string> engineNames = TheThingRegistry.GetEngineNames(false);
         foreach (string engineName in engineNames)
         {
             KPIIndexes.TryAdd($"TSMbyENG-{engineName}", KPIIndexes.Count);
         }
     }
 }
        private static bool IsValidEngine(string strEngineContext)
        {
            bool bValidEngine = false;

            if (strEngineContext == "*")
            {
                bValidEngine = true;
            }
            else
            {
                List <string> listEngines = TheThingRegistry.GetEngineNames(true);
                foreach (string strEngineName in listEngines)
                {
                    if (strEngineContext == strEngineName)
                    {
                        bValidEngine = true;
                        break;
                    }
                }
            }

            return(bValidEngine);
        }
        /// <summary>
        /// sinkThingApiInterceptor - Called when our URL is referenced.
        /// </summary>
        /// <param name="pRequest"></param>
        public void sinkThingApiInterceptor(TheRequestData pRequest)
        {
            string[] astrURLPath = pRequest.cdeRealPage.Split(new char[1] {
                '/'
            }, StringSplitOptions.RemoveEmptyEntries);
            bool bHandled = false;

            if (astrURLPath.Length > 1)
            {
                Dictionary <string, string> aParameters = new Dictionary <string, string>();
                ParseQueryParameters(pRequest.RequestUri.Query, aParameters);

                string strEngineContext   = astrURLPath[1];
                string strThingContext    = (astrURLPath.Length < 3) ? "" : astrURLPath[2];
                string strPropertyContext = (astrURLPath.Length < 4) ? "" : astrURLPath[3];
                string strPropertyName    = (astrURLPath.Length < 5) ? "" : astrURLPath[4];

                // Login is only user action that does not require a login key.
                if (strEngineContext.StartsWith("Login"))
                {
                    // Is user logging in?
                    //
                    // Sample URL:
                    //     http://c-labs-paul-yao-laptop:8700/ThingApi/Login?User=paul&Pwd=yao
                    //
                    // Sample Return value:
                    //     "a6221f63-c88c-4aa1-8a83-b0fd18a6811e"
                    ValidateUserCredentials(pRequest, astrURLPath, aParameters);
                    bHandled = true;
                }
                else if (IsTokenValid(aParameters))
                {
                    if (strEngineContext.StartsWith("Engines"))
                    {
                        // Is user enumerating engines?
                        //
                        // Sample URL:
                        //     http://c-labs-paul-yao-laptop:8700/ThingApi/Engines?key=a6221f63-c88c-4aa1-8a83-b0fd18a6811e
                        //
                        // Sample Return value:
                        //     ["NMIService.TheNMIHtml5RT","NMIService","ThingService","CDMyThingInspector.cdePluginService1","ContentService"]

                        List <string> listEngines = TheThingRegistry.GetEngineNames(true);
                        pRequest.ResponseMimeType = "application/json";
                        string strJson = TheCommonUtils.SerializeObjectToJSONString <List <string> >(listEngines);
                        pRequest.ResponseBuffer = TheCommonUtils.CUTF8String2Array(strJson);
                        pRequest.StatusCode     = (int)eHttpStatusCode.OK;
                        pRequest.DontCompress   = true;
                        pRequest.AllowStatePush = false;
                        bHandled = true;
                    }
                    else
                    {
                        // Is user performing a query on a specific engine?
                        //
                        // Sample URL:
                        //     http://c-labs-paul-yao-laptop:8700/ThingApi/NMIService/Things?key=a6221f63-c88c-4aa1-8a83-b0fd18a6811e
                        //
                        // Sample Return value:
                        //     ["b0710f68-5d0f-4e00-93ea-b32c1a5a40c7", "d7bec9f2-4dbc-4119-97e0-f34ed09e5ae1"]
                        //
                        // Sample URL to get all things for all engines:
                        //     http://c-labs-paul-yao-laptop:8700/ThingApi/*/Things?key=a6221f63-c88c-4aa1-8a83-b0fd18a6811e
                        //
                        bool bEngineFound = IsValidEngine(strEngineContext);
                        if (bEngineFound)
                        {
                            if (!String.IsNullOrEmpty(strThingContext))
                            {
                                if (strThingContext == "Things")
                                {
                                    // Enumerate available things for this engine.
                                    List <TheThing> listThings       = TheThingRegistry.GetThingsOfEngine(strEngineContext);
                                    List <string>   listReturnValues = new List <string>();
                                    foreach (TheThing t in listThings)
                                    {
                                        listReturnValues.Add(t.cdeMID.ToString());
                                    }

                                    pRequest.ResponseMimeType = "application/json";
                                    string strJson = TheCommonUtils.SerializeObjectToJSONString <List <string> >(listReturnValues);
                                    pRequest.ResponseBuffer = TheCommonUtils.CUTF8String2Array(strJson);
                                    pRequest.StatusCode     = (int)eHttpStatusCode.OK;
                                    pRequest.DontCompress   = true;
                                    pRequest.AllowStatePush = false;
                                    bHandled = true;
                                }
                                else
                                {
                                    // Is user performing a query on all of the properties of a specific thing?
                                    //
                                    // Sample URL:
                                    //     http://c-labs-paul-yao-laptop:8700/ThingApi/*/b0710f68-5d0f-4e00-93ea-b32c1a5a40c7/Properties?key=a6221f63-c88c-4aa1-8a83-b0fd18a6811e
                                    //
                                    // Sample Return value:
                                    //     ["Id", "Value", "FriendlyName"]
                                    //
                                    // Sample URL to get all things for all engines:
                                    //     http://c-labs-paul-yao-laptop:8700/ThingApi/*/Things?key=a6221f63-c88c-4aa1-8a83-b0fd18a6811e
                                    //
                                    // Value is Guid for a specific thing
                                    List <TheThing> listThings       = TheThingRegistry.GetThingsOfEngine(strEngineContext);
                                    List <string>   listReturnValues = new List <string>();
                                    foreach (TheThing t in listThings)
                                    {
                                        if (strThingContext == t.cdeMID.ToString())
                                        {
                                            if (strPropertyContext == "Properties")
                                            {
                                                // Enumerate all properties for the thing.

                                                bHandled = true;
                                            }
                                            else if (strPropertyContext == "Property")
                                            {
                                                // Act on an individual property

                                                bHandled = true;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (!bHandled)
            {
                ResponseNotImplemented(pRequest);
            }
        }
예제 #4
0
 internal static void RecordServices()
 {
     MyServiceHostInfo.MyLiveServices = TheCommonUtils.CListToString(TheThingRegistry.GetEngineNames(true), ";");
 }