public static SmartDrawerDatabase.DAL.Device GetDeviceEntity() { if (_deviceEntity != null) { return(_deviceEntity); } if ((Device == null) || string.IsNullOrWhiteSpace(Device.SerialNumber)) { return(null); } try { var ctx = RemoteDatabase.GetDbContext(); _deviceEntity = ctx.Devices .Where(d => d.RfidSerial == Device.SerialNumber) .Include(d => d.DeviceType) .FirstOrDefault(); ctx.Database.Connection.Close(); ctx.Dispose(); return(_deviceEntity); } catch (Exception error) { ExceptionMessageBox msg = new ExceptionMessageBox(error, "Error GetDeviceEntity"); msg.ShowDialog(); return(null); } }
private void enrollmentControl_OnEnroll(object control, int fingerNbr, DPFP.Template template, ref EventHandlerStatus eventHandlerStatus) { if (eventHandlerStatus == EventHandlerStatus.Failure) { MessageBox.Show("Finger enrollment failed.", "Enrollment Failure", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } var fingerIndex = DpfpFingerNbrToFingerIndex[fingerNbr]; string base64EncodedTemplate = FingerprintReader.EncodeBase64Template(template); var ctx = RemoteDatabase.GetDbContext(); { ctx.Fingerprints.Add(new SmartDrawerDatabase.DAL.Fingerprint { Index = (int)fingerIndex, GrantedUserId = _currentUser.GrantedUserId, Template = base64EncodedTemplate }); ctx.SaveChanges(); ctx.Database.Connection.Close(); ctx.Dispose(); } }
private void enrollmentControl_OnDelete(object control, int fingerNbr, ref EventHandlerStatus eventHandlerStatus) { if (eventHandlerStatus == EventHandlerStatus.Failure) { MessageBox.Show("Finger deletion failed.", "Deletion Failure", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } var fingerIndex = DpfpFingerNbrToFingerIndex[fingerNbr]; var ctx = RemoteDatabase.GetDbContext(); { var fingerprint = ctx.Fingerprints.FirstOrDefault( fp => fp.Index == (int)fingerIndex && fp.GrantedUserId == _currentUser.GrantedUserId); if (fingerprint == null) { return; } ctx.Fingerprints.Remove(fingerprint); ctx.SaveChanges(); ctx.Database.Connection.Close(); ctx.Dispose(); } }
public string StockOutProduct(Stream streamdata) { try { StreamReader reader = new StreamReader(streamdata); string res = reader.ReadToEnd(); reader.Close(); reader.Dispose(); JsonProductToStockOut ListOfTags = JsonConvert.DeserializeObject <JsonProductToStockOut>(res); if (ListOfTags != null) { var ctx = RemoteDatabase.GetDbContext(); int nbSuccess = 0; for (int loop = 0; loop < ListOfTags.listOfTagId.Length; loop++) { Product p = ctx.Products.GetByTagUid(ListOfTags.listOfTagId[loop]); if (p != null) { ctx.Products.Remove(p); } nbSuccess++; } ctx.SaveChanges(); ctx.Database.Connection.Close(); ctx.Dispose(); if (MyHostEvent != null) { MyHostEvent(this, new MyHostEventArgs("StockOutProduct", nbSuccess + " Product(s) Stocked out")); } return("Success"); } else { return("Failed : List of tags is null or empty"); } } catch (Exception exp) { return("failed : " + exp.InnerException); } }
public static void HandleNewScanCompleted(int drawerId) { lock (lockMethodInventory) { LogToFile.LogMessageToFile("______________________________________________________________ "); LogToFile.LogMessageToFile("Process Inventory for drawer " + drawerId); var ctx = RemoteDatabase.GetDbContext(); ctx.Configuration.AutoDetectChangesEnabled = false; //ctx.Configuration.ValidateOnSaveEnabled = false; Inventory newInventory = null; try { if (_deviceEntity == null) { _deviceEntity = DevicesHandler.GetDeviceEntity(); if (_deviceEntity == null) { return; } } //remove previous entry Older than 7 days DateTime dtToKeep = DateTime.Now.AddDays(-7.0); using (new PerfTimerLogger("remove previous entry Older than 7 days")) { //remove event drawer var itemBinding2 = ctx.EventDrawerDetails.Where(i => i.DrawerNumber == drawerId && i.DeviceId == _deviceEntity.DeviceId && i.EventDrawerDate < dtToKeep); if (itemBinding2 != null) { foreach (var ib in itemBinding2) { ctx.EventDrawerDetails.Remove(ib); } } //removed inventory var itemBinding = ctx.Inventories.Where(i => i.DrawerNumber == drawerId && i.DeviceId == _deviceEntity.DeviceId && i.InventoryDate < dtToKeep); if (itemBinding != null) { foreach (var ib in itemBinding) { ctx.Inventories.Remove(ib); } } ctx.ChangeTracker.DetectChanges(); ctx.SaveChanges(); } using (new PerfTimerLogger("Store inventory")) { AccessType newInventoryAccessType = null; int? newInventoryGrantedUserId = null; switch (DevicesHandler.LastScanAccessTypeName) { case AccessType.Badge: newInventoryAccessType = ctx.AccessTypes.Badge(); newInventoryGrantedUserId = GrantedUsersCache.LastAuthenticatedUser.GrantedUserId; break; case AccessType.Fingerprint: newInventoryAccessType = ctx.AccessTypes.Fingerprint(); newInventoryGrantedUserId = GrantedUsersCache.LastAuthenticatedUser.GrantedUserId; break; default: newInventoryAccessType = ctx.AccessTypes.Manual(); newInventoryGrantedUserId = null; break; } newInventory = new Inventory { DeviceId = _deviceEntity.DeviceId, DrawerNumber = drawerId, GrantedUserId = newInventoryGrantedUserId, AccessTypeId = newInventoryAccessType.AccessTypeId, TotalAdded = DevicesHandler.ListTagAddedPerDrawer.Where(kvp => kvp.Value == drawerId).ToList().Count(), TotalPresent = DevicesHandler.ListTagPerDrawer.Where(kvp => kvp.Value == drawerId).ToList().Count(), TotalRemoved = DevicesHandler.ListTagRemovedPerDrawer.Where(kvp => kvp.Value == drawerId).ToList().Count(), InventoryDate = DateTime.Now, InventoryStream = SerializeHelper.SeralizeObjectToXML(DevicesHandler.Device.ReaderData), }; ctx.Inventories.Add(newInventory); ctx.ChangeTracker.DetectChanges(); ctx.SaveChanges(); // now the inventory is saved, we can use its ID } using (new PerfTimerLogger("Store movement added")) { var addedTags = DevicesHandler.GetTagFromDictionnary(drawerId, DevicesHandler.ListTagAddedPerDrawer); AddMovementToInventory(ctx, newInventory, addedTags, MovementType.Added, drawerId); //ctx.ChangeTracker.DetectChanges(); ctx.SaveChanges(); } using (new PerfTimerLogger("Store movement present")) { var presentTags = DevicesHandler.GetTagFromDictionnary(drawerId, DevicesHandler.ListTagPerDrawer); AddMovementToInventory(ctx, newInventory, presentTags, MovementType.Present, drawerId); ctx.ChangeTracker.DetectChanges(); ctx.SaveChanges(); } using (new PerfTimerLogger("Store movement removed")) { var removedTags = DevicesHandler.GetTagFromDictionnary(drawerId, DevicesHandler.ListTagRemovedPerDrawer); AddMovementToInventory(ctx, newInventory, removedTags, MovementType.Removed, drawerId); ctx.ChangeTracker.DetectChanges(); ctx.SaveChanges(); } //Update event drawer using (new PerfTimerLogger("Update Inventory")) { ctx.EventDrawerDetails.UpdateInventoryForEventDrawer(_deviceEntity, drawerId, newInventory); ctx.ChangeTracker.DetectChanges(); ctx.SaveChanges(); } using (new PerfTimerLogger("Send to Indian server")) { ProcessSelectionFromServer.PostInventoryForDrawer(_deviceEntity, drawerId, newInventory); } var handler = InventoryCompleted; if (handler != null) { handler(new InventoryEventArgs(newInventory, null)); } } catch (Exception ex) { Console.WriteLine(ex.GetType().FullName); Console.WriteLine(ex.Message); var handler = InventoryAborted; if (handler != null) { handler(null); } if (newInventory != null) { ctx.Inventories.Remove(newInventory); ctx.SaveChanges(); } } finally { ctx.Database.Connection.Close(); ctx.Dispose(); } } }
public string AddOrUpdateProduct(Stream streamdata) { try { StreamReader reader = new StreamReader(streamdata); string res = reader.ReadToEnd(); reader.Close(); reader.Dispose(); JsonProductAddOrUpdate jsonProducts = JsonConvert.DeserializeObject <JsonProductAddOrUpdate>(res); if (jsonProducts != null) { var ctx = RemoteDatabase.GetDbContext(); int nbSuccess = 0; for (int loop = 0; loop < jsonProducts.listOfProducts.Length; loop++) { int LastCol = 100; while (LastCol > 0 && string.IsNullOrEmpty(jsonProducts.listOfProducts[loop].productInfo[LastCol])) { LastCol--; } for (int bcl = 0; bcl < LastCol; bcl++) { if (string.IsNullOrEmpty(jsonProducts.listOfProducts[loop].productInfo[bcl])) { jsonProducts.listOfProducts[loop].productInfo[bcl] = " "; } } RfidTag tag = ctx.RfidTags.AddIfNotExisting(jsonProducts.listOfProducts[loop].tagUID); Product p = ctx.Products.GetByTagUid(jsonProducts.listOfProducts[loop].tagUID); if (p != null) { ctx.Products.Attach(p); p.ProductInfo0 = jsonProducts.listOfProducts[loop].productInfo[0]; p.ProductInfo1 = jsonProducts.listOfProducts[loop].productInfo[1]; p.ProductInfo2 = jsonProducts.listOfProducts[loop].productInfo[2]; p.ProductInfo3 = jsonProducts.listOfProducts[loop].productInfo[3]; p.ProductInfo4 = jsonProducts.listOfProducts[loop].productInfo[4]; p.ProductInfo5 = jsonProducts.listOfProducts[loop].productInfo[5]; p.ProductInfo6 = jsonProducts.listOfProducts[loop].productInfo[6]; p.ProductInfo7 = jsonProducts.listOfProducts[loop].productInfo[5]; p.ProductInfo8 = jsonProducts.listOfProducts[loop].productInfo[8]; p.ProductInfo9 = jsonProducts.listOfProducts[loop].productInfo[9]; p.ProductInfo10 = jsonProducts.listOfProducts[loop].productInfo[10]; p.ProductInfo11 = jsonProducts.listOfProducts[loop].productInfo[11]; p.ProductInfo12 = jsonProducts.listOfProducts[loop].productInfo[12]; p.ProductInfo13 = jsonProducts.listOfProducts[loop].productInfo[13]; p.ProductInfo14 = jsonProducts.listOfProducts[loop].productInfo[14]; p.ProductInfo15 = jsonProducts.listOfProducts[loop].productInfo[15]; p.ProductInfo16 = jsonProducts.listOfProducts[loop].productInfo[16]; p.ProductInfo17 = jsonProducts.listOfProducts[loop].productInfo[17]; p.ProductInfo18 = jsonProducts.listOfProducts[loop].productInfo[18]; p.ProductInfo19 = jsonProducts.listOfProducts[loop].productInfo[19]; ctx.Entry(p).State = System.Data.Entity.EntityState.Modified; nbSuccess++; } else { var newProduct = new Product { RfidTag = tag, ProductInfo0 = jsonProducts.listOfProducts[loop].productInfo[0], ProductInfo1 = jsonProducts.listOfProducts[loop].productInfo[1], ProductInfo2 = jsonProducts.listOfProducts[loop].productInfo[2], ProductInfo3 = jsonProducts.listOfProducts[loop].productInfo[3], ProductInfo4 = jsonProducts.listOfProducts[loop].productInfo[4], ProductInfo5 = jsonProducts.listOfProducts[loop].productInfo[5], ProductInfo6 = jsonProducts.listOfProducts[loop].productInfo[6], ProductInfo7 = jsonProducts.listOfProducts[loop].productInfo[5], ProductInfo8 = jsonProducts.listOfProducts[loop].productInfo[8], ProductInfo9 = jsonProducts.listOfProducts[loop].productInfo[9], ProductInfo10 = jsonProducts.listOfProducts[loop].productInfo[10], ProductInfo11 = jsonProducts.listOfProducts[loop].productInfo[11], ProductInfo12 = jsonProducts.listOfProducts[loop].productInfo[12], ProductInfo13 = jsonProducts.listOfProducts[loop].productInfo[13], ProductInfo14 = jsonProducts.listOfProducts[loop].productInfo[14], ProductInfo15 = jsonProducts.listOfProducts[loop].productInfo[15], ProductInfo16 = jsonProducts.listOfProducts[loop].productInfo[16], ProductInfo17 = jsonProducts.listOfProducts[loop].productInfo[17], ProductInfo18 = jsonProducts.listOfProducts[loop].productInfo[18], ProductInfo19 = jsonProducts.listOfProducts[loop].productInfo[19], }; ctx.Products.Add(newProduct); nbSuccess++; } ctx.SaveChanges(); } ctx.Database.Connection.Close(); ctx.Dispose(); if (MyHostEvent != null) { MyHostEvent(this, new MyHostEventArgs("AddOrUpdateProduct", nbSuccess + " Product(s) added")); } return("Success"); } else { return("Failed : List of product is null or empty"); } } catch (Exception exp) { return("failed : " + exp.InnerException); } }