using System; using System.Collections.Generic; using System.Linq; using System.Text; using Aitex.Core.RT.SCCore; using Aitex.Triton160.RT.Device; using Aitex.Core.RT.Event; using Aitex.Triton160.Common; using System.Diagnostics; using Aitex.Core.RT.IOCore; using Aitex.Core.RT.Fsm; using Aitex.Triton160.RT.Module; using Aitex.Core.Util; using Aitex.Core.RT.Log; using System.IO; using Aitex.Triton160.RT.PLC; using Aitex.Core.RT.Routine; namespace Aitex.Triton160.RT.Routine.RT { public class InitRoutine : SeqenecRoutine { private Entity plcEntity = null; private DeviceEntity deviceEntity = null; private PMEntity pmEntity = null; enum INIT { PLC, DEVICE, PM, } public string Module { get; set; } public string Name { get; set; } public InitRoutine(string module, string name) { Module = module; Name = name; } public bool Initalize() { if (SC.GetValue(SCName.System_IsSimulatorMode)) plcEntity = Singleton.Instance; else plcEntity = Singleton.Instance; deviceEntity = Singleton.Instance; pmEntity = Singleton.Instance; return true; } public Result Start(params object[] objs) { Reset(); LOG.Write(String.Format("Routine {0} {1} Start",Module,Name)); return Result.RUN; } public Result Monitor() { try { LaunchMoudle(INIT.PLC, plcEntity, "IOLayer"); LaunchMoudle(INIT.DEVICE, deviceEntity, "DeviceLayer"); LaunchMoudle(INIT.PM, pmEntity, "PM"); } catch(RoutineBreakException) { return Result.RUN; } catch(RoutineFaildException) { return Result.FAIL; } return Result.DONE; } public void Abort() { if (pmEntity != null) pmEntity.Terminate(); if (plcEntity != null) plcEntity.Terminate(); } private void LaunchMoudle(INIT id, T entity, string name) where T : Entity { Tuple ret = ExecuteAndWait(id, () => { if (!entity.Initialize()) { LOG.Error(String.Format("系统启动:{0}启动失败.", name)); return false; } return true; },() => { return entity.Running; }, 10 * 1000); if (ret.Item1) { if (ret.Item2 == Result.FAIL) { throw (new RoutineFaildException()); } else if (ret.Item2 == Result.TIMEOUT) //timeout { LOG.Error(String.Format("系统启动:{0}启动超时失败,超时时间为{1}秒", name, Timeout)); throw(new RoutineFaildException()); } else throw (new RoutineBreakException()); } } } }