Example #1
0
        /// <summary>
        /// Get UserId via UserName
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public string GetUserId(string UserName)
        {
            string UserId = string.Empty;

            if (UserTable.ContainsKey(UserName))
            {
                UserId = UserTable[UserName].Id;
            }
            return(UserId);
        }
Example #2
0
        /// <summary>
        /// Since balance is the only mutuable property in user, update User means update balance.
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public enumStatus UpdateUser(User user)
        {
            string jsonDoc = user.ToString();

            try
            {
                lock (syncRoot)
                {
                    if (!Users.ContainsKey(user.Id) || !UserTable.ContainsKey(user.UserName))
                    {
                        return(enumStatus.Error);
                    }
                    Users[user.Id]           = jsonDoc;
                    UserTable[user.UserName] = user;
                    return(enumStatus.BalanceAdjusted);
                }
            }
            catch (Exception ex)
            {
                ServerLog.LogException(ex, string.Format("Update User: {0}", jsonDoc));
                return(enumStatus.BalanceAdjustFailed);
            }
        }
Example #3
0
        private void registerButton_Click(object sender, EventArgs e)
        {
            CurentDataLocked = true;

            realSenseProgram.Paush();//暫停影像更新

            //按下註冊瞬間的名稱
            var onClickName  = CurrentName;
            var registerForm = new RegisterForm()
            {
                UserName = onClickName,
                Picture  = FaceImage
            };

            if (registerForm.ShowDialog() != DialogResult.OK)
            {
                realSenseProgram.UnPaush();
                return;
            }

            int?registedUserId = null;

            if (UserTable.ContainsValue(registerForm.UserName))
            {
                //重複項目,選取使用者後registedUserId
                Dictionary <int, Image> mapping = new Dictionary <int, Image>();
                var userIds = UserTable.Where(x => x.Value == registerForm.UserName).Select(x => x.Key).ToArray();
                for (int i = 0; i < userIds.Length; i++)
                {
                    mapping[userIds[i]] =
                        FaceData.Where(x =>
                                       x.ForeignKey == userIds[i]).FirstOrDefault()?.Image;
                    if (mapping[userIds[i]] == null)
                    {
                        mapping[userIds[i]] = new Bitmap(128, 128);
                        using (Graphics g = Graphics.FromImage(mapping[userIds[i]])) {
                            g.DrawString("找不到圖片", new Font("Arial", 16), Brushes.Black, 0, 0);
                        }
                    }
                }

                var dupForm = new DuplicateUserForm()
                {
                    IdFaceMapping = mapping
                };

                if (dupForm.ShowDialog() == DialogResult.OK)
                {
                    registedUserId = dupForm.UserId;
                }
            }

            //註冊並寫入資料庫
            var userId = Current.RegisterUser();

            if (userId == -1)
            {
                MessageBox.Show(
                    "註冊過程出現異常,請再嘗試一次",
                    "註冊失敗", MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                CurentDataLocked = false;
                realSenseProgram.UnPaush();
                return;
            }

            //取得資料庫緩衝區
            var buffer =
                CurrentData.QueryRecognitionModule()
                .GetDatabaseBuffer();

            FaceData = RecognitionFaceData
                       .FromDatabaseBuffer(buffer);

            //有選擇使用者,但是註冊後ID不合,修正ID
            if (registedUserId.HasValue && userId != registedUserId.Value)
            {
                var userItem = FaceData.Last();
                userItem.ForeignKey = registedUserId.Value;
                userId = userItem.ForeignKey;
                FaceDatabaseManager.UpdateBuffer(buffer, userItem);
                //FaceDatabaseManager.ClearRemovedBuffer(buffer);

                realSenseProgram.recognitionConfig
                .SetDatabaseBuffer(buffer);
                realSenseProgram.moduleConfiguration.ApplyChanges();
            }
            else if (!registedUserId.HasValue && UserTable.ContainsKey(userId))
            {
                //新建使用者,但是UserID指向了現有使用者
                var userItem = FaceData.Last();
                //新建使用者ID
                userItem.ForeignKey = UserTable.ToArray().Select(x => x.Key).Max() + 1;
                userId = userItem.ForeignKey;
                FaceDatabaseManager.UpdateBuffer(buffer, userItem);

                //FaceDatabaseManager.ClearRemovedBuffer(buffer);
                realSenseProgram.recognitionConfig
                .SetDatabaseBuffer(buffer);
                realSenseProgram.moduleConfiguration.ApplyChanges();
            }

            UserTable[userId] = registerForm.UserName;

            realSenseProgram.UnPaush();//暫停影像更新
            CurentDataLocked = false;
        }