Esempio n. 1
0
        public void On_Connection_Message(object MsgObj)
        {
            try
            {
                string Msg = (string)MsgObj;
                //if (Vendor.ToUpper().Equals("ACDT"))
                //{
                //    byte[] byteAry = Encoding.ASCII.GetBytes(Msg);


                //    logger.Debug(DeviceName + " Recieve:" + BitConverter.ToString(byteAry));
                //}
                //else
                //{

                //}
                //Node Target = null;

                List <CommandReturnMessage> ReturnMsgList = _Decoder.GetMessage(Msg);
                foreach (CommandReturnMessage ReturnMsg in ReturnMsgList)
                {
                    //logger.Info(DeviceName + " Recieve:" + ReturnMsg.OrgMsg);
                    if (!this.Vendor.ToUpper().Equals("MITSUBISHI_PLC"))
                    {
                        _ReportTarget.On_Message_Log("CMD", DeviceName + " Recieve:" + ReturnMsg.OrgMsg);
                    }
                    try
                    {
                        Transaction Txn  = null;
                        Node        Node = null;
                        if (ReturnMsg != null)
                        {
                            lock (TransactionList)
                            {
                                if (ReturnMsg.Command != null)
                                {
                                    if (ReturnMsg.Command.Equals("PAUSE"))
                                    {
                                        foreach (Transaction t in TransactionList.Values)
                                        {
                                            t.SetTimeOutMonitor(false);
                                        }
                                    }
                                    if (ReturnMsg.Command.Equals("CONT_"))
                                    {
                                        foreach (Transaction t in TransactionList.Values)
                                        {
                                            t.SetTimeOutMonitor(true);
                                        }
                                    }
                                }


                                string key = "";
                                if (Vendor.ToUpper().Equals("KAWASAKI"))
                                {
                                    key = ReturnMsg.Seq;
                                }
                                else if (Vendor.ToUpper().Equals("HST") || Vendor.ToUpper().Equals("COGNEX"))
                                {
                                    key = "1" + ReturnMsg.Command;
                                }

                                else if (Vendor.ToUpper().Equals("SANWA") || Vendor.ToUpper().Equals("ATEL_NEW"))
                                {
                                    key = ReturnMsg.NodeAdr + ReturnMsg.Command;
                                    for (int seq = 0; seq <= 99; seq++)
                                    {
                                        string tmpKey = key + seq.ToString("00");

                                        if (TransactionList.ContainsKey(tmpKey))
                                        {
                                            key = tmpKey;
                                            break;
                                        }
                                        if (seq == 99)
                                        {
                                            logger.Error("seq is run out!");
                                        }
                                    }
                                }
                                else if (Vendor.ToUpper().Equals("SANWA_MC"))
                                {
                                    //if (ReturnMsg.Command.Equals("MCR__"))
                                    //{
                                    if (ReturnMsg.Command.Equals("RESET"))
                                    {
                                        key = "0";
                                    }
                                    else
                                    {
                                        key = ReturnMsg.NodeAdr;
                                    }
                                    //}
                                    //else
                                    //{
                                    //    key = "0" + ReturnMsg.Command;
                                    //}
                                }
                                else if (DeviceType.Equals("SMARTTAG"))
                                {
                                    key = "00";
                                }
                                else
                                {
                                    key = (ReturnMsg.NodeAdr + ReturnMsg.Command).Equals("") ? "0" : ReturnMsg.NodeAdr + ReturnMsg.Command;
                                }

                                if (Vendor.ToUpper().Equals("KAWASAKI"))
                                {
                                    if (TransactionList.TryGetValue(key, out Txn))
                                    {
                                        Node = NodeManagement.Get(Txn.NodeName);

                                        if (!Txn.CommandType.Equals("CMD"))
                                        {
                                            if (ReturnMsg.Type.Equals(CommandReturnMessage.ReturnType.Excuted))
                                            {
                                                continue;
                                            }
                                            else if (ReturnMsg.Type.Equals(CommandReturnMessage.ReturnType.Finished))
                                            {
                                                ReturnMsg.Type = CommandReturnMessage.ReturnType.Excuted;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        logger.Debug("Transaction not exist:key=" + key);
                                        return;
                                    }
                                }
                                else if (Vendor.ToUpper().Equals("TDK"))
                                {
                                    if (TransactionList.TryGetValue(key, out Txn))
                                    {
                                        Node = NodeManagement.Get(Txn.NodeName);
                                        if (Txn.CommandType.Equals("SET") && ReturnMsg.Type.Equals(CommandReturnMessage.ReturnType.Excuted))
                                        {
                                            //continue;
                                        }
                                    }
                                    else
                                    {
                                        Node = NodeManagement.GetByController(DeviceName, ReturnMsg.NodeAdr);
                                    }
                                }
                                else
                                {
                                    //if (ReturnMsg.NodeAdr.Equals("") || ReturnMsg.Command.Equals("RESET") || ReturnMsg.Command.Equals("SP___") || ReturnMsg.Command.Equals("PAUSE") || ReturnMsg.Command.Equals("CONT_") || ReturnMsg.Command.Equals("STOP_") || ReturnMsg.Command.Equals("TGEVT"))
                                    //{
                                    //    Node = NodeManagement.GetFirstByController(DeviceName);
                                    //}
                                    //else
                                    //{
                                    //    Node = NodeManagement.GetByController(DeviceName, ReturnMsg.NodeAdr);
                                    //}
                                    Node = NodeManagement.GetByController(DeviceName, ReturnMsg.NodeAdr.Equals("") ? "0" : ReturnMsg.NodeAdr);
                                    if (Node == null)
                                    {
                                        Node = NodeManagement.GetOCRByController(DeviceName);
                                    }
                                    if (Node == null)
                                    {
                                        Node = NodeManagement.GetFirstByController(DeviceName);
                                    }
                                }
                                //lock (TransactionList)
                                //{
                                //lock (Node)
                                //{
                                //Target = Node;
                                if (Node.Vendor.ToUpper().Equals("COGNEX"))
                                {
                                    if (ReturnMsg.Type == CommandReturnMessage.ReturnType.UserName)
                                    {
                                        //conn.Send("admin\r\n");
                                        continue;
                                    }
                                    if (ReturnMsg.Type == CommandReturnMessage.ReturnType.Password)
                                    {
                                        //conn.Send("\r\n");
                                        continue;
                                    }
                                }
                                if (ReturnMsg.Type == CommandReturnMessage.ReturnType.Event)
                                {
                                    //_ReportTarget.On_Event_Trigger(Node, ReturnMsg);
                                }
                                else if ((ReturnMsg.Type == CommandReturnMessage.ReturnType.Information && Node.Vendor.ToUpper().Equals("TDK") && !TransactionList.ContainsKey(key)))
                                {
                                    if (ReturnMsg.Type.Equals(CommandReturnMessage.ReturnType.Information))
                                    {
                                        //ThreadPool.QueueUserWorkItem(new WaitCallback(conn.Send), ReturnMsg.FinCommand);
                                        //conn.Send(ReturnMsg.FinCommand);
                                        //isWaiting = false;
                                        logger.Debug(DeviceName + " Send:" + ReturnMsg.FinCommand);
                                    }
                                }
                                else if (TransactionList.TryRemove(key, out Txn))
                                {
                                    // Node.InitialComplete = false;
                                    Node = NodeManagement.Get(Txn.NodeName);
                                    switch (ReturnMsg.Type)
                                    {
                                    case CommandReturnMessage.ReturnType.Excuted:
                                        if (!Txn.CommandType.Equals("CMD") && !Txn.CommandType.Equals("MOV") && !Txn.CommandType.Equals("HCS"))
                                        {
                                            logger.Debug("Txn timmer stoped.");
                                            Txn.SetTimeOutMonitor(false);
                                            lock (Node.ExcuteLock)
                                            {
                                                Node.IsExcuting = false;
                                            }
                                        }
                                        else
                                        {
                                            //if (Txn.Method.Equals(Transaction.Command.LoadPortType.Reset))
                                            //{
                                            //    logger.Debug("Txn timmer stoped.");
                                            //    Txn.SetTimeOutMonitor(false);

                                            //}
                                            //else if (Txn.Method.Equals(Transaction.Command.RobotType.OrginSearch))
                                            //{
                                            //    logger.Debug("Txn timmer stoped.");
                                            //    Txn.SetTimeOutMonitor(false);
                                            //    //Node.IsExcuting = false;
                                            //    TransactionList.TryAdd(key, Txn);
                                            //}
                                            //else
                                            //{
                                            Txn.SetTimeOutMonitor(false);
                                            Txn.SetTimeOut(Txn.MotionTimeOut);
                                            Txn.SetTimeOutMonitor(true);
                                            TransactionList.TryAdd(key, Txn);

                                            //}
                                        }
                                        //_ReportTarget.On_Command_Excuted(Node, Txn, ReturnMsg);
                                        break;

                                    case CommandReturnMessage.ReturnType.Finished:
                                        logger.Debug("Txn timmer stoped.");
                                        Txn.SetTimeOutMonitor(false);
                                        lock (Node.ExcuteLock)
                                        {
                                            Node.IsExcuting = false;
                                        }
                                        //_ReportTarget.On_Command_Finished(Node, Txn, ReturnMsg);
                                        break;

                                    case CommandReturnMessage.ReturnType.Error:
                                        logger.Debug("Txn timmer stoped.");
                                        Txn.SetTimeOutMonitor(false);
                                        lock (Node.ExcuteLock)
                                        {
                                            Node.IsExcuting = false;
                                        }
                                        //_ReportTarget.On_Command_Error(Node, Txn, ReturnMsg);
                                        if (Vendor.ToUpper().Equals("TDK") || DeviceType.ToUpper().Equals("SMARTTAG"))
                                        {
                                            //conn.Send(ReturnMsg.FinCommand);
                                            logger.Debug(DeviceName + " Send:" + ReturnMsg.FinCommand);
                                        }
                                        break;

                                    case CommandReturnMessage.ReturnType.Information:
                                        logger.Debug("Txn timmer stoped.");
                                        Txn.SetTimeOutMonitor(false);
                                        lock (Node.ExcuteLock)
                                        {
                                            Node.IsExcuting = false;
                                        }
                                        if (Vendor.ToUpper().Equals("TDK") && Txn.CommandType.Equals("SET"))
                                        {
                                            ReturnMsg.Type = CommandReturnMessage.ReturnType.Excuted;
                                        }
                                        else
                                        {
                                            ReturnMsg.Type = CommandReturnMessage.ReturnType.Finished;
                                            //Node.IsExcuting = false;
                                        }
                                        SpinWait.SpinUntil(() => false, 50);
                                        //ThreadPool.QueueUserWorkItem(new WaitCallback(conn.Send), ReturnMsg.FinCommand);
                                        //conn.Send(ReturnMsg.FinCommand);

                                        logger.Debug(DeviceName + " Send:" + ReturnMsg.FinCommand);

                                        break;
                                    }
                                }
                                else
                                {
                                    if (ReturnMsg.Type != null)
                                    {
                                        if (ReturnMsg.Type.Equals(CommandReturnMessage.ReturnType.Information))
                                        {
                                            //ThreadPool.QueueUserWorkItem(new WaitCallback(conn.Send), ReturnMsg.FinCommand);
                                            //conn.Send(ReturnMsg.FinCommand);

                                            logger.Debug(DeviceName + " Send:" + ReturnMsg.FinCommand);
                                        }
                                        else
                                        {
                                            if (ReturnMsg.Type == CommandReturnMessage.ReturnType.Error)
                                            {
                                                Txn = TransactionList.First().Value;

                                                logger.Debug("Txn timmer stoped.");
                                                Txn.SetTimeOutMonitor(false);
                                                lock (Node.ExcuteLock)
                                                {
                                                    Node.IsExcuting = false;
                                                }
                                                //_ReportTarget.On_Command_Error(Node, Txn, ReturnMsg);
                                                if (Vendor.ToUpper().Equals("TDK") || DeviceType.ToUpper().Equals("SMARTTAG"))
                                                {
                                                    //conn.Send(ReturnMsg.FinCommand);
                                                    logger.Debug(DeviceName + " Send:" + ReturnMsg.FinCommand);
                                                }

                                                TransactionList.TryRemove(TransactionList.First().Key, out Txn);
                                            }
                                            else
                                            {
                                                logger.Debug(DeviceName + "(On_Connection_Message Txn is not found. msg:" + Msg);
                                                continue;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        logger.Debug(DeviceName + "(On_Connection_Message Return type is null. msg:" + Msg);
                                        continue;
                                    }
                                }
                                //}
                            }
                            switch (ReturnMsg.Type)
                            {
                            case CommandReturnMessage.ReturnType.Information:
                            case CommandReturnMessage.ReturnType.Event:
                                Transaction t = new Transaction();
                                t.NodeName         = Node.Name;
                                t.NodeType         = Node.Type;
                                t.Value            = ReturnMsg.Value;
                                t.CommandEncodeStr = ReturnMsg.OrgMsg;
                                t.Method           = ReturnMsg.Command;

                                //TransactionRecord.AddDetail(TransactionRecord.GetUUID(), Node.Name,Node.Type,ReturnMsg.Type,ReturnMsg.Value);
                                _ReportTarget.On_Event_Trigger(Node, ReturnMsg);
                                break;

                            case CommandReturnMessage.ReturnType.Excuted:

                                _ReportTarget.On_Command_Excuted(Node, Txn, ReturnMsg);
                                if (Txn.CommandType.Equals("CMD") && !Node.Type.Equals("LOADPORT"))
                                {
                                    _ReportTarget.On_Node_State_Changed(Node, "Busy");
                                }
                                break;

                            case CommandReturnMessage.ReturnType.Finished:

                                //if (Node.Type.Equals("LOADPORT"))
                                //{
                                //    Node.InterLock = false;
                                //}

                                _ReportTarget.On_Command_Finished(Node, Txn, ReturnMsg);
                                if (!Node.Type.Equals("LOADPORT"))
                                {
                                    _ReportTarget.On_Node_State_Changed(Node, "StandBy");
                                }


                                break;

                            case CommandReturnMessage.ReturnType.Error:

                                //if (Node.Type.Equals("LOADPORT"))
                                //{
                                //    Node.InterLock = false;
                                //}
                                _ReportTarget.On_Command_Error(Node, Txn, ReturnMsg);

                                break;
                            }
                        }
                        else
                        {
                            logger.Debug(DeviceName + "(On_Connection_Message Message decode fail:" + Msg);
                        }
                    }
                    catch (Exception e)
                    {
                        logger.Error(DeviceName + "(On_Connection_Message " + IPAdress + ":" + Port.ToString() + ")" + e.Message + "\n" + e.StackTrace);
                    }
                }
            }
            catch (Exception e)
            {
                logger.Error(DeviceName + "(On_Connection_Message " + IPAdress + ":" + Port.ToString() + ")" + e.Message + "\n" + e.StackTrace);
            }
        }