Example #1
0
        public void Verify(String[] _devices, String[] _hosts, int[] _ports)
        {
            try
            {
                // This has to be started to parse the devices.xml file

                String[] xmldevices = data.getDevices();
                shdrobjs = new ShdrObj[_devices.Length];
                for (int i = 0; i < _devices.Length; i++)
                {
                    if (_hosts[i] == null || _hosts[i].Length < 1)
                    {
                        throw new AgentException("Illegal Host Name");
                    }
                    if (_devices[i] == null || _devices[i].Length < 1)
                    {
                        throw new AgentException("Illegal Device Name" + _devices[i]);
                    }
                    if (!FindString(xmldevices, _devices[i].Trim()))
                    {
                        throw new AgentException("Illegal Device Name" + _devices[i]);
                    }

                    ShdrObj shdrobj = new ShdrObj();
                    shdrobj.index    = i;
                    shdrobj.port     = _ports[i];
                    shdrobj.device   = _devices[i].Trim();
                    shdrobj.host     = _hosts[i].Trim();
                    shdrobj.running  = 0;
                    shdrobj.ns       = null;
                    shdrobj.sr       = null;
                    shdrobj.sw       = null;
                    shdrobj.attempts = 0;



                    shdrobj.entry = Dns.GetHostEntry(shdrobj.host);
                    shdrobj.addr  = shdrobj.entry.AddressList[0];



                    shdrobjs[i] = shdrobj;
                }
            }
            catch (AgentException e)
            {
                throw e;
            }
        }
Example #2
0
        public void Verify(String[] _devices, String[] _hosts, int[] _ports)
        {
            try
            {
                // This has to be started to parse the devices.xml file

                String[] xmldevices = data.getDevices();
                shdrobjs = new ShdrObj[_devices.Length];
                for (int i = 0; i < _devices.Length; i++)
                {

                    if (_hosts[i] == null || _hosts[i].Length < 1)
                        throw new AgentException("Illegal Host Name");
                    if (_devices[i] == null || _devices[i].Length < 1)
                        throw new AgentException("Illegal Device Name" + _devices[i]);
                    if (!FindString(xmldevices, _devices[i].Trim()))
                        throw new AgentException("Illegal Device Name" + _devices[i]);

                    ShdrObj shdrobj = new ShdrObj();
                    shdrobj.index = i;
                    shdrobj.port = _ports[i];
                    shdrobj.device = _devices[i].Trim();
                    shdrobj.host = _hosts[i].Trim();
                    shdrobj.running = 0;
                    shdrobj.ns = null;
                    shdrobj.sr = null;
                    shdrobj.sw = null;
                    shdrobj.attempts = 0;

                    shdrobj.entry = Dns.GetHostEntry(shdrobj.host);
                    shdrobj.addr = shdrobj.entry.AddressList[0];

                    shdrobjs[i] = shdrobj;

                }
            }
            catch (AgentException e)
            {
                throw e;
            }
        }
