using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Reflection; using System.Xml; using Mesnac.Action.Base; using Mesnac.Controls.Base; namespace Mesnac.ActionService { public class Service : IDesignService, IRuntimeService { //#region 日志 //private BaseAction logAction = null; //private void LogInfo(object message) //{ // if (logAction == null) // { // logAction = new BaseAction(); // } // logAction.LogInfo(message); //} //private void LogError(object message, Exception exception) //{ // if (logAction == null) // { // logAction = new BaseAction(); // } // logAction.LogError(message, exception); //} //#endregion #region 单例模式 private static Service _this; public static Service Instance { get { try { if (null == _this) _this = new Service(); } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(ex.Message, ex); } return _this; } } private Service() { DesignActionTree = new ActionService.DesignActionNode(); AllRuntimeAction = new Dictionary(); AllDesignToRuntime = new Dictionary(); designActionList = new List(); DeviceDataNameDic = new Dictionary(); } #endregion #region 基本信息 private readonly string ActionConfigPath = "Data\\EventConfig\\"; private string GetConfigFilePath(string path) { string appPath = System.Windows.Forms.Application.StartupPath; return Path.Combine(appPath, ActionConfigPath, path); } private string GetFilePath(string path) { string appPath = System.Windows.Forms.Application.StartupPath; return Path.Combine(appPath, path); } #endregion #region IDesignService 接口实现 public DesignActionNode DesignActionTree { get; set; } private List designActionList { get; set; } private DesignActionNode IniDesignActionTree(string path) { int i = 0; DesignActionNode curPath = this.DesignActionTree; string[] splittedPath = path.Split('/'); while (i < splittedPath.Length) { string ss = splittedPath[i]; i++; if (string.IsNullOrWhiteSpace(ss)) { continue; } bool isExist = false; foreach (DesignActionNode child in curPath.Children) { if (!string.IsNullOrWhiteSpace(child.Name) && child.Name.Equals(ss)) { curPath = child; isExist = true; break; } } if (isExist) { continue; } DesignActionNode node = new DesignActionNode(); node.Name = ss; curPath.Children.Add(node); curPath = node; } return curPath; } private void IniDesignAction(FileInfo fi) { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(fi.FullName); foreach (XmlNode serviceNode in xmlDocument) { if (!serviceNode.Name.Equals("ActionService", StringComparison.CurrentCultureIgnoreCase)) { continue; } foreach (XmlNode designNode in serviceNode.ChildNodes) { if (!designNode.Name.Equals("Design", StringComparison.CurrentCultureIgnoreCase)) { continue; } foreach (XmlNode pathNode in designNode.ChildNodes) { if (!pathNode.Name.Equals("Path", StringComparison.CurrentCultureIgnoreCase)) { continue; } string ss = pathNode.Attributes["path"].Value.ToString(); DesignActionNode node = IniDesignActionTree(ss); foreach (XmlNode actionNode in pathNode.ChildNodes) { if (!actionNode.Name.Equals("Action", StringComparison.CurrentCultureIgnoreCase)) { continue; } DesignAction action = new DesignAction(); action.AutoRun = false; action.GUID = actionNode.Attributes["action"].Value.ToString(); if (actionNode.Attributes["auto"] != null && actionNode.Attributes["auto"].Value != null) { string auto = actionNode.Attributes["auto"].Value.ToLower().Replace(" ", ""); if (auto == "true" || auto == "1") { action.AutoRun = true; } } designActionList.Add(action); foreach (XmlNode actioninfonode in actionNode.ChildNodes) { if (actioninfonode.Name.Equals("Caption", StringComparison.CurrentCultureIgnoreCase)) { action.Name = actioninfonode.FirstChild.Value.ToString(); } if (actioninfonode.Name.Equals("Remark", StringComparison.CurrentCultureIgnoreCase)) { if (actioninfonode.FirstChild == null || actioninfonode.FirstChild.Value == null) { action.Remark = string.Empty; } else { action.Remark = actioninfonode.FirstChild.Value.ToString(); } } } node.ActionNode.Add(action); } } } } } public void IniDesignAction(string path) { DesignActionTree = new ActionService.DesignActionNode(); designActionList.Clear(); DirectoryInfo dir = new DirectoryInfo(GetConfigFilePath(path)); if (dir.Exists) { foreach (FileInfo fi in dir.GetFiles()) { try { IniDesignAction(fi); } catch { } } } } #endregion #region IRuntimeService 接口实现 public Dictionary AllDesignToRuntime { get; set; } public Dictionary AllRuntimeAction { get; set; } public Dictionary DeviceDataNameDic { get; private set; } private void IniDesignToRuntime(FileInfo fi) { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(fi.FullName); foreach (XmlNode serviceNode in xmlDocument) { if (!serviceNode.Name.Equals("ActionService", StringComparison.CurrentCultureIgnoreCase)) { continue; } foreach (XmlNode designToRunTimeNode in serviceNode.ChildNodes) { if (!designToRunTimeNode.Name.Equals("DesignToRuntime", StringComparison.CurrentCultureIgnoreCase)) { continue; } foreach (XmlNode designNode in designToRunTimeNode.ChildNodes) { if (!designNode.Name.Equals("Design", StringComparison.CurrentCultureIgnoreCase)) { continue; } string design = designNode.Attributes["action"].Value.ToString(); bool isExists = false; foreach (DesignAction ss in designActionList) { if (ss.GUID.Equals(design, StringComparison.CurrentCultureIgnoreCase)) { isExists = true; break; } } if (!isExists) { continue; } DesignToRuntime dtr = new DesignToRuntime(); AllDesignToRuntime.TryGetValue(design, out dtr); if (dtr == null) { dtr = new DesignToRuntime(); } dtr.GUID = design; foreach (XmlNode runtimeNode in designNode.ChildNodes) { if (!runtimeNode.Name.Equals("Runtime", StringComparison.CurrentCultureIgnoreCase)) { continue; } string runtime = runtimeNode.Attributes["action"].Value.ToString(); string parameters = string.Empty; if (runtimeNode.Attributes["parameters"] != null && runtimeNode.Attributes["parameters"].Value != null) { parameters = runtimeNode.Attributes["parameters"].Value.ToString(); } dtr.Runtime.Add(new DesignRuntime(runtime, parameters)); } if (!AllDesignToRuntime.ContainsKey(dtr.GUID)) AllDesignToRuntime.Add(dtr.GUID, dtr); } } } } private void IniDesignToRuntime(string path) { AllDesignToRuntime.Clear(); DirectoryInfo dir = new DirectoryInfo(GetConfigFilePath(path)); if (dir.Exists) { foreach (FileInfo fi in dir.GetFiles()) { try{ IniDesignToRuntime(fi); } catch { } } } } private void IniRuntimeAction(FileInfo fi) { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(fi.FullName); foreach (XmlNode serviceNode in xmlDocument) { if (!serviceNode.Name.Equals("ActionService", StringComparison.CurrentCultureIgnoreCase)) { continue; } foreach (XmlNode runtimeNode in serviceNode.ChildNodes) { if (!runtimeNode.Name.Equals("Runtime", StringComparison.CurrentCultureIgnoreCase)) { continue; } foreach (XmlNode importNode in runtimeNode.ChildNodes) { if (!importNode.Name.Equals("Import", StringComparison.CurrentCultureIgnoreCase)) { continue; } string assemblyFile = importNode.Attributes["assembly"].Value.ToString(); assemblyFile = GetFilePath(assemblyFile); if (string.IsNullOrWhiteSpace(assemblyFile) || (!File.Exists(assemblyFile))) { continue; } Assembly ass = Assembly.LoadFrom(assemblyFile); if (ass == null) { continue; } foreach (XmlNode actionNode in importNode.ChildNodes) { if (!actionNode.Name.Equals("Action", StringComparison.CurrentCultureIgnoreCase)) { continue; } string guid = actionNode.Attributes["action"].Value.ToString(); string className = actionNode.Attributes["class"].Value.ToString(); if (string.IsNullOrWhiteSpace(guid) || string.IsNullOrWhiteSpace(className)) { continue; } if (this.AllRuntimeAction.ContainsKey(guid)) { continue; } bool isExists = false; foreach (DesignToRuntime dtr in AllDesignToRuntime.Values) { foreach (DesignRuntime runtime in dtr.Runtime) { if (runtime.GUID.Equals(guid, StringComparison.CurrentCultureIgnoreCase)) { isExists = true; break; } } if (isExists) break; } if (!isExists) { continue; } Type type = ass.GetType(className); if (type == null) { continue; } if (!typeof(IAction).IsAssignableFrom(type)) { continue; } IAction instance = (IAction)Activator.CreateInstance(type); if (instance == null) { continue; } RuntimeAction action = new RuntimeAction(); action.GUID = guid; action.File = assemblyFile; action.Assembly = ass; action.Class = type; action.Instance = instance; AllRuntimeAction.Add(action.GUID, action); } } } } } private string _runtimeProjectWizardName = string.Empty; public void IniRuntimeAction(string path, bool isAutoRun) { IniDesignAction(path); IniDesignToRuntime(path); AllRuntimeAction.Clear(); this._runtimeProjectWizardName = path; DirectoryInfo dir = new DirectoryInfo(GetConfigFilePath(path)); if (dir.Exists) { foreach (FileInfo fi in dir.GetFiles()) { try{ IniRuntimeAction(fi); } catch { } } if (isAutoRun) { foreach (DesignAction designAction in designActionList) { if (designAction.AutoRun) { Run(null, new List() { designAction.GUID }); } } } } } private RuntimeAction NewRuntimeAction(RuntimeAction runtime) { try { RuntimeAction Result = new RuntimeAction(); Result.GUID = runtime.GUID; Result.AutoRun = runtime.AutoRun; Result.File = runtime.File; Result.Assembly = runtime.Assembly; Result.Class = runtime.Class; Result.Instance = (IAction)Activator.CreateInstance(Result.Class); return Result; } catch { return null; } } public void Run(object sender, List action) { try { foreach (string design in action) { DesignToRuntime dtr = new DesignToRuntime(); if (AllDesignToRuntime.TryGetValue(design, out dtr)) { foreach (DesignRuntime runtime in dtr.Runtime) { RuntimeAction runtimeaction = null; if (AllRuntimeAction.TryGetValue(runtime.GUID, out runtimeaction)) { RuntimeAction run = NewRuntimeAction(runtimeaction); if (run == null) { continue; } RuntimeParameter par = new RuntimeParameter(); par.Sender = sender; par.DesignGuid = design; par.RuntimeGuid = runtime.GUID; par.Parameters = runtime.Parameters; par.ProjectWizardName = this._runtimeProjectWizardName; par.IsReturn = false; run.Instance.Run(par); if (par.IsReturn) { return; } } } } } } catch (Exception ex) { //LogError("Mesnac.ActionService.Service", ex); ICSharpCode.Core.LoggingService.Error("Mesnac.ActionService.Service", ex); } } /// /// 尹治丰添加,添加参数e /// /// /// public void Run(object sender, System.EventArgs e, List action) { try { foreach (string design in action) { DesignToRuntime dtr = new DesignToRuntime(); if (AllDesignToRuntime.TryGetValue(design, out dtr)) { foreach (DesignRuntime runtime in dtr.Runtime) { RuntimeAction runtimeaction = null; if (AllRuntimeAction.TryGetValue(runtime.GUID, out runtimeaction)) { RuntimeAction run = NewRuntimeAction(runtimeaction); if (run == null) { continue; } RuntimeParameter par = new RuntimeParameter(); par.evn = e; par.Sender = sender; par.DesignGuid = design; par.RuntimeGuid = runtime.GUID; par.Parameters = runtime.Parameters; par.ProjectWizardName = this._runtimeProjectWizardName; par.IsReturn = false; run.Instance.Run(par); if (par.IsReturn) { return; } } } } } } catch (Exception ex) { //LogError("Mesnac.ActionService.Service", ex); ICSharpCode.Core.LoggingService.Error("Mesnac.ActionService.Service", ex); } } #endregion } }