123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- 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<bool>(SCName.System_IsSimulatorMode))
- plcEntity = Singleton<PLCDummyEntity>.Instance;
- else
- plcEntity = Singleton<PLCEntity>.Instance;
- deviceEntity = Singleton<DeviceEntity>.Instance;
- pmEntity = Singleton<PMEntity>.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<Entity>(INIT.PLC, plcEntity, "IOLayer");
- LaunchMoudle<DeviceEntity>(INIT.DEVICE, deviceEntity, "DeviceLayer");
-
-
- LaunchMoudle<PMEntity>(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<T>(INIT id, T entity, string name) where T : Entity
- {
- Tuple<bool, Result> 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());
- }
- }
- }
- }
|