public bool Write <T>(string instruction, T value)
        {
            StartOPcServerIfStopped();
            bool      bOk       = false;
            OpcThread opcthread = new OpcThread(opcServer);

            try
            {
                opcthread.Request(new OpcRequest(Command.Write, instruction, value));
            }
            catch (Exception errMsg)
            {
                bOk = false;
            }
            finally
            {
            }
            return(bOk);
        }
        //public static OpcServer GetOPCServerConnection()
        //{


        //    if (opcServer == null) opcServer = new OpcServer();

        //    int rtc = 0;
        //    SERVERSTATUS objSERVERSTATUS = new SERVERSTATUS();
        //    bool isConnected = false;
        //    bool isServerRunning = true;

        //    lock (opcConLock)
        //    {

        //        //do
        //        //{
        //            try
        //            {
        //                isConnected = opcServer.isConnectedDA;
        //                if (isConnected)
        //                {
        //                    opcServer.GetStatus(out objSERVERSTATUS);
        //                    isServerRunning = objSERVERSTATUS.eServerState == OpcServerState.Running;

        //                }


        //                if (!isConnected || !isServerRunning)
        //                {
        //                    Connection dbpm = new Connection();
        //                    //opcMachineHost = GlobalValues.OPC_MACHINE_HOST;
        //                    //opcServerName = GlobalValues.OPC_SERVER_NAME;
        //                    //opcMachineHost = dbpm.GetConfigValue("HostServer", "", "Name");
        //                    //opcServerName = dbpm.GetConfigValue("OpcServerName", "", "Name");
        //                    opcMachineHost = ConfigurationManager.AppSettings["OPCSHostMachine"];
        //                    opcServerName = ConfigurationManager.AppSettings["OPCServer"];
        //                    //opcMachineHost = "NISAR";
        //                    //opcServerName = "Matrikon.OPC.Simulation.1";
        //                    rtc = opcServer.Connect(opcMachineHost, opcServerName);
        //                    //if (!isServerRunning && IsOPCServerIsRunning())
        //                    //    new InitializeEngine().AsynchReadSettings();

        //                }


        //            }
        //            catch (Exception errMsg)
        //            {
        //                Console.WriteLine("" + errMsg.Message);

        //            }
        //            finally { }

        //        //} while (opcServer.isConnectedDA == false);
        //    }



        //    return opcServer;



        //}
        //static bool IsOPCServerIsRunning()
        //{
        //    bool isRunning = false;
        //    SERVERSTATUS objSERVERSTATUS = new SERVERSTATUS();
        //    opcServer.GetStatus(out objSERVERSTATUS);
        //    isRunning = objSERVERSTATUS.eServerState == OpcServerState.Running;
        //    return isRunning;
        //}
        //public static T ReadTag<T>(string command)
        //{
        //    StartOPcServerIfStopped();

        //    T result = default(T);

        //    ItemProperties[] item = null;


        //    //get value of command.
        //    item = opcServer.GetProperties(new string[1] { command }, true, new int[] { 2 });

        //    bool bOk = false;

        //    bOk = item != null && item.Length > 0;
        //    bOk &= bOk &&
        //              item[0].Properties != null && item[0].Properties.Length > 0;

        //    if (item[0].Properties == null) return result;
        //    Property property = item[0].Properties[0];

        //    if (property.Value != null)
        //        result = (T)property.Value;

        //    return result;

        //}



        //public static void ReIssueCommand(string command)
        //{
        //    StartOPcServerIfStopped();
        //    SrvAccess.Request(new OpcRequest(Command.Write, command, (object)true));
        //}

        static void StartOPcServerIfStopped()
        {
            int rtc = 0;

            if (opcServer == null)
            {
                opcServer = new OpcServer();
            }

            if (SrvAccess == null)
            {
                SrvAccess = new OpcThread(opcServer);
            }

            if (opcServer.isConnectedDA == false)
            {
                rtc = opcServer.Connect("HPIServer", "Intellution.IntellutionGatewayOPCServer");
            }
        }
        bool PhotoWrite <T>(string instruction, T value)
        {
            bool bOk = false;
            // bOk = IsValidTag(instruction);
            OpcThread opcthread = new OpcThread(camOPCServer);

            try
            {
                opcthread.Request(new OpcRequest(Command.Write, instruction, value));
            }
            catch (Exception errMsg)
            {
                bOk = false;
            }
            finally
            {
                opcthread.Stop();
            }
            return(bOk);
        }
        void StartOPcServerIfStopped()
        {
            int rtc = 0;

            if (opcServer == null)
            {
                //   rptEngine.Notification("FROM GUI : TRY TO RECONNECT OPC SERVER 1");
                opcServer = new OpcServer();
            }

            if (SrvAccess == null)
            {
                //  rptEngine.Notification("FROM GUI : TRY TO RECONNECT OPC SERVER 2");
                SrvAccess = new OpcThread(opcServer);
            }

            if (opcServer.isConnectedDA == false)
            {
                rtc = opcServer.Connect("HPIServer", "Intellution.IntellutionGatewayOPCServer");
            }
        }
        public static bool StartOPCServer(out string errorMessage)
        {
            int rtc = 0;

            errorMessage = "";
            try
            {
                opcServer = new OpcServer();



                //opcMachineHost = ConfigurationManager.AppSettings["OPCSHostMachine"];
                //opcServerName = ConfigurationManager.AppSettings["OPCServer"];

                Connection dbpm = new Connection();
                //for main l2 server
                //opcMachineHost = dbpm.GetConfigValue("HostServer", "", "Name");
                //opcServerName = dbpm.GetConfigValue("OpcServerName", "", "Name");
                //opcMachineHost = "NISAR";
                //opcServerName = "Matrikon.OPC.Simulation.1";
                //for working machine
                opcMachineHost = ConfigurationManager.AppSettings["OPCSHostMachine"];
                opcServerName  = ConfigurationManager.AppSettings["OPCServer"];

                if (opcServer.isConnectedDA == false)
                {
                    rtc = opcServer.Connect(opcMachineHost, opcServerName);
                }

                SrvAccess = new OpcThread(opcServer);
            }
            catch (Exception errMsg)
            {
                errorMessage = errMsg.Message;
                throw errMsg;
            }
            return(HRESULTS.Succeeded(rtc));
        }
 public OPCServerDirector()
 {
     SrvAccess = OPCServerManagement.SrvAccess;
     opcServer = OPCServerManagement.opcServer;
 }