示例#1
0
        public JArrayString ProcessMessage(RbAppMasterCache rbappmc, RbAppRouterCache rbapprc, RbHeader rbh, string rbBodyString)
        {
            var results = new JArray();

            var appInfo          = JsonConvert.DeserializeObject <JObject>(rbappmc.AppInfo);
            var connectionString = (appInfo["SqlConnectionString"] ?? "").ToString();

            PersonInfoModel.Connection(connectionString);
            PersonFaceInfoModel.Connection(connectionString);

            var       rbBody    = JsonConvert.DeserializeObject <JObject>(rbBodyString);
            RbMessage rbMessage = null;

            if (rbh.MessageId == "GetFaceInfo")
            {
                rbMessage = GetFaceInfo(rbh, appInfo, rbBody);
            }
            else if (rbh.MessageId == "RegisterFace")
            {
                rbMessage = ResisterPersonFace(rbh, appInfo, rbBody);
            }
            else if (rbh.MessageId == "DeleteFace")
            {
                rbMessage = DeleteFace(rbh, appInfo, rbBody);
            }
            results.Add(JsonConvert.DeserializeObject <JObject>(JsonConvert.SerializeObject(rbMessage)));

            return(new JArrayString(results));
        }
        /// <summary>
        /// Get application master information.
        /// </summary>
        /// <returns>RbAppMasterCache</returns>
        private RbAppMasterCache GetAppMasterInfo(RbHeader rbh)
        {
            AppMaster        am        = null;
            bool             am_action = true;
            RbAppMasterCache rbappmc   = null;

            if (rbAppMasterCacheDic.ContainsKey(rbh.AppId))
            {
                rbappmc = (RbAppMasterCache)rbAppMasterCacheDic[rbh.AppId];
                if (rbappmc.CacheExpiredDatetime >= DateTime.Now)
                {
                    am_action = false;
                }
            }
            if (am_action)
            {
                am      = new AppMaster(rbh.AppId, rbEncPassPhrase, sqlConnectionString, rbCacheExpiredTimeSec);
                rbappmc = am.GetAppMaster();
                if (rbappmc != null)
                {
                    lock (thisLock)
                    {
                        rbAppMasterCacheDic[rbh.AppId] = rbappmc;
                    }
                }
                else
                {
                    throw new ApplicationException("Error ** GetAppMaster() returns Null Object");
                }
            }

            return(rbappmc);
        }
        /// <summary>
        /// Call Apps (Azure Functions or Custom App) with HTTP
        /// </summary>
        /// <param name="rbh"></param>
        /// <param name="rbBodyString"></param>
        /// <param name="partitionId"></param>
        /// <returns>Task<JArray></returns>
        private async Task <JArray> CallAppsWithHttp(RbHeader rbh, string rbBodyString, string partitionId)
        {
            // Get App Master Info
            RbAppMasterCache rbappmc = GetAppMasterInfo(rbh);
            // Get App Routing Info
            RbAppRouterCache rbapprc = GetAppRoutingInfo(rbh);

            JArray    ja_messages = new JArray();
            RbMessage message     = new RbMessage();

            message.RbHeader = rbh;
            message.RbBody   = (JObject)JsonConvert.DeserializeObject(rbBodyString);
            var strMessage = (string)JsonConvert.SerializeObject(message);

            // HTTP Client
            var client = new HttpClient();

            // Create request body
            byte[] byteData = Encoding.UTF8.GetBytes(strMessage);
            var    content  = new ByteArrayContent(byteData);

            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            // Call REST API
            var response = await client.PostAsync(rbapprc.HttpUri, content);

            if (response.IsSuccessStatusCode)
            {
                var responseText = await response.Content.ReadAsStringAsync();

                try
                {
                    message.RbBody = (JObject)JsonConvert.DeserializeObject(responseText);
                }
                catch
                {
                    var badResponse = new BadResponse();
                    badResponse.StatusCode   = "Parse Error";
                    badResponse.ReasonPhrase = "HTTP response is not regular JSON format";
                    message.RbBody           = badResponse;
                }
            }
            else
            {
                var badResponse = new BadResponse();
                badResponse.StatusCode   = response.StatusCode.ToString();
                badResponse.ReasonPhrase = response.ReasonPhrase.ToString();
                message.RbBody           = badResponse;
            }
            message.RbHeader = rbh;

            string  json_message = JsonConvert.SerializeObject(message);
            JObject jo           = (JObject)JsonConvert.DeserializeObject(json_message);

            ja_messages.Add(jo);

            return(ja_messages);
        }
        /// <summary>
        /// Call Apps Async (Azure Functions) with Queue Storage
        /// </summary>
        /// <param name="rbh"></param>
        /// <param name="rbBodyString"></param>
        /// <param name="partitionId"></param>
        /// <returns>Task</returns>
        private async Task CallAppsWithQueue(RbHeader rbh, string rbBodyString, string partitionId)
        {
            // Get App Master Info
            RbAppMasterCache rbappmc = GetAppMasterInfo(rbh);
            // Get App Routing Info
            RbAppRouterCache rbapprc = GetAppRoutingInfo(rbh);

            RbMessage message = new RbMessage();

            message.RbHeader = rbh;
            message.RbBody   = (JObject)JsonConvert.DeserializeObject(rbBodyString);
            string queueName = rbapprc.AppId + "-" + rbapprc.AppProcessingId;

            var sender = new RbAppMessageToQueue(message, rbappmc.QueueStorageAccount, rbappmc.QueueStorageKey, queueName);
            await sender.SendAsync();
        }
        public JArrayString ProcessMessage(RbAppMasterCache rbappmc, RbAppRouterCache rbapprc, RbHeader rbh, string rbBodyString)
        {
            var appInfo = JsonConvert.DeserializeObject <JObject>(rbappmc.AppInfo);

            appInfo["DeviceId"] = rbh.SourceDeviceId;
            var          appParams = JsonConvert.DeserializeObject <JObject>(rbBodyString);
            JArrayString message   = null;

            if (rbh.MessageId == "Speech")
            {
                var speechAppBody = new SpeechAppBody();
                var personId      = (appParams["PersonId"] ?? "").ToString();
                var talk          = (appParams["talk"] ?? "").ToString();
                var luisService   = new LuisService(rbh.SourceDeviceId, appInfo);
                speechAppBody.Behavior = luisService.CreateRobotBehavior(talk);
                speechAppBody.Behavior.NaturalTalkText = TextOverflow(speechAppBody.Behavior.NaturalTalkText);
                if (personId != "")
                {
                    var actionClient = new HwsRobotBehaviorApi.Person.Action.Client(appInfo["SqlConnectionString"].ToString());
                    foreach (var entity in speechAppBody.Behavior.LuisEntities)
                    {
                        actionClient.CreateTalkLog(rbh.SourceDeviceId, personId, speechAppBody.Behavior.NaturalTalkText, talk, entity);
                    }
                }

                message = new JArrayString(MakeProcessMessages(rbh, speechAppBody));
            }
            else if (rbh.MessageId == "RobotSpeech")
            {
                var speechAppBody = new SpeechAppBody();
                var talk          = (appParams["talk"] ?? "").ToString();
                var luisService   = new LuisService(rbh.SourceDeviceId, appInfo);
                speechAppBody.Behavior = luisService.CreateRobotBehaviorDirectSpeech(talk);
                speechAppBody.Behavior.NaturalTalkText = TextOverflow(speechAppBody.Behavior.NaturalTalkText);
                message = new JArrayString(MakeProcessMessages(rbh, speechAppBody));
            }
            else if (rbh.MessageId == "GetRobotAction")
            {
                var speechAppBody = RobotAction(rbh, appInfo, appParams);
                message = new JArrayString(MakeProcessMessages(rbh, speechAppBody));
            }

            return(message);
        }
