예제 #1
0
        public dynamic Update(FPUser model)
        {
            model.Errors = new Dictionary <string, string>();
            try
            {
                if (!AuthenticationModel.IsAllowed("Edit", Core.Constants.Constant.MenuName.FPUser, Core.Constants.Constant.MenuGroupName.Setting))
                {
                    model.Errors.Add("Generic", "You are Not Allowed to Edit record");

                    return(Json(new
                    {
                        model.Errors
                    }, JsonRequestBehavior.AllowGet));
                }

                var data = _fpUserService.GetObjectById(model.Id);
                data.IsInSync   = (data.PIN == model.PIN && data.Name == model.Name && data.Password == model.Password && data.Card == model.Card && data.Privilege == model.Privilege && data.Group == model.Group && data.TimeZones == model.TimeZones && data.VerifyMode == model.VerifyMode && data.IsEnabled == model.IsEnabled);
                data.EmployeeId = model.EmployeeId;
                data.PIN        = model.PIN;
                data.PIN2       = model.PIN2;
                data.Name       = model.Name;
                data.Password   = model.Password;
                data.Card       = model.Card;
                data.Privilege  = model.Privilege;
                data.Group      = model.Group;
                data.TimeZones  = model.TimeZones;
                data.VerifyMode = model.VerifyMode;
                data.IsEnabled  = model.IsEnabled;
                data.Remark     = model.Remark;

                model = _fpUserService.UpdateObject(data, _employeeService);
            }
            catch (Exception ex)
            {
                LOG.Error("Update Failed", ex);
                model.Errors.Add("Generic", "Error " + ex);

                return(Json(new
                {
                    model.Errors
                }, JsonRequestBehavior.AllowGet));
            }

            return(Json(new
            {
                model.Errors
            }));
        }
