private void fillProductBuffer() { try { // fill the buffer only if it is empty ! if (this.productBuffer.Count == 0) { Batch enBatch = null; // check database for new batches in queue ?! lineQueue.Refresh(); // check and get first batch from LineQueue if (lineQueue.Count > 0) { enBatch = lineQueue.Peek(); } if (enBatch == null) { //myLog.LogAlert(AlertType.System, detroitDataSet.LineId.ToString(), this.GetType().ToString(), "fillProductBuffer()", // String.Format("Line Queue is empty! There is no any batch."), "system"); } else { lineQueue.Dequeue(); // remove the batch from LineQueue !! enBatch.PutOnLine(); // change state of the Batch myLog.LogAlert(AlertType.Info, detroitDataSet.LineId.ToString(), this.GetType().ToString(), "fillProductBuffer()", String.Format("Putting batch {0} on line #{1}.", enBatch.Name, detroitDataSet.LineId.ToString()), "system"); int i = 0; while (i < enBatch.Capacity) { i++; Product newProduct = new Product(enBatch, i, this.myLog, this.detroitDataSet); this.productBuffer.Enqueue(newProduct); enBatch.AddProduct(newProduct); myLog.LogAlert(AlertType.Info, detroitDataSet.LineId.ToString(), this.GetType().ToString(), "fillProductBuffer()", String.Format("Putting new product {0} on line #{1}.", newProduct.Name, detroitDataSet.LineId.ToString()), "system"); // This functionality is providing by SlowTask! // put product in table in Detroit LineSnapshot for plan calculation // //this.lineSnapshotTable.AddLineSnapshotRow( // -1, // enBatch.Id, // newProduct.Id, // (int)ProductState.InBuffer, // this.detroitDataSet.LineId, // newProduct.Name, // 123456, //TODO: this.taktDuration - this.lineCounter, // DateTime.Now, // newProduct.Router.State //); } //x this.lineSnapshotTableAdapter.Update(this.lineSnapshotTable); batchesOnLine.Enqueue(enBatch); } } } catch (Exception ex) { this.myLog.LogAlert(AlertType.Error, this.detroitDataSet.LineId.ToString(), ex.TargetSite.ToString(), ex.Source.ToString(), ex.Message.ToString(), "system"); } }
/// <summary> /// Load products from line's inbox from database /// </summary> /// <param name="enProduct"></param> /// <returns></returns> private void fillProductBufferFromInbox() { /* * 1. read filtered rows from ProductBufferTable * 2. in routine create batch for product if necessary and put it into BatchesOnLine * 3. create product, link it to batch and put into this.productBuffer queue * 4. delete row from ProductBufferTable * * enjoy! */ List <DataRow> rowsToRemove = new List <DataRow>(); try { this.productInboxBufferTableAdapter.FillByLineId(this.detroitDataSet.ProductBuffer, this.detroitDataSet.LineId); int i = 0; while (i < this.detroitDataSet.ProductBuffer.Rows.Count) { DataRow queueRow = this.detroitDataSet.ProductBuffer.Rows[i]; //---------------------------------------------------------------- // 2. create batch for product if necessary and put it into BatchesOnLine. // Check if there is already batch with ID in "BatchesOnLine" //---------------------------------------------------------------- int batchId = Convert.ToInt32(queueRow["BatchId"]); Batch enBatch = this.batchesOnLine.FirstOrDefault(p => p.Id.Equals(batchId)); if (enBatch != null) { // batch already exists in "BatchesOnLine" } else // create new batch { enBatch = new Batch( null, queueRow["Nummer"].ToString(), queueRow["BatchType_Name"].ToString() , (int)queueRow["Capacity"], (int)queueRow["BatchId"], (int)queueRow["BatchTypeId"], 0 , (int)queueRow["Takt"] ); this.batchesOnLine.Enqueue(enBatch); enBatch.PutOnLine(); } //------------------------------------------------------------------------------- // 3. create product, link it to batch and put into this.productBuffer queue //------------------------------------------------------------------------------- int productId = Convert.ToInt32(queueRow["ProductNummer"]); Product newProduct = new Product(enBatch, productId, this.myLog, this.detroitDataSet); enBatch.AddProduct(newProduct); this.productBuffer.Enqueue(newProduct); // put product in table in Detroit LineSnapshot for plan calculation //this.lineSnapshotTable.AddLineSnapshotRow( // -1, // enBatch.Id, // newProduct.Id, // (int)ProductState.InBuffer, // this.detroitDataSet.LineId, // newProduct.Name, // 123456, //TODO: this.taktDuration - this.lineCounter, // DateTime.Now, // newProduct.Router.State //); //------------------------------------------------------------------------------- // 4. delete row from ProductBufferTable //------------------------------------------------------------------------------- if (queueRow.RowState != DataRowState.Deleted) { queueRow.Delete(); } i++; } this.productInboxBufferTableAdapter.Update(this.detroitDataSet.ProductBuffer); //x this.detroitDataSet.ProductBuffer.AcceptChanges(); } catch (Exception ex) { this.myLog.LogAlert(AlertType.Error, this.detroitDataSet.LineId.ToString(), ex.TargetSite.ToString(), ex.Source.ToString(), ex.Message.ToString(), "system"); } }
/// <summary> /// restoreProductsFormLineSnapshot() /// Restores objects form Detroit database, table LineSnapshot: /// - products on line /// - product buffer /// - batches on line /// - ? uncomplete products /// </summary> private void restoreProductsFormLineSnapshot() { try { DetroitDataSetTableAdapters.LineSnapshotReaderTableAdapter readerTableAdapter = new LineSnapshotReaderTableAdapter(); readerTableAdapter.Fill(this.detroitDataSet.LineSnapshotReader, this.detroitDataSet.LineId); int i = 0; while (i < this.detroitDataSet.LineSnapshotReader.Rows.Count) { DataRow queueRow = this.detroitDataSet.LineSnapshotReader.Rows[i]; int lineId = Convert.ToInt32(queueRow["LineId"]); if (lineId == this.detroitDataSet.LineId) { //---------------------------------------------------------------- // 2. create batch for product if necessary and put it into BatchesOnLine. // Check if there is already batch with ID in "BatchesOnLine" //---------------------------------------------------------------- int batchId = Convert.ToInt32(queueRow["BatchId"]); Batch enBatch = batchesOnLine.FirstOrDefault(p => p.Id.Equals(batchId)); if (enBatch != null) { // batch already exists in "BatchesOnLine" } else // create new batch { enBatch = new Batch( null, queueRow["BatchNummer"].ToString(), queueRow["BatchType_Name"].ToString() , (int)queueRow["Capacity"], (int)queueRow["BatchId"], (int)queueRow["BatchTypeId"], 0 , (int)queueRow["Takt"] ); batchesOnLine.Enqueue(enBatch); enBatch.PutOnLine(); } //------------------------------------------------------------------------------- // 3. create product, link it to batch and put into this.productBuffer queue //------------------------------------------------------------------------------- int productId = Convert.ToInt32(queueRow["ProductNummer"]); Product enProduct = this.productsOnLine.FirstOrDefault(p => p.Id.Equals(productId)); if (enProduct == null) { enProduct = new Product(enBatch, productId, this.myLog, this.detroitDataSet); } else { enBatch.AddProduct(enProduct); } int stationId = Convert.ToInt32(queueRow["StationId"]); if (stationId < 0) { productBuffer.Enqueue(enProduct); } else if (stationId > 0) { string stationName = queueRow["StationName"].ToString(); this.lineDispatcher.RestoreProductOnStation(enProduct, stationName); if (!this.productsOnLine.Contains(enProduct)) { this.productsOnLine.Enqueue(enProduct); } string savedState = queueRow["RouterState"].ToString(); enProduct.Router.Restore(savedState); } } //------------------------------------------------------------------------------- // 4. next row from ProductBufferTable //------------------------------------------------------------------------------- i++; } } catch (Exception ex) { this.myLog.LogAlert(AlertType.Error, this.detroitDataSet.LineId.ToString(), ex.TargetSite.ToString(), ex.Source.ToString(), ex.Message.ToString(), "system"); } }