Interaction logic for DiagnosticWindow.xaml
Inheritance: System.Windows.Window, INotifyPropertyChanged
Exemplo n.º 1
0
        public bool IsHeartBeatAlive()
        {
            try
            {
                AdsStream       dataStream = new AdsStream(4);
                AdsBinaryReader binReader  = new AdsBinaryReader(dataStream);

                int iHandle = 0;
                int iValue  = 0;
                iHandle = _tcAds.CreateVariableHandle("TwinCAT_SystemInfoVarList._AppInfo.OnlineChangeCnt");

                _tcAds.Read(iHandle, dataStream);
                iValue = binReader.ReadInt32();
                dataStream.Position = 0;
                if (iValue != _onlineChangeCount)
                {
                    _onlineChangeCount = iValue;
                    isHierarchieLoaded = false;
                }
            }
            catch (Exception e)
            {
                DiagnosticWindow.AddToDebug(e.Message);
                return(false);
            }
            return(true);
        }
Exemplo n.º 2
0
 public MainWindow(WoopsaAdsController controller)
 {
     InitializeComponent();
     this.controller = controller;
     ConfigWoopsa_TabControl.DataContext = this;
     diagnostic  = new DiagnosticWindow();
     this.Width  = 0;
     this.Height = 0;
     WindowStyle = WindowStyle.None;
     diagnostic.InitWindow();
     MyNotifyIcon.Visibility = Visibility.Visible;
 }
Exemplo n.º 3
0
 public MainWindow(WoopsaAdsController controller)
 {
     InitializeComponent();
     this.controller = controller;
     ConfigWoopsa_TabControl.DataContext = this;
     diagnostic = new DiagnosticWindow();
     this.Width = 0;
     this.Height = 0;
     WindowStyle = WindowStyle.None;
     diagnostic.InitWindow();
     MyNotifyIcon.Visibility = Visibility.Visible;
 }
Exemplo n.º 4
0
        public void Load()
        {
            _rootWoopsaObject = new WoopsaObject(null, "root");
            _woopsaServer     = new WoopsaServer(_rootWoopsaObject, port);
            _woopsaServer.WebServer.Routes.Add("/Pages", HTTPMethod.GET, new RouteHandlerFileSystem(folderPathWebPages));
            Thread thread;

            #region infoDebug
            DiagnosticWindow.AddToDebug("\nWoopsa server listening on http://localhost:" + _woopsaServer.WebServer.Port + _woopsaServer.RoutePrefix);
            DiagnosticWindow.AddToDebug("Some examples of what you can do directly from your browser:");
            DiagnosticWindow.AddToDebug(" * View the object hierarchy of the root object:");
            DiagnosticWindow.AddToDebug("   http://localhost:" + _woopsaServer.WebServer.Port + _woopsaServer.RoutePrefix + "meta/");
            DiagnosticWindow.AddToDebug(" * Read the value of a property:");
            DiagnosticWindow.AddToDebug("   http://localhost:" + _woopsaServer.WebServer.Port + _woopsaServer.RoutePrefix + "read/Temperature ");
            DiagnosticWindow.AddToDebug(" * Surfing on the web pages found in the directory \n   specified in Advanced Settings :");
            DiagnosticWindow.AddToDebug("   http://localhost:" + _woopsaServer.WebServer.Port + "/Pages/ \n");

            #endregion

            bool allThreadAreAbort;
            do
            {
                lock (_thisLock)
                {
                    allThreadAreAbort = _allThreadAreAbort;
                }
            }while (!allThreadAreAbort);
            DiagnosticWindow.ResetPlcStatus();
            _woopsaAdsThreadList = new List <Thread>();
            lock (plcParameterList)
            {
                _shouldStop = false;
            }

            foreach (PlcParameter parameter in plcParameterList)
            {
                thread      = new Thread(Start);
                thread.Name = "WoopsaAdsController - " + parameter.name;
                thread.Start(parameter);
                _woopsaAdsThreadList.Add(thread);
            }
            lock (_thisLock)
            {
                _allThreadAreAbort = false;
            }
            isRunning = true;
        }
