コード例 #1
0
 public void FrontRepairReleasePallet()
 {
     PLCWrapper plc = new PLCWrapper(opcURL);
     
     Opc.Da.ItemValue[] writeValues = new Opc.Da.ItemValue[1];
     writeValues[0] = new Opc.Da.ItemValue();
     writeValues[0].ItemName = frontRepairTesterReleasePallet;
     writeValues[0].Value = 1;
     var t = plc.WriteTags(writeValues);
 }
コード例 #2
0
        public async Task<List<TestRepairRequirement>> InitializeRepairTester([FromBody]List<TestRepairRequirement> req, int lineId, int testerMode, int testId)
       {
            PLCWrapper plc = new PLCWrapper(opcURL);
            CancellationTokenSource cts = new CancellationTokenSource();
            var timeout = 1000000;

            var requestedTest = req.Where(r => r.TestId == testId).FirstOrDefault();

            if (lineId == 1) //front line
            {
                if (requestedTest != null)
                {
                    //if RU OR tester mode is in RT/RU InTouch Mode
                    if (((requestedTest.BuildCode & (int)Math.Pow(2, 17)) == Math.Pow(2, 17)) || testerMode == 2)
                    {
                        if (requestedTest.TestId == 3) //FAM Tester
                        {
                            Tester inTouchFamTester = new InTouchTester(opcRUURL, frtRepairFamTesterTopic);
                            inTouchFamTester.ResetTester();

                            Tester inTouchRepairTester = new InTouchTester(opcRUURL, frtRepairTesterTopic);
                            inTouchRepairTester.ResetTester();

                            await Task.Delay(2000);
                            inTouchFamTester.InitializeTester(requestedTest.JobId, requestedTest.PalletId, requestedTest.BuildCode, requestedTest.BuildCode);

                            var result = await inTouchFamTester.WaitForResponse(requestedTest);
                            requestedTest = result;
                            
                        }
                        else if (requestedTest.TestId == 1 || requestedTest.TestId == 2)
                        {
                            //check if fam is done
                            if (req.Where(r => r.TestId == 3).Any())
                            {
                                var famTest = req.Where(r => r.TestId == 3).FirstOrDefault();
                                if (!famTest.Repaired)
                                    throw new HttpResponseException(
                                        HttpErrorResponse.GetHttpErrorResponse(
                                        HttpStatusCode.RequestTimeout, "FAM Test Required", string.Format("FAM Test Required")));
                            }

                            var drv = req.Where(r => r.TestId == 1).FirstOrDefault();
                            var pas = req.Where(r => r.TestId == 2).FirstOrDefault();

                            if (drv != null && pas != null)
                            {
                                Tester inTouchTester = new InTouchTester(opcRUURL, frtRepairTesterTopic);
                                inTouchTester.ResetTester();

                                await Task.Delay(2000);

                                inTouchTester.InitializeTester(requestedTest.JobId, requestedTest.PalletId, drv.BuildCode, pas.BuildCode);
                                var result = await inTouchTester.WaitForResponse(requestedTest);
                                if (result.Repaired)
                                {
                                    var allfrtTest = req.Where(r => r.TestId == 1 || r.TestId == 2).ToList();
                                    foreach (var t in allfrtTest)
                                    {
                                        t.Repaired = true;
                                        t.Dtm = DateTime.Now;
                                        t.TestResult = new TestRepairResult {
                                            TestResult = 1,
                                            TestStatus = 1,
                                            TestResultDetails = new List<TestRepairResultDetail>()
                                        };
                                    }

                                    Tester inTouchFamTester = new InTouchTester(opcRUURL, frtRepairFamTesterTopic);
                                    inTouchFamTester.ResetTester();
                                }
                            }
                            
                        }
                    }
                    else //RT LEGACY CODE
                    {
                        #region RT Tester Legacy Code
                        Opc.Da.ItemValue[] writeValues2 = new Opc.Da.ItemValue[1];
                        writeValues2[0] = new Opc.Da.ItemValue();
                        writeValues2[0].ItemName = frontRepairTesterBuildCode;
                        writeValues2[0].Value = requestedTest.BuildCode;
                        plc.WriteTags(writeValues2);

                        PLCFactory waitFactory = new PLCFactory(opcURL);
                        Opc.Da.Item[] tags = new Opc.Da.Item[1];
                        tags[0] = new Opc.Da.Item();
                        tags[0].ItemName = frontRepairTesterDataReady;

                        var task = waitFactory.WaitReadTags(tags, cts.Token);

                        if (await Task.WhenAny(task, Task.Delay(timeout, cts.Token)) == task)
                        {
                            var result = await task;

                            if (result.Status == 1)
                            {
                                //Read Result and Status Tags
                                Opc.Da.Item[] readResultTags = new Opc.Da.Item[2];
                                readResultTags[0] = new Opc.Da.Item();
                                readResultTags[0].ItemName = frontRepairTesterResult;
                                readResultTags[1] = new Opc.Da.Item();
                                readResultTags[1].ItemName = frontRepairTesterStatus;

                                var results = plc.ReadTags(readResultTags);

                                int testResult = Convert.ToInt32(results[0].Value.ToString());
                                int testStatus = Convert.ToInt32(results[1].Value.ToString());

                                //passed
                                if (testResult == testStatus)
                                    requestedTest.Repaired = true;
                                else
                                    requestedTest.Repaired = false;

                                requestedTest.TestResult = new TestRepairResult
                                {
                                    TestResult = testResult,
                                    TestStatus = testStatus,
                                    TestResultDetails = GetTestRepairResult(requestedTest, testResult, testStatus),
                                };

                                //Ack PLC
                                Opc.Da.Item[] requestIDTags = new Opc.Da.Item[1];
                                requestIDTags[0] = new Opc.Da.Item();
                                requestIDTags[0].ItemName = frontRepairTesterRequestID;

                                var requestIDTagsResults = plc.ReadTags(requestIDTags);
                                var requestIDTagsResult = Convert.ToInt32(requestIDTagsResults[0].Value.ToString());

                                Opc.Da.ItemValue[] responseIDTags = new Opc.Da.ItemValue[1];
                                responseIDTags[0] = new Opc.Da.ItemValue();
                                responseIDTags[0].ItemName = frontRepairTesterResponseID;
                                responseIDTags[0].Value = requestIDTagsResult;
                                plc.WriteTags(responseIDTags);

                                return req;
                            }
                            else
                            {
                                throw new HttpResponseException(
                                    HttpErrorResponse.GetHttpErrorResponse(
                                    HttpStatusCode.BadRequest, "Error", string.Format("Error Has Occured")));
                            }
                        }
                        else
                        {
                            throw new HttpResponseException(
                                HttpErrorResponse.GetHttpErrorResponse(
                                HttpStatusCode.RequestTimeout, "Request Timeout", string.Format("Timeout Has Occured")));
                        }
                        #endregion
                    }
                }

                return req;
            }
            else
            {
                if (requestedTest != null)
                {
                    //if RU OR tester mode is in RT/RU InTouch Mode
                    if (((requestedTest.BuildCode & (int)Math.Pow(2, 14)) == Math.Pow(2, 14)) || testerMode == 2)
                    {
                        #region RU Tester
                        if (requestedTest.TestId == 3 || requestedTest.TestId == 4)
                        {
                            Tester inTouchTester = new InTouchTester(opcRUURL, rearRepairTesterTopic);
                            inTouchTester.ResetTester();

                            await Task.Delay(4000);

                            if (requestedTest.TestId == 3)  //2nd row recipe
                                inTouchTester.InitializeTester(requestedTest.JobId, requestedTest.PalletId, requestedTest.BuildCode, 0);
                            else if (requestedTest.TestId == 4) //3rd row recipe
                                inTouchTester.InitializeTester(requestedTest.JobId, requestedTest.PalletId, 0, requestedTest.BuildCode);

                            var result = await inTouchTester.WaitForResponse(requestedTest);
                            requestedTest = result;
                        }
                        #endregion
                    }
                    else
                    {
                        #region RT Tester Legacy Code
                        Opc.Da.ItemValue[] writeValues2 = new Opc.Da.ItemValue[1];
                        writeValues2[0] = new Opc.Da.ItemValue();
                        writeValues2[0].ItemName = rearRepairTesterBuildCode;
                        writeValues2[0].Value = requestedTest.BuildCode;
                        plc.WriteTags(writeValues2);

                        PLCFactory waitFactory = new PLCFactory(opcURL);
                        Opc.Da.Item[] tags = new Opc.Da.Item[1];
                        tags[0] = new Opc.Da.Item();
                        tags[0].ItemName = rearRepairTesterDataReady;

                        var task = waitFactory.WaitReadTags(tags, cts.Token);

                        if (await Task.WhenAny(task, Task.Delay(timeout, cts.Token)) == task)
                        {
                            var result = await task;

                            if (result.Status == 1)
                            {
                                //Read Result and Status Tags
                                Opc.Da.Item[] readResultTags = new Opc.Da.Item[2];
                                readResultTags[0] = new Opc.Da.Item();
                                readResultTags[0].ItemName = rearRepairTesterResult;
                                readResultTags[1] = new Opc.Da.Item();
                                readResultTags[1].ItemName = rearRepairTesterStatus;

                                var results = plc.ReadTags(readResultTags);

                                int testResult = Convert.ToInt32(results[0].Value.ToString());
                                int testStatus = Convert.ToInt32(results[1].Value.ToString());

                                //passed
                                if (testResult == testStatus)
                                    requestedTest.Repaired = true;
                                else
                                    requestedTest.Repaired = false;

                                requestedTest.TestResult = new TestRepairResult
                                {
                                    TestId = requestedTest.TestId,
                                    TestResult = testResult,
                                    TestStatus = testStatus,
                                    TestResultDetails = GetTestRepairResult(requestedTest, testResult, testStatus),
                                };

                                await Task.Delay(2000);

                                //Ack PLC
                                Opc.Da.Item[] requestIDTags = new Opc.Da.Item[1];
                                requestIDTags[0] = new Opc.Da.Item();
                                requestIDTags[0].ItemName = rearRepairTesterRequestID;

                                var requestIDTagsResults = plc.ReadTags(requestIDTags);
                                var requestIDTagsResult = Convert.ToInt32(requestIDTagsResults[0].Value.ToString());

                                Opc.Da.ItemValue[] responseIDTags = new Opc.Da.ItemValue[1];
                                responseIDTags[0] = new Opc.Da.ItemValue();
                                responseIDTags[0].ItemName = rearRepairTesterRespondID;
                                responseIDTags[0].Value = requestIDTagsResult;
                                plc.WriteTags(responseIDTags);

                                return req;
                            }
                            else
                            {
                                throw new HttpResponseException(
                                    HttpErrorResponse.GetHttpErrorResponse(
                                    HttpStatusCode.BadRequest, "Error", string.Format("Error Has Occured")));
                            }
                        }
                        else
                        {
                            throw new HttpResponseException(
                                HttpErrorResponse.GetHttpErrorResponse(
                                HttpStatusCode.RequestTimeout, "Request Timeout", string.Format("Timeout Has Occured")));
                        }
                        #endregion
                    }
                }

                return req;

            }

        }