using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace MisDataName
{
    public  delegate void TestPlcDelegate();
    public  class TestPlcConnect
    {
        public static Hashtable WorkStationStatus_Hash = new Hashtable();
        public static Hashtable WorkStationTestExample = new Hashtable();

        string WorkStationStr = "";

        public EventHandler PlcBreak,PlcConnect;

        bool PlcStatus=false ;

        static  int testTime = 10;
        int TestInterval = testTime;

        string OpName;

      
        public TestPlcConnect(string opName)
        {
            OpName = opName;
        }
  
        public void ExternalSignalChange()
        {
            TestInterval = testTime;
            if(!PlcStatus)
            {
                
                WorkStationStr=ReadWorkStationStr("OnlineWorkStation");
                HashTable_Write(WorkStationStatus_Hash,"OnlineWorkStation", String_Combine(WorkStationStr, OpName));
                WorkStationStr = ReadWorkStationStr("DownlineWorkStation");
                HashTable_Write(WorkStationStatus_Hash, "DownlineWorkStation", String_Spl(WorkStationStr, OpName));
                
                if (PlcConnect != null)
                {
                    PlcConnect(OpName, null);
                }


                PlcStatus = true;
            }


        }

        public void ExternalTimerExcute()
        {
            if (TestInterval>0)
            {
                TestInterval--;
            }
            else
            {
                
                if(PlcStatus)
                {
                    
                    WorkStationStr = ReadWorkStationStr("DownlineWorkStation");
                    HashTable_Write(WorkStationStatus_Hash, "DownlineWorkStation", String_Combine(WorkStationStr, OpName));
                    WorkStationStr = ReadWorkStationStr("OnlineWorkStation");
                    HashTable_Write(WorkStationStatus_Hash, "OnlineWorkStation", String_Spl(WorkStationStr, OpName));

                    if (PlcBreak != null)
                    {
                        PlcBreak(OpName, null);
                    }

                    PlcStatus = false;
                }
                
                
            }
            
        }


        public static void HashTable_Write(Hashtable hashtable, string Key,object obj)
        {
            if (hashtable.ContainsKey (Key))
            {
                hashtable[Key] = obj;
            }
            else
            {
                hashtable.Add(Key, obj);
            }


        }

        public  static  object HashTable_Read(Hashtable hashtable, string Key)
        {
            if (hashtable.ContainsKey(Key))
            {
                return hashtable[Key];
            }
            else
            {
                return null;
            }
        }

        string  ReadWorkStationStr(string  Key)
        {
           if ( HashTable_Read(WorkStationStatus_Hash, Key) !=null )
            {
                return HashTable_Read(WorkStationStatus_Hash, Key).ToString();

            }
            return "";
        }


        string  String_Combine(string MainStr,string AddStr)
        {
            return MainStr+ AddStr + "|";

        }
        string String_Spl(string MainStr, string DelStr)
        {
            return MainStr.Replace(DelStr + "|", "");
        }


        
        public static  int CalculateWorkStationCount(string Key)
        {
            string wksta="";
            if(WorkStationStatus_Hash.ContainsKey (Key))
            {
                wksta = WorkStationStatus_Hash[Key].ToString();
            }
            string[] wksta_SplitArray = wksta.Split('|');

            return wksta_SplitArray.Length-1;

        }




        public static void HeartBeatChange(string OpName, int TagTypeID)
        {
            if (TagTypeID == 14)
            {
                Thread t = new Thread(new ParameterizedThreadStart(HeartBeatChange_Thread));
                t.Start(OpName);
            }


        }

        static void HeartBeatChange_Thread(object obj)
        {
            string OpName = (string)obj;
            if (TestPlcConnect.HashTable_Read(TestPlcConnect.WorkStationTestExample, OpName) != null)
            {
                TestPlcConnect PlcConnectExample = (TestPlcConnect)TestPlcConnect.HashTable_Read(TestPlcConnect.WorkStationTestExample, OpName);
                PlcConnectExample.ExternalSignalChange();
            }

        }

    }
}