Example #3
0
        public void Cycle()
        {
            ShdrObj shdrobj = this;
            String  device  = this.device;
            String  line    = null;

            // shdrobj.messages = "";
            // this a reference
            try
            {
                if (shdrobj.attempts > 0)
                {
                    shdrobj.attempts--;
                    Thread.Sleep(100);
                    return;
                }

                if (shdrobj.ipep == null)
                {
                    shdrobj.ipep = new IPEndPoint(shdrobj.addr, shdrobj.port);
                }

                if (shdrobj.server == null)
                {
                    shdrobj.attempts = 30;

                    shdrobj.server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    //shdrobj.server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
                    shdrobj.server.Connect(shdrobj.ipep);
                    shdrobj.attempts = 0;
                }

                if (shdrobj.ns == null)
                {
                    shdrobj.ns = new NetworkStream(shdrobj.server);
                }
                if (shdrobj.sr == null)
                {
                    shdrobj.sr = new StreamReader(shdrobj.ns);
                }
                if (shdrobj.sw == null)
                {
                    shdrobj.sw = new StreamWriter(shdrobj.ns);
                }

                //The default value, System.Threading.Timeout.Infinite, specifies that the read operation does not time out
                shdrobj.ns.ReadTimeout = ReadTimeout; // System.Threading.Timeout.Infinite=-1
                shdrobj.running        = 1;

                if (!shdrobj.server.Connected)
                {
                    shdrobj.messages += "Server disconnected: for " + shdrobj.host + "\n";
                    throw new Exception("Server disconnected");
                }

                // Manually turn Power
                //String nowtimestamp = System.DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffffff", System.Globalization.CultureInfo.InvariantCulture);
                String nowtimestamp = System.DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffffff", System.Globalization.CultureInfo.InvariantCulture);
                agent.Data().StoreEvent(nowtimestamp, shdrobj.device, "power", "ON", "", "", null, null, null, null);

                while (true)
                {
                    AgentSHDR.LogMessage("Enter Readline" + shdrobj.host + "\n", 3);
                    line = shdrobj.sr.ReadLine();
                    AgentSHDR.LogMessage("Exit Readline" + shdrobj.host + "\n", 3);
                    nowtimestamp = System.DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffffff", System.Globalization.CultureInfo.InvariantCulture);


                    if (line == null)
                    {
                        throw new Exception(shdrobj.host + ": Readline failed\n");
                    }


                    String[] values    = line.Split('|');
                    String   timestamp = values[0];
                    String   item      = values[1];

                    // update heartbeat to confirm socket not zombie
                    agent.Data().StoreEvent(nowtimestamp, shdrobj.device, "heartbeat", nowtimestamp, "", "", null, null, null, null);

                    // Check if this is an Alarm. Alarms must be on one line.
                    if (agent.Data().isAlarm(item, device) && values.Length >= 7)
                    {
                        String code       = values[2];
                        String nativeCode = values[3];
                        String severity   = values[4];
                        String state      = values[5];
                        String text       = values[6];
                        //short StoreEvent(String timestamp, String deviceName, String name, String value, String partId, String workPieceId, String alarm_code, String alarm_severity, String alarm_nativecode, String alarm_state);
                        agent.Data().StoreEvent(timestamp, device, item, text, "", "", code, severity, nativeCode, state);
                    }
                    else
                    {
                        String value;
                        for (int i = 1; i < values.Length; i += 2)
                        {
                            item  = values[i];
                            value = values[i + 1];

                            try
                            {
                                if (agent.Data().isEvent(item, device))
                                {
                                    agent.Data().StoreEvent(timestamp, device, item, value, "", "", null, null, null, null);
                                }
                                else
                                {
                                    //if (item == "spindle_speed")
                                    //{
                                    //    sw.WriteLine("\n================================================================================");
                                    //    agent.Data().getCurrentDevice(null, "//Axes//DataItem[@type=\"SPINDLE_SPEED\"and@subType=\"ACTUAL\"]", rpmsw);
                                    //    sw.WriteLine("\n-----------------------------------------------");
                                    //    agent.Data().getStreamDevice(null,
                                    //        "//Axes//DataItem[@type=\"SPINDLE_SPEED\"and@subType=\"ACTUAL\"]",
                                    //        "0", "100", rpmsw);
                                    //    rpmsw.Flush();

                                    //    //System.Diagnostics.Debugger.Break();
                                    //}
                                    agent.Data().StoreSample(timestamp, device, item, value, "", "");
                                }
                            }
                            catch (AgentException e)
                            {
                                string msg = item + "|" + value + " is ignored. " + e.Message + "\n";
                                AgentSHDR.LogMessage(msg, 1);
                                shdrobj.messages += msg;
                            }
                        }
                        //short StoreEvent(String timestamp, String deviceName, String name, String value, String partId, String workPieceId, String alarm_code, String alarm_severity, String alarm_nativecode, String alarm_state);
                    }
                    // We need other threads to run
                    Thread.Sleep(50);
                }
                return;
            }


            catch (TimeoutException e)
            {
                string msg = "TimeoutException: " + e.Message + " for " + shdrobj.host + "\n";
                shdrobj.messages += msg;
                AgentSHDR.LogMessage(msg, -1);
                return;
            }

            catch (ThreadAbortException e)
            {
                string msg = "ThreadAbortException: " + e.Message + " for " + shdrobj.host + "\n";
                shdrobj.messages += msg;
                AgentSHDR.LogMessage(msg, -1);
                throw e;
            }
            catch (SocketException e)
            {
                string msg = "SocketException: " + e.Message + " for " + shdrobj.host + "\n";
                shdrobj.messages += msg;
                AgentSHDR.LogMessage(msg, 1);
            }
            catch (IOException e)
            {
                string msg = "IOException: " + e.Message + " for " + shdrobj.host + "\n";
                shdrobj.messages += msg;
                AgentSHDR.LogMessage(msg, 1);
            }
            catch (Exception e)
            {
                string msg = "Exception: " + e.Message + " for " + shdrobj.host + "\n";
                shdrobj.messages += msg;
                AgentSHDR.LogMessage(msg, 1);
            }

            /////////// agent stopped or exception incurred
            try
            {
                if (shdrobj.server != null && shdrobj.server.Connected)
                {
                    shdrobj.server.Shutdown(SocketShutdown.Both);
                }

                if (shdrobj.server != null)
                {
                    shdrobj.server.Close();
                }

                if (shdrobj.server.Connected)
                {
                    AgentSHDR.LogMessage("Winsock shutdown close error: " + Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()), 1);
                }


                shdrobj.server = null;

                if (shdrobj.sr != null)
                {
                    shdrobj.sr.Close();
                }
                if (shdrobj.sw != null)
                {
                    shdrobj.sw.Close();
                }
                if (shdrobj.ns != null)
                {
                    shdrobj.ns.Close();
                }
                shdrobj.sr = null;
                shdrobj.sw = null;
                shdrobj.ns = null;

                shdrobj.running = 0;
                string timestamp = System.DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffffff", System.Globalization.CultureInfo.InvariantCulture);

                agent.Data().ClearDataBuffer(shdrobj.device);
                agent.Data().StoreEvent(timestamp, shdrobj.device, "power", "OFF", "", "", null, null, null, null);
            }
            catch (Exception e)
            {
                string msg = "Exception: " + e.Message + " exception  while resetting MTC Agent" + shdrobj.host + "SHDR socket connection\n";
                shdrobj.messages += msg;
                AgentSHDR.LogMessage(msg, 1);
            }
        }
