Can be used to manager the properties of an object in OSA
Пример #1
0
        public static void CreateComputerObject(string sourceName)
        {
            Logging.GetLogger().AddToLog("Creating Computer object", true);
            string computerIp = Common.GetComputerIP();

            if (OSAEObjectManager.GetObjectByName(Common.ComputerName) == null)
            {
                OSAEObject obj = OSAEObjectManager.GetObjectByAddress(computerIp);
                if (obj == null)
                {
                    OSAEObjectManager.ObjectAdd(Common.ComputerName, Common.ComputerName, Common.ComputerName, "COMPUTER", computerIp, string.Empty, true);
                    OSAEObjectPropertyManager.ObjectPropertySet(Common.ComputerName, "Host Name", Common.ComputerName, sourceName);
                }
                else if (obj.Type == "COMPUTER")
                {
                    OSAEObjectManager.ObjectUpdate(obj.Name, Common.ComputerName, obj.Alias, obj.Description, "COMPUTER", computerIp, obj.Container, obj.Enabled);
                    OSAEObjectPropertyManager.ObjectPropertySet(Common.ComputerName, "Host Name", Common.ComputerName, sourceName);
                }
                else
                {
                    OSAEObjectManager.ObjectAdd(Common.ComputerName + "." + computerIp, Common.ComputerName, Common.ComputerName, "COMPUTER", computerIp, string.Empty, true);
                    OSAEObjectPropertyManager.ObjectPropertySet(Common.ComputerName + "." + computerIp, "Host Name", Common.ComputerName, sourceName);
                }
            }
            else
            {
                OSAEObject obj = OSAEObjectManager.GetObjectByName(Common.ComputerName);
                OSAEObjectManager.ObjectUpdate(obj.Name, obj.Name, obj.Alias, obj.Description, "COMPUTER", computerIp, obj.Container, obj.Enabled);
                OSAEObjectPropertyManager.ObjectPropertySet(obj.Name, "Host Name", Common.ComputerName, sourceName);
            }
        }
        /// <summary>
        /// Returns an OSAEObject with the specified name
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static OSAEObject GetObjectByName(string name)
        {
            MySqlCommand command = new MySqlCommand();
            DataSet      dataset = new DataSet();

            try
            {
                command.CommandText = "SELECT object_name, object_description, object_type, address, container_name, enabled, state_name, base_type, coalesce(time_in_state, 0) as time_in_state FROM osae_v_object WHERE object_name=@Name";
                command.Parameters.AddWithValue("@Name", name);
                dataset = OSAESql.RunQuery(command);

                if (dataset.Tables[0].Rows.Count > 0)
                {
                    OSAEObject obj = new OSAEObject(dataset.Tables[0].Rows[0]["object_name"].ToString(), dataset.Tables[0].Rows[0]["object_description"].ToString(), dataset.Tables[0].Rows[0]["object_type"].ToString(), dataset.Tables[0].Rows[0]["address"].ToString(), dataset.Tables[0].Rows[0]["container_name"].ToString(), int.Parse(dataset.Tables[0].Rows[0]["enabled"].ToString()));
                    obj.State.Value       = dataset.Tables[0].Rows[0]["state_name"].ToString();
                    obj.State.TimeInState = Convert.ToInt64(dataset.Tables[0].Rows[0]["time_in_state"]);
                    obj.BaseType          = dataset.Tables[0].Rows[0]["base_type"].ToString();

                    obj.Properties = OSAEObjectPropertyManager.GetObjectProperties(obj.Name);

                    obj.Methods = GetObjectMethods(obj.Name);

                    return(obj);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                Logging.GetLogger().AddToLog("API - GetObjectByName (" + name + ")error: " + ex.Message, true);
                return(null);
            }
        }
Пример #3
0
        /// <summary>
        /// Determines if the System object has a SecurityKey property.
        /// If property exist, will verify it contains a valid Key.
        /// If no key is found, a new key is created.
        /// If Property does not exist, will create and populate a new property.
        /// </summary>
        public static void GenerateSecurityKey()
        {
            string securityKey = "";

            try
            {
                Log.Info("Checking if System Security Key exist");
                string cSSK = OSAE.OSAEObjectPropertyManager.GetObjectPropertyValue("System", "SecurityKey").Value;
                if (cSSK == " ")
                {
                    Log.Info("System Security Key found, but is empty.");
                    Log.Info("Generating a new Security Key...");
                    securityKey = GenerateRandomCryptographicKey(12);
                    OSAEObjectPropertyManager.ObjectPropertySet("System", "SecurityKey", securityKey, "Rest");
                }
                else
                {
                    Log.Info("System Security Key found.");
                }
            }
            catch
            {
                Log.Info("System Security Key does not exist..");
                Log.Info("Creating System object property: SecurityKey.");
                OSAEObjectTypeManager.ObjectTypePropertyAdd("System", "SecurityKey", "String", "", "", false, true, "This key is used in HTTP and Rest authorization. Must be 16 characters.");
                Log.Info("Generating a new Security Key...");
                securityKey = GenerateRandomCryptographicKey(12);
                OSAEObjectPropertyManager.ObjectPropertySet("System", "SecurityKey", securityKey, "Rest");
            }
        }
Пример #4
0
        /// <summary>
        /// Determines if the Rest Plugin has an APIKEY property.
        /// If property exist, will verify it contains a valid Key.
        /// If no key is found, a new key is created.
        /// If Property does not exist, will create and populate a new property.
        /// </summary>
        public static void GenerateAPIKey()
        {
            string apiKey = "";

            try
            {
                Log.Info("Checking if REST API Key exist");
                string cRAK = OSAE.OSAEObjectPropertyManager.GetObjectPropertyValue("Rest", "APIKEY").Value;
                if (cRAK == " ")
                {
                    Log.Info("REST API Key found, but is empty.");
                    Log.Info("Generating a new Rest API Key...");
                    apiKey = GenerateRandomCryptographicKey(24);
                    OSAEObjectPropertyManager.ObjectPropertySet("Rest", "APIKEY", apiKey, "Rest");
                }
                else
                {
                    Log.Info("REST API Key found.");
                }
            }
            catch
            {
                Log.Info("REST API Key does not exist..");
                Log.Info("Creating Rest Plugin property: APIKEY.");
                OSAEObjectTypeManager.ObjectTypePropertyAdd("Rest", "APIKEY", "String", "", "", false, true, "This is the Rest Plugin API Key. This property isused by remote clients and devices to encrypt and decrypt user information for authentication.");
                Log.Info("Generating a new Rest API Key...");
                apiKey = GenerateRandomCryptographicKey(24);
                OSAEObjectPropertyManager.ObjectPropertySet("Rest", "APIKEY", apiKey, "Rest");;
            }
        }
Пример #5
0
        // Authorize
        /// <summary>
        /// Decrypts an OSAE Security encrypted Authentication string, and compares trust level with the object being accessed.
        /// Used to verify User Trust in GET request
        /// </summary>
        /// <param name="authkey">An OSAE Security encrypted String</param>
        /// <param name="objName">The name of the OSAE Object being accessed</param>
        /// <returns>True or False, depending on criteria matches.</returns>
        public static Boolean Authorize(string authkey, string objName)
        {
            OSAEObject OSAEobj = OSAEObjectManager.GetObjectByName(objName);

            Log.Debug("OSAE Security is Decoding an AuthKey");
            Boolean auth             = false;
            int     uTrustLevel      = 0;
            string  decryptedAuthKey = DecryptString(authkey);

            string[] user = decryptedAuthKey.Split(':');
            try
            {
                OSAEObject ouser = OSAE.OSAEObjectManager.GetObjectByName(user[0]);
                Log.Debug("Found User: "******"Password").Value)
                {
                    DateTime authDate = new DateTime();
                    try
                    {
                        authDate = Convert.ToDateTime(user[1].Substring(0, 4) + "-" + user[1].Substring(4, 2) + "-" + user[1].Substring(6, 2) + " " + user[1].Substring(8, 2) + ":" + user[1].Substring(10, 2) + ":" + user[1].Substring(12, 2));
                    }
                    catch
                    {
                        Log.Debug("Could not resolve TimeStamp: FAILED");
                        return(auth);
                    }
                    Log.Debug("Auth Date and Time: " + authDate.ToString());
                    //DateTime nowAuth = DateTime.Now;
                    DateTime sDate = DateTime.Now.AddSeconds(timeSplit - (timeSplit * 2));
                    DateTime eDate = DateTime.Now.AddSeconds(timeSplit);
                    if (authDate > sDate && authDate < eDate)
                    {
                        uTrustLevel = Convert.ToInt32(OSAEObjectPropertyManager.GetObjectPropertyValue(ouser.Name, "Trust Level").Value);
                        if (uTrustLevel > OSAEobj.MinTrustLevel)
                        {
                            Log.Debug("Authintication: PASSED");
                            auth = true;
                        }
                        else
                        {
                            Log.Debug("Trust Level too low. Access denied!");
                        }
                    }
                    else
                    {
                        Log.Debug("Authintication: FAILED");
                    }
                }
                else
                {
                    Log.Debug("Authintication: FAILED");
                }
            }
            catch
            {
                Log.Debug("Authintication: FAILED");
            }
            return(auth);
        }
