123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- using Aitex.Core.Common;
- using Aitex.Core.Common.DeviceData;
- using Aitex.Core.RT.DataCenter;
- using Aitex.Core.RT.Device;
- using Aitex.Core.RT.Event;
- using Aitex.Core.RT.Log;
- using Aitex.Core.RT.OperationCenter;
- using Aitex.Core.RT.SCCore;
- using Aitex.Core.Util;
- using Aitex.Sorter.Common;
- using MECF.Framework.Common.Equipment;
- using MECF.Framework.Common.SubstrateTrackings;
- using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robot;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using System.Xml;
- using VirgoCommon;
- using VirgoRT.Devices;
- using VirgoRT.Devices.EFEM;
- using VirgoRT.Devices.YASKAWA;
- using VirgoRT.HostWrapper;
- using VirgoRT.Modules;
- namespace VirgoRT.Device
- {
- class EfemCommunicationBase
- {
- protected readonly AsyncSocket _socket = new AsyncSocket("");
- public bool IsConnected
- {
- get
- {
- return _socket.IsConnected;
- }
- }
- private string _ip;
- protected EfemCommunicationBase(string sIP)
- {
- _ip = sIP;
- _socket.Connect(_ip);
- _socket.OnDataChanged += Hardware_OnDataChanged;
- _socket.OnErrorHappened += Hardware_OnErrorHappened;
- //if (!_socket.IsConnected)
- //{
- // EV.PostAlarmLog(ModuleName.EFEM.ToString(), "Cannot connect to EFEM");
- //}
- }
- public void Reconnect()
- {
- _socket.Connect(_ip);
- }
- private void Hardware_OnDataChanged(string message)
- {
- Singleton<RouteManager>.Instance.EFEM.PostMsg(EfemEntity.MSG.RecHwMsg, message);
- }
- private void Hardware_OnErrorHappened(ErrorEventArgs args)
- {
- Singleton<RouteManager>.Instance.EFEM.PostMsg(EfemEntity.MSG.Error);
- }
- public virtual void SendTo(string str) { }
- public virtual void SendTo(IEfemMessage yas_msg) { }
- }
- }
- namespace VirgoRT.Device.YASKAWA
- {
- sealed class MessageHandler : IMessageHandler
- {
- // Fields
- //
- private readonly IList<EfemMessage> _msgQueue = new List<EfemMessage>();
- private readonly EfemBase _efem;
- // Properties
- //
- public bool IsCompleted
- {
- get
- {
- if (_msgQueue.Count <= 0) return false;
- return EfemMessage.MsgHead.INF == _msgQueue[_msgQueue.Count - 1].Head;
- }
- }
- public event EventHandler<EfemActionArgs> CommandUpdated;
- public event EventHandler<EfemEventArgs> EventUpdated;
- public event EventHandler<EfemErrorArgs> ErrorOccurred;
- // Constructor
- //
- public MessageHandler(EfemBase device)
- {
- _efem = device;
- }
- public void Send(IEfemMessage msg)
- {
- _efem.Comm.SendTo(msg);
- _msgQueue.Add(msg as EfemMessage);
- }
- public void ReceiveMessage(string sRec)
- {
- string[] msgs = sRec.Split('\r');
- foreach (var msg in msgs)
- {
- if (string.IsNullOrWhiteSpace(msg)) continue;
- EfemMessage rec_msg = msg.ToMessage();
- switch (rec_msg.Head)
- {
- case EfemMessage.MsgHead.ACK:
- _msgQueue.Add(rec_msg);
- OnCommandUpdated(new EfemActionArgs
- {
- Module = rec_msg.Port,
- CommandType = rec_msg.Operation,
- Status = ActionStatus.RecACK
- });
- break;
- case EfemMessage.MsgHead.INF:
- OnCommandUpdated(new EfemActionArgs
- {
- Module = rec_msg.Port,
- CommandType = rec_msg.Operation,
- Status = ActionStatus.RecINF
- });
- // 收到INF之后发送ACK确认
- string strACK = rec_msg.RawString.Replace("INF", "ACK");
- _efem.Comm.SendTo(strACK);
- EfemMessage ack_msg = strACK.ToMessage();
- ack_msg.Direct = MsgDirection.To;
- _msgQueue.Add(ack_msg);
- OnCommandUpdated(new EfemActionArgs
- {
- Module = rec_msg.Port,
- CommandType = rec_msg.Operation,
- Status = ActionStatus.Completed,
- Data = rec_msg.Data.Count > 0 ? rec_msg.Data.First() : string.Empty
- });
- break;
- case EfemMessage.MsgHead.EVT:
- OnEventUpdated(new EfemEventArgs
- {
- EvtStr = rec_msg.ToParamString(),
- Module = rec_msg.Port,
- CommandType = rec_msg.Operation,
- DataList = rec_msg.Data
- });
- break;
- case EfemMessage.MsgHead.NAK:
- OnErrorOccurred(new EfemErrorArgs
- {
- Factor = rec_msg.Factor,
- Description = Constant.FactorString[rec_msg.Factor],
- CommandType = rec_msg.Operation,
- Module = rec_msg.Port,
- });
- break;
- case EfemMessage.MsgHead.CAN:
- OnErrorOccurred(new EfemErrorArgs
- {
- Factor = rec_msg.Factor,
- Description = Constant.FactorString.ContainsKey(rec_msg.Factor) ? Constant.FactorString[rec_msg.Factor] : rec_msg.Factor,
- Message = rec_msg.Data[0],
- CommandType = rec_msg.Operation,
- Module = rec_msg.Port,
- });
- break;
- case EfemMessage.MsgHead.ABS:
- OnErrorOccurred(new EfemErrorArgs
- {
- Factor = rec_msg.Factor,
- Description = $"{rec_msg.Data[0]}, {rec_msg.Data[1]}",
- CommandType = rec_msg.Operation,
- Module = rec_msg.Port,
- });
- break;
- }
- }
- }
- private void OnCommandUpdated(EfemActionArgs args)
- {
- CommandUpdated?.Invoke(this, args);
- }
- private void OnEventUpdated(EfemEventArgs args)
- {
- EventUpdated?.Invoke(this, args);
- }
- private void OnErrorOccurred(EfemErrorArgs args)
- {
- ErrorOccurred?.Invoke(this, args);
- }
- }
- sealed class EfemComm : EfemCommunicationBase
- {
- public EfemComm() : base(SC.GetStringValue("EFEM.IPAddress"))
- {
- }
- public override void SendTo(string str) { _socket.Write(str + '\r'); }
- public override void SendTo(IEfemMessage msg)
- {
- if (msg is EfemMessage yas_msg)
- {
- //string str = EfemParser.Instance.TranslateBack(yas_msg);
- string str = yas_msg.ToString();
- if (string.IsNullOrEmpty(str))
- throw new ApplicationException("Yaskawa message translation error");
- yas_msg.RawString = str;
- SendTo(str);
- }
- }
- }
- sealed class SignalTower
- {
- // Fields
- //
- private readonly Dictionary<LightType, LightStatus> _lights = new Dictionary<LightType, LightStatus>(5);
- public SignalTower()
- {
- _lights.Add(LightType.RED, LightStatus.OFF);
- _lights.Add(LightType.YELLOW, LightStatus.OFF);
- _lights.Add(LightType.BLUE, LightStatus.OFF);
- _lights.Add(LightType.GREEN, LightStatus.OFF);
- _lights.Add(LightType.WHITE, LightStatus.OFF);
- _lights.Add(LightType.BUZZER1, LightStatus.OFF);
- DATA.Subscribe($"{ModuleName.EFEM}.SignalTower", () =>
- {
- return new AITSignalTowerData
- {
- IsGreenLightOn = _lights[LightType.GREEN] == LightStatus.ON,
- IsRedLightOn = _lights[LightType.RED] == LightStatus.ON,
- IsYellowLightOn = _lights[LightType.YELLOW] == LightStatus.ON,
- IsWhiteLightOn = _lights[LightType.WHITE] == LightStatus.ON,
- IsBlueLightOn = _lights[LightType.BLUE] == LightStatus.ON,
- IsBuzzerOn = _lights[LightType.BUZZER1] == LightStatus.ON,
- };
- });
- }
- // Methods
- //
- public void ChangeLightStatus(LightType light, LightStatus st)
- {
- if (!_lights.ContainsKey(light))
- throw new ApplicationException($"NO {light} configured");
- _lights[light] = st;
- }
- }
- }
|