示例#6
0
        public JArrayString ProcessMessage(RbAppMasterCache rbappmc, RbAppRouterCache rbapprc, RbHeader rbh, string rbBodyString)
        {
            JArray  ja_messages = new JArray();
            AppBody appbody     = new AppBody();

            appbody.Hello = "Hello World !!!!!!";

            RbMessage message = new RbMessage();

            message.RbHeader = rbh;
            message.RbBody   = appbody;

            string  json_message = JsonConvert.SerializeObject(message);
            JObject jo           = (JObject)JsonConvert.DeserializeObject(json_message);

            ja_messages.Add(jo);
            JArrayString jaString = new JArrayString(ja_messages);

            return(jaString);
        }
        JArray ProcessControlMessage(RbHeader rbh)
        {
            JArray ja_messages = new JArray();

            try
            {
                RbAppMasterCache rbappmc = GetAppMasterInfo(rbh);
                RbMessage        message = new RbMessage();
                message.RbHeader = rbh;
                message.RbBody   = JsonConvert.DeserializeObject <JObject>(rbappmc.AppInfoDevice);
                string  json_message = JsonConvert.SerializeObject(message);
                JObject jo           = (JObject)JsonConvert.DeserializeObject(json_message);
                ja_messages.Add(jo);
            }
            catch (Exception ex)
            {
                RbTraceLog.WriteError("E004", ex.ToString());
                ae = new ApplicationException("Error ** <<CONTROL Message Processing>> Exception occured during JSON processing on RBFX.AppMaster[AppInfoDevice]");
                throw ae;
            }
            return(ja_messages);
        }
