示例#1
0
        private JArrayString BuildMessages(JArray jArrayMessages, RbHeader rbh, object rbBody)
        {
            RbMessage rbMessage = new RbMessage();

            rbMessage.RbHeader = rbh;
            rbMessage.RbBody   = rbBody;

            var jsonRbMessage = JsonConvert.SerializeObject(rbMessage);
            var joRbMessage   = (JObject)JsonConvert.DeserializeObject(jsonRbMessage);

            jArrayMessages.Add(joRbMessage);
            JArrayString jArrayString = new JArrayString(jArrayMessages);

            return(jArrayString);
        }
        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);
        }
示例#3
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);
        }
示例#4
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);
        }
        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);
        }
示例#8
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);
        }