public override void PrepareForExecution()
        {
            //Creates the DLLS for model

            string lcCode       = "";
            string lcCodeIn     = "";
            string lcCodeOut    = "";
            string lcCodeInOnly = "";
            //string lcCodeInNme = "";
            int nargout = 0;

            //creates output parameter data
            nargout = ModelDataOutputs.Count;
            foreach (Data dt in ModelDataOutputs)
            {
                string outParamsIdent = "[In, Out] ref IntPtr ";
                string outParamsName  = dt.Name + "_1" + ",";
                lcCodeOut = lcCodeOut + outParamsIdent + outParamsName;
            }
            //creates input parameter data
            foreach (Data dt in ModelDataInputs)
            {
                string inParamsIdent = "[In]IntPtr ";
                string inParamsName  = dt.Name + "_1" + ",";
                lcCodeIn = lcCodeIn + inParamsIdent + inParamsName;
                //lcCodeInOnly = lcCodeInOnly + "double " + dt.name + ",";
                if (dt is DoubleData)
                {
                    lcCodeInOnly = lcCodeInOnly + "double " + dt.Name + ",";
                }
                if (dt is DoubleVectorData)
                {
                    lcCodeInOnly = lcCodeInOnly + "double[] " + dt.Name + ",";
                }
                if (dt is DoubleMatrixData)
                {
                    lcCodeInOnly = lcCodeInOnly + "double[,] " + dt.Name + ",";
                }
                if (dt is IntegerData)
                {
                    lcCodeInOnly = lcCodeInOnly + "int " + dt.Name + ",";
                }
                if (dt is IntegerVectorData)
                {
                    lcCodeInOnly = lcCodeInOnly + "int[] " + dt.Name + ",";
                }
                if (dt is StringData)
                {
                    lcCodeInOnly = lcCodeInOnly + "string " + dt.Name + ",";
                }
            }
            if (lcCodeIn != "")
            {
                lcCodeIn = lcCodeIn.Remove(lcCodeIn.Length - 1);

                lcCodeInOnly = lcCodeInOnly.Remove(lcCodeInOnly.Length - 1);
            }

            // main MATLAB functional method
            lcCode = "\n[DllImport(@\"" + Directory.GetCurrentDirectory() + "\\" + DllPath.Substring(DllPath.LastIndexOf("\\") + 1) + "\", CallingConvention = CallingConvention.Cdecl)] \n";
            string methodTempName = MethodName;

            if (methodTempName.Length <= 1)
            {
                Code = methodTempName.ToUpper();
            }
            else
            {
                char[] letters = methodTempName.ToCharArray();
                letters[0]     = Char.ToUpper(letters[0]);
                methodTempName = new string(letters);
            }
            lcCode = lcCode + "public static extern void _mlf" + methodTempName + "(" + "[In]int nargout," +
                     " " + lcCodeOut + lcCodeIn + "); \n";

            // main MATLAB functional intialization method
            lcCode = lcCode + "[DllImport(@\"" + Directory.GetCurrentDirectory() + "\\" + DllPath.Substring(DllPath.LastIndexOf("\\") + 1) + "\", CallingConvention = CallingConvention.Cdecl)] \n";
            lcCode = lcCode + "public static extern void _" + MethodName + "Initialize(); \n";

            // main MATLAB functional termination method
            lcCode = lcCode + "[DllImport(@\"" + Directory.GetCurrentDirectory() + "\\" + DllPath.Substring(DllPath.LastIndexOf("\\") + 1) + "\", CallingConvention = CallingConvention.Cdecl)] \n";
            lcCode = lcCode + "public static extern void _" + MethodName + "Terminate(); \n";

            if (Counter == 0)
            {
                // load MATLAB engine methods needed
                lcCode = lcCode + "[DllImport(\"mclmcrrt77.dll\", CallingConvention = CallingConvention.Cdecl)] \n";
                lcCode = lcCode + "public static extern IntPtr mxCreateDoubleScalar(double value); \n";

                lcCode = lcCode + "[DllImport(\"mclmcrrt77.dll\", CallingConvention = CallingConvention.Cdecl)] \n";
                lcCode = lcCode + "private static extern IntPtr mxCreateDoubleMatrix_730(int noOfRows, int noOfCols, string realOrComplex); \n";

                lcCode = lcCode + "[DllImport(\"mclmcrrt77.dll\", CallingConvention = CallingConvention.Cdecl)] \n";
                lcCode = lcCode + "public static extern IntPtr mxCreateString(string value); \n";

                lcCode = lcCode + "[DllImport(\"mclmcrrt77.dll\", CallingConvention = CallingConvention.Cdecl)] \n";
                lcCode = lcCode + "public static extern void mxDestroyArray(IntPtr value); \n";

                lcCode = lcCode + "[DllImport(\"mclmcrrt77.dll\", CallingConvention = CallingConvention.Cdecl)] \n";
                lcCode = lcCode + "public static extern double mxGetScalar(IntPtr value); \n";

                lcCode = lcCode + "[DllImport(\"mclmcrrt77.dll\", CallingConvention = CallingConvention.Cdecl)] \n";
                lcCode = lcCode + "public static extern string mxGetString_730(IntPtr value); \n";

                lcCode = lcCode + "[DllImport(\"mclmcrrt77.dll\", CallingConvention = CallingConvention.Cdecl)] \n";
                lcCode = lcCode + "public static extern bool mclInitializeApplication(string options, Int32 count); \n";

                lcCode = lcCode + "[DllImport(\"mclmcrrt77.dll\", CallingConvention = CallingConvention.Cdecl)] \n";
                lcCode = lcCode + "public static extern void mclTerminateApplication(); \n";

                lcCode = lcCode + "[DllImport(\"mclmcrrt77.dll\", CallingConvention = CallingConvention.Cdecl)] \n";
                lcCode = lcCode + "private static extern IntPtr mxGetPr(IntPtr value); \n";
                Counter++;
            }

            lcCode = lcCode + "public object " + Name + "(" + lcCodeInOnly + " ) \n";
            lcCode = lcCode + "{ \n";

            //lcCode = lcCode + "bool RetVal = mclInitializeApplication(\"NULL\", 0); \n";

            // intialise the MATLAB functional method
            lcCode = lcCode + "_" + MethodName + "Initialize(); \n";
            lcCode = lcCode + "int nargout = " + nargout + "; \n";
            //lcCode = lcCode + "ArrayList outputsNames = new ArrayList(); \n";
            foreach (Data dt in ModelDataInputs)
            {
                if (dt is DoubleData)
                {
                    lcCode = lcCode + "IntPtr " + dt.Name + "_1" + " = mxCreateDoubleScalar(" + dt.Name + "); \n";
                    // outputsNames.Add(dt.name);
                }
                else if (dt is DoubleVectorData)
                {
                    lcCode = lcCode + "int " + dt.Name + "_" + "NoElements = " + (dt.Value as double[]).GetLength(0) + "; \n";
                    lcCode = lcCode + "IntPtr " + dt.Name + "_1" + " = mxCreateDoubleMatrix_730(1, " + dt.Name + "_" + "NoElements, \"mxREAL\"); \n";
                    lcCode = lcCode + "Marshal.Copy(" + dt.Name + ", 0, mxGetPr(" + dt.Name + "_1" + "), " + dt.Name + "_" + "NoElements);\n";
                }
                else if (dt is DoubleMatrixData)
                {
                    lcCode = lcCode + "int " + dt.Name + "_" + "NoInElements_x = " + (dt.Value as double[, ]).GetLength(0) + "; \n";
                    lcCode = lcCode + "int " + dt.Name + "_" + "NoInElements_y = " + (dt.Value as double[, ]).GetLength(1) + "; \n";
                    lcCode = lcCode + "IntPtr " + dt.Name + "_1" + " = mxCreateDoubleMatrix_730(" + dt.Name + "_" + "NoInElements_x, " + dt.Name + "_" + "NoInElements_y, \"mxREAL\"); \n";
                    lcCode = lcCode + "double[] " + dt.Name + "_ArrayConversion" + " = new double[" + dt.Name + "_" + "NoInElements_x * " + dt.Name + "_" + "NoInElements_y];\n";

                    lcCode = lcCode + "int " + dt.Name + "_" + "ArrayConversionCounter = 0; \n";
                    lcCode = lcCode + "foreach (double MatrixElement in (" + dt.Name + " as double[,])) \n";
                    lcCode = lcCode + "{ \n";
                    lcCode = lcCode + dt.Name + "_ArrayConversion[" + dt.Name + "_ArrayConversionCounter] = MatrixElement; \n";
                    lcCode = lcCode + "        " + dt.Name + "_ArrayConversionCounter++; \n";
                    lcCode = lcCode + "} \n";

                    lcCode = lcCode + "Marshal.Copy(" + dt.Name + "_ArrayConversion, 0, mxGetPr(" + dt.Name + "_1" + "), " + dt.Name + "_" + "NoInElements_x * " + dt.Name + "_" + "NoInElements_y);\n";
                }
                else if (dt is StringData)
                {
                    //lcCode = lcCode + "Console.WriteLine(\"Input string ...\"); \n";
                    lcCode = lcCode + "IntPtr " + dt.Name + "_1" + " = mxCreateString(" + Convert.ToString(dt.Name) + "); \n";
                    //lcCode = lcCode + "Console.WriteLine(\"Input string ok!\"); \n";
                }
            }

            foreach (Data dt in ModelDataOutputs)
            {
                if (dt is DoubleData)
                {
                    lcCode = lcCode + "IntPtr " + dt.Name + "_1" + " = mxCreateDoubleScalar(" + (double)(dt.Value) + "); \n";
                }
                else if (dt is DoubleVectorData)
                {
                    lcCode = lcCode + "int " + dt.Name + "_" + "NoElements = " + (dt.Value as double[]).GetLength(0) + "; \n";
                    lcCode = lcCode + "double[] " + dt.Name + " = new double[" + dt.Name + "_" + "NoElements]; \n";
                    lcCode = lcCode + "IntPtr " + dt.Name + "_1" + " = mxCreateDoubleMatrix_730(1, " + dt.Name + "_" + "NoElements, \"mxREAL\"); \n";
                }
                else if (dt is DoubleMatrixData)
                {
                    lcCode = lcCode + "int " + dt.Name + "_" + "NoOutElements_x = " + (dt.Value as double[, ]).GetLength(0) + "; \n";
                    lcCode = lcCode + "int " + dt.Name + "_" + "NoOutElements_y = " + (dt.Value as double[, ]).GetLength(1) + "; \n";
                    lcCode = lcCode + "double[,] " + dt.Name + " = new double[" + dt.Name + "_" + "NoOutElements_x," + dt.Name + "_" + "NoOutElements_y]; \n";
                    lcCode = lcCode + "IntPtr " + dt.Name + "_1" + " = mxCreateDoubleMatrix_730(" + dt.Name + "_" + "NoOutElements_x, " + dt.Name + "_" + "NoOutElements_y, \"mxREAL\"); \n";
                    lcCode = lcCode + "double[] " + dt.Name + "_ArrayConversion" + " = new double[" + dt.Name + "_" + "NoOutElements_x * " + dt.Name + "_" + "NoOutElements_y];\n";
                }
                else if (dt is StringData)
                {
                    //lcCode = lcCode + "Console.WriteLine(\"Output string ...\"); \n";
                    lcCode = lcCode + "IntPtr " + dt.Name + "_1" + " = mxCreateString(" + "Convert.ToString(0)" + "); \n";
                    //lcCode = lcCode + "Console.WriteLine(\"Output string ok!\"); \n";
                }
            }

            // creating results storage fields
            //int resultSize = outputs.Count;
            //lcCode = lcCode + "Dictionary<string, double> results = new Dictionary<string, double>("
            //                                                          + resultSize + "); \n";


            // begin execution of MATLAB functional method - TRY
            lcCode = lcCode + "try { \n";

            // get actual OUTPUT parameters to supply to MATLAB function method
            string actualParamsOut = "";

            foreach (Data dt in ModelDataOutputs)
            {
                actualParamsOut = actualParamsOut + "ref " + dt.Name + "_1" + ",";                //Check if _1 is necessary!!!!!
            }

            // get actual INPUT parameters to supply to MATLAB function method
            string actualParamsIn = "";

            foreach (Data dt in ModelDataInputs)
            {
                actualParamsIn = actualParamsIn + dt.Name + "_1" + ",";                //Check if _1 is necessary!!!!!
            }
            if (actualParamsIn != "")
            {
                actualParamsIn = actualParamsIn.Remove(actualParamsIn.Length - 1);
            }
            lcCode = lcCode + "_mlf" + methodTempName + "(" + "nargout, "
                     + actualParamsOut + actualParamsIn + "); \n";
            foreach (Data dt in ModelDataOutputs)
            {
                if (dt is DoubleData)
                {
                    lcCode = lcCode + "double " + dt.Name + "= mxGetScalar(" + dt.Name + "_1" + ");\n";
                }
                else if (dt is DoubleVectorData)
                {
                    //lcCode = lcCode + "int " + dt.name + "_" + "NoElements = " + (dt.values as double[]).GetLength(0) + "; \n";
                    lcCode = lcCode + "Marshal.Copy(mxGetPr(" + dt.Name + "_1), " + dt.Name + ", 0," + dt.Name + "_NoElements); \n";
                    //System.Console.WriteLine("Output for ARRAY IO test = [" + actualOut3[0] + ", " + actualOut3[1] + "]");
                }
                else if (dt is DoubleMatrixData)
                {
                    lcCode = lcCode + "Marshal.Copy(mxGetPr(" + dt.Name + "_1), " + dt.Name + "_ArrayConversion, 0," + dt.Name + "_NoOutElements_x *" + dt.Name + "_NoOutElements_y); \n";
                    lcCode = lcCode + "for (int IthRow = 0; IthRow <" + dt.Name + "_NoOutElements_x; IthRow++) \n";
                    lcCode = lcCode + "{ \n";
                    lcCode = lcCode + "for (int JthCol = 0; JthCol <" + dt.Name + "_NoOutElements_y; JthCol++) \n";
                    lcCode = lcCode + "{ \n";
                    lcCode = lcCode + dt.Name + "[IthRow,JthCol] = " + dt.Name + "_ArrayConversion[IthRow * " + dt.Name + "_NoOutElements_y + JthCol]; \n";
                    lcCode = lcCode + "} \n";
                    lcCode = lcCode + "} \n";
                }
                else if (dt is StringData)
                {
                    lcCode = lcCode + "string " + dt.Name + "= mxGetString_730(" + dt.Name + "_1" + ");\n";
                    //lcCode = lcCode + "Console.WriteLine(\"Check2 ok!\"); \n";
                }
            }

            //lcCode = lcCode + "foreach (string s1 in outputsNames) \n";
            //lcCode = lcCode + "{ \n";
            //lcCode = lcCode + "double s1 = mxGetScalar(s1);";
            //lcCode = lcCode + "} \n";
            lcCode += "object[] outputs_all=new object[" + Convert.ToString(ModelDataOutputs.Count) + "]; \n";
            int ncount = 0;

            foreach (Data dt in ModelDataOutputs)
            {
                lcCode += "outputs_all[" + Convert.ToString(ncount) + "]=" + dt.Name + ";\n";
                ncount++;
                //lcCode = lcCode +"return " + dt.name + ";";
            }
            lcCode += "object outputs_return=outputs_all; \n";
            lcCode  = lcCode + "return outputs_return; \n";
            // end of TRY bracket
            lcCode = lcCode + "} \n";
            lcCode = lcCode + "catch (Exception e) \n";
            lcCode = lcCode + "{ \n";
            lcCode = lcCode + "Console.WriteLine(e.Message); \n";
            lcCode = lcCode + "return null; \n";
            lcCode = lcCode + "} \n";

            // Terminate libraries and MCR
            //foreach (Data dt in outputs)
            // {
            //     lcCode = lcCode + "mxDestroyArray(dt.name); \n";
            //}
            lcCode = lcCode + "_" + MethodName + "Terminate(); \n";
            //lcCode = lcCode + "mclTerminateApplication(); \n";
            lcCode = lcCode + "} \n";
            //System.Console.WriteLine(lcCode);
            Code = lcCode;

            Compile(AircadiaProject.Instance.ProjectPath);
        }
        public override void PrepareForExecution()
        {
            //Creates the DLLS for model

            string lcCode       = "";
            string lcCodeIn     = "";
            string lcCodeOut    = "";
            string lcCodeInOnly = "";
            //string lcCodeInNme = "";
            int nargout = 0;

            //creates output parameter data
            nargout = ModelDataOutputs.Count;
            foreach (Data dt in ModelDataOutputs)
            {
                string outParamsIdent = "[In, Out] ref IntPtr ";
                string outParamsName  = dt.Name + "_1" + ",";
                lcCodeOut = lcCodeOut + outParamsIdent + outParamsName;
            }
            //creates input parameter data
            foreach (Data dt in ModelDataInputs)
            {
                string inParamsIdent = "[In]IntPtr ";
                string inParamsName  = dt.Name + "_1" + ",";
                lcCodeIn = lcCodeIn + inParamsIdent + inParamsName;
                //lcCodeInOnly = lcCodeInOnly + "double " + dt.name + ",";
                if (dt is DoubleData)
                {
                    lcCodeInOnly = lcCodeInOnly + "double " + dt.Name + ",";
                }
                if (dt is DoubleVectorData)
                {
                    lcCodeInOnly = lcCodeInOnly + "double[] " + dt.Name + ",";
                }
                if (dt is DoubleMatrixData)
                {
                    lcCodeInOnly = lcCodeInOnly + "double[,] " + dt.Name + ",";
                }
                if (dt is IntegerData)
                {
                    lcCodeInOnly = lcCodeInOnly + "int " + dt.Name + ",";
                }
                if (dt is IntegerVectorData)
                {
                    lcCodeInOnly = lcCodeInOnly + "int[] " + dt.Name + ",";
                }
                if (dt is StringData)
                {
                    lcCodeInOnly = lcCodeInOnly + "string " + dt.Name + ",";
                }
            }
            if (lcCodeIn != "")
            {
                lcCodeIn = lcCodeIn.Remove(lcCodeIn.Length - 1);

                lcCodeInOnly = lcCodeInOnly.Remove(lcCodeInOnly.Length - 1);
            }

            // main MATLAB functional method
            lcCode = "\n[DllImport(@\"" + Directory.GetCurrentDirectory() + "\\" + DllPath.Substring(DllPath.LastIndexOf("\\") + 1) + "\")]" + "\n";
            string methodTempName = MethodName;

            if (methodTempName.Length <= 1)
            {
                Code = methodTempName.ToUpper();
            }
            else
            {
                char[] letters = methodTempName.ToCharArray();
                letters[0]     = Char.ToUpper(letters[0]);
                methodTempName = new string(letters);
            }
            lcCode = lcCode + "public static extern void _mlf" + methodTempName + "(" + "[In]int nargout," +
                     " " + lcCodeOut + lcCodeIn + "); \n";

            // main MATLAB functional intialization method
            lcCode = lcCode + "[DllImport(@\"" + Directory.GetCurrentDirectory() + "\\" + DllPath.Substring(DllPath.LastIndexOf("\\") + 1) + "\")]" + "\n";
            lcCode = lcCode + "public static extern void _" + MethodName + "Initialize(); \n";

            // main MATLAB functional termination method
            lcCode = lcCode + "[DllImport(@\"" + Directory.GetCurrentDirectory() + "\\" + DllPath.Substring(DllPath.LastIndexOf("\\") + 1) + "\")]" + "\n";
            lcCode = lcCode + "public static extern void _" + MethodName + "Terminate(); \n";

            if (Counter == 0)
            {
                // load MATLAB engine methods needed
                lcCode = lcCode + "[DllImport(\"mclmcrrt78.dll\")] \n";
                lcCode = lcCode + "public static extern IntPtr mxCreateDoubleScalar_proxy(double value); \n";

                lcCode = lcCode + "[DllImport(\"mclmcrrt78.dll\")] \n";
                lcCode = lcCode + "public static extern void mxDestroyArray_proxy(IntPtr value); \n";

                lcCode = lcCode + "[DllImport(\"mclmcrrt78.dll\")] \n";
                lcCode = lcCode + "public static extern double mxGetScalar_proxy(IntPtr value); \n";

                lcCode = lcCode + "[DllImport(\"mclmcrrt78.dll\")] \n";
                lcCode = lcCode + "public static extern bool mclInitializeApplication_proxy(string options, Int32 count); \n";

                lcCode = lcCode + "[DllImport(\"mclmcrrt78.dll\")] \n";
                lcCode = lcCode + "public static extern void mclTerminateApplication_proxy(); \n";
                Counter++;
            }

            lcCode = lcCode + "public object " + Name + "(" + lcCodeInOnly + " ) \n";
            lcCode = lcCode + "{ \n";

            //lcCode = lcCode + "bool RetVal = mclInitializeApplication(\"NULL\", 0); \n";

            // intialise the MATLAB functional method
            lcCode = lcCode + "_" + MethodName + "Initialize(); \n";
            lcCode = lcCode + "int nargout = " + nargout + "; \n";
            //lcCode = lcCode + "ArrayList outputsNames = new ArrayList(); \n";
            foreach (Data dt in ModelDataInputs)
            {
                lcCode = lcCode + "IntPtr " + dt.Name + "_1" + " = mxCreateDoubleScalar_proxy(" + dt.Name + "); \n";
                // outputsNames.Add(dt.name);
            }

            foreach (Data dt in ModelDataOutputs)
            {
                lcCode = lcCode + "IntPtr " + dt.Name + "_1" + " = mxCreateDoubleScalar_proxy(" + (double)(dt.Value) + "); \n";
            }

            // creating results storage fields
            //int resultSize = outputs.Count;
            //lcCode = lcCode + "Dictionary<string, double> results = new Dictionary<string, double>("
            //                                                          + resultSize + "); \n";


            // begin execution of MATLAB functional method - TRY
            lcCode = lcCode + "try { \n";

            // get actual OUTPUT parameters to supply to MATLAB function method
            string actualParamsOut = "";

            foreach (Data dt in ModelDataOutputs)
            {
                actualParamsOut = actualParamsOut + "ref " + dt.Name + "_1" + ",";
            }

            // get actual INPUT parameters to supply to MATLAB function method
            string actualParamsIn = "";

            foreach (Data dt in ModelDataInputs)
            {
                actualParamsIn = actualParamsIn + dt.Name + "_1" + ",";
            }
            if (actualParamsIn != "")
            {
                actualParamsIn = actualParamsIn.Remove(actualParamsIn.Length - 1);
            }
            lcCode = lcCode + "_mlf" + methodTempName + "(" + "nargout, "
                     + actualParamsOut + actualParamsIn + "); \n";
            foreach (Data dt in ModelDataOutputs)
            {
                lcCode = lcCode + "double " + dt.Name + "= mxGetScalar_proxy(" + dt.Name + "_1" + ");\n";
            }
            //lcCode = lcCode + "foreach (string s1 in outputsNames) \n";
            //lcCode = lcCode + "{ \n";
            //lcCode = lcCode + "double s1 = mxGetScalar(s1);";
            //lcCode = lcCode + "} \n";
            lcCode += "object[] outputs_all=new object[" + Convert.ToString(ModelDataOutputs.Count) + "]; \n";
            int ncount = 0;

            foreach (Data dt in ModelDataOutputs)
            {
                lcCode += "outputs_all[" + Convert.ToString(ncount) + "]=" + dt.Name + ";\n";
                ncount++;
                //lcCode = lcCode +"return " + dt.name + ";";
            }
            lcCode += "object outputs_return=outputs_all; \n";
            lcCode  = lcCode + "return outputs_return; \n";
            // end of TRY bracket
            lcCode = lcCode + "} \n";
            lcCode = lcCode + "catch (Exception e) \n";
            lcCode = lcCode + "{ \n";
            lcCode = lcCode + "Console.WriteLine(e.Message); \n";
            lcCode = lcCode + "return null; \n";
            lcCode = lcCode + "} \n";

            // Terminate libraries and MCR
            //foreach (Data dt in outputs)
            // {
            //     lcCode = lcCode + "mxDestroyArray(dt.name); \n";
            //}
            lcCode = lcCode + "_" + MethodName + "Terminate(); \n";
            //lcCode = lcCode + "mclTerminateApplication(); \n";
            lcCode = lcCode + "} \n";
            //System.Console.WriteLine(lcCode);
            Code = lcCode;

            Compile(AircadiaProject.Instance.ProjectPath);
        }
 public override int GetHashCode()
 {
     return(Clsid.GetHashCode() ^ Name.GetSafeHashCode() ^ DllPath.GetSafeHashCode()
            ^ Server.GetSafeHashCode() ^ ActivationType.GetHashCode() ^ TrustLevel.GetHashCode()
            ^ Permissions.GetSafeHashCode() & Threading.GetHashCode());
 }