Пример #6
0
 public void SetProperty(string prop, string value, string source)
 {
     OSAEObjectPropertyManager.ObjectPropertySet(Name, prop, value, source);
     foreach (OSAEObjectProperty p in Properties)
     {
         if (p.Name == prop)
         {
             p.Value = value;
         }
     }
 }
Пример #7
0
 public static Logging GetLogger()
 {
     lock (memoryLock)
     {
         if (privateInstance == null)
         {
             privateInstance = new Logging("Default");
         }
     }
     debug = OSAEObjectPropertyManager.GetObjectPropertyValue("SYSTEM", "Debug").Value;
     return(privateInstance);
 }
Пример #8
0
        /// <summary>
        /// Decrypts an OSAE Security encrypted Authentication string.
        /// Used to verify User to use in POST request.
        /// </summary>
        /// <param name="authkey">An OSAE Security encrypted String</param>
        /// <returns>Returns the Username, if criteria matches</returns>
        public static string DecryptUser(string authkey)
        {
            Log.Info("OSAE Security is Decoding an AuthKey");
            string uName            = null;
            string decryptedAuthKey = DecryptString(authkey);

            string[] user = decryptedAuthKey.Split(':');
            try
            {
                OSAEObject ouser = OSAE.OSAEObjectManager.GetObjectByName(user[0]);
                Log.Debug("Found User: "******"Password").Value)
                {
                    DateTime authDate = new DateTime();
                    try
                    {
                        authDate = Convert.ToDateTime(user[1].Substring(0, 4) + "-" + user[1].Substring(4, 2) + "-" + user[1].Substring(6, 2) + " " + user[1].Substring(8, 2) + ":" + user[1].Substring(10, 2) + ":" + user[1].Substring(12, 2));
                    }
                    catch
                    {
                        Log.Debug("Could not resolve TimeStamp: FAILED");
                        return(uName);
                    }
                    Log.Debug("Auth Date and Time: " + authDate.ToString());
                    //DateTime nowAuth = DateTime.Now;
                    DateTime sDate = DateTime.Now.AddSeconds(timeSplit - (timeSplit * 2));
                    DateTime eDate = DateTime.Now.AddSeconds(timeSplit);
                    if (authDate > sDate && authDate < eDate)
                    {
                        Log.Debug("Authintication: PASSED");
                        uName = ouser.Name;
                    }
                    else
                    {
                        Log.Debug("Authintication: FAILED");
                    }
                }
                else
                {
                    Log.Debug("Authintication: FAILED");
                }
            }
            catch
            {
                Log.Debug("Invalid Authintication key: FAILED");
            }
            return(uName);
        }
