Net app to change camera function

If you've made a patch to quick fix a bug or to add a new feature not yet in the main tree then post it here so others can try it out.
Post Reply
BobS0327
Posts: 86
Joined: Mon Oct 16, 2006 6:22 pm

Net app to change camera function

Post by BobS0327 »

Below is the Net c# source code to change the function of a specific camera. Prerequisites for using the util are as follows:

1. Open up mySQL port 3306 on your Linux box to the outside world. That is, make mySQL on your Linux box accessible from a remote computer.
2. Make the root mySQL userid capable of logging into the Linux mySQL from a remote workstation.
3. Download and install the .Net mySQL data connector from http://dev.mysql.com/downloads/connector/

Finally here is the code. Note the comment at top of code indicating how to compile the code.

Code: Select all

// compile: csc /target:exe /reference:"C:\Progra~2\MySql\MySQLC~1.4\Assemb~1\v4.0\MySQLD~2.dll" Function.cs
using System;
using System.Text;
using MySql.Data.MySqlClient;
using System.IO;
using System.Xml;

namespace ConsoleApplication1
{
    class Program
    {
        private static string server;
        private static string database;
        private static string uid;
        private static string password;
		private static string monitorname;
        private static string function;
        private static bool bfunctionchanged = true;
		private static bool bmonitorfound = false;		
        static int Main(string[] args)
        {
            MySqlTransaction tr = null;
            string connectionString;
            MySqlConnection conn = null;
            String command = "SELECT * FROM Monitors";
            MySqlDataReader reader;
            
            string[] functions = new string[] { "None", "Monitor", "Modect", "Record", "Mocord", "Nodect"};

				if(args.Length != 2)
				{
				  Console.WriteLine("Program aborting, insufficient arguments on command line\n Example, how to use: {0} MonitorName Function", System.AppDomain.CurrentDomain.FriendlyName);
				  return -1;
				}
				else
				{
				 monitorname = args[0];
				 function = args[1];
				}

            string result = Array.Find(functions, delegate(string s) { return s == function; });
            if (result == null)
            {
                Console.WriteLine("Program aborting, Function {0} is not a valid function (Function names are case sensitive)", function);
                return 0;
            }
            if (ReadXMLFile(ref server, ref database, ref uid, ref password, "ZMMysql.xml") == false)
            {
                Console.WriteLine("Error, Program aborting!!!");
                return 1;
            }
            connectionString = "SERVER=" + server + ";" + "DATABASE=" +
                       database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

            MySqlConnection connection = new MySqlConnection(connectionString);
            MySqlCommand cmd = new MySqlCommand(command, connection);
            try
            {
                connection.Open();
                cmd.ExecuteNonQuery();
                reader = cmd.ExecuteReader();
                cmd.CommandType = System.Data.CommandType.Text;
              
                while (reader.Read() != false)
                {
                    if (monitorname == (string)reader["Name"])
                    {
                        Console.WriteLine("Found camera {0}", reader["Name"]);
						bmonitorfound = true;
                        if (function == (string)reader["Function"])
                        {
                            Console.WriteLine("Camera {0} is already set to function {1}, program terminating, nothing more to do", reader["Name"], reader["Function"]);
                            connection.Close();
                            return 0;
                        }
                    }                   
                }
            }
            catch (MySqlException MySqlError)
            {
               // Console.WriteLine(MySqlError.Message);
			   Console.WriteLine("MySqlError Message: {0}", MySqlError.Message.ToString());
            }
            connection.Close();
			   if(bmonitorfound == false)
			   {
			      Console.WriteLine("Camera {0} could not be found (be advised, names are case sensitive), program terminating", monitorname);
			       return -1;
			   }
            try
            {
                conn = new MySqlConnection(connectionString);
                conn.Open();
                tr = conn.BeginTransaction();
                MySqlCommand cmd1 = new MySqlCommand();
                cmd1.Connection = conn;
                cmd1.Transaction = tr;

				 string formatString = String.Format("UPDATE Monitors SET Function='{0}' WHERE Name='{1}'" , function,monitorname); 
               // cmd1.CommandText = "UPDATE Monitors SET Function='Record' WHERE Name='Front-Porch'";
               cmd1.CommandText = formatString;
			    cmd1.ExecuteNonQuery();
            }
            catch (MySqlException ex)
            {
                try
                {
                    tr.Rollback();
					bfunctionchanged = false;
                }
                catch (MySqlException ex1)
                {
                    Console.WriteLine("Error: {0}", ex1.ToString());
					bfunctionchanged = false;
                }
                Console.WriteLine("Error: {0}", ex.ToString());
				bfunctionchanged = false;
            }
			if(bfunctionchanged == true)
			Console.WriteLine("Camera: {0} successfully change to {1} function", monitorname, function);
			
            return 0;
        }

        static bool ReadXMLFile(ref string IPAddress, ref string Database, ref string Userid, ref string Password, string XMLFileName)
        {
            bool bReturn = true;
            FileStream READER = null;
            try
            {
                READER = new FileStream(XMLFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); //Set up the filestream (READER) //
                System.Xml.XmlDocument myZMParms = new System.Xml.XmlDocument();// Set up the XmlDocument (myZMParms) //
                myZMParms.Load(READER); //Load the data from the file into the XmlDocument (myZMParms) //
                System.Xml.XmlNodeList NodeList = myZMParms.GetElementsByTagName("ZMParms"); // Create a list of the nodes in the xml file //

                IPAddress = NodeList[0].FirstChild.ChildNodes[0].InnerText; // mySqlIPAddress //
                Database = NodeList[0].FirstChild.ChildNodes[1].InnerText; // database name //
                Userid = NodeList[0].FirstChild.ChildNodes[2].InnerText; // userid //  
                Password = NodeList[0].FirstChild.ChildNodes[3].InnerText; // password //  
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.ToString());
                bReturn = false;
            }
            finally
            {
                if (READER != null)
                {
                    READER.Close();
                    READER.Dispose();
                }
            }
            return bReturn;
        }
    }
}
The util uses a XML file for parameter inputs such as the ip address of your linux ZM box etc. The name should be ZMMysql.xml and it should look as follows and it should be in the same directory as the util.

Code: Select all

<?xml version="1.0"?>
      <ZMParms>
	  <ZM>
	    <mySqlIPAddress>000.000.000.000</mySqlIPAddress>
	    <databasename>zm</databasename>
	    <userid>root</userid>
		 <password>password</password>
	  </ZM>
	</ZMParms>	

Inputs to the util are the Camera name which is case sensitive and the function (which is also case sensitive) that the camera should be changed to

Example Front-Porch Record

This example will change the function of the Front-Porch camera to Record if it is not currently recording.
Post Reply