예제 #2
0
        public bool DownloadAllUserData(FPMachine fpMachine, IFPUserService _fpUserService, IFPTemplateService _fpTemplateService, IEmployeeService _employeeService)
        {
            if (fpMachine == null || fpMachine.Id <= 0)
            {
                fpMachine.Errors.Add("Generic", "FingerPrint Machine ID not found!");
            }
            else
            {
                if (FPMachines.fpDevices[fpMachine.Id] == null)
                {
                    fpMachine.IsConnected = false;                                             // FPMachines.fpDevices.Add(FPMachineId, new FPDevice.ZKEvents());
                }
                else
                {
                    fpMachine.IsConnected = FPMachines.fpDevices[fpMachine.Id].bIsConnected;
                }
                if (fpMachine.IsConnected == false)
                {
                    fpMachine.Errors.Add("Generic", "Please connect the device first!");
                    return(false);
                }
                bool ok = true;
                lock (FPMachines.fpDevices[fpMachine.Id]._locker)
                {
                    //judge whether the device supports 9.0 fingerprint arithmetic
                    string sOption = "~ZKFPVersion";
                    string sValue  = "";
                    if (FPMachines.fpDevices[fpMachine.Id].axCZKEM1.GetSysOption(fpMachine.MachineNumber, sOption, out sValue))
                    {
                        //FPTemplate of v9 (uses 512B) and v10(uses 1.6kB) are not compatible
                        //if (sValue == "10")
                        //{
                        //    fpMachine.Errors.Add("Generic", "Your device is not using 9.0 arithmetic!");
                        //    return false;
                        //}
                    }
                    FPMachines.fpDevices[fpMachine.Id].Disable(); //.axCZKEM1.DisableDeviceWithTimeOut(fpMachine.MachineNumber, FPMachines.fpDevices[fpMachine.Id].DisabledTime); //.EnableDevice(fpMachine.MachineNumber, false); // Prevent user from using the device
                    try
                    {
                        FPMachines.fpDevices[fpMachine.Id].axCZKEM1.BeginBatchUpdate(fpMachine.MachineNumber, (int)FPDevice.BatchUpdateFlag.Overwrite); //create memory space for batching data

                        if (!FPMachines.fpDevices[fpMachine.Id].axCZKEM1.ReadAllUserID(fpMachine.MachineNumber))                                        //read all the user information to the memory
                        {
                            string err = FPMachines.fpDevices[fpMachine.Id].GetLastErrorMsg();
                            if (FPMachines.fpDevices[fpMachine.Id].iLastError != (int)FPDevice.ErrorCode.DataNotFound)
                            {
                                fpMachine.Errors.Add("Generic", "Unable to read All UserID (Error:" + err + ")");
                                ok = false;
                            }
                        }
                        if (ok && !FPMachines.fpDevices[fpMachine.Id].axCZKEM1.ReadAllTemplate(fpMachine.MachineNumber)) //read all the users' fingerprint templates to the memory
                        {
                            string err = FPMachines.fpDevices[fpMachine.Id].GetLastErrorMsg();
                            if (FPMachines.fpDevices[fpMachine.Id].iLastError != (int)FPDevice.ErrorCode.DataNotFound)
                            {
                                fpMachine.Errors.Add("Generic", "Unable to read All Templates (Error:" + err + ")");
                                ok = false;
                            }
                        }
                        int    idwEnrollNumber = 0;
                        string sName           = "";
                        string sPassword       = "";
                        int    iPrivilege      = 0;
                        bool   bEnabled        = false;
                        string card            = "";
                        int    pin2            = 0;
                        int    grp             = 0;
                        string TZstr           = "";

                        //string sTmpData = "";
                        byte[] bTmpData   = new byte[2048];
                        int    iTmpLength = 0;
                        int    iFlag      = 0;
                        //FPDevice._Template9_ tmp9;
                        int    vs  = (int)FPDevice.VerifyMethod.UnSet;
                        byte[] rsv = new byte[4];

                        if (ok)
                        {
                            // Mark All User as Not InSync first
                            foreach (var fpUser in _fpUserService.GetAll())
                            {
                                fpUser.IsInSync = false;
                            }

                            while (FPMachines.fpDevices[fpMachine.Id].axCZKEM1.GetAllUserInfo(fpMachine.MachineNumber, ref idwEnrollNumber, ref sName, ref sPassword, ref iPrivilege, ref bEnabled)) //get all the users' information from the memory
                            {
                                FPMachines.fpDevices[fpMachine.Id].axCZKEM1.GetPIN2(idwEnrollNumber, ref pin2);
                                FPMachines.fpDevices[fpMachine.Id].axCZKEM1.GetStrCardNumber(out card);
                                FPMachines.fpDevices[fpMachine.Id].axCZKEM1.GetUserGroup(fpMachine.MachineNumber, idwEnrollNumber, ref grp);
                                FPMachines.fpDevices[fpMachine.Id].axCZKEM1.GetUserTZStr(fpMachine.MachineNumber, idwEnrollNumber, ref TZstr);
                                FPMachines.fpDevices[fpMachine.Id].axCZKEM1.GetUserInfoEx(fpMachine.MachineNumber, idwEnrollNumber, out vs, out rsv[0]);
                                var err = FPMachines.fpDevices[fpMachine.Id].GetLastErrorMsg();

                                FPUser fpUser = _fpUserService.GetObjectByPIN(idwEnrollNumber);
                                if (fpUser == null)
                                {
                                    fpUser = new FPUser();
                                }
                                fpUser.PIN        = idwEnrollNumber;
                                fpUser.Name       = sName;
                                fpUser.Password   = sPassword;
                                fpUser.Privilege  = (byte)iPrivilege;
                                fpUser.IsEnabled  = bEnabled;
                                fpUser.Card       = card;
                                fpUser.PIN2       = pin2;
                                fpUser.Group      = (byte)grp;
                                fpUser.TimeZones  = TZstr;
                                fpUser.VerifyMode = vs;
                                fpUser.Reserved   = BitConverter.ToString(rsv).Replace("-", ""); //rsv.CopyTo(fpUser.Reserved, 0);
                                fpUser.IsInSync   = true;
                                _fpUserService.UpdateOrCreateObject(fpUser, _employeeService);
                                //Mark All fpTemplates as not InSync, thus non-existing fpTemplates will remain not InSync
                                var tmplist = _fpTemplateService.GetQueryable().Where(x => x.FPUserId == fpUser.Id);
                                foreach (var tmp in tmplist.Where(x => x.IsInSync && !x.IsDeleted).ToList())
                                {
                                    tmp.IsInSync = false;
                                    //_fpTemplateService.GetRepository().Update(tmp);
                                }
                                //Create or Update existing fpTemplates
                                for (int idwFingerIndex = 0; idwFingerIndex < 10; idwFingerIndex++)
                                {
                                    //if (FPMachines.fpDevices[fpMachine.Id].axCZKEM1.GetUserTmpExStr(fpMachine.MachineNumber, idwEnrollNumber, idwFingerIndex, ref sTmpData, ref iTmpLength)) //get the corresponding templates string and length from the memory
                                    if (FPMachines.fpDevices[fpMachine.Id].axCZKEM1.GetUserTmpEx(fpMachine.MachineNumber, idwEnrollNumber.ToString(), idwFingerIndex, out iFlag, out bTmpData[0], out iTmpLength))
                                    {
                                        FPTemplate fpTemplate = _fpTemplateService.GetQueryable().Where(x => x.FingerID == idwFingerIndex && x.FPUserId == fpUser.Id && !x.IsDeleted).FirstOrDefault();
                                        if (fpTemplate == null)
                                        {
                                            fpTemplate = new FPTemplate();
                                        }
                                        fpTemplate.FPUserId = fpUser.Id;
                                        fpTemplate.PIN      = (int)fpUser.PIN2;
                                        fpTemplate.FingerID = (short)idwFingerIndex;
                                        //fpTemplate.Template = sTmpData;
                                        fpTemplate.Template = Convert.ToBase64String(bTmpData, 0, iTmpLength); //Encoding.UTF8.GetString(bTmpData, 0, iTmpLength);
                                        fpTemplate.Size     = fpTemplate.Template.Length;                      //iTmpLength
                                        //tmp9 = FPDevice.ConvertStruct.ByteArrayToStruct<FPDevice._Template9_>(bTmpData);
                                        fpTemplate.Valid    = (byte)iFlag;                                     //1; // tmp9.Valid;
                                        fpTemplate.IsInSync = true;
                                        _fpTemplateService.UpdateOrCreateObject(fpTemplate, _fpUserService);
                                    }
                                }
                                tmplist         = tmplist.Where(x => !x.IsInSync);
                                fpUser.IsInSync = (tmplist.Count() == 0);
                                _fpUserService.UpdateObject(fpUser, _employeeService); // reupdate to prevent fpTemplate from changing fpUser.IsInSync to False when Updating
                            }
                        }
                        int errc = FPMachines.fpDevices[fpMachine.Id].GetLastError();
                        if (errc != (int)FPDevice.ErrorCode.NoError && errc != (int)FPDevice.ErrorCode.DataNotFound)
                        {
                            fpMachine.Errors.Add("Generic", "Download Failed (Error:" + FPMachines.fpDevices[fpMachine.Id].GetErrorMsg(errc) + ")");
                            ok = false;
                            //FPMachines.fpDevices[fpMachine.Id].axCZKEM1.CancelBatchUpdate(fpMachine.MachineNumber);
                        }
                        FPMachines.fpDevices[fpMachine.Id].axCZKEM1.BatchUpdate(fpMachine.MachineNumber); //download all the information in the memory
                    }
                    finally
                    {
                        //FPMachines.fpDevices[fpMachine.Id].axCZKEM1.RefreshData(fpMachine.MachineNumber); //the data in the device should be refreshed
                        FPMachines.fpDevices[fpMachine.Id].Enable(); //.axCZKEM1.EnableDevice(fpMachine.MachineNumber, true);
                    }
                }
                return(ok);
            }
            return(false);
        }