Example #1
0
        /// <summary>
        /// Runs the formula
        /// </summary>
        /// <param name="formula">The formula to run</param>
        /// <param name="startTime">The start time to evaluate from</param>
        /// <param name="endTime">The end time to stop evaluating from</param>
        /// <param name="skipMissingValues">Whether or not to skip missing values</param>
        /// <param name="reason">The reason for the evaluation</param>
        /// <returns>The sensor used and the newly evaulated state</returns>
        private KeyValuePair<Sensor, SensorState> RunCode(Formula formula, DateTime startTime, DateTime endTime, bool skipMissingValues, ChangeReason reason)
        {
            var executingAssembly = formula.CompilerResults.CompiledAssembly;
            try
            {
                //cant call the entry method if the assembly is null
                if (executingAssembly != null)
                {
                    var assemblyInstance = executingAssembly.CreateInstance("IndiaTango.Calculator");
                    //Use reflection to call the static Main function

                    var modules = executingAssembly.GetModules(false);
                    var types = modules[0].GetTypes();

                    //loop through each class that was defined and look for the first occurrance of the entry point method
                    foreach (var mi in from type in types select type.GetMethods() into mis from mi in mis where mi.Name == "ApplyFormula" select mi)
                    {
                        return (KeyValuePair<Sensor, SensorState>)mi.Invoke(assemblyInstance, new object[] { _sensorVariables, formula.SensorAppliedTo, startTime, endTime, skipMissingValues, reason });
                    }

                }
            }
            catch (Exception ex)
            {
                Common.ShowMessageBoxWithException("Error", "An exception occurred while executing the script", false, true, ex);
                Debug.WriteLine("Error:  An exception occurred while executing the script: \n" + ex);
            }

            return new KeyValuePair<Sensor, SensorState>(null, null);
        }
Example #2
0
        /// <summary>
        /// Evaluates a formula
        /// </summary>
        /// <param name="formula">The formula to evaluate</param>
        /// <param name="startTime">The start time to evaluate from</param>
        /// <param name="endTime">The end time to stop evaluating from</param>
        /// <param name="skipMissingValues">Whether or not to skip missing values</param>
        /// <param name="reason">The reason for the evaluation</param>
        /// <returns>The sensor used and the newly evaulated state</returns>
        public KeyValuePair<Sensor, SensorState> EvaluateFormula(Formula formula, DateTime startTime, DateTime endTime, bool skipMissingValues, ChangeReason reason)
        {
            if (startTime >= endTime)
                throw new ArgumentException("End time must be greater than start time");

            // if the code compiled okay,
            // run the code using the new assembly (which is inside the results)
            if (formula.CompilerResults != null && formula.CompilerResults.CompiledAssembly != null)
            {
                // run the evaluation function
                return RunCode(formula, startTime, endTime, skipMissingValues, reason);
            }

            Debug.WriteLine("Could not evaluate formula");
            return new KeyValuePair<Sensor, SensorState>(null, null);
        }