Example #1
0
        private void ScannerOnToolAttach(HcToolInfo info)
        {
            var lockTakenScan     = false;
            var lockTakenRegister = false;

            try
            {
                // lock
                Monitor.TryEnter(ScannedTools, Timeout, ref lockTakenScan);
                Monitor.TryEnter(RegisteredTools, Timeout, ref lockTakenRegister);
                // check lock taken
                if (!lockTakenScan || !lockTakenRegister)
                {
                    return;
                }

                // get tools
                var register = FindToolInfo(RegisteredTools, info.Mac);
                var scan     = FindToolInfo(ScannedTools, info.Mac);
                // check scan tool
                switch (scan)
                {
                case null when register == null:
                    // add scan tool
                    ScannedTools.Add(info);
                    break;

                case null:
                    // set values
                    register.SetValues(info.GetValues());
                    // tool connect event
                    ToolConnect?.Invoke(register);
                    break;
                }
            }
            finally
            {
                // unlock
                if (lockTakenScan)
                {
                    Monitor.Exit(ScannedTools);
                }
                if (lockTakenRegister)
                {
                    Monitor.Exit(RegisteredTools);
                }
            }
        }
Example #2
0
        private void ScannerOnToolAlive(HcToolInfo info)
        {
            var lockTakenScan     = false;
            var lockTakenRegister = false;

            try
            {
                // lock
                Monitor.TryEnter(ScannedTools, Timeout, ref lockTakenScan);
                Monitor.TryEnter(RegisteredTools, Timeout, ref lockTakenRegister);
                // check lock taken
                if (!lockTakenScan || !lockTakenRegister)
                {
                    return;
                }

                // get tools
                var register = FindToolInfo(RegisteredTools, info.Mac);
                var scan     = FindToolInfo(ScannedTools, info.Mac);
                // check scan/register tool
                if (scan == null && register == null)
                {
                    // add scan tool
                    ScannedTools.Add(info);
                }
                else if (register != null)
                {
                    // event
                    ToolAlive?.Invoke(info);
                }
            }
            finally
            {
                // unlock
                if (lockTakenScan)
                {
                    Monitor.Exit(ScannedTools);
                }
                if (lockTakenRegister)
                {
                    Monitor.Exit(RegisteredTools);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Tool register
        /// </summary>
        /// <param name="info">Tool information</param>
        /// <returns>result</returns>
        public bool RegisterTool(HcToolInfo info)
        {
            var lockTakenScan     = false;
            var lockTakenRegister = false;

            try
            {
                // lock
                Monitor.TryEnter(ScannedTools, Timeout, ref lockTakenScan);
                Monitor.TryEnter(RegisteredTools, Timeout, ref lockTakenRegister);
                // check lock taken
                if (!lockTakenScan || !lockTakenRegister)
                {
                    return(false);
                }

                // get register tool
                if (FindToolInfo(RegisteredTools, info.Mac) != null)
                {
                    return(false);
                }
                // remove scan tool
                ScannedTools.Remove(FindToolInfo(ScannedTools, info.Mac));
                // add register tool
                RegisteredTools.Add(info);
                // tool added event
                ToolAdded?.Invoke(info);

                return(true);
            }
            finally
            {
                // unlock
                if (lockTakenScan)
                {
                    Monitor.Exit(ScannedTools);
                }
                if (lockTakenRegister)
                {
                    Monitor.Exit(RegisteredTools);
                }
            }
        }
Example #4
0
        /// <summary>
        /// Registered tools load binary file
        /// </summary>
        /// <param name="path">file path</param>
        /// <returns>result</returns>
        public bool LoadRegisterTools(string path)
        {
            var lockTakenScan     = false;
            var lockTakenRegister = false;

            try
            {
                // lock
                Monitor.TryEnter(ScannedTools, Timeout, ref lockTakenScan);
                Monitor.TryEnter(RegisteredTools, Timeout, ref lockTakenRegister);
                // check lock taken
                if (!lockTakenRegister)
                {
                    return(false);
                }

                // file stream
                using (var fs = new FileStream(path, FileMode.Open))
                {
                    // binary writer
                    using (var br = new BinaryReader(fs))
                    {
                        do
                        {
                            // get tool information
                            var tool = new HcToolInfo(br.ReadBytes(HcToolInfo.Count));
                            // find scan tool
                            var scan = FindToolInfo(ScannedTools, tool.Mac);
                            // check scan tool
                            if (scan != null)
                            {
                                // remove
                                ScannedTools.Remove(scan);
                            }
                            // find register tool
                            if (FindToolInfo(RegisteredTools, tool.Mac) != null)
                            {
                                continue;
                            }
                            // register tool
                            RegisteredTools.Add(tool);

                            // check file length
                        } while (br.BaseStream.Position < br.BaseStream.Length);
                    }
                }

                return(true);
            }
            catch (Exception e)
            {
                // debug
                Console.WriteLine($@"{e.Message}");
                // result
                return(false);
            }
            finally
            {
                // unlock
                if (lockTakenScan)
                {
                    Monitor.Exit(ScannedTools);
                }
                if (lockTakenRegister)
                {
                    Monitor.Exit(RegisteredTools);
                }
            }
        }