Beispiel #1
0
        private void ScannerOnToolDetach(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)
                {
                    // remove scan tool
                    ScannedTools.Remove(scan);
                }
                else if (scan == null && register != null)
                {
                    // tool disconnect event
                    ToolDisconnect?.Invoke(info);
                }
            }
            finally
            {
                // unlock
                if (lockTakenScan)
                {
                    Monitor.Exit(ScannedTools);
                }
                if (lockTakenRegister)
                {
                    Monitor.Exit(RegisteredTools);
                }
            }
        }
Beispiel #2
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);
                }
            }
        }
Beispiel #3
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);
                }
            }
        }