예제 #4
0
        public MainWindowViewModel()
        {
            // Load
            LoadDefaultConfiguration();

            var useConnectionType = UseConnectionTypeSelectedIndex
                                    .Select(x => (ConnectionProtocol)Enum.Parse(typeof(ConnectionProtocol), UseConnectionType[x]))
                                    .ToReactiveProperty();

            // Setup peer
            peer = new ReactiveProperty <ObservablePhotonPeer>(new UseJsonObservablePhotonPeer(useConnectionType.Value));
            peer.Select(x => x.ObserveStatusChanged())
            .Switch()
            .Subscribe(x =>
            {
                if (x == StatusCode.Connect)
                {
                    CurrentConnectionStatus.Value = "Connecting : " + Address.Value + " " + AppName.Value;
                }
                else
                {
                    CurrentConnectionStatus.Value = x.ToString();
                }
                Log.WriteLine(CurrentConnectionStatus.Value);
            });

            // Setup Properties

            HubInfoListSelectedIndex.Subscribe(x =>
            {
                foreach (var item in OperationInfoList)
                {
                    item.Dispose();
                }
                OperationInfoList.Clear();
                if (x == -1)
                {
                    return;
                }
                if (HubInfoList.Count - 1 < x)
                {
                    return;
                }

                var hub = HubInfoList[x];
                foreach (var item in hub.Operations)
                {
                    OperationInfoList.Add(new OperationItemViewModel(peer, Log, item));
                }
            });

            // Setup Commands

            var photonProcessExists = Observable.Interval(TimeSpan.FromSeconds(1)).Select(x => Process.GetProcessesByName("PhotonSocketserver").Any());

            KillPhotonProcess = photonProcessExists.ToReactiveCommand();
            KillPhotonProcess.Subscribe(_ =>
            {
                var processes = Process.GetProcessesByName("PhotonSocketServer");
                foreach (var item in processes)
                {
                    item.Kill();
                }
            });

            StartPhotonProcess = ProcessPath.CombineLatest(WorkingDir, (processPath, workingDir) => new { processPath, workingDir })
                                 .Select(x => !string.IsNullOrWhiteSpace(x.processPath + x.workingDir))
                                 .CombineLatest(photonProcessExists, (x, y) => x && !y)
                                 .ToReactiveCommand();
            StartPhotonProcess.Subscribe(_ =>
            {
                try
                {
                    var processPath = ProcessPath.Value;
                    var workingDir  = WorkingDir.Value;

                    var pi = new ProcessStartInfo
                    {
                        FileName         = ProcessPath.Value,
                        Arguments        = ProcessArgument.Value,
                        WorkingDirectory = workingDir
                    };
                    System.Diagnostics.Process.Start(pi);

                    SaveConfiguration(); // can start, save path
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            });

            ReloadDll = DllPath.Select(x => File.Exists(x.Trim('\"'))).ToReactiveCommand(ImmediateScheduler.Instance); // needs Immediate check for InitialLoad(see:bottom code)
            ReloadDll.Subscribe(_ =>
            {
                try
                {
                    HubInfoList.Clear();
                    var hubInfos = HubAnalyzer.LoadHubInfos(DllPath.Value.Trim('\"'));
                    SaveConfiguration(); // can load, save path

                    hubInfoLookup = hubInfos.ToDictionary(x => x.HubId);

                    foreach (var hub in hubInfos)
                    {
                        HubInfoList.Add(hub);
                    }
                    HubInfoListSelectedIndex.Value = 0;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            });

            Connect = peer.Select(x => x.ObserveStatusChanged())
                      .Switch()
                      .CombineLatest(Address, (x, y) => x != StatusCode.Connect && !string.IsNullOrEmpty(y))
                      .ToReactiveCommand();
            Connect.Subscribe(async _ =>
            {
                try
                {
                    peer.Value.Dispose();
                    peer.Value = new UseJsonObservablePhotonPeer(useConnectionType.Value);
                    var b      = await peer.Value.ConnectAsync(Address.Value, AppName.Value);
                    Log.WriteLine("Connect:" + b);
                    if (b)
                    {
                        SaveConfiguration(); // can connect, save path

                        // Register Listener
                        listenerSubscription.Disposable = peer.Value.ObserveReceiveEventData().Subscribe(ReceiveEvent);
                    }
                    else
                    {
                        listenerSubscription.Disposable = System.Reactive.Disposables.Disposable.Empty;
                    }
                }
                catch (Exception ex)
                {
                    Log.WriteLine("Can't connect:" + ex.ToString());
                    listenerSubscription.Disposable = System.Reactive.Disposables.Disposable.Empty;
                }
            });

            Disconnect = peer.Select(x => x.ObserveStatusChanged())
                         .Switch()
                         .Select(x => x == StatusCode.Connect)
                         .ToReactiveCommand();
            Disconnect.Subscribe(_ =>
            {
                try
                {
                    peer.Value.Disconnect();
                }
                catch (Exception ex)
                {
                    Log.WriteLine("Can't disconnect:" + ex.ToString());
                }
            });

            LogClear = new ReactiveCommand();
            LogClear.Subscribe(_ =>
            {
                Log.Value = "";
            });

            // Initial Load
            if (ReloadDll.CanExecute())
            {
                ReloadDll.Execute();
            }

            // Initial VersionInfo
            VersionInfo = Assembly.GetExecutingAssembly().GetCustomAttribute <AssemblyFileVersionAttribute>().Version.ToString();
            if (peer.Value.ObserveStatusChanged().FirstAsync().GetAwaiter().GetResult() == StatusCode.Disconnect)
            {
                CurrentConnectionStatus.Value = "PhotonWire.HubInvoker " + VersionInfo;
            }
        }
예제 #5
0
        public override void PrepareForExecution()
        {
            //Creates the DLLS for model
            string lcCode      = "";
            string lcCodeIn    = "";
            string lcCodeInNme = "";

            foreach (Data dt in ModelDataInputs)
            {
                //lcCode = lcCode + "double " + dt.name + " = " + Convert.ToString(dt.value) + ";\n";
                lcCodeIn    = lcCodeIn + "double " + dt.Name + ",";
                lcCodeInNme = lcCodeInNme + dt.Name + ",";
            }
            if (lcCodeIn != "")
            {
                lcCodeIn    = lcCodeIn.Remove(lcCodeIn.Length - 1);
                lcCodeInNme = lcCodeInNme.Remove(lcCodeInNme.Length - 1);
            }
            foreach (Data dt in ModelDataOutputs)
            {
                lcCode = lcCode + "double " + dt.Name + " = " + Convert.ToString((double)(dt.Value)) + ";\n";

                /*if (dt.name == "rwswa")
                 * {
                 * Console.Write(dt.name + "=" + Convert.ToString(dt.value) + "\n");
                 * this.dllname = "rwswa=phi;";
                 * }*/
            }
            lcCode  = lcCode + (ModelDataOutputs[0] as Data).Name + "=" + MethodName + "(" + lcCodeInNme + ");" + "\n";
            lcCode += "object[] outputs_all=new object[" + Convert.ToString(ModelDataOutputs.Count) + "];";
            int ncount = 0;

            foreach (Data dt in ModelDataOutputs)
            {
                lcCode += "outputs_all[" + Convert.ToString(ncount) + "]=" + dt.Name + ";\n";
                ncount++;
                //lcCode = lcCode +"return " + dt.name + ";";
            }
            lcCode += "object outputs_return=outputs_all;";
            lcCode  = lcCode + "return outputs_return;";
            // *** Must create a fully functional assembly
            lcCode = "[DllImport(@\"" + Directory.GetCurrentDirectory() + "\\" + DllPath.Substring(DllPath.LastIndexOf("\\") + 1) + "\")]" +
                     "private static extern double " + MethodName + "(" + lcCodeIn + ");" +
                     "public object " + Name + @"(" + lcCodeIn + @") {
				"                 + lcCode +
                     "} ";

            Code = lcCode;

            Compile(AircadiaProject.Instance.ProjectPath);
        }
예제 #6
0
 private void metroButton2_Click(object sender, EventArgs e)
 {
     DllPath.ShowDialog();
     Dll_Box.Text = DllPath.FileName;
 }