Exemplo n.º 5
0
        // Adds the exception as a new top-level node to the tree with child nodes
        // for all the exception's properties.
        void AddException(Exception e)
        {
            // Create a list of Inlines containing all the properties of the exception object.
            // The three most important properties (message, type, and stack trace) go first.

            var exceptionItem = new TreeViewItem();
            var inlines       = new List <Inline>();

            System.Reflection.PropertyInfo[] properties = e.GetType().GetProperties();

            exceptionItem.Header = e.GetType();
            exceptionItem.Tag    = inlines;
            treeView1.Items.Add(exceptionItem);

            Inline inline = new Bold(new Run(e.GetType().ToString()));

            inline.FontSize = _large;
            inlines.Add(inline);

            AddProperty(inlines, "Message", e.Message);
            AddProperty(inlines, "Stack Trace", e.StackTrace);

            foreach (PropertyInfo info in properties)
            {
                // Skip InnerException because it will get a whole
                // top-level node of its own.

                if (info.Name != "InnerException")
                {
                    var value = info.GetValue(e, null);

                    if (value != null)
                    {
                        if (value is string)
                        {
                            if (string.IsNullOrEmpty(value as string))
                            {
                                continue;
                            }
                        }
                        else if (value is IDictionary)
                        {
                            value = RenderDictionary(value as IDictionary);
                            if (string.IsNullOrEmpty(value as string))
                            {
                                continue;
                            }
                        }
                        else if (value is IEnumerable && !(value is string))
                        {
                            value = RenderEnumerable(value as IEnumerable);
                            if (string.IsNullOrEmpty(value as string))
                            {
                                continue;
                            }
                        }

                        if (info.Name != "Message" &&
                            info.Name != "StackTrace")
                        {
                            // Add the property to list for the exceptionItem.
                            AddProperty(inlines, info.Name, value);
                        }

                        // Create a TreeViewItem for the individual property.
                        var propertyItem    = new TreeViewItem();
                        var propertyInlines = new List <Inline>();

                        propertyItem.Header = info.Name;
                        propertyItem.Tag    = propertyInlines;
                        exceptionItem.Items.Add(propertyItem);
                        AddProperty(propertyInlines, info.Name, value);
                    }
                }
                else
                {
                    DiagnosticWindow.AddToDebug(info.ToString());
                }
            }
        }
Exemplo n.º 6
0
        public void loadHierarchy(WoopsaObject root)
        {
            try
            {
                _symbolLoader     = _tcAds.CreateSymbolInfoLoader();
                _woopsaObjects    = new Dictionary <string, WoopsaObject>();
                _woopsaProperties = new Dictionary <string, WoopsaAdsProperty>();

                foreach (TcAdsSymbolInfo symbol in _symbolLoader)
                {
                    WoopsaObject      newObject   = null;
                    WoopsaAdsProperty newProperty = null;
                    TcAdsSymbolInfo   parentInfo;
                    WoopsaValueType   propertyType;
                    string[]          path = symbol.Name.Split('.');
                    string            name = path[path.Length - 1];
                    bool isProperties      = BeckhoffToWoopsaValueType(symbol.Type, out propertyType);
                    if (symbol.Parent != null)
                    {
                        parentInfo = symbol.Parent;
                        if (_woopsaObjects.ContainsKey(parentInfo.Name))
                        {
                            if (isProperties)
                            {
                                newProperty = new WoopsaAdsProperty(_woopsaObjects[parentInfo.Name], name, propertyType,
                                                                    _woopsaAdsPropertyGet, _woopsaAdsPropertySet, symbol);
                            }
                            else
                            {
                                newObject = new WoopsaObject(_woopsaObjects[parentInfo.Name], name);
                            }
                        }
                        else
                        {
                            throw new Exception("Parent WoopsaObject not found !");
                        }
                    }
                    else
                    {
                        if (isProperties)
                        {
                            newProperty = new WoopsaAdsProperty(root, name, propertyType, _woopsaAdsPropertyGet, _woopsaAdsPropertySet, symbol);
                        }
                        else
                        {
                            newObject = new WoopsaObject(root, name);
                        }
                    }

                    if (isProperties)
                    {
                        _woopsaProperties.Add(symbol.Name, newProperty);
                    }
                    else
                    {
                        _woopsaObjects.Add(symbol.Name, newObject);
                    }
                }
            }catch (Exception e)
            {
                DiagnosticWindow.AddToDebug(e.Message);
            }
            isHierarchieLoaded = true;
        }