示例#8
0
        public JArrayString ProcessMessage(RbAppMasterCache rbappmc, RbAppRouterCache rbapprc, RbHeader rbh, string rbBodyString)
        {
            string translatorAccountKey = string.Empty;

            // RbAppLog
            var    appName = Path.GetFileNameWithoutExtension(this.GetType().Assembly.Location);
            string storageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}",
                                                           rbappmc.StorageAccount, rbappmc.StorageKey);
            RbAppLog RbAppLog = new RbAppLog();

            RbAppLog.Initialize(storageConnectionString, "RbAppLog", appName);

            // Request Message
            var requestBody = new ReqBody();
            var jo_reqBody  = (JObject)JsonConvert.DeserializeObject(rbBodyString);

            try
            {
                requestBody.visitor    = (string)jo_reqBody["visitor"];
                requestBody.visitor_id = (string)jo_reqBody["visitor_id"];
            }
            catch
            {
                requestBody.visitor    = string.Empty;
                requestBody.visitor_id = string.Empty;
            }
            requestBody.text   = (string)jo_reqBody["text"];
            requestBody.tolang = (string)jo_reqBody["tolang"];

            // Response Message
            JArray    ja_messages  = new JArray();
            RbMessage message      = new RbMessage();
            string    json_message = string.Empty;
            var       responseBody = new ResBody();

            responseBody.translated_text = string.Empty;
            var responseBodyWhenError = new ResBodyWhenError();

            if (requestBody.tolang == null || requestBody.tolang == string.Empty)  //in case no language is selected.
            {
                requestBody.tolang = languageCode;
            }

            // Set cognitive service account key
            JObject jo_appInfo = (JObject)JsonConvert.DeserializeObject(rbappmc.AppInfo);
            var     p1         = jo_appInfo["TranslatorApiKey"];

            if (p1 != null)
            {
                if ((string)p1 != "")
                {
                    translatorAccountKey = (string)p1;
                }
            }

            if (translatorAccountKey == string.Empty)
            {
                // Send Error to device
                responseBodyWhenError.error_message = "[TranslatorApiKey] not found in RBFX.AppMaster !!";
                responseBodyWhenError.success       = "false";
                RbAppLog.WriteError("E001", responseBodyWhenError.error_message);

                message.RbHeader = rbh;
                message.RbBody   = responseBodyWhenError;
                json_message     = JsonConvert.SerializeObject(message);
                JObject jo1 = (JObject)JsonConvert.DeserializeObject(json_message);
                ja_messages.Add(jo1);

                return(new JArrayString(ja_messages));
            }

            ApiResult apiResult = TranslateText(requestBody.text, translatorAccountKey, requestBody.tolang);

            // Respond to device
            message          = new RbMessage();
            message.RbHeader = rbh;
            if (apiResult.IsSuccessStatusCode)
            {
                responseBody.success         = "true";
                responseBody.visitor         = requestBody.visitor;
                responseBody.visitor_id      = requestBody.visitor_id;
                responseBody.translated_text = apiResult.Result;
                message.RbBody = responseBody;
            }
            else
            {
                responseBodyWhenError.success       = "false";
                responseBodyWhenError.error_message = apiResult.Message;
                message.RbBody = responseBodyWhenError;
                RbAppLog.WriteError("E002", apiResult.Message);
            }

            json_message = JsonConvert.SerializeObject(message);
            JObject jo = (JObject)JsonConvert.DeserializeObject(json_message);

            ja_messages.Add(jo);
            JArrayString jaString = new JArrayString(ja_messages);

            return(jaString);
        }
        CachedDllFileInfo CopyBlobToLocalDir(RbAppMasterCache rbappmc, RbAppRouterCache rbapprc, string partitionId)
        {
            //string curdir = Environment.CurrentDirectory;
            CachedDllFileInfo cachedDllFileInfo = new CachedDllFileInfo();
            string            curdir            = AppDomain.CurrentDomain.BaseDirectory;

            cachedDllFileInfo.BaseDirectory       = curdir;
            cachedDllFileInfo.PrivateDllDirectory = Path.Combine(curdir, "P" + partitionId);

            string            blobTargetFilePath     = string.Empty;
            RbAppDllCacheInfo rbAppDllInfo           = null;
            RbAppDllCacheInfo rbAppDllInfo_partition = null;
            bool   loadAction             = true;
            bool   blobCopyAction         = true;
            string partitionedFileNameKey = "P" + partitionId + "_" + rbapprc.FileName;

            // Check original DLL info
            if (rbAppDllCacheInfoDic.ContainsKey(rbapprc.FileName))
            {
                // Original DLL
                rbAppDllInfo       = (RbAppDllCacheInfo)rbAppDllCacheInfoDic[rbapprc.FileName];
                blobTargetFilePath = Path.Combine(rbAppDllInfo.CacheDir, rbAppDllInfo.CachedFileName);

                // Use cached original DLL if Registered_Datetime not changed.
                if (rbAppDllInfo.AppId == rbapprc.AppId &&
                    rbAppDllInfo.AppProcessingId == rbapprc.AppProcessingId &&
                    rbAppDllInfo.Registered_DateTime == rbapprc.Registered_DateTime)
                {
                    blobCopyAction = false;
                }
            }

            // Check partitioned DLL info
            if (rbAppDllCacheInfoDic.ContainsKey(partitionedFileNameKey))
            {
                // DLL copied into each partition directory
                rbAppDllInfo_partition = (RbAppDllCacheInfo)rbAppDllCacheInfoDic[partitionedFileNameKey];
                cachedDllFileInfo.PrivateDllFilePath = Path.Combine(rbAppDllInfo_partition.CacheDir, rbAppDllInfo_partition.CachedFileName);

                // Use cached DLL copied into each partition directory if Registered_Datetime not changed.
                if (rbAppDllInfo_partition.AppId == rbapprc.AppId &&
                    rbAppDllInfo_partition.AppProcessingId == rbapprc.AppProcessingId &&
                    rbAppDllInfo_partition.Registered_DateTime == rbapprc.Registered_DateTime)
                {
                    loadAction = false;
                }
            }

            if (loadAction)
            {
                if (blobTargetFilePath != string.Empty)
                {
                    AppDomain appDomain = null;
                    if (appDomainList.ContainsKey(partitionId))
                    {
                        appDomain = appDomainList[partitionId];
                        AppDomain.Unload(appDomain);
                        appDomainList[partitionId] = null;
                    }

                    if (blobCopyAction)
                    {
                        // Move current DLL to archive directory
                        if (File.Exists(blobTargetFilePath))
                        {
                            string archivedDirectory   = Path.Combine(curdir, archivedDirectoryName);
                            string archivedDllFilePath = archivedDirectory + @"\" + rbapprc.FileName
                                                         + ".bk" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                            if (!Directory.Exists(archivedDirectory))
                            {
                                Directory.CreateDirectory(archivedDirectory);
                            }
                            File.Move(blobTargetFilePath, archivedDllFilePath);
                        }
                    }
                }

                if (blobCopyAction)
                {
                    // Download DLL from BLOB
                    RbAzureStorage rbAzureStorage = new RbAzureStorage(rbappmc.StorageAccount, rbappmc.StorageKey);
                    rbAppDllInfo          = new RbAppDllCacheInfo();
                    rbAppDllInfo.FileName = rbapprc.FileName;
                    rbAppDllInfo.CacheDir = Path.Combine(curdir, "cache");
                    if (!Directory.Exists(rbAppDllInfo.CacheDir))
                    {
                        Directory.CreateDirectory(rbAppDllInfo.CacheDir);
                    }
                    rbAppDllInfo.AppId               = rbapprc.AppId;
                    rbAppDllInfo.AppProcessingId     = rbapprc.AppProcessingId;
                    rbAppDllInfo.Registered_DateTime = rbapprc.Registered_DateTime;
                    //rbAppDllInfo.GenerateCachedFileName();
                    rbAppDllInfo.CachedFileName = rbAppDllInfo.FileName;
                    blobTargetFilePath          = Path.Combine(rbAppDllInfo.CacheDir, rbAppDllInfo.CachedFileName);

                    using (var fileStream = File.OpenWrite(blobTargetFilePath))
                    {
                        rbAzureStorage.BlockBlobDownload(fileStream, rbapprc.BlobContainer, rbapprc.FileName);
                    }
                    // Update cache info if DLL download from BLOB is successful.
                    rbAppDllCacheInfoDic[rbapprc.FileName] = rbAppDllInfo;

                    // Logging
                    if (rbTraceLevel == RbTraceType.Detail)
                    {
                        RbTraceLog.WriteLog(string.Format("App DLL is copied from BLOB strage.  Dir:{0}, FileName:{1}",
                                                          curdir, rbAppDllInfo.CachedFileName));
                    }
                }

                // Copy original DLL into partition directory
                rbAppDllInfo_partition                     = new RbAppDllCacheInfo();
                rbAppDllInfo_partition.FileName            = rbapprc.FileName;
                rbAppDllInfo_partition.CacheDir            = cachedDllFileInfo.PrivateDllDirectory;
                rbAppDllInfo_partition.AppId               = rbapprc.AppId;
                rbAppDllInfo_partition.AppProcessingId     = rbapprc.AppProcessingId;
                rbAppDllInfo_partition.Registered_DateTime = rbapprc.Registered_DateTime;
                rbAppDllInfo_partition.CachedFileName      = rbAppDllInfo_partition.FileName;

                string sourceFilePath = Path.Combine(rbAppDllInfo.CacheDir, rbAppDllInfo.CachedFileName);
                string targetFilePath = Path.Combine(rbAppDllInfo_partition.CacheDir, rbAppDllInfo_partition.CachedFileName);
                cachedDllFileInfo.PrivateDllFilePath = targetFilePath;
                if (!Directory.Exists(rbAppDllInfo_partition.CacheDir))
                {
                    Directory.CreateDirectory(rbAppDllInfo_partition.CacheDir);
                }
                File.Copy(sourceFilePath, targetFilePath, true);

                // Update cache info if DLL copied successfully.
                rbAppDllCacheInfoDic[partitionedFileNameKey] = rbAppDllInfo_partition;

                // Logging
                if (rbTraceLevel == RbTraceType.Detail)
                {
                    RbTraceLog.WriteLog(string.Format("Original App DLL is copied into partition directory.  Dir:{0}, FileName:{1}, PartitionId:{2}",
                                                      curdir, rbAppDllInfo.CachedFileName, partitionId));
                }
            }

            return(cachedDllFileInfo);
        }
        JArray CallApps(RbHeader rbh, string rbBodyString, string partitionId)
        {
            // Get App Master Info
            RbAppMasterCache rbappmc = GetAppMasterInfo(rbh);

            // Get App Routing Info
            RbAppRouterCache rbapprc = GetAppRoutingInfo(rbh);

            JArrayString ja_messagesString = null;
            JArray       ja_messages       = null;
            string       dllFilePath       = string.Empty;

            IAppRouterDll routedAppDll = null;
            Assembly      assembly     = null;

            // Load DLL from BLOB
            string baseDirectory            = string.Empty;
            string privateDllDirectory      = string.Empty;
            string cachedFileName           = string.Empty;
            string cachedFileNameWithoutExt = string.Empty;

            if (rbapprc.DevMode == "True")
            {
                string devdir = rbapprc.DevLocalDir;
                int    pos    = devdir.Length - 1;
                if (devdir.Substring(pos, 1) == @"\")
                {
                    dllFilePath = rbapprc.DevLocalDir + rbapprc.FileName;
                }
                else
                {
                    dllFilePath = rbapprc.DevLocalDir + @"\" + rbapprc.FileName;
                }

                baseDirectory            = Path.GetDirectoryName(dllFilePath);
                privateDllDirectory      = baseDirectory;
                cachedFileName           = Path.GetFileName(dllFilePath);
                cachedFileNameWithoutExt = Path.GetFileNameWithoutExtension(dllFilePath);
            }
            else
            {
                CachedDllFileInfo cachedDllFileInfo = null;
                lock (thisLock2)
                {
                    cachedDllFileInfo = CopyBlobToLocalDir(rbappmc, rbapprc, partitionId);
                }
                baseDirectory            = cachedDllFileInfo.BaseDirectory;
                privateDllDirectory      = cachedDllFileInfo.PrivateDllDirectory;
                cachedFileName           = Path.GetFileName(cachedDllFileInfo.PrivateDllFilePath);
                cachedFileNameWithoutExt = Path.GetFileNameWithoutExtension(cachedDllFileInfo.PrivateDllFilePath);
            }

            ////Static load without AppDomain
            //assembly = System.Reflection.Assembly.LoadFrom(dllFilePath);
            //routedAppDll = assembly.CreateInstance(rbapprc.ClassName) as IAppRouterDll;

            //Dynamic load using AppDomain
            try
            {
                string    appDomainName = appDomanNameBase + partitionId;
                AppDomain appDomain     = null;
                if (appDomainList.ContainsKey(partitionId))
                {
                    appDomain = appDomainList[partitionId];
                }

                if (appDomain == null)
                {
                    appDomain = CreateAppDomain(appDomainName, baseDirectory, privateDllDirectory);
                    lock (thisLock2)
                    {
                        appDomainList[partitionId] = appDomain;
                    }
                }
                routedAppDll = appDomain.CreateInstanceAndUnwrap(cachedFileNameWithoutExt, rbapprc.ClassName) as IAppRouterDll;
            }
            catch (Exception ex)
            {
                RbTraceLog.WriteError("E003", ex.ToString());
                ae = new ApplicationException("Error ** Exception occured during creating AppDomain & Instance(App DLL)");
                throw ae;
            }

            // ProcessMessage
            try
            {
                rbh.ProcessingStack = rbapprc.FileName;
                ja_messagesString   = routedAppDll.ProcessMessage(rbappmc, rbapprc, rbh, rbBodyString);
                ja_messages         = ja_messagesString.ConvertToJArray();
            }
            catch (Exception ex)
            {
                RbTraceLog.WriteError("E002", ex.ToString());
                ae = new ApplicationException("Error ** Exception occured in routed App DLL");
                throw ae;
            }

            return(ja_messages);
        }
        private void callAppButton_Click(object sender, EventArgs e)
        {
            if (textBoxDllFilePath.Text == string.Empty)
            {
                MessageBox.Show("** Error ** DLL File Local Path must be set !!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (textBoxDeviceId.Text == string.Empty)
            {
                MessageBox.Show("** Error ** Device ID must be set !!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (checkBoxSkipAppRouter.Checked && textBoxClassName.Text == string.Empty)
            {
                MessageBox.Show("** Error ** Class Name must be set !!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            // Save the TextBox content
            saveTextBoxContent();

            JObject jo_message = JsonConvert.DeserializeObject <JObject>(textBoxInput.Text);

            // Check RbHeader in detail
            RbHeaderBuilder hdBuilder = new RbHeaderBuilder(jo_message, textBoxDeviceId.Text);
            RbHeader        rbh       = hdBuilder.ValidateJsonSchema();
            // RbBody
            JObject jo_temp      = (JObject)jo_message[RbFormatType.RbBody];
            string  rbBodyString = JsonConvert.SerializeObject(jo_temp);

            // App Master Cache (RBFX.AppMaster)
            AppMaster        am      = new AppMaster(rbh.AppId, activeEncPassPhrase, activeSqlConnectionString, constTimeOutSec);
            RbAppMasterCache rbappmc = am.GetAppMaster();

            // App Router Cache (RBFX.AppRouting)
            RbAppRouterCache rbapprc;

            if (checkBoxSkipAppRouter.Checked)
            {
                rbapprc                 = new RbAppRouterCache();
                rbapprc.AppId           = rbh.AppId;
                rbapprc.AppProcessingId = rbh.AppProcessingId;
                rbapprc.ClassName       = textBoxClassName.Text;
                rbapprc.FileName        = textBoxDllFilePath.Text;
            }
            else
            {
                AppRouter ar = new AppRouter(rbh.AppId, rbh.AppProcessingId, activeSqlConnectionString, constTimeOutSec);
                rbapprc = ar.GetAppRouting();
            }

            // Load DLL
            //Assembly assembly = null;
            AppDomain     appDomain    = null;
            IAppRouterDll routedAppDll = null;

            try
            {
                //assembly = System.Reflection.Assembly.LoadFrom(textBoxDllFilePath.Text);
                //routedAppDll = assembly.CreateInstance(rbapprc.ClassName) as IAppRouterDll;
                string pid                      = Thread.CurrentThread.ManagedThreadId.ToString();
                string appDomainName            = "AppDomain_P" + pid;
                string cachedDirectory          = Path.GetDirectoryName(textBoxDllFilePath.Text);
                string cachedFileName           = Path.GetFileName(textBoxDllFilePath.Text);
                string cachedFileNameWithoutExt = Path.GetFileNameWithoutExtension(textBoxDllFilePath.Text);
                appDomain    = createAppDomain(appDomainName, cachedDirectory);
                routedAppDll = appDomain.CreateInstanceAndUnwrap(cachedFileNameWithoutExt, rbapprc.ClassName) as IAppRouterDll;
            }
            catch (Exception ex)
            {
                MessageBox.Show("** Application (DLL) Load Error ** Check File Path or Class Name \n" + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Process Message
            try
            {
                rbh.ProcessingStack = activeFileName;
                JArrayString ja_messagesString = routedAppDll.ProcessMessage(rbappmc, rbapprc, rbh, rbBodyString);
                JArray       ja_messages       = ja_messagesString.ConvertToJArray();
                textBoxOutput.Text = JsonConvert.SerializeObject(ja_messages);
                AppDomain.Unload(appDomain);
            }
            catch (Exception ex)
            {
                MessageBox.Show("** Error occured in Application (DLL) **\n" + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                AppDomain.Unload(appDomain);
                return;
            }
        }
        public JArrayString ProcessMessage(RbAppMasterCache rbappmc, RbAppRouterCache rbapprc, RbHeader rbh, string rbBodyString)
        {
            // Prepare variables for storage account.
            string storageAccount       = string.Empty;
            string storageKey           = string.Empty;
            string storageContainer     = string.Empty;
            string translatorAccountKey = string.Empty;
            string languageCode         = "en"; //set english as the default
            string visionApiKey         = string.Empty;
            string visionApiEndpoint    = string.Empty;
            string facesOption          = string.Empty;

            JArray    ja_messages = new JArray();
            RbMessage message     = new RbMessage();

            // RbAppLog
            var    appName = Path.GetFileNameWithoutExtension(this.GetType().Assembly.Location);
            string storageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}",
                                                           rbappmc.StorageAccount, rbappmc.StorageKey);
            RbAppLog RbAppLog = new RbAppLog();

            RbAppLog.Initialize(storageConnectionString, "RbAppLog", appName);

            // Set cognitive service account key
            JObject jo_appInfo = (JObject)JsonConvert.DeserializeObject(rbappmc.AppInfo);
            JObject jo_input   = (JObject)JsonConvert.DeserializeObject(rbBodyString);

            var p1 = jo_appInfo["StorageAccount"];

            if (p1 != null)
            {
                storageAccount = (string)p1;
            }
            var p2 = jo_appInfo["StorageKey"];

            if (p2 != null)
            {
                storageKey = (string)p2;
            }
            var p3 = jo_appInfo["VisionStorageContainer"];

            if (p3 != null)
            {
                storageContainer = (string)p3;
            }
            var p4 = jo_appInfo["VisionTranslatorToLang"];

            if (p4 != null)
            {
                languageCode = (string)p4;
            }
            var p5 = jo_appInfo["VisionTranslatorApiKey"];

            if (p5 != null)
            {
                translatorAccountKey = (string)p5;
            }
            var p6 = jo_appInfo["VisionApiEndpoint"];

            if (p6 != null)
            {
                visionApiEndpoint = (string)p6;
            }
            var p7 = jo_appInfo["VisionApiKey"];

            if (p7 != null)
            {
                visionApiKey = (string)p7;
            }

            if (rbh.MessageId == "init")
            {
                InitBody initBody = new InitBody();
                initBody.storageAccount   = storageAccount;
                initBody.storageContainer = storageContainer;
                initBody.storageKey       = storageKey;
                message.RbBody            = initBody;
            }
            else if (rbh.MessageId == "analyze")
            {
                // Prepare response body
                AnalyzeBody analyzeBody = new AnalyzeBody();
                if ((string)jo_input["visitor"] != null)
                {
                    if ((string)jo_input["visitor"] != "")
                    {
                        analyzeBody.visitor = (string)jo_input["visitor"];
                    }
                }

                if ((string)jo_input["visitor_id"] != null)
                {
                    if ((string)jo_input["visitor_id"] != "")
                    {
                        analyzeBody.visitor = (string)jo_input["visitor_id"];
                    }
                }

                // Set http client
                var client = new HttpClient();
                client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", visionApiKey);
                var visionDescribeUrl = $"{visionApiEndpoint}/analyze?visualFeatures=Tags,Description,Faces,Adult";


                try
                {
                    // Prepare target file data
                    BlobData blobData = new BlobData(storageAccount, storageKey, storageContainer);
                    string   fileName = (string)jo_input["blobFileName"];
                    byte[]   buffer   = blobData.GetStream(fileName);
                    var      content  = new ByteArrayContent(buffer);
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

                    // Get result which describe the image.
                    DateTime dt1 = DateTime.Now;
                    var      visionDescribeResult = client.PostAsync(visionDescribeUrl, content);
                    visionDescribeResult.Wait();
                    DateTime dt2 = DateTime.Now;
                    TimeSpan ts  = dt2 - dt1;


                    if (visionDescribeResult.Result.IsSuccessStatusCode)
                    {
                        // If success, convert to the response body form and translate messages.
                        analyzeBody.success = "true";
                        var responseBody = visionDescribeResult.Result.Content.ReadAsStringAsync();
                        var resultBody   = JObject.Parse(responseBody.Result.ToString());

                        // combine text and tags to make one sentence
                        string descriptionText = (string)resultBody["description"]["captions"][0]["text"];
                        string baseTextTags    = joinTags((JArray)resultBody["tags"]);

                        // translate text.
                        DateTime dt3 = DateTime.Now;
                        string   translatedDescription = translateText(descriptionText, translatorAccountKey, languageCode);
                        string   translatedTags        = translateText(baseTextTags, translatorAccountKey, languageCode);
                        DateTime dt4 = DateTime.Now;
                        TimeSpan ts2 = dt4 - dt3;

                        // reform the translated result.
                        var resultTextTags = convertTextTags(translatedTags, (JArray)resultBody["tags"]);

                        // set results
                        analyzeBody.Description    = (string)resultBody["description"]["captions"][0]["text"];
                        analyzeBody.IsAdultContent = ((string)resultBody["adult"]["isAdultContent"]).ToLower();
                        analyzeBody.IsRacyContent  = ((string)resultBody["adult"]["isRacyContent"]).ToLower();
                        analyzeBody.Faces          = convertFaces((JArray)resultBody["faces"]);
                        analyzeBody.Description_jp = translatedDescription;
                        analyzeBody.Tags           = resultTextTags;

                        message.RbBody = analyzeBody;
                    }
                    else
                    {
                        // If failure, convert to the response body form and translate messages.
                        AppBodyWhenError appBodyWhenError = new AppBodyWhenError();
                        appBodyWhenError.success       = "false";
                        appBodyWhenError.error_message = visionDescribeResult.Result.ToString();
                        RbAppLog.WriteError("E001", appBodyWhenError.error_message);

                        message.RbBody = appBodyWhenError;
                    }

                    // if deleteFile value is true, delete data from blob container.
                    if ((string)jo_input["deleteFile"] == "true")
                    {
                        blobData.Delete(fileName);
                    }
                }
                // catch (ApplicationException ex)
                catch (Exception ex)
                {
                    AppBodyWhenError appBodyWhenError = new AppBodyWhenError();
                    appBodyWhenError.success       = "false";
                    appBodyWhenError.error_message = ex.Message;
                    RbAppLog.WriteError("E002", ex.ToString());

                    message.RbBody = appBodyWhenError;
                }
            }

            message.RbHeader = rbh;

            string  json_message = JsonConvert.SerializeObject(message);
            JObject jo           = (JObject)JsonConvert.DeserializeObject(json_message);

            ja_messages.Add(jo);

            JArrayString jaString = new JArrayString(ja_messages);

            return(jaString);
        }
示例#13
0
        public JArrayString ProcessMessage(RbAppMasterCache rbappmc, RbAppRouterCache rbapprc, RbHeader rbh, string rbBodyString)
        {
            // RbAppLog
            var    appName = Path.GetFileNameWithoutExtension(this.GetType().Assembly.Location);
            string storageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}",
                                                           rbappmc.StorageAccount, rbappmc.StorageKey);
            RbAppLog RbAppLog = new RbAppLog();

            RbAppLog.Initialize(storageConnectionString, "RbAppLog", appName);

            // Request Message
            var requestBody = new ReqBody();
            var jo_reqBody  = (JObject)JsonConvert.DeserializeObject(rbBodyString);

            requestBody.visitor    = (string)jo_reqBody["visitor"];
            requestBody.visitor_id = (string)jo_reqBody["visitor_id"];

            // Response Message
            JArray jArrayMessages = new JArray();

            // rbh.MessageId == "finish"
            if (rbh.MessageId == typeFinish)
            {
                var resBody = new ResBody();
                resBody.visitor    = requestBody.visitor;
                resBody.visitor_id = requestBody.visitor_id;
                resBody.type       = typeFinish;

                return(BuildMessages(jArrayMessages, rbh, resBody));
            }

            // rbh.MessageId == "init" or "talkXxxx"
            requestBody.talkByMe = (string)jo_reqBody["talkByMe"];

            // AppMaster
            JObject jo_appInfo = (JObject)JsonConvert.DeserializeObject(rbappmc.AppInfo);

            sqlConnString = (string)jo_appInfo["SqlConnString"];
            try
            {
                rinnaApiEndpoint = (string)jo_appInfo["RinnaApiEndpoint"];
                rinnaKey         = (string)jo_appInfo["RinnaKey"];
                rinnaId          = (string)jo_appInfo["RinnaId"];
            }
            catch
            {
                rinnaApiEndpoint = string.Empty;
                rinnaKey         = string.Empty;
                rinnaId          = string.Empty;
            }
            luisApiEndpoint    = (string)jo_appInfo["LuisApiEndpoint"];
            luisPgmApiEndpoint = (string)jo_appInfo["LuisPgmApiEndpoint"];
            luisKey            = (string)jo_appInfo["LuisKey"];
            luisAppId          = (string)jo_appInfo["LuisAppId"];

            // Edit Response Message
            var responseBody = new ResBodyTalk();

            responseBody.visitor    = requestBody.visitor;
            responseBody.visitor_id = requestBody.visitor_id;
            responseBody.talkByAi   = new List <TalkMessage>();

            IntentState intentState = new IntentState();
            string      intent      = string.Empty;

            // Call LUIS API
            if (rbh.MessageId == typeInit)
            {
                try
                {
                    intentState = GetIntent(requestBody.talkByMe);
                }
                catch (Exception ex)
                {
                    ResBodyWhenError resBodyWhenError = new ResBodyWhenError();
                    resBodyWhenError.success       = "false";
                    resBodyWhenError.error_message = ex.Message;
                    RbAppLog.WriteError("E001", ex.ToString());

                    return(BuildMessages(jArrayMessages, rbh, resBodyWhenError));
                }
                intentState.processState = stateBegin;
                intent = (string)intentState.topScoringIntent["intent"];

                // set Conversation State
                ConversationState convState = new ConversationState(sqlConnString, rbh.SourceDeviceId, rbh.AppId);
                string            conversationStateString = JsonConvert.SerializeObject(intentState);
                ApiResult         apiResult = convState.SetState(conversationStateString);
                if (!apiResult.IsSuccessStatusCode)
                {
                    ResBodyWhenError resBodyWhenError = new ResBodyWhenError();
                    resBodyWhenError.success       = "false";
                    resBodyWhenError.error_message = "** Error ** Conversation State Save failed !! : " + apiResult.Message;
                    RbAppLog.WriteError("E002", apiResult.Message);

                    return(BuildMessages(jArrayMessages, rbh, resBodyWhenError));
                }
            }
            else
            {
                // get Conversation State
                ConversationState convState = new ConversationState(sqlConnString, rbh.SourceDeviceId, rbh.AppId);
                AppResult         appResult = convState.GetState();
                if (appResult.apiResult.IsSuccessStatusCode)
                {
                    var joIntentState = (JObject)JsonConvert.DeserializeObject(appResult.conversationStateString);
                    intentState.processState     = (string)joIntentState["processState"];
                    intentState.query            = (string)joIntentState["query"];
                    intentState.topScoringIntent = (JObject)joIntentState["topScoringIntent"];
                    try
                    {
                        intentState.actionList = (JArray)joIntentState["actionList"];
                    }
                    catch
                    {
                        intentState.actionList = null;
                    }
                }
                else
                {
                    ResBodyWhenError resBodyWhenError = new ResBodyWhenError();
                    resBodyWhenError.success       = "false";
                    resBodyWhenError.error_message = "** Error ** Conversation State Get failed !! : " + appResult.apiResult.Message;
                    RbAppLog.WriteError("E003", appResult.apiResult.Message);

                    return(BuildMessages(jArrayMessages, rbh, resBodyWhenError));
                }
            }

            // Continue talk session
            bool boolFinishTalk = false;

            if (rbh.MessageId.Substring(0, 4) == "talk")
            {
                intent = rbh.MessageId;
                // Check intent of finishing talk
                boolFinishTalk = CheckIntentOfFinishTalk(requestBody.talkByMe);
            }
            // Finish Talk
            if (boolFinishTalk)
            {
                responseBody.visitor    = requestBody.visitor;
                responseBody.visitor_id = requestBody.visitor_id;
                responseBody.type       = typeFinishTalk;

                var talkMessage = new TalkMessage();
                talkMessage.SayText = "会話を終了します。再度、ご用件をお話しください。";
                responseBody.talkByAi.Add(talkMessage);

                return(BuildMessages(jArrayMessages, rbh, responseBody));
            }

            // Call Rinna API
            if (intent == intentTalkRinna || intent == intentNone)
            {
                if (rinnaApiEndpoint != string.Empty)
                {
                    ApiResult apiResult = TalkRinna(rinnaKey, rinnaId, requestBody.talkByMe);
                    if (apiResult.IsSuccessStatusCode)
                    {
                        responseBody.success = "true";
                        responseBody.type    = typeTalkRinna;

                        JObject jo_result    = (JObject)JsonConvert.DeserializeObject(apiResult.Result);
                        var     ja_responses = (JArray)jo_result["Responses"];
                        foreach (var jo_content in ja_responses)
                        {
                            var    talkMessage = new TalkMessage();
                            string text        = (string)jo_content["Content"]["Text"];
                            // Replace Emoji
                            talkMessage.SayText = ReplaceTalkMessage(text);
                            responseBody.talkByAi.Add(talkMessage);
                        }
                    }
                    else
                    {
                        ResBodyWhenError resBodyWhenError = new ResBodyWhenError();
                        resBodyWhenError.success       = "false";
                        resBodyWhenError.error_message = "** Error ** Rinna API failed !! : " + apiResult.Message;
                        RbAppLog.WriteError("E004", apiResult.Message);

                        return(BuildMessages(jArrayMessages, rbh, resBodyWhenError));
                    }
                }
                else
                {
                    responseBody.success = "true";
                    responseBody.type    = typeFinishTalk;
                    var talkMessage = new TalkMessage();
                    talkMessage.SayText = ReplaceTalkMessage("上手く聞き取ることが出来ませんでした。再度、ご用件をお話しください。");
                    responseBody.talkByAi.Add(talkMessage);
                }
            }
            // Various Talk Service defined by LUIS web service
            else if (intent.Length >= 4 && intent.Substring(0, 4) == "talk")
            {
                var talkMessage = new TalkMessage();

                string sayText = string.Empty;
                if (intentState.processState == stateBegin)
                {
                    sayText = GetNextTalkMessage(ref intentState);
                }
                else
                {
                    sayText = GetNextTalkMessageWithUpdate(ref intentState, requestBody.talkByMe);
                }

                if (sayText == string.Empty)
                {
                    responseBody.type = typeFinishTalk;
                    sayText           = CompleteTalkMessage(ref intentState);
                }
                else
                {
                    responseBody.type = intent;
                }
                responseBody.success = "true";
                talkMessage.SayText  = sayText;
                responseBody.talkByAi.Add(talkMessage);

                // set Conversation State
                ConversationState convState = new ConversationState(sqlConnString, rbh.SourceDeviceId, rbh.AppId);
                string            conversationStateString = JsonConvert.SerializeObject(intentState);
                ApiResult         apiResult = convState.SetState(conversationStateString);

                if (!apiResult.IsSuccessStatusCode)
                {
                    ResBodyWhenError resBodyWhenError = new ResBodyWhenError();
                    resBodyWhenError.success       = "false";
                    resBodyWhenError.error_message = "** Error ** Conversation State Save(2) failed !! : " + apiResult.Message;
                    RbAppLog.WriteError("E005", apiResult.Message);

                    return(BuildMessages(jArrayMessages, rbh, resBodyWhenError));
                }
            }
            else
            {
                responseBody.success = "true";
                responseBody.type    = intent;
            }

            return(BuildMessages(jArrayMessages, rbh, responseBody));
        }
示例#14
0
        public JArrayString ProcessMessage(RbAppMasterCache rbappmc, RbAppRouterCache rbapprc, RbHeader rbh, string rbBodyString)
        {
            string sqlConnString    = string.Empty;
            string storageAccount   = string.Empty;
            string storageKey       = string.Empty;
            string storageContainer = string.Empty;
            string faceApiEndpoint  = string.Empty;
            string faceApiKey       = string.Empty;
            bool   success          = true;

            JArray    ja_messages = new JArray();
            RbMessage message     = new RbMessage();

            // RbAppLog
            var    appName = Path.GetFileNameWithoutExtension(this.GetType().Assembly.Location);
            string storageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}",
                                                           rbappmc.StorageAccount, rbappmc.StorageKey);
            RbAppLog RbAppLog = new RbAppLog();

            RbAppLog.Initialize(storageConnectionString, "RbAppLog", appName);

            // Get appinfo
            JObject jo_appInfo = (JObject)JsonConvert.DeserializeObject(rbappmc.AppInfo);
            var     p1         = jo_appInfo["SqlConnString"];

            if (p1 != null)
            {
                sqlConnString = (string)p1;
            }
            var p2 = jo_appInfo["StorageAccount"];

            if (p2 != null)
            {
                storageAccount = (string)p2;
            }
            var p3 = jo_appInfo["StorageKey"];

            if (p3 != null)
            {
                storageKey = (string)p3;
            }
            var p4 = jo_appInfo["FaceStorageContainer"];

            if (p4 != null)
            {
                storageContainer = (string)p4;
            }
            var p5 = jo_appInfo["FaceApiEndpoint"];

            if (p5 != null)
            {
                faceApiEndpoint = (string)p5;
            }
            var p6 = jo_appInfo["FaceApiKey"];

            if (p6 != null)
            {
                faceApiKey = (string)p6;
            }

            JObject          jo_input         = (JObject)JsonConvert.DeserializeObject(rbBodyString);
            AppBodyWhenError appBodyWhenError = new AppBodyWhenError();

            if (rbh.MessageId == "init")
            {
                AppBodyInit appBody = new AppBodyInit();
                appBody.storageAccount   = storageAccount;
                appBody.storageContainer = storageContainer;
                appBody.storageKey       = storageKey;
                message.RbBody           = appBody;
            }
            else if (rbh.MessageId == "getFaceInfo")
            {
                AppBodyFaceInfo appBody = new AppBodyFaceInfo();
                appBody.visitor    = (string)jo_input["visitor"];
                appBody.groupId    = (string)jo_input["groupId"];
                appBody.locationId = (string)jo_input["locationId"];
                if (appBody.locationId == null || appBody.locationId == string.Empty)
                {
                    appBody.locationId = "all";
                }

                // Get image data stream
                string   fileName   = (string)jo_input["blobFileName"];
                string   deleteFile = (string)jo_input["deleteFile"];
                BlobData blobData   = new BlobData(storageAccount, storageKey, storageContainer);
                try
                {
                    byte[] buffer = blobData.GetStream(fileName);

                    // Delete File
                    if (deleteFile == "true")
                    {
                        blobData.Delete(fileName);
                    }

                    // Get Confidence threshold
                    double faceConfidence = 0;
                    try
                    {
                        faceConfidence = (double)jo_appInfo["FaceConfidence"];
                    }
                    catch
                    {
                        faceConfidence = 0.5;
                    }
                    double smileConfidence = 0;
                    try
                    {
                        smileConfidence = (double)jo_appInfo["SmileConfidence"];
                    }
                    catch
                    {
                        smileConfidence = 0.5;
                    }
                    double facialHairConfidence = 0;
                    try
                    {
                        facialHairConfidence = (double)jo_appInfo["FacialHairConfidence"];
                    }
                    catch
                    {
                        facialHairConfidence = 0.5;
                    }

                    // Call Face API (Detection)
                    FaceDetection fd         = new FaceDetection(faceApiEndpoint, faceApiKey, smileConfidence, facialHairConfidence);
                    AppResult     appResult1 = fd.DetectFace(buffer, appBody);
                    appBody = appResult1.appBody;
                    if (appResult1.apiResult.IsSuccessStatusCode)
                    {
                        // Call Face API (Identification)
                        FaceIdentification fi         = new FaceIdentification(faceApiEndpoint, faceApiKey, faceConfidence);
                        AppResult          appResult2 = fi.IdentifyFace(appBody);
                        appBody = appResult2.appBody;

                        if (appResult2.apiResult.IsSuccessStatusCode)
                        {
                            if (appBody.visitor_id != string.Empty)
                            {
                                PersonDbData personDbData = new PersonDbData(sqlConnString, rbh.AppId);
                                AppResult    appResult3   = personDbData.GetInfo(appBody);
                                if (appResult3.apiResult.IsSuccessStatusCode)
                                {
                                    appBody = appResult3.appBody;
                                }
                                else
                                {
                                    success = false;
                                    appBodyWhenError.error_message = appResult3.apiResult.Message;
                                    RbAppLog.WriteError("E001", appResult3.apiResult.Message);
                                }
                            }
                        }
                        else
                        {
                            // Set success "true" even if identification doesn't succeed
                            rbh.ProcessingStack = "** Notice ** Face identification missed.";
                            RbAppLog.WriteError("E002", appResult2.apiResult.Message);
                        }
                    }
                    else
                    {
                        success = false;
                        appBodyWhenError.error_message = appResult1.apiResult.Message;
                        RbAppLog.WriteError("E003", appResult1.apiResult.Message);
                    }

                    if (success)
                    {
                        appBody.success = "true";
                        message.RbBody  = appBody;
                    }
                    else
                    {
                        appBodyWhenError.success = "false";
                        message.RbBody           = appBodyWhenError;
                    }
                }
                catch (Exception ex)
                {
                    appBodyWhenError.error_message = ex.Message;
                    RbAppLog.WriteError("E004", ex.ToString());

                    appBodyWhenError.success = "false";
                    message.RbBody           = appBodyWhenError;
                }
            }
            else if (rbh.MessageId == "registerFace")
            {
                AppBodyRegResult appBody = new AppBodyRegResult();
                appBody.visitor    = (string)jo_input["visitor"];
                appBody.groupId    = (string)jo_input["groupId"];
                appBody.locationId = (string)jo_input["locationId"];
                if (appBody.locationId == null || appBody.locationId == string.Empty)
                {
                    appBody.locationId = "all";
                }
                appBody.visitor_name      = (string)jo_input["visitor_name"];
                appBody.visitor_name_kana = (string)jo_input["visitor_name_kana"];

                // Check Person Group existence
                ApiResult apiResult1 = new ApiResult();
                apiResult1.IsSuccessStatusCode = true;

                PersonGroup personGroup = new PersonGroup(faceApiEndpoint, faceApiKey, appBody.groupId);
                if (!personGroup.GetGroupExistence())
                {
                    // Create Person Group
                    apiResult1 = personGroup.CreatePersonGroup();
                }

                if (apiResult1.IsSuccessStatusCode)
                {
                    // Create Person
                    Person    person     = new Person(faceApiEndpoint, faceApiKey, appBody.groupId);
                    ApiResult apiResult2 = person.CreatePerson(appBody.visitor_name);

                    if (apiResult2.IsSuccessStatusCode)
                    {
                        // Extract PersonId
                        JObject joRsult2 = (JObject)JsonConvert.DeserializeObject(apiResult2.Result);
                        appBody.visitor_id = (string)joRsult2["personId"];

                        // Get image data stream
                        string   fileName   = (string)jo_input["blobFileName"];
                        string   deleteFile = (string)jo_input["deleteFile"];
                        BlobData blobData   = new BlobData(storageAccount, storageKey, storageContainer);
                        try
                        {
                            byte[] buffer = blobData.GetStream(fileName);

                            // Delete File
                            if (deleteFile == "true")
                            {
                                blobData.Delete(fileName);
                            }

                            // Add Face to the Person
                            ApiResult apiResult3 = person.AddPersonFace(appBody.visitor_id, buffer);
                            if (apiResult3.IsSuccessStatusCode)
                            {
                                appBody.success = "true";
                                personGroup.TrainPersonGroup();

                                AppBodyFaceInfo appBodyFaceInfo = new AppBodyFaceInfo();
                                appBodyFaceInfo.visitor           = appBody.visitor;
                                appBodyFaceInfo.groupId           = appBody.groupId;
                                appBodyFaceInfo.locationId        = appBody.locationId;
                                appBodyFaceInfo.visitor_id        = appBody.visitor_id;
                                appBodyFaceInfo.visitor_name      = appBody.visitor_name;
                                appBodyFaceInfo.visitor_name_kana = appBody.visitor_name_kana;
                                appBodyFaceInfo.visit_count       = "1";
                                PersonDbData personDbData = new PersonDbData(sqlConnString, rbh.AppId);
                                personDbData.InsertInfo(appBodyFaceInfo);
                            }
                            else
                            {
                                success = false;
                                appBodyWhenError.error_message = apiResult3.Message;
                                RbAppLog.WriteError("E005", apiResult3.Message);
                            }
                        }
                        catch (Exception ex)
                        {
                            if (ex.Message.Length > 100)
                            {
                                appBodyWhenError.error_message = ex.Message.Substring(0, 100);
                            }
                            else
                            {
                                appBodyWhenError.error_message = ex.Message;
                            }

                            appBodyWhenError.success = "false";
                            message.RbBody           = appBodyWhenError;
                            RbAppLog.WriteError("E006", ex.ToString());
                        }
                    }
                    else
                    {
                        success = false;
                        appBodyWhenError.error_message = apiResult2.Message;
                        RbAppLog.WriteError("E007", apiResult2.Message);
                    }
                }
                else
                {
                    success = false;
                    appBodyWhenError.error_message = apiResult1.Message;
                    RbAppLog.WriteError("E008", apiResult1.Message);
                }

                if (success)
                {
                    appBody.success = "true";
                    message.RbBody  = appBody;
                }
                else
                {
                    appBodyWhenError.success = "false";
                    message.RbBody           = appBodyWhenError;
                }
            }

            message.RbHeader = rbh;

            string  json_message = JsonConvert.SerializeObject(message);
            JObject jo           = (JObject)JsonConvert.DeserializeObject(json_message);

            ja_messages.Add(jo);

            JArrayString jaString = new JArrayString(ja_messages);

            return(jaString);
        }