using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Xml; using System.IO; using System.Runtime.InteropServices; using PVOID = System.IntPtr; using DWORD = System.UIntPtr; using Microsoft.Win32; using FusionTypes; using FusionCOM; namespace Fusion_Control_Centre_UberMDX { public partial class MainMDX : Form { public FusionCOM.FusionServer FCOMServer = new FusionCOM.FusionServer(); public List COM_AIList = new List(); public List COM_DOList = new List(); public List COM_DIList = new List(); public List COM_VARBOOLList = new List(); public List COM_VARNUMList = new List(); public int[] Array_Counts = new int[5]; public Timer COM_Vote_Timer = new Timer(); //public static object RoadRunner_ExecuteCommandObject = new object(); //public static System.Type RoadRunner_ObjectType = System.Type.GetTypeFromProgID("RoadRunner.SDK"); public static List COM_Execution_Commands_List = new List(); public UInt32 COM_hr; // result code returned by API call public IntPtr COM_pFCOMIUnknown; // IUnknown interface pointer for object public Guid COM_FCOMGuid; // GUID of object public UInt32 COM_dwFlags = 0; // default to STRONG registration, requires Revoke public UInt32 COM_dwRegister = 0; // handle to be passed to RevokeActiveObject API call static class FCOMOLE { [System.Runtime.InteropServices.DllImport("Oleaut32.dll")] public static extern UInt32 RegisterActiveObject(IntPtr punk, ref System.Guid rclsid, UInt32 dwFlags, ref UInt32 pdwRegister); [System.Runtime.InteropServices.DllImport("Oleaut32.dll")] public static extern UInt32 RevokeActiveObject(UInt32 pdwRegister, UInt32 pvReserved); } public class COM_Execution_Object_Class { public class COM_Execution_Object_Command_IDs_Class { public string external_Command; public string internal_Command; public List external_Command_Parameters; public COM_Execution_Object_Command_IDs_Class(string input_external_command, string input_internal_command, List input_external_command_parameters) { external_Command = input_external_command; internal_Command = input_internal_command; external_Command_Parameters = input_external_command_parameters; } } public class COM_Check_Return_Class { public bool isFound; public COM_Execution_Object_Command_IDs_Class foundItem; public COM_Check_Return_Class(bool input_isFound, COM_Execution_Object_Command_IDs_Class input_COMEOCIC) { isFound = input_isFound; foundItem = input_COMEOCIC; } } public class COM_External_Executable_Parameter_Class : IComparable { public int _order; public string _param; public COM_External_Executable_Parameter_Class() { } public COM_External_Executable_Parameter_Class(int input_order, string input_parameter) { _order = input_order; _param = input_parameter; } #region IComparable Members public int CompareTo(COM_External_Executable_Parameter_Class other) { return this._order.CompareTo(other._order); } #endregion } public object COM_Execution_Object; public System.Type COM_Execution_Type; public List COM_Commands = new List(); public COM_Execution_Object_Class(string input_CommandType) { try { COM_Execution_Type = System.Type.GetTypeFromProgID(input_CommandType); COM_Execution_Object = Activator.CreateInstance(COM_Execution_Type); } catch (Exception e) { return; } } public void Add_Command(string input_external_command, string input_internal_command, List input_external_command_parameters) { COM_Commands.Add(new COM_Execution_Object_Command_IDs_Class(input_external_command, input_internal_command, input_external_command_parameters)); } public COM_Check_Return_Class Check_Command_Availability(string command) { foreach (COM_Execution_Object_Command_IDs_Class COM_Commands_Command in COM_Commands) { if (COM_Commands_Command.internal_Command.ToLower() == command.ToLower()) { return new COM_Check_Return_Class(true, COM_Commands_Command); } } return new COM_Check_Return_Class(false, null); } public bool Do_Command(COM_Execution_Object_Command_IDs_Class input_do) { try { COM_Execution_Type.InvokeMember(input_do.external_Command, System.Reflection.BindingFlags.InvokeMethod, null, COM_Execution_Object, input_do.external_Command_Parameters.ToArray()); return true; } catch (Exception Do_Command_Error) { return false; } } } public void COM_Initialize() { // // Variables used for API call to register COM object in Running Object Table (ROT) // COM_pFCOMIUnknown = Marshal.GetIUnknownForObject(FCOMServer); //COM_pFCOMIUnknown = Marshal.GetIUnknownForObject(FusionCOM.FusionServer); // // Register object in Running Object Table so that other programs can access it // COM_FCOMGuid = FCOMServer.GetType().GUID; //COM_FCOMGuid = new Guid("0664fde4-3d52-43ac-acf0-b4548832f407"); COM_hr = FCOMOLE.RegisterActiveObject(COM_pFCOMIUnknown, ref COM_FCOMGuid, COM_dwFlags, ref COM_dwRegister); // // Initialize test AI, DO, DI data and set up COM object // COM_AIList.Clear(); COM_DIList.Clear(); COM_DOList.Clear(); COM_VARBOOLList.Clear(); COM_VARNUMList.Clear(); foreach (AnalogueInputChannel _ain in AllAnalogueInputs) { COM_AIList.Add(new AIButtonProperties(_ain.CurrentValue)); Array_Counts[0]++; } foreach (DigitalOutputChannel _ouc in AllDigitalOutputs) { COM_DOList.Add(new DOButtonProperties(_ouc.CurrentState)); Array_Counts[1]++; } foreach (DigitalInputChannel _inc in AllDigitalInputs) { COM_DIList.Add(new DIButtonProperties(_inc.CurrentState)); Array_Counts[2]++; } foreach (Variable _var in AllVariables) { bool temp_var_bool; if (bool.TryParse(_var.variableValue.ToString(), out temp_var_bool)) { COM_VARBOOLList.Add(new VARBOOLButtonProperties(temp_var_bool)); Array_Counts[3]++; } else { COM_VARNUMList.Add(new VARNUMButtonProperties((double)_var.variableValue)); Array_Counts[4]++; } } FCOMServer.SetupServer(COM_AIList, COM_DOList, COM_DIList, COM_VARNUMList, COM_VARBOOLList); if (COM_checkCommunication) { //COM_Vote_Timer.Interval = 1000; //COM_Vote_Timer.Tick += new EventHandler(COM_Vote_Timer_Tick); //COM_Vote_Timer.Enabled = true; FCOMServer.COM_CommandReceived += new FusionServer.DELEGATE_COM_CommandReceived(FCOMServer_COM_CommandReceived); } /* try { //RoadRunner_ExecuteCommandObject = Activator.CreateInstance(RoadRunner_ObjectType); //RoadRunner_ObjectType.InvokeMember("Execute", System.Reflection.BindingFlags.InvokeMethod, null, RoadRunner_ExecuteCommandObject, new object[] { "PLAY" }); } catch (Exception RR_Error) { } */ } void FCOMServer_COM_CommandReceived(string commandToSend) { foreach (COM_Command com in AllCOMCommands) { if (com.id.ToLower() == commandToSend.ToLower()) { foreach (XmlNode doNode in com.do_statements) { EvaluateDoThenNode(doNode, com.last_vote, com.minimum_delta_vote_time); } com.last_vote = DateTime.Now; break; } } } #region Old Timer Ticking /* private void COM_Vote_Timer_Tick(object sender, EventArgs e) { for (int i = 0; i < FCOMServer.VOTE_LIST.Count; i++) { FCOMServer.VOTE_LIST.RemoveAt(i); i--; } } */ #endregion public uint COM_Revoke() { try { COM_hr = FCOMOLE.RevokeActiveObject(COM_dwRegister, 0); Marshal.Release(COM_pFCOMIUnknown); } catch (Exception e) { return 0; } return (COM_hr); } public void COM_PrintAllValues() { Console.WriteLine("\r\n\r\n"); Console.WriteLine("===== ANALOGUES ====="); for (int i = 0; i < Array_Counts[0]; i++) { Console.WriteLine(FCOMServer.GetAnalogueValue(i).ToString()); } Console.WriteLine("===== DIGITAL OUTPUTS ====="); for (int i = 0; i < Array_Counts[1]; i++) { Console.WriteLine(FCOMServer.GetDigitalOutValue(i).ToString()); } Console.WriteLine("===== DIGITAL INPUTS ====="); for (int i = 0; i < Array_Counts[2]; i++) { Console.WriteLine(FCOMServer.GetDigitalInValue(i).ToString()); } Console.WriteLine("===== BOOLEAN VARIABLES ====="); for (int i = 0; i < Array_Counts[3]; i++) { Console.WriteLine(FCOMServer.GetBooleanVariableValue(i).ToString()); } Console.WriteLine("===== NUMERIC VARIABLES ====="); for (int i = 0; i < Array_Counts[4]; i++) { Console.WriteLine(FCOMServer.GetNumericVariableValue(i).ToString()); } Console.WriteLine("\r\n\r\n"); } public void COM_RefreshAllValues() { for (int i = 0; i < AllAnalogueInputs.Count; i++) { FCOMServer.SetAnalogueValue(i, AllAnalogueInputs[i].CurrentValue); } for (int i = 0; i < AllDigitalOutputs.Count; i++) { FCOMServer.SetDigitalOutValue(i, AllDigitalOutputs[i].CurrentState); } for (int i = 0; i < AllDigitalInputs.Count; i++) { FCOMServer.SetDigitalInValue(i, AllDigitalInputs[i].CurrentState); } for (int i = 0; i < AllVariables.Count; i++) { try { bool temp_var_bool; if (bool.TryParse(AllVariables[i].variableValue.ToString(), out temp_var_bool)) { FCOMServer.SetBooleanVariableValue(i, temp_var_bool); } else { double testDouble; if (double.TryParse(AllVariables[i].variableValue.ToString(), out testDouble)) { FCOMServer.SetNumericVariableValue(i, testDouble); } else { FCOMServer.SetNumericVariableValue(i, 0.0); } } } catch (InvalidCastException eCast) { Console.WriteLine(eCast); } } } } }