Exemplo n.º 7
0
        public void Start(object argsObject)
        {
            PlcParameter plcParameter = argsObject as PlcParameter;

            try
            {
                WoopsaAdsServer serverConnection = new WoopsaAdsServer(plcParameter.adsNetId);
                WoopsaProperty  propertyIsConnected;
                WoopsaObject    plc            = null;
                bool            isWorking      = false;
                bool            shouldShutDown = false;

                lock (_rootWoopsaObject)
                {
                    DiagnosticWindow.AddPlcStatus(new PlcStatus(plcParameter.name, false, "Starting up"));
                    propertyIsConnected = new WoopsaProperty(_rootWoopsaObject, "IsAlive" + plcParameter.name, WoopsaValueType.Logical, (property) => serverConnection.isAdsConnected);
                }


                while (!shouldShutDown)
                {
                    if (serverConnection.IsHeartBeatAlive())
                    {
                        serverConnection.isAdsConnected = true;
                        if (!serverConnection.isHierarchieLoaded)
                        {
                            if (plc != null)
                            {
                                plc.Dispose();
                            }
                            lock (_rootWoopsaObject)
                            {
                                plc = new WoopsaObject(_rootWoopsaObject, plcParameter.name);
                            }
                            serverConnection.loadHierarchy(plc);
                        }
                        if (!isWorking)
                        {
                            isWorking = true;
                            DiagnosticWindow.AddToDebug(Thread.CurrentThread.Name + " thread  : working...");
                            lock (_rootWoopsaObject)
                            {
                                DiagnosticWindow.PlcStatusChange(plc.Name, isWorking, "Working");
                            }
                        }
                    }
                    else
                    {
                        serverConnection.isAdsConnected     = false;
                        serverConnection.isHierarchieLoaded = false;
                        if (plc != null)
                        {
                            plc.Dispose();
                        }
                        DiagnosticWindow.AddToDebug(Thread.CurrentThread.Name + " - Error connection ... Try again");
                        isWorking = false;
                        lock (_rootWoopsaObject)
                        {
                            DiagnosticWindow.PlcStatusChange(plcParameter.name, isWorking, "Error");
                        }
                    }
                    Thread.Sleep(500);
                    lock (plcParameterList)
                    {
                        shouldShutDown = _shouldStop;
                    }
                }
                serverConnection.Dispose();
            }
            catch (SocketException e)
            {
                // A SocketException is caused by an application already listening on a port in 90% of cases
                // Applications known to use port 80:
                //  - On Windows 10, IIS is on by default on some configurations. Disable it here:
                //    http://stackoverflow.com/questions/30758894/apache-server-xampp-doesnt-run-on-windows-10-port-80
                //  - IIS
                //  - Apache
                //  - Nginx
                //  - Skype

                DiagnosticWindow.AddToDebug("Error: Could not start Woopsa Server. Most likely because an application is already listening on port 80.");
                DiagnosticWindow.AddToDebug("Known culprits:");
                DiagnosticWindow.AddToDebug(" - On Windows 10, IIS is on by default on some configurations.");
                DiagnosticWindow.AddToDebug(" - Skype");
                DiagnosticWindow.AddToDebug(" - Apache, nginx, etc.");
                DiagnosticWindow.AddToDebug("SocketException: " + e.Message);
                MessageBox.Show("SocketException ! : See diagnostic log for more information", "Er ror", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            catch (Exception ex)
            {
                Application.Current.Dispatcher.Invoke(
                    System.Windows.Threading.DispatcherPriority.Normal,
                    new Action <Exception>((exc) =>
                {
                    throw new Exception("Exception from another Thread : " + Thread.CurrentThread.Name, exc);
                }), ex);
            }
            DiagnosticWindow.AddToDebug(Thread.CurrentThread.Name + " thread: terminating gracefully.");
            lock (_rootWoopsaObject)
            {
                lock (App.appLock)
                {
                    if (App.isExiting)
                    {
                        return;
                    }
                }
                DiagnosticWindow.PlcStatusChange(plcParameter.name, false, "Stop");
            }
        }