using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; using Asm.As.Oib.Client; using com.itac.mes.imsapi.client.dotnet; using com.itac.mes.tools; using com.itac.mes.imsapi.domain.container; using KeyValue = com.itac.mes.imsapi.domain.container.KeyValue; using www.siplace.com.OIB._2008._05.SetupCenter.Contracts.Data; using com.itac.mes.imsapi; namespace com.itac.mes { class ContainerCheckThread { private static string attributeName = "SIMM-ManualSetToZero"; public static int MIN_INTERVAL_SECONDS = 10; private TimeSpan interval; private Thread thread; private IIMSApiDotNet imsapi; private SetupCenterNotificationClient setupCenterNotificationClient; private string stationNumber; private IMSApiSessionContextStruct sessionContext; private KeyValue[] materialBinFilters = new KeyValue[] { new KeyValue("MATERIAL_BIN_NUMBER", "*") }; private AttributeInfo[] attributes = new AttributeInfo[] { new AttributeInfo(attributeName, "1") }; private string[] materialBinResultKeys = new string[] { "MATERIAL_BIN_NUMBER" }; public ContainerCheckThread(TimeSpan interval, IIMSApiDotNet imsapi, IMSApiSessionContextStruct sessionContext, SetupCenterNotificationClient setupCenterNotificationClient, String stationNumber) { // none of those values must be null this.interval = interval; this.imsapi = imsapi; this.setupCenterNotificationClient = setupCenterNotificationClient; this.stationNumber = stationNumber; this.sessionContext = sessionContext; if (interval.TotalSeconds >= MIN_INTERVAL_SECONDS) { thread = new Thread(CheckContainers); } } public void Start() { if (thread != null) { thread.Start(); } } public void CheckContainers() { LogHandler.log(Constants.LOGGER, TraceEventType.Information, "start checking empty containers in MES (having attribute " + attributeName + "==1)"); while (true) { try { string[] resultValues; int objectType = 2; string attributeValue = "1"; int maxRows = 100; KeyValue[] attributeFilters = new KeyValue[0]; // getContainers with attribute int returnCode = imsapi.attribGetObjectsForAttributeValues(sessionContext, stationNumber, objectType, attributeName, attributeValue, maxRows, attributeFilters, materialBinResultKeys, out resultValues); if (returnCode == 0 || returnCode == 3) { // for every container: // send this info to setupCenter and close them within SC // if OK remove the attribute from the container, otherwise set appropriate attribute value foreach (String containerId in resultValues) { consumePackagingUnitInSetupCenter(containerId); } } else if (returnCode == -932 || returnCode > 0) { LogHandler.log(Constants.LOGGER, TraceEventType.Information, "no empty containers found"); } else if (returnCode < 0) { LogHandler.log(Constants.LOGGER, TraceEventType.Error, "failed to retrieve empty containers from MES, apiRet Code=" + returnCode); } } catch (Exception exception) { LogHandler.log(Constants.LOGGER, TraceEventType.Critical, "processing empty containers failed", exception); } finally { DateTime nextRun = DateTime.Now + interval; LogHandler.log(Constants.LOGGER, TraceEventType.Information, "next run for checking empty containers (having attribute " + attributeName + "==1) in " + interval + " at " + nextRun.ToLongTimeString()); Thread.Sleep(interval); } } } protected void consumePackagingUnitInSetupCenter(string containerId) { // create an ASM packaging unit; for delete it is sufficient to have an UID only PackagingUnit packagingUnit = new PackagingUnit(); packagingUnit.UID = containerId; PackagingUnitConsumedReport packUnitConsumedReport = new PackagingUnitConsumedReport(); packUnitConsumedReport.PackagingUnits = new PackagingUnit[] { packagingUnit }; LogHandler.log(Constants.LOGGER, TraceEventType.Information, "raise delete packagingUnit " + containerId + " in SetupCenter"); try { setupCenterNotificationClient.RaisePackagingUnitConsumed(packUnitConsumedReport); // delete Packaging unite succeeded // remove attribute from container /* example call: attribRemoveAttributeValue() stationNumber= 01010010 objectType= 2 objectNumber= 000197630005 objectDetail= -1 attributeCode= SIMM - ManualSetToZero attributeValueKey= 1 ------------------------------------------------------------------ Result_attribRemoveAttributeValue .return_value= 0 '' */ LogHandler.log(Constants.LOGGER, TraceEventType.Information, "remove attribute " + attributeName + " from container " + containerId); int objectType = 2; string objectDetail = "-1"; string attributeValueKey = "1"; int retValue = imsapi.attribRemoveAttributeValue(sessionContext, stationNumber, objectType, containerId, objectDetail, attributeName, attributeValueKey); if (retValue != 0) { LogHandler.log(Constants.LOGGER, TraceEventType.Error, "removing attribute " + attributeName + " from container " + containerId + " failed"); } else { LogHandler.log(Constants.LOGGER, TraceEventType.Information, "removing attribute " + attributeName + " from container " + containerId + " succeded"); } } catch (Exception e) { LogHandler.log(Constants.LOGGER, TraceEventType.Error, "raise consumed event for packagingUnit " + containerId + " failed", e); // modify attribute value /* example call attribAppendAttributeValues() stationNumber= 01010010 objectType= 2 objectNumber= 000197630005 objectDetail= -1 bookDate= -1 allowOverWrite= 1 attributeUploadKeys.length= 3 attributeUploadKeys[0]= ATTRIBUTE_CODE attributeUploadKeys[1]= ATTRIBUTE_VALUE attributeUploadKeys[2]= ERROR_CODE attributeUploadValues.length= 3 attributeUploadValues[0]= SIMM - ManualSetToZero attributeUploadValues[1]= 3 attributeUploadValues[2]= 0 ------------------------------------------------------------------ Result_attribAppendAttributeValues .return_value= 0 '' .attributeResultValues[] empty */ LogHandler.log(Constants.LOGGER, TraceEventType.Information, "update attribute " + attributeName + " from container " + containerId + " to value 2"); int objectType = 2; string objectDetail = "-1"; long bookDate = -1; int allowOverwrite = 1; string[] attributeUploadKeys = new string[] { "ATTRIBUTE_CODE", "ATTRIBUTE_VALUE", "ERROR_CODE" }; string[] attributeUploadValues = new string[] { attributeName, "2", "0" }; string[] attributeResultValues = null; int retValue = imsapi.attribAppendAttributeValues(sessionContext, stationNumber, objectType, containerId, objectDetail, bookDate, allowOverwrite, attributeUploadKeys, attributeUploadValues, out attributeResultValues); if (retValue != 0) { LogHandler.log(Constants.LOGGER, TraceEventType.Error, "setting attribute " + attributeName + " from container " + containerId + " to value 2 failed"); } } } } }