Пример #9
0
        /// <summary>
        /// Returns a Dataset of all objects in a specified container
        /// </summary>
        /// <param name="ContainerName"></param>
        /// <returns></returns>
        public static OSAEObjectCollection GetObjectsByContainer(string ContainerName)
        {
            MySqlCommand         command = new MySqlCommand();
            DataSet              dataset = new DataSet();
            OSAEObject           obj     = new OSAEObject();
            OSAEObjectCollection objects = new OSAEObjectCollection();

            try
            {
                if (ContainerName == string.Empty)
                {
                    command.CommandText = "SELECT object_name, object_alias, object_description, object_type, address, container_name, enabled, state_name, base_type, coalesce(time_in_state, 0) as time_in_state, last_updated FROM osae_v_object WHERE container_name is null ORDER BY object_name ASC";
                }
                else
                {
                    command.CommandText = "SELECT object_name, object_alias, object_description, object_type, address, container_name, enabled, state_name, base_type, coalesce(time_in_state, 0) as time_in_state, last_updated FROM osae_v_object WHERE container_name=@ContainerName AND enabled = 1 ORDER BY object_name ASC";
                    command.Parameters.AddWithValue("@ContainerName", ContainerName);
                }

                dataset = OSAESql.RunQuery(command);
                if (dataset.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in dataset.Tables[0].Rows)
                    {
                        obj                   = new OSAEObject(dr["object_name"].ToString(), dr["object_alias"].ToString(), dr["object_description"].ToString(), dr["object_type"].ToString(), dr["address"].ToString(), dr["container_name"].ToString(), int.Parse(dr["enabled"].ToString()));
                        obj.State.Value       = dr["state_name"].ToString();
                        obj.State.TimeInState = Convert.ToInt64(dr["time_in_state"]);
                        obj.BaseType          = dr["base_type"].ToString();
                        obj.LastUpd           = dr["last_updated"].ToString();
                        obj.Properties        = OSAEObjectPropertyManager.GetObjectProperties(obj.Name);
                        obj.Methods           = GetObjectMethods(obj.Name);
                        objects.Add(obj);
                    }

                    return(objects);
                }

                return(objects);
            }
            catch (Exception ex)
            {
                Logging.GetLogger().AddToLog("API - GetObjectsByContainer error: " + ex.Message, true);
                return(objects);
            }
        }
