/// <summary>
 /// reset robot PLC status, change the busy to no busy status, so that auto test could  restart to work
 /// </summary>
 /// <param></param>
 public void resetRobotPLCState()
 {
     PlCisbusy = false;
     if (dutIdListInQueue.Count > 0)
     {
         dutIdListInQueue.RemoveAt(0);
     }
     RobotPLCStatus.UpdateRobotPLCStatus("Idle");
     LogerHelper2.ToLog("Robot Arm has reset.", 2);
 }
 void SendMsgToServer_moveRobotPLCManually(int stationNo, string result)
 {
     if (PlCisbusy == false && plcserver.isPLConLine == true)
     {
         if (result.Contains("NG"))
         {
             plcserver.MoveDUTFromTester(stationNo.ToString(), "01");//fail
         }
         else
         {
             plcserver.MoveDUTFromTester(stationNo.ToString(), "00");//pass
         }
         PlCisbusy = true;
         LogerHelper2.ToLog("Manually Move DUT from" + stationNo + " to " + result, 2);
     }
     else
     {
         RobotPLCStatus.UpdateRobotPLCStatus("busy");
     }
 }
        /// <summary>
        /// the event of server receive message from PLC, different messagetype do different action
        /// </summary>
        /// <param name="sender">plc ip&port</param>
        /// <param name="msg">plc message</param>
        public void plcserver_MessageReceived(object sender, MainPLCMessage msg)
        {
            if (msg.InvalidMessage == true)
            {
                LogerHelper2.ToLog("error format PLC commond" + msg.Info, 3);
                return;
            }
            string msgTemp = null;

            try
            {
                msgTemp = string.Format("From PLC: MessageHead: {0}\t Info:{1}", msg.MsgHead.ToString(), msg.Info.ToString());
            }
            catch (Exception e)
            {
                LogerHelper2.ToLog(e.Message, 2);
            }

            //LogerHelper2.ToLog(msg.MsgType + " " + msg.MsgHead + " " + msg.StationId + " " + msg.Info, 0);

            if (msg.MsgType != PLCMsgType.Heartbead)
            {
                LogerHelper2.ToLog(msgTemp, 0);
            }
            else
            {
                LogerHelper2.ToLog(msgTemp, 4);
            }
            int    freeStationID;
            string MessageHead = msg.MsgHead;
            string MessageInfo = msg.Info;

            //LogerHelper2.ToLog(msg.MsgType + " " , 0);
            switch (msg.MsgType)
            {
            case PLCMsgType.ReportMsg:
                if (MessageHead == MainPLCMessage.DUTIdToServer)
                {
                    plcserver.NewSNFeedback();      //feedback plc F020

                    //check the dutid, if it same as the current test duts? ignore it if yes
                    if (!check_DUTID_Valid(msg.Info))
                    {
                        LogerHelper2.ToLog("Found new Dut Id <" + msg.Info + ">  is same as current testing Duts' Id, will ignore this new Dut Id ", 2);
                        LogerHelper2.ToAutoTestLogFile(DateTime.Now.ToString() + ": Dut ID: " + msg.Info + " duplicated with the current testing Duts' Id, will ignore it.");
                        return;
                    }

                    freeStationID = FetchFreeStation();
                    int loop = 0;
                    while (loop < stationList.Length)
                    {
                        newDutNm = msg.Info;
                        //newSerailNumber.Enqueue(msg.Info);  // when coming a new sn, add to qu
                        if (freeStationID >= 1 && freeStationID <= 6)
                        {
                            stationList[freeStationID - 1].clientStatusType = ClientStatusType.Wait;
                            //if (server.SendMsg(new Message(freeStationID.ToString(), 1, MsgType.QueryClientStatus, ""), stationList[freeStationID - 1].ahaddress))
                            //{
                            //    break;
                            //}

                            freeStationID = FetchFreeStation(freeStationID);
                        }
                        else
                        {
                            //idMessageQueue.sendMsg("DUTID", msg.Info, System.Messaging.MessagePriority.Normal);  // if there is no idle station, send the id message to messagequeue
                            //LogerHelper2.ToLog("NO freestation, add newdutid <" + msg.Info + ">  to queue. idqueue number is: " + idMessageQueue.messageNum, 2);
                            break;
                        }

                        loop++;
                    }
                    if (loop >= stationList.Length)
                    {
                        //idMessageQueue.sendMsg("DUTID", msg.Info, System.Messaging.MessagePriority.Normal);  // if there is no idle station, send the id message to messagequeue
                        //LogerHelper2.ToLog("NO freestation, add newdutid <" + msg.Info + ">  to queue. idqueue number is: " + idMessageQueue.messageNum, 2);
                    }
                }
                break;

            case PLCMsgType.Feedback:
                plcserver.stopPlcCommandTimer();
                System.Threading.Thread.Sleep(1000);     //sleep wait PLC ready

                if (MessageHead == MainPLCMessage.MoveDUTToStationACK)
                {
                    //PlCisbusy = false;
                    RobotPLCStatus.UpdateRobotPLCStatus("Idle");
                    server.SendMsg(new Message(msg.StationId.ToString(), 1, MsgType.NewDut, stationList[msg.StationId - 1].onTestingSerialNumber), stationList[msg.StationId - 1].ahaddress);    //dutid
                    UpdateStation(msg.StationId.ToString(), ClientStatusType.Run);
                    //PlcMessageQueue.DeleteMsg(msg.StationId.ToString());
                    PlCisbusy = false;
                }
                else if (MessageHead == MainPLCMessage.MoveDUTFromStationACK)
                {
                    isOutBufferFree = false;

                    // here add if to check whether the tester is set to offline?
                    if ((stationList[msg.StationId - 1].isForcedOffLine == true) || (stationList[msg.StationId - 1].DUTTestConsecutiveFailedTimes >= DUTReTest.alarmWhenDUTTestFailTimes))
                    {
                        stationList[msg.StationId - 1].isAvaliable     = false;
                        stationList[msg.StationId - 1].isForcedOffLine = false;
                        stationList[msg.StationId - 1].errorInfo       = "This station was forced offline by admin or DUT test consecutive failures";
                        UpdateStation((msg.StationId).ToString(), ClientStatusType.Error);

                        ClientGUFStatus.UpdateStationsData(msg.StationId, 2, ClientStatusType.Unknown);
                        ClientGUFStatus.UpdateStationsData(msg.StationId, 3, ClientStatusType.Unknown);
                    }
                    else
                    {
                        //PlCisbusy = false;
                        RobotPLCStatus.UpdateRobotPLCStatus("Idle");
                        stationList[msg.StationId - 1].isAvaliable           = true;
                        stationList[msg.StationId - 1].currentDUTtestedCount = 0;
                        stationList[msg.StationId - 1].onTestingSerialNumber = "";
                        UpdateStation((msg.StationId).ToString(), ClientStatusType.Idle);
                    }

                    PlCisbusy = false;
                }
                else if (MessageHead == MainPLCMessage.queryPLCStateACK)
                {
                    PlCisbusy = msg.PLCIsBusy;
                }

                if (PlCisbusy == false)
                {
                    QueryMessageListAndRunFirstMessage("DUTSTATUS");
                }
                break;

            case PLCMsgType.Heartbead:
                // RobotPLCStatus.UpdateRobotPLCStatus("Connected");
                plcserver.CheckHeartbeat();
                break;

            case PLCMsgType.Ack:
                plcserver.plcResendCommandTimer.Stop();
                plcserver.lastAction = string.Empty;
                break;

            default:
                break;
            }
        }