using System;
using System.Collections.Generic;
using Aitex.Core.Util;
using Aitex.Sorter.RT.EFEMs.Tasks;
namespace Aitex.Sorter.RT.EFEMs.Servers
{
public interface ITaskT
{
bool Execute(out string result, string cmd, params string[] args);
///
/// null value, wait,
/// true completed,
/// false error
///
bool? Monitor(out string result, params string[] args);
bool HasInfoMessage { get; }
bool IsAcked { get; }
bool IsWaitAck { get; }
bool Ack(EfemCommandType type, EfemCommand cmd, params string[] args);
EfemCommandType CommandType { get; }
EfemCommand CommandName { get; }
string CommandData { get; set; }
}
public interface ITask
{
bool Execute(out string result, params string[] args);
///
/// novalue, wait, true completed, false error
///
bool? Monitor(out string result, params string[] args);
bool HasInfoMessage { get; set; }
}
public class TaskT : ITaskT, ICloneable
where T : ITask, new()
{
private T _imp = default(T);
private double _deley = 60;
private DeviceTimer _timer = new DeviceTimer();
private string _cmd;
private string[] _args;
public bool HasInfoMessage { get { return _imp.HasInfoMessage; } set { }}
public bool IsAcked { get { return true; } }
public bool IsWaitAck
{
get { return false; }
}
public EfemCommandType CommandType
{
get { return _commandType; }
}
public EfemCommand CommandName
{
get { return _commandName; }
}
public string CommandData
{
get;
set;
}
private EfemCommandType _commandType;
private EfemCommand _commandName;
public TaskT(EfemCommandType type, EfemCommand cmd)
{
_commandType = type;
_commandName = cmd;
_imp = new T();
}
public bool Ack(EfemCommandType type, EfemCommand cmd, params string[] args)
{
return true;
}
public bool Execute(out string result, string cmd, params string[] args)
{
result = string.Empty;
_cmd = cmd.Substring(4);
if (!_imp.Execute(out result, args))
{
result = string.Format("CAN:{0}|{1}", _cmd, result);
return false;
}
_args = new string[args.Length];
args.CopyTo(_args, 0);
_timer.Start(_deley*1000);
result = string.Format("ACK:{0}", _cmd);
return true;
}
public bool? Monitor(out string result, params string[] args)
{
result = string.Empty;
/*
if (_timer.IsTimeout())
{
_timer.Stop();
result = string.Format("INF:{0}", _cmd);
return true;
}
return false;
*/
bool? ret = _imp.Monitor(out result, _args);
if (ret.HasValue)
{
_timer.Stop();
if(ret.Value) //completed
{
if(string.IsNullOrEmpty(result))
result = string.Format("INF:{0}", _cmd);
else
result = string.Format("INF:{0}/{1}", _cmd, result);
return true;
}
else
{
result = string.Format("ABS:{0}|ERROR/{1}/{2}", _cmd,result, "FAILED");
return true;
}
}
if(_timer.IsTimeout())
{
_timer.Stop();
result = string.Format("ABS:{0}|ERROR/{1}/{2}", _cmd, "SYSTEM_FF10", "TIMEOUT");
return false;
}
return null;
}
public object Clone()
{
return this.MemberwiseClone();
}
}
public class TaskFactory
{
private Dictionary _tasks = new Dictionary();
private List _hideTasks = new List();
public TaskFactory()
{
_tasks["MOV.INIT"] = new TaskT(EfemCommandType.MOV, EfemCommand.INIT);
_tasks["MOV.ORGSH"] = new TaskT(EfemCommandType.MOV, EfemCommand.ORGSH);
_tasks["MOV.HOME"] = new TaskT(EfemCommandType.MOV, EfemCommand.HOME);
_tasks["MOV.LOCK"] = new TaskT(EfemCommandType.MOV, EfemCommand.LOCK);
_tasks["MOV.UNLOCK"] = new TaskT(EfemCommandType.MOV, EfemCommand.UNLOCK);
_tasks["MOV.DOCK"] = new TaskT(EfemCommandType.MOV, EfemCommand.DOCK);
_tasks["MOV.UNDOCK"] = new TaskT(EfemCommandType.MOV, EfemCommand.UNDOCK);
//open cassette wafsh = open
_tasks["MOV.OPEN"] = new TaskT(EfemCommandType.MOV, EfemCommand.OPEN);
_tasks["MOV.CLOSE"] = new TaskT(EfemCommandType.MOV, EfemCommand.CLOSE);
_tasks["GET.CSTID"] = new TaskT(EfemCommandType.GET, EfemCommand.CSTID);
_tasks["MOV.WAFSH"] = new TaskT(EfemCommandType.MOV, EfemCommand.WAFSH);
_tasks["GET.MAPDT"] = new TaskT(EfemCommandType.GET, EfemCommand.MAPDT);
_tasks["MOV.ALIGN"] = new TaskT(EfemCommandType.MOV, EfemCommand.ALIGN);
_tasks["SET.ALIGN"] = new TaskT(EfemCommandType.SET, EfemCommand.ALIGN);
_tasks["MOV.GOTO"] = new TaskT(EfemCommandType.MOV, EfemCommand.GOTO);
_tasks["MOV.LOAD"] = new TaskT(EfemCommandType.MOV, EfemCommand.LOAD);
_tasks["MOV.UNLOAD"] = new TaskT(EfemCommandType.MOV, EfemCommand.UNLOAD);
_tasks["MOV.TRANS"] = new TaskT(EfemCommandType.MOV, EfemCommand.TRANS);
_tasks["MOV.CHANGE"] = new TaskT(EfemCommandType.MOV, EfemCommand.CHANGE);
_tasks["MOV.EMS"] = new TaskT(EfemCommandType.MOV, EfemCommand.EMS);
_tasks["MOV.HOLD"] = new TaskT(EfemCommandType.MOV, EfemCommand.HOLD);
_tasks["MOV.RESTR"] = new TaskT(EfemCommandType.MOV, EfemCommand.RESTR);
_tasks["MOV.ABORT"] = new TaskT(EfemCommandType.MOV, EfemCommand.ABORT);
_tasks["SET.ERROR"] = new TaskT(EfemCommandType.SET, EfemCommand.ERROR);
_tasks["GET.ERROR"] = new TaskT(EfemCommandType.GET, EfemCommand.ERROR);
_tasks["SET.EVENT"] = new TaskT(EfemCommandType.SET, EfemCommand.EVENT);
_tasks["GET.EVENT"] = new TaskT(EfemCommandType.GET, EfemCommand.EVENT);
_tasks["SET.SIGOUT"] = new TaskT(EfemCommandType.SET, EfemCommand.SIGOUT);
_tasks["GET.SIGSTAT"] = new TaskT(EfemCommandType.GET, EfemCommand.SIGSTAT);
_tasks["SET.CLAMP"] = new TaskT(EfemCommandType.SET, EfemCommand.CLAMP);
_tasks["GET.CLAMP"] = new TaskT(EfemCommandType.GET, EfemCommand.CLAMP);
_tasks["SET.MODE"] = new TaskT(EfemCommandType.SET, EfemCommand.MODE);
_tasks["GET.MODE"] = new TaskT(EfemCommandType.GET, EfemCommand.MODE);
_tasks["MOV.TRANSREQ"] = new TaskT(EfemCommandType.MOV, EfemCommand.TRANSREQ);
_tasks["GET.TRANSREQ"] = new TaskT(EfemCommandType.GET, EfemCommand.TRANSREQ);
_tasks["GET.STATE"] = new TaskT(EfemCommandType.GET, EfemCommand.STATE);
_tasks["SET.FFU"] = new TaskT(EfemCommandType.SET, EfemCommand.FFU);
_tasks["SET.WTYPE"] = new TaskT(EfemCommandType.SET, EfemCommand.WTYPE);
_tasks["GET.WTYPE"] = new TaskT(EfemCommandType.GET, EfemCommand.WTYPE);
_tasks["SET.PURGE"] = new TaskT(EfemCommandType.SET, EfemCommand.PURGE);
_tasks["GET.PURGE"] = new TaskT(EfemCommandType.GET, EfemCommand.PURGE);
_tasks["MOV.ADPLOCK"] = new TaskT(EfemCommandType.MOV, EfemCommand.ADPLOCK);
_tasks["SET.ADPUNLOCK"] = new TaskT(EfemCommandType.SET, EfemCommand.ADPUNLOCK);
_tasks["SET.LED"] = new TaskT(EfemCommandType.SET, EfemCommand.LED);
_tasks["MOV.WORKCHK"] = new TaskT(EfemCommandType.MOV, EfemCommand.WORKCHK);
_tasks["SET.FFUFAN"] = new TaskT(EfemCommandType.SET, EfemCommand.FFUFAN);
}
public bool UnSupport(EfemCommandType type, EfemCommand cmd)
{
string key = string.Format("{0}.{1}", type, cmd);
return _hideTasks.Exists(item => item == key) || !_tasks.ContainsKey(key);
}
public ITaskT Create(EfemCommandType type, EfemCommand cmd)
{
if (!UnSupport(type,cmd))
{
string key = string.Format("{0}.{1}", type, cmd);
return (ITaskT)(((ICloneable)_tasks[key]).Clone());
}
return null;
}
}
}