Пример #10
0
        public static void AddToLog(string audit, bool alwaysLog, string logFile)
        {
            try
            {
                if (debug != "FALSE" || alwaysLog)
                {
                    lock (logLocker)
                    {
                        string             filePath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\OSAE\Logs\" + logFile + ".log";
                        System.IO.FileInfo file     = new System.IO.FileInfo(filePath);
                        file.Directory.Create();
                        StreamWriter sw = File.AppendText(filePath);

                        sw.WriteLine(System.DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt") + " - " + audit);
                        sw.Close();

                        if (OSAEObjectPropertyManager.GetObjectPropertyValue("SYSTEM", "Prune Logs").Value == "TRUE")
                        {
                            if (file.Length > 1000000)
                            {
                                file.Delete();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                lock (logLocker)
                {
                    string             filePath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "/Logs/" + logFile + ".log";
                    System.IO.FileInfo file     = new System.IO.FileInfo(filePath);
                    file.Directory.Create();
                    StreamWriter sw = File.AppendText(filePath);
                    sw.WriteLine(System.DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt") + " - LOGGING ERROR: "
                                 + ex.Message + " - " + ex.InnerException);
                    sw.Close();
                    if (file.Length > 1000000)
                    {
                        file.Delete();
                    }
                }
            }
        }
Пример #11
0
 public static void InitialiseLogFolder()
 {
     try
     {
         FileInfo file = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\OSAE\Logs\");
         file.Directory.Create();
         if (OSAEObjectPropertyManager.GetObjectPropertyValue("SYSTEM", "Prune Logs").Value == "TRUE")
         {
             string[] files = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\OSAE\Logs\");
             foreach (string f in files)
             {
                 File.Delete(f);
             }
         }
     }
     catch (Exception ex)
     {
         Logging.GetLogger().AddToLog("Error getting registry settings and/or deleting logs: " + ex.Message, true);
     }
 }
        public static OSAEObjectCollection GetObjectsByOwner(string ObjectOwner)
        {
            OSAEObjectCollection objects = new OSAEObjectCollection();

            try
            {
                using (MySqlCommand command = new MySqlCommand())
                {
                    DataSet    dataset = new DataSet();
                    OSAEObject obj     = new OSAEObject();

                    command.CommandText = "SELECT object_name, object_description, object_type, address, container_name, enabled, state_name, base_type, coalesce(time_in_state, 0) as time_in_state FROM osae_v_object WHERE owned_by=@ObjectOwner";
                    command.Parameters.AddWithValue("@ObjectOwner", ObjectOwner);
                    dataset = OSAESql.RunQuery(command);

                    if (dataset.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow dr in dataset.Tables[0].Rows)
                        {
                            obj                   = new OSAEObject(dr["object_name"].ToString(), dr["object_description"].ToString(), dr["object_type"].ToString(), dr["address"].ToString(), dr["container_name"].ToString(), int.Parse(dr["enabled"].ToString()));
                            obj.State.Value       = dr["state_name"].ToString();
                            obj.State.TimeInState = Convert.ToInt64(dr["time_in_state"]);
                            obj.BaseType          = dr["base_type"].ToString();

                            obj.Properties = OSAEObjectPropertyManager.GetObjectProperties(obj.Name);
                            obj.Methods    = GetObjectMethods(obj.Name);
                            objects.Add(obj);
                        }

                        return(objects);
                    }
                }

                return(objects);
            }
            catch (Exception ex)
            {
                Logging.GetLogger().AddToLog("API - GetObjectsByBaseType error: " + ex.Message, true);
                return(objects);
            }
        }
        /// <summary>
        /// Returns an OSAEObject with the specified property
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static OSAEObjectCollection GetObjectsByPropertyValue(string property, string value)
        {
            MySqlCommand         command = new MySqlCommand();
            DataSet              dataset = new DataSet();
            OSAEObject           obj     = new OSAEObject();
            OSAEObjectCollection objects = new OSAEObjectCollection();

            try
            {
                command.CommandText = "SELECT object_name, object_alias, object_description, object_type, address, container_name, min_trust_level, enabled, state_name, base_type, coalesce(time_in_state, 0) as time_in_state, last_updated FROM osae_v_object_property WHERE property_name=@Property AND UPPER(property_value) = UPPER(@Value)";
                command.Parameters.AddWithValue("@Property", property);
                command.Parameters.AddWithValue("@Value", value);
                dataset = OSAESql.RunQuery(command);

                if (dataset.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in dataset.Tables[0].Rows)
                    {
                        obj                   = new OSAEObject(dataset.Tables[0].Rows[0]["object_name"].ToString(), dataset.Tables[0].Rows[0]["object_alias"].ToString(), dataset.Tables[0].Rows[0]["object_description"].ToString(), dataset.Tables[0].Rows[0]["object_type"].ToString(), dataset.Tables[0].Rows[0]["address"].ToString(), dataset.Tables[0].Rows[0]["container_name"].ToString(), Convert.ToUInt16(dataset.Tables[0].Rows[0]["min_trust_level"].ToString()), (dataset.Tables[0].Rows[0]["enabled"].ToString() != "0"));
                        obj.State.Value       = dataset.Tables[0].Rows[0]["state_name"].ToString();
                        obj.State.TimeInState = Convert.ToInt64(dataset.Tables[0].Rows[0]["time_in_state"]);
                        obj.BaseType          = dataset.Tables[0].Rows[0]["base_type"].ToString();
                        obj.LastUpd           = dataset.Tables[0].Rows[0]["last_updated"].ToString();
                        obj.Properties        = OSAEObjectPropertyManager.GetObjectProperties(obj.Name);
                        obj.Methods           = GetObjectMethods(obj.Name);
                        objects.Add(obj);
                    }
                    return(objects);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                Logging.GetLogger().AddToLog("API - GetObjectByPropertyValue (" + property + ") error: " + ex.Message, true);
                return(null);
            }
        }
        /// <summary>
        /// Returns an OSAEObject with the specified address
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        public static OSAEObject GetObjectByAddress(string address)
        {
            OSAEObject obj = null;

            try
            {
                using (MySqlCommand command = new MySqlCommand())
                {
                    DataSet dataset = new DataSet();

                    command.CommandText = "SELECT object_name, object_alias, object_description, object_type, address, container_name, min_trust_level, enabled, state_name, base_type, coalesce(time_in_state, 0) as time_in_state, last_updated FROM osae_v_object WHERE address=@Address";
                    command.Parameters.AddWithValue("@Address", address);
                    dataset = OSAESql.RunQuery(command);

                    if (dataset.Tables[0].Rows.Count > 0)
                    {
                        obj                   = new OSAEObject(dataset.Tables[0].Rows[0]["object_name"].ToString(), dataset.Tables[0].Rows[0]["object_alias"].ToString(), dataset.Tables[0].Rows[0]["object_description"].ToString(), dataset.Tables[0].Rows[0]["object_type"].ToString(), dataset.Tables[0].Rows[0]["address"].ToString(), dataset.Tables[0].Rows[0]["container_name"].ToString(), Convert.ToInt16(dataset.Tables[0].Rows[0]["min_trust_level"].ToString()), (dataset.Tables[0].Rows[0]["enabled"].ToString() != "0"));
                        obj.State.Value       = dataset.Tables[0].Rows[0]["state_name"].ToString();
                        obj.State.TimeInState = Convert.ToInt64(dataset.Tables[0].Rows[0]["time_in_state"]);
                        obj.BaseType          = dataset.Tables[0].Rows[0]["base_type"].ToString();
                        obj.LastUpd           = dataset.Tables[0].Rows[0]["last_updated"].ToString();

                        obj.Properties = OSAEObjectPropertyManager.GetObjectProperties(obj.Name);
                        obj.Methods    = GetObjectMethods(obj.Name);
                        return(obj);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.GetLogger().AddToLog("API - GetObjectByAddress (" + address + ")error: " + ex.Message, true);
                return(obj);
            }
        }
Пример #15
0
        public static void CheckComputerObject(string sourceName)
        {
            Logging.GetLogger().AddToLog("Checking for Computer object", true);
            string computerIp = Common.GetComputerIP();

            if (OSAEObjectManager.GetObjectByName(Common.ComputerName) == null)
            {
                OSAEObject obj = OSAEObjectManager.GetObjectByAddress(computerIp);
                if (obj == null)
                {
                    Logging.GetLogger().AddToLog("Computer Object not found, creating it...", true);
                    OSAEObjectManager.ObjectAdd(Common.ComputerName, "", Common.ComputerName, "COMPUTER", computerIp, "", 30, true);
                    OSAEObjectPropertyManager.ObjectPropertySet(Common.ComputerName, "Host Name", Common.ComputerName, sourceName);
                    Logging.GetLogger().AddToLog("Computer Object created called: " + Common.ComputerName, true);
                }
                else if (obj.Type == "COMPUTER")
                {
                    Logging.GetLogger().AddToLog("Computer Object found under a different name, updating it...", true);
                    OSAEObjectManager.ObjectUpdate(obj.Name, Common.ComputerName, obj.Alias, obj.Description, "COMPUTER", computerIp, obj.Container, obj.MinTrustLevel, obj.Enabled);
                    OSAEObjectPropertyManager.ObjectPropertySet(Common.ComputerName, "Host Name", Common.ComputerName, sourceName);
                }
                else
                {
                    Logging.GetLogger().AddToLog("Computer Object found under a different Name and Object Type, updating it...", true);
                    OSAEObjectManager.ObjectAdd(Common.ComputerName, "", Common.ComputerName, "COMPUTER", computerIp, string.Empty, obj.MinTrustLevel, true);
                    OSAEObjectPropertyManager.ObjectPropertySet(Common.ComputerName + "." + computerIp, "Host Name", Common.ComputerName, sourceName);
                }
            }
            else
            {
                Logging.GetLogger().AddToLog("Computer Object found, updating it...", true);
                OSAEObject obj = OSAEObjectManager.GetObjectByName(Common.ComputerName);
                OSAEObjectManager.ObjectUpdate(obj.Name, obj.Name, obj.Alias, obj.Description, "COMPUTER", computerIp, obj.Container, obj.MinTrustLevel, obj.Enabled);
                OSAEObjectPropertyManager.ObjectPropertySet(obj.Name, "Host Name", Common.ComputerName, sourceName);
            }
        }
Пример #16
0
        public static string MatchPattern(string str)
        {
            string ScriptParameter = "";

            try
            {
                str = str.TrimEnd('?', '.', '!');
                str = str.Replace(" 'S", "'S");
                str = str.Replace(" 's", "'s");
                DataSet dataset = new DataSet();
                //command.CommandText = "SELECT pattern FROM osae_v_pattern WHERE `match`=@Name";
                //command.Parameters.AddWithValue("@Name", str);
                dataset = OSAESql.RunSQL("SELECT pattern FROM osae_v_pattern_match WHERE `match`='" + str.Replace("'", "''") + "'");

                if (dataset.Tables[0].Rows.Count > 0)
                {
                    //Since we have a match, lets execute the scripts
                    OSAEScriptManager.RunPatternScript(dataset.Tables[0].Rows[0]["pattern"].ToString(), "", "Jabber");
                    return(dataset.Tables[0].Rows[0]["pattern"].ToString());
                }
                else
                {
                    //Replace Words with place holders and retry the pattern match
                    //example  "Please turn the main light on" becomes "Please turn the [OBJECT] [STATE]"

                    //Step 1: Break the Input into an Array to Query the Words for DB matches
                    str = str.ToUpper();

                    string[] words = str.Split(' ');

                    DataSet dsObjects = new DataSet();
                    foreach (String word in words)
                    {
                        dsObjects = OSAE.Common.ObjectNamesStartingWith(word.Replace("'S", ""));
                        foreach (DataRow dr in dsObjects.Tables[0].Rows)
                        {
                            if (str.IndexOf(dr["object_name"].ToString().ToUpper()) > -1)
                            //return "Found " + dr["object_name"].ToString();
                            {
                                str = str.Replace(dr["object_name"].ToString().ToUpper(), "[OBJECT]");
                                if (ScriptParameter.Length > 1)
                                {
                                    ScriptParameter = ScriptParameter + ",";
                                }
                                ScriptParameter += dr["object_name"].ToString();
                                //Determine if the Object is Possessive, which would be followed by a Property
                                if (str.ToUpper().IndexOf("[OBJECT]'S") > -1)
                                {
                                    //Here We have found our Possessive Object, so we need to look for an appropriate property afterwards
                                    //So we are going to retrieve a property list and compare it to the start of theremainder of the string

                                    DataSet dsProperties = new DataSet();
                                    dsProperties = OSAEObjectPropertyManager.ObjectPropertyListGet(dr["object_name"].ToString());
                                    foreach (DataRow drProperty in dsProperties.Tables[0].Rows)
                                    {
                                        //Here we need to break the string into words to avoid partial matches
                                        int    objectStartLoc = str.ToUpper().IndexOf("[OBJECT]'S");
                                        string strNewSearch   = str.Substring(objectStartLoc + 11);
                                        if (strNewSearch.ToUpper().IndexOf(drProperty["property_name"].ToString().ToUpper()) > -1)
                                        {
                                            str = str.Replace("[OBJECT]'S " + drProperty["property_name"].ToString().ToUpper(), "[OBJECT]'S [PROPERTY]");
                                            //str = str.Replace(drState["state_label"].ToString().ToUpper(), "[STATE]");
                                            ScriptParameter += "," + drProperty["property_name"].ToString();
                                        }
                                    }
                                }

                                //Here We have found our Object, so we need to look for an appropriate state afterwards
                                //So we are going to retrieve a state list and compare it to the remainder of the string
                                DataSet dsStates = new DataSet();
                                dsStates = OSAEObjectStateManager.ObjectStateListGet(dr["object_name"].ToString());
                                foreach (DataRow drState in dsStates.Tables[0].Rows)
                                {
                                    //Here we need to break the string into words to avoid partial matches
                                    string   replacementString = "";
                                    string[] wordArray         = str.Split(new Char[] { ' ' });
                                    foreach (string w in wordArray)
                                    {
                                        if (replacementString.Length > 1)
                                        {
                                            replacementString = replacementString + " ";
                                        }
                                        if (drState["state_label"].ToString().ToUpper() == w || drState["state_name"].ToString().ToUpper() == w)
                                        {
                                            replacementString = replacementString + "[STATE]";
                                            //str = str.Replace(drState["state_label"].ToString().ToUpper(), "[STATE]");
                                            ScriptParameter += "," + drState["state_name"].ToString();
                                        }
                                        else
                                        {
                                            replacementString = replacementString + w;
                                        }
                                    }
                                    //Now that we have replaced the Object and State, Lets check for a match again
                                    //DataSet dataset = new DataSet();
                                    //command.CommandText = "SELECT pattern FROM osae_v_pattern WHERE `match`=@Name";
                                    //command.Parameters.AddWithValue("@Name", str);
                                    //dataset = OSAESql.RunQuery(command);
                                    replacementString = replacementString.Replace(" 'S", "'S");
                                    dataset           = OSAESql.RunSQL("SELECT pattern FROM osae_v_pattern_match WHERE `match`='" + replacementString.Replace("'", "''") + "'");
                                    if (dataset.Tables[0].Rows.Count > 0)
                                    {
                                        //return dataset.Tables[0].Rows[0]["pattern"].ToString();
                                        //Since we have a match, lets execute the scripts
                                        OSAEScriptManager.RunPatternScript(dataset.Tables[0].Rows[0]["pattern"].ToString(), ScriptParameter, "Jabber");
                                        return(dataset.Tables[0].Rows[0]["pattern"].ToString());
                                    }
                                    //break;
                                }
                                //break;
                            }
                        }
                    }
                    return(string.Empty);
                }
            }
            catch (Exception ex)
            {
                Logging.GetLogger().AddToLog("API - MatchPattern error: " + ex.Message, true);
                return(string.Empty);
            }
        }
Пример #17
0
 private static void GetConfiguration()
 {
     debug     = OSAEObjectPropertyManager.GetObjectPropertyValue("SYSTEM", "Debug").Value;
     pruneLogs = OSAEObjectPropertyManager.GetObjectPropertyValue("SYSTEM", "Prune Logs").Value;
 }