/// <summary>
 /// Main call to store values in the StorageService
 /// </summary>
 /// <typeparam name="T">Type of class to be stored</typeparam>
 /// <param name="MyValue">Dictionary of Values of the class to be stored</param>
 /// <param name="pCMD">Command for the store process</param>
 /// <param name="pMagicID">MagicID to be used for back-publish</param>
 /// <param name="pCallBack">If set, and the call came from a local service, the sevices calls back without using the communication system</param>
 public void EdgeDataStore <T>(Dictionary <string, T> MyValue, eSCMD pCMD, string pMagicID, Action <TSM> pCallBack, string pTableName)
 {
     if (TheCDEngines.MyIStorageService != null)
     {
         string serializedXML = TheStorageUtilities.SerializeDataToStore(MyValue, pMagicID, pCMD, typeof(T).Equals(typeof(cdeP)) ? null : pTableName);
         string post          = "CDE_EDGESTORE";
         post += ":";
         if (!string.IsNullOrEmpty(pMagicID) && pCallBack != null)
         {
             MyTimedCallbacks.AddAnItem(new TheTimedCallback()
             {
                 cdeMID = TheCommonUtils.CGuid(pMagicID), MyCallback = pCallBack
             });
             post += pMagicID;
         }
         if (typeof(T).Equals(typeof(cdeP)))
         {
             post += ":" + TheStorageUtilities.GenerateUniqueIDFromType(typeof(TheThing), pTableName);
         }
         TSM tTSM = new TSM(MyBaseEngine.GetEngineName(), post, serializedXML);
         if (TheCDEngines.MyIStorageService.GetBaseEngine().GetEngineState().IsService)
         {
             ProcessStorageServiceCommands(tTSM, pCallBack, true);
         }
         //else //Later for Remote Storage
         //{
         //    tTSM.SetToServiceOnly(true);
         //    TheCommCore.PublishCentral(tTSM);
         //}
     }
 }
        /// <summary>
        /// Requests data from the StorageService
        /// Do not use this call if your StorageMirror has a preset TableName
        /// </summary>
        /// <param name="MyClass">type of the class to be retreived</param>
        /// <param name="pColFilter">request results only for certain colums</param>
        /// <param name="pTopRows">TOP statement to reduce amount of returned rows</param>
        /// <param name="pPageNo">If >0 this number describes a page in the store to be retreived. If set to -1 the last page is retreived</param>
        /// <param name="pSQLFilter">A SQL Filter for the query</param>
        /// <param name="pSQLOrder">A ORDER BY setting for the query</param>
        /// <param name="pGrouping"></param>
        /// <param name="pMagicID">MagicID to be used for back-publish</param>
        /// <param name="pCallBack">If set, and the call came from a local service, the sevices calls back without using the communication system</param>
        /// <param name="LocalCallBackOnly">if Set, no Publish or requested data is executed, just local callback</param>
        public void RequestEdgeStorageData(Type MyClass, string pColFilter, int pTopRows, int pPageNo, string pSQLFilter, string pSQLOrder, string pGrouping, string pMagicID, Action <TSM> pCallBack, bool LocalCallBackOnly, string pTableName)
        {
            string classID = "";

            if (MyClass == null)
            {
                classID = "<null>";
            }
            else
            {
                classID = TheStorageUtilities.GenerateUniqueIDFromType(MyClass, pTableName);
            }
            RequestEdgeStorageData(classID, pColFilter, pTopRows, pPageNo, pSQLFilter, pSQLOrder, pGrouping, pMagicID, pCallBack, LocalCallBackOnly);
        }
        /// <summary>
        /// Creates a new store in the Storage Service
        /// </summary>
        /// <param name="MyType">Type Definition of the class to be stored</param>
        /// <param name="pDefaults">a list of defaults for the class</param>
        /// <param name="pDeviceName">Name of the store</param>
        /// <param name="StoreDescription">Description of the store</param>
        /// <param name="ResetContent"></param>
        /// <param name="pCallBack">Callback to be called when store was created successfully</param>
        public void EdgeDataCreateStore(Type MyType, object pDefaults, string pDeviceName, string StoreDescription, bool ResetContent, Action <TSM> pCallBack, string pTableName)
        {
            string UniqueID;
            string serializedStore = TheStorageUtilities.SerializeCreateStore(MyType, pDefaults, pDeviceName, StoreDescription, out UniqueID, pTableName);

            if (!MyBaseEngine.GetEngineState().IsSimulated)
            {
                if (TheCDEngines.MyIStorageService != null)
                {
                    string post = "CDE_CREATESTORE";
                    if (ResetContent)
                    {
                        post = "CDE_CREATENEWSTORE";
                    }
                    if (pCallBack != null)
                    {
                        Guid ReqGUID = Guid.NewGuid();
                        MyTimedCallbacks.AddAnItem(new TheTimedCallback()
                        {
                            cdeMID = ReqGUID, MyCallback = pCallBack
                        });
                        post += ":" + ReqGUID.ToString();
                    }
                    TSM tTSM = new TSM(MyBaseEngine.GetEngineName(), post, serializedStore);

                    if (TheCDEngines.MyIStorageService.GetBaseEngine().GetEngineState().IsService)
                    {
                        ProcessStorageServiceCommands(tTSM, pCallBack, true);
                    }
                    //else //Later for Remote Storage
                    //{
                    //    tTSM.SetToServiceOnly(true);
                    //    TheCommCore.PublishCentral(tTSM);
                    //}
                }
            }
            else
            {
                pCallBack?.Invoke(new TSM(MyBaseEngine.GetEngineName(), "UniqueID: " + UniqueID, UniqueID));
            }
        }
 /// <summary>
 /// Executes a SQL query against the store of the MyClass
 /// </summary>
 /// <param name="MyClass">Type of the Class determining the store to run the query against</param>
 /// <param name="SQLExec">SQL statement to execute</param>
 /// <param name="pColFilter"></param>
 /// <param name="pMagicID">MagicID to be used for back-publish</param>
 /// <param name="pCallBack">If set, and the call came from a local service, the sevices calls back without using the communication system</param>
 public void EdgeStorageExecuteSql(Type MyClass, string SQLExec, string pColFilter, string pMagicID, Action <TSM> pCallBack, string pTableName)
 {
     if (!MyBaseEngine.GetEngineState().IsSimulated)
     {
         if (TheCDEngines.MyIStorageService != null)
         {
             string tableName = pTableName;
             string post      = "CDE_EXECUTESQL";
             post += ":";
             if (pMagicID.Length > 0 && pCallBack != null)
             {
                 MyTimedCallbacks.AddAnItem(new TheTimedCallback()
                 {
                     cdeMID = TheCommonUtils.CGuid(pMagicID), MyCallback = pCallBack
                 });
                 post += pMagicID;
             }
             if (MyClass != null)
             {
                 if (MyClass.Equals(typeof(cdeP)))
                 {
                     tableName = null;
                     post     += ":" + TheStorageUtilities.GenerateUniqueIDFromType(typeof(TheThing), pTableName);
                 }
             }
             string serializedXML = TheStorageUtilities.SerializeDataToExecute(MyClass, pMagicID, SQLExec, pColFilter, tableName);
             TSM    tTSM          = new TSM(MyBaseEngine.GetEngineName(), post, serializedXML);
             if (TheCDEngines.MyIStorageService.GetBaseEngine().GetEngineState().IsService)
             {
                 ProcessStorageServiceCommands(tTSM, pCallBack, true);
             }
             //else //Later for Remote Storage
             //{
             //    tTSM.SetToServiceOnly(true);
             //    TheCommCore.PublishCentral(tTSM);
             //}
         }
     }
 }
 /// <summary>
 /// Send the requested Charts data information back to the Originator
 /// </summary>
 /// <param name="pChartGuid">Guid of the defined Charts Data</param>
 /// <param name="pOriginator">Originator to send the data to</param>
 /// <param name="IChartFactory">AssemblyQualifiedName of the ChartFactory Type that should handle the Chart Data conversion</param>
 public static bool PushChartsData(Guid pChartGuid, Guid pOriginator, string IChartFactory = null)
 {
     TheStorageUtilities.PushChartsData(pChartGuid, pOriginator, IChartFactory);
     return(true);
 }
        /// <summary>
        /// Requests data from the StorageService
        /// </summary>
        /// <param name="pUniqueID">Known UniqueID of the storage data</param>
        /// <param name="pColFilter">request results only for certain colums</param>
        /// <param name="pTopRows">TOP statement to reduce amount of returned rows</param>
        /// <param name="pPageNo">If >0 this number describes a page in the store to be retreived. If set to -1 the last page is retreived</param>
        /// <param name="pSQLFilter">A SQL Filter for the query</param>
        /// <param name="pSQLOrder">A ORDER BY setting for the query</param>
        /// <param name="pGrouping"></param>
        /// <param name="pMagicID">MagicID to be used for back-publish</param>
        /// <param name="pCallBack">If set, and the call came from a local service, the sevices calls back without using the communication system</param>
        /// <param name="LocalCallBackOnly">if Set, no Publish or requested data is executed, just local callback</param>
        public void RequestEdgeStorageData(string pUniqueID, string pColFilter, int pTopRows, int pPageNo, string pSQLFilter, string pSQLOrder, string pGrouping, string pMagicID, Action <TSM> pCallBack, bool LocalCallBackOnly)
        {
            // Grab the numerical part of the ID
            string tUniqueID = "";

            if (pUniqueID.StartsWith(CommonTypeUIDs.cdePUniqueID))
            {
                tUniqueID = pUniqueID.Substring(0, pUniqueID.IndexOf('-'));
            }
            else
            {
                tUniqueID = pUniqueID;
            }
            StorageGetRequest tRequest = new StorageGetRequest
            {
                SFI = pSQLFilter,
                SOR = pSQLOrder,
                CFI = pColFilter,
                UID = tUniqueID,
                TOP = pTopRows,
                MID = pMagicID,
                PAG = pPageNo,
                GRP = pGrouping,
                DID = TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.DeviceID
            };
            string serializedXML = TheCommonUtils.SerializeObjectToJSONString(tRequest);
            string post          = "";

            if (!MyBaseEngine.GetEngineState().IsSimulated)
            {
                if (TheCDEngines.MyIStorageService != null)
                {
                    post  = "CDE_EDGEGET";
                    post += ":";
                    if (pCallBack != null)
                    {
                        Guid ReqGUID = Guid.NewGuid();
                        MyTimedCallbacks.AddAnItem(new TheTimedCallback()
                        {
                            cdeMID = ReqGUID, MyCallback = pCallBack
                        });
                        post += ReqGUID.ToString();
                    }
                    if (pUniqueID.StartsWith(CommonTypeUIDs.cdePUniqueID))
                    {
                        post += ":" + TheStorageUtilities.GenerateUniqueIDFromType(typeof(TheThing), pUniqueID.Substring(pUniqueID.IndexOf('-') + 1));  // Grabs the CacheTableName part of the ID
                    }
                    TSM tTSM = new TSM(MyBaseEngine.GetEngineName(), post, serializedXML);
                    if (TheCDEngines.MyIStorageService.GetBaseEngine().GetEngineState().IsService)
                    {
                        ProcessStorageServiceCommands(tTSM, pCallBack, true);
                    }
                    //else //Later for Remote Storage
                    //{
                    //    tTSM.SetToServiceOnly(true);
                    //    TheCommCore.PublishCentral(tTSM);
                    //}
                }
            }
            else
            {
                pCallBack(null);
            }
        }