Example #4
0
        public void Cycle(int k)
        {
            ShdrObj shdrobj = shdrobjs[k];
            String  device  = shdrobjs[k].device;
            String  line    = null;

            // shdrobj.messages = "";
            // this a reference
            try
            {
                if (shdrobj.attempts > 0)
                {
                    shdrobj.attempts--;
                    Thread.Sleep(100);
                    return;
                }

                //if (!TestSHDRConnection(shdrobj.host))
                //{

                //    messages += "Ping failed for " + shdrobj.host + "\n";
                //    throw new Exception("Ping failed for " + shdrobj.host);

                //}

                if (shdrobj.ipep == null)
                {
                    shdrobj.ipep = new IPEndPoint(shdrobj.addr, shdrobj.port);
                }

                //bool bConnected = TestSocketConnection(ipep,  port, 100);
                //if (!bConnected)
                if (shdrobj.server == null)
                {
                    shdrobj.attempts = 30;

                    shdrobj.server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    shdrobj.server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
                    shdrobj.server.Connect(shdrobj.ipep);
                    shdrobj.attempts = 0;
                }

                if (shdrobj.ns == null)
                {
                    shdrobj.ns = new NetworkStream(shdrobj.server);
                }
                if (shdrobj.sr == null)
                {
                    shdrobj.sr = new StreamReader(shdrobj.ns);
                }
                if (shdrobj.sw == null)
                {
                    shdrobj.sw = new StreamWriter(shdrobj.ns);
                }

                shdrobj.ns.ReadTimeout = 200;
                shdrobj.running        = 1;

                if (!shdrobj.ns.DataAvailable)
                {
                    shdrobj.messages += "No data: for " + shdrobj.host + "\n";
                    return;
                }

                try
                {
                    line = shdrobj.sr.ReadLine();
                }
                catch (TimeoutException e)
                {
                    shdrobj.messages += "TimeoutException: " + e.Message + " for " + shdrobj.host + "\n";
                    return;
                }


                String[] values    = line.Split('|');
                String   timestamp = values[0];
                String   item      = values[1];

                // Check if this is an Alarm. Alarms must be on one line.
                if (data.isAlarm(item, device) && values.Length >= 7)
                {
                    String code       = values[2];
                    String nativeCode = values[3];
                    String severity   = values[4];
                    String state      = values[5];
                    String text       = values[6];
                    //short StoreEvent(String timestamp, String deviceName, String name, String value, String partId, String workPieceId, String alarm_code, String alarm_severity, String alarm_nativecode, String alarm_state);
                    data.StoreEvent(timestamp, device, item, text, "", "", code, severity, nativeCode, state);
                }
                else
                {
                    String value;
                    for (int i = 1; i < values.Length; i += 2)
                    {
                        item  = values[i];
                        value = values[i + 1];

                        try
                        {
                            if (data.isEvent(item, device))
                            {
                                data.StoreEvent(timestamp, device, item, value, "", "", null, null, null, null);
                            }
                            else
                            {
                                data.StoreSample(timestamp, device, item, value, "", "");
                            }
                        }
                        catch (AgentException e)
                        {
                            shdrobj.messages += item + "|" + value + " is ignored. " + e.Message + "\n";
                        }
                    }
                    //short StoreEvent(String timestamp, String deviceName, String name, String value, String partId, String workPieceId, String alarm_code, String alarm_severity, String alarm_nativecode, String alarm_state);
                }
                return;
            }



            catch (ThreadAbortException e)
            {
                shdrobj.messages += "ThreadAbortException: " + e.Message + " for " + shdrobj.host + "\n";
            }
            catch (SocketException e)
            {
                shdrobj.messages += "SocketException: " + e.Message + " for " + shdrobj.host + "\n";
            }
            catch (IOException e)
            {
                shdrobj.messages += "IOException: " + e.Message + " for " + shdrobj.host + "\n";
            }
            catch (Exception e)
            {
                shdrobj.messages += "Exception: " + e.Message + " for " + shdrobj.host + "\n";
            }

            /////////// agent stopped or exception incurred
            {
                if (shdrobj.server != null && shdrobj.server.Connected)
                {
                    shdrobj.server.Shutdown(SocketShutdown.Both);
                    shdrobj.server.Close();
                }
                shdrobj.server = null;

                if (shdrobj.sr != null)
                {
                    shdrobj.sr.Close();
                }
                if (shdrobj.sw != null)
                {
                    shdrobj.sw.Close();
                }
                if (shdrobj.ns != null)
                {
                    shdrobj.ns.Close();
                }
                shdrobj.sr = null;
                shdrobj.sw = null;
                shdrobj.ns = null;

                shdrobj.running = 0;
            }
        }