コード例 #1
0
        // 获得门锁状态
        public GetLockStateResult GetShelfLockState(string lockNameList)
        {
            List <LockState> states = new List <LockState>();

            string[] list = lockNameList.Split(new char[] { ',' });
            foreach (var one in list)
            {
                // 探测锁状态
                // parameters:
                // parameters:
                //      lockNameParam   为 "锁控板名字.卡编号.锁编号"。
                //                      其中卡编号部分可以是 "1" 也可以是 "1|2" 这样的形态
                //                      其中锁编号部分可以是 "1" 也可以是 "1|2|3|4" 这样的形态
                //                      如果缺乏卡编号和锁编号部分,缺乏的部分默认为 "1"
                GetLockStateResult result = null;

                if (Program.MainForm.InSimuLock)
                {
                    result = _simuLock.GetShelfLockState(one);
                }
                else
                {
                    result = Program.Rfid.GetShelfLockState(one);
                }

                if (result.Value == -1)
                {
                    return(result);
                }
                states.AddRange(result.States);
            }

            return(new GetLockStateResult {
                Value = 0, States = states
            });
        }
コード例 #2
0
        // 增加了无标签时延迟等待功能。敏捷响应
        // parameters:
        //      style   风格。逗号间隔的字符串内容
        //              session:会话ID
        //              dont_delay  不根据 session 来进行探测、延迟。也就是说确保要做一次 invetnory 并且立即返回
        public ListTagsResult ListTags(string reader_name, string style)
        {
            if (Program.Rfid.Pause)
            {
                return new ListTagsResult
                       {
                           Value     = -1,
                           ErrorInfo = "RFID 功能处于暂停状态",
                           ErrorCode = "paused"
                       }
            }
            ;

            string lockNameList = StringUtil.GetParameterByPrefix(style, "getLockState");

            if (lockNameList != null)
            {
                lockNameList = StringUtil.UnescapeString(lockNameList);
            }

            bool output_time = false;

            Program.Rfid.IncApiCount();
            try
            {
                if (Program.Rfid.Pause)
                {
                    return new ListTagsResult
                           {
                               Value     = -1,
                               ErrorInfo = "RFID 功能处于暂停状态",
                               ErrorCode = "paused"
                           }
                }
                ;

                // 2019/12/15
                bool dont_delay = StringUtil.IsInList("dont_delay", style);

                string session_id = StringUtil.GetParameterByPrefix(style, "session");

                if (output_time)
                {
                    Program.MainForm.OutputHistory($"session start: {session_id}, reader_name={reader_name}, style={style}");
                }

                TimeSpan length = TimeSpan.FromSeconds(2);

                ListTagsResult     result      = null;
                GetLockStateResult lock_result = null;

                string   current_uids = "";
                DateTime start        = DateTime.Now;
                while (DateTime.Now - start < length ||
                       result == null)
                {
                    Stopwatch watch = null;

                    // 执行 inventory
                    if (string.IsNullOrEmpty(reader_name) == false)
                    {
                        if (output_time)
                        {
                            watch = new Stopwatch();
                        }
                        else
                        {
                            watch = null;
                        }
                        watch?.Start();
                        result = _listTags(reader_name, style);
                        // testing
                        //if (reader_name.StartsWith("M201"))
                        //    Thread.Sleep(5000);
                        watch?.Stop();
                        if (output_time)
                        {
                            Program.MainForm.OutputHistory($"{session_id} inventory time:{watch.Elapsed.TotalSeconds}, count:{result.Results?.Count}");
                        }
                    }
                    else
                    {
                        if (output_time)
                        {
                            Program.MainForm.OutputHistory($"{session_id} inventory skipped");
                        }
                        result = new ListTagsResult();
                    }

                    // 执行门锁状态获取
                    // 目前是 1:1 次数分配
                    if (lockNameList != null)
                    {
                        Thread.Sleep(100);

                        if (output_time)
                        {
                            watch = new Stopwatch();
                        }
                        else
                        {
                            watch = null;
                        }
                        watch?.Start();
                        lock_result = GetShelfLockState(lockNameList);
                        watch?.Stop();
                        if (output_time)
                        {
                            Program.MainForm.OutputHistory($"{session_id} getLockState time:{watch.Elapsed.TotalSeconds}, count:{lock_result.States?.Count}");
                        }
                        // 从此开始 result.GetLockStateResult 就有值了
                        result.GetLockStateResult = lock_result;
                    }
                    else
                    {
                        if (output_time)
                        {
                            Program.MainForm.OutputHistory($"{session_id} getLockState skipped");
                        }
                    }

                    // 判断 inventory 结果
                    if (string.IsNullOrEmpty(reader_name) == false)
                    {
                        // 立即返回
                        if (dont_delay)
                        {
                            return(result);
                        }

                        if (result != null && result.Results != null)
                        {
                            current_uids = BuildUids(result.Results);
                        }
                        else
                        {
                            current_uids = "";
                        }

                        // TODO: 这里的比较应该按照 Session 来进行
                        // 只要本次和上次 tag 数不同,立刻就返回
                        if (CompareLastUids(session_id, current_uids))
                        {
                            SetLastUids(session_id, current_uids);
                            return(result);
                        }

                        if (result.Value == -1)
                        {
                            return(result);
                        }
                    }


                    // 判断门锁状态
                    if (lockNameList != null)
                    {
                        // 这里的疑问是,如果 _listTags 没有出错,是否应该坚持返回正确结果?
                        if (lock_result.Value != -1)
                        {
                            string current_states = BuildStateString(lock_result.States);
                            if (CompareLastUids(session_id + "_lock", current_states))
                            {
                                SetLastUids(session_id + "_lock", current_states);
                                return(result);
                            }
                        }
                    }

                    /*
                     * // TODO: 如果本次和上次都是 2,是否立即返回?可否先对比一下 uid,有差别再返回?
                     * if (result.Results != null
                     *  && result.Results.Count > 0)
                     * {
                     *  SetLastUids(current_uids);
                     *  return result;
                     * }
                     */
                    Thread.Sleep(10);  // 10? 200?
                }

                SetLastUids(session_id, current_uids);
                return(result);
            }
            catch (Exception ex)
            {
                return(new ListTagsResult
                {
                    Value = -1,
                    // TODO: 如何返回异常信息?
                    ErrorInfo = $"ListTags() 出现异常:{ExceptionUtil.GetDebugText(ex)}"
                });
            }
            finally
            {
                Program.Rfid.DecApiCount();
            }
        }
コード例 #3
0
        // 增加了无标签时延迟等待功能。敏捷响应
        // parameters:
        //      style   风格。逗号间隔的字符串内容
        //              session:会话ID
        //              dont_delay  不根据 session 来进行探测、延迟。也就是说确保要做一次 invetnory 并且立即返回
        public ListTagsResult ListTags(string reader_name, string style)
        {
            if (Program.Rfid.Pause)
            {
                return new ListTagsResult
                       {
                           Value     = -1,
                           ErrorInfo = "RFID 功能处于暂停状态",
                           ErrorCode = "paused"
                       }
            }
            ;

            string lockNameList = StringUtil.GetParameterByPrefix(style, "getLockState");

            if (lockNameList != null)
            {
                lockNameList = StringUtil.UnescapeString(lockNameList);
            }

            bool output_time = false;

            Program.Rfid.IncApiCount();
            try
            {
                if (Program.Rfid.Pause)
                {
                    return new ListTagsResult
                           {
                               Value     = -1,
                               ErrorInfo = "RFID 功能处于暂停状态",
                               ErrorCode = "paused"
                           }
                }
                ;

                // 2019/12/15
                bool dont_delay = StringUtil.IsInList("dont_delay", style);

                string session_id = StringUtil.GetParameterByPrefix(style, "session");

                if (output_time)
                {
                    Program.MainForm.OutputHistory($"session start: {session_id}, reader_name={reader_name}, style={style}");
                }

                TimeSpan length = TimeSpan.FromSeconds(2);

                ListTagsResult     result      = null;
                GetLockStateResult lock_result = null;

                string   current_uids = "";
                DateTime start        = DateTime.Now;
                while (DateTime.Now - start < length ||
                       result == null)
                {
                    Stopwatch watch = null;

                    // 执行 inventory
                    if (string.IsNullOrEmpty(reader_name) == false)
                    {
                        if (output_time)
                        {
                            watch = new Stopwatch();
                        }
                        else
                        {
                            watch = null;
                        }
                        watch?.Start();
                        result = _listTags(reader_name, style);
                        // testing
                        //if (reader_name.StartsWith("M201"))
                        //    Thread.Sleep(5000);
                        watch?.Stop();
                        if (output_time)
                        {
                            Program.MainForm.OutputHistory($"{session_id} inventory time:{watch.Elapsed.TotalSeconds}, count:{result.Results?.Count}");
                        }
                    }
                    else
                    {
                        if (output_time)
                        {
                            Program.MainForm.OutputHistory($"{session_id} inventory skipped");
                        }
                        result = new ListTagsResult();
                    }

                    // 执行门锁状态获取
                    // 目前是 1:1 次数分配
                    if (lockNameList != null)
                    {
                        if (output_time)
                        {
                            watch = new Stopwatch();
                        }
                        else
                        {
                            watch = null;
                        }
                        watch?.Start();
                        lock_result = GetShelfLockState(lockNameList);
                        watch?.Stop();
                        if (output_time)
                        {
                            Program.MainForm.OutputHistory($"{session_id} getLockState time:{watch.Elapsed.TotalSeconds}, count:{lock_result.States?.Count}");
                        }
                        // 从此开始 result.GetLockStateResult 就有值了
                        result.GetLockStateResult = lock_result;
                    }
                    else
                    {
                        if (output_time)
                        {
                            Program.MainForm.OutputHistory($"{session_id} getLockState skipped");
                        }
                    }

                    // 判断 inventory 结果
                    if (string.IsNullOrEmpty(reader_name) == false)
                    {
                        // 立即返回
                        if (dont_delay)
                        {
                            return(result);
                        }

                        if (result != null && result.Results != null)
                        {
                            current_uids = BuildUids(result.Results);
                        }
                        else
                        {
                            current_uids = "";
                        }

                        // TODO: 这里的比较应该按照 Session 来进行
                        // 只要本次和上次 tag 数不同,立刻就返回
                        if (CompareLastUids(session_id, current_uids))
                        {
                            SetLastUids(session_id, current_uids);
                            return(result);
                        }

                        if (result.Value == -1)
                        {
                            return(result);
                        }
                    }


                    // 判断门锁状态
                    if (lockNameList != null)
                    {
                        // 这里的疑问是,如果 _listTags 没有出错,是否应该坚持返回正确结果?
                        if (lock_result.Value != -1)
                        {
                            string current_states = BuildStateString(lock_result.States);
                            if (CompareLastUids(session_id + "_lock", current_states))
                            {
                                SetLastUids(session_id + "_lock", current_states);
                                return(result);
                            }
                        }
                    }

                    /*
                     * // TODO: 如果本次和上次都是 2,是否立即返回?可否先对比一下 uid,有差别再返回?
                     * if (result.Results != null
                     *  && result.Results.Count > 0)
                     * {
                     *  SetLastUids(current_uids);
                     *  return result;
                     * }
                     */
                    Thread.Sleep(10);  // 10? 200?
                }

                SetLastUids(session_id, current_uids);
                return(result);
            }
            catch (Exception ex)
            {
                return(new ListTagsResult
                {
                    Value = -1,
                    // TODO: 如何返回异常信息?
                    ErrorInfo = $"ListTags() 出现异常:{ExceptionUtil.GetDebugText(ex)}"
                });
            }
            finally
            {
                Program.Rfid.DecApiCount();
            }
        }

        //static uint _currenAntenna = 1;
        //DateTime _lastTime;

        // parameters:
        //      reader_name_list    读卡器名字列表。形态为 "*" 或 "name1,name2" 或 "name1:1|2|3|4,name2"
        //      style   如果为 "getTagInfo",表示要在结果中返回 TagInfo
        ListTagsResult _listTags(string reader_name_list, string style)
        {
            InventoryResult result = new InventoryResult();

            if (Program.MainForm.ErrorState != "normal")
            {
                return new ListTagsResult
                       {
                           Value     = -1,
                           ErrorInfo = $"{Program.MainForm.ErrorStateInfo}",
                           ErrorCode = $"state:{Program.MainForm.ErrorState}"
                       }
            }
            ;

            List <OneTag> tags = new List <OneTag>();

            // uid --> OneTag
            Hashtable uid_table = new Hashtable();

            foreach (Reader reader in Program.Rfid.Readers)
            {
#if NO
                if (reader_name == "*" || reader.Name == reader_name)
                {
                }
                else
                {
                    continue;
                }
#endif

                // 顺便要从 reader_name_list 中解析出天线部分
                if (Reader.MatchReaderName(reader_name_list, reader.Name, out string antenna_list) == false)
                {
                    continue;
                }

                InventoryResult inventory_result = Program.Rfid.Inventory(reader.Name,
                                                                          antenna_list,
                                                                          style // ""
                                                                          );

                if (inventory_result.Value == -1)
                {
                    return(new ListTagsResult {
                        Value = -1, ErrorInfo = inventory_result.ErrorInfo, ErrorCode = inventory_result.ErrorCode
                    });
                }

                foreach (InventoryInfo info in inventory_result.Results)
                {
                    OneTag tag = null;
                    if (uid_table.ContainsKey(info.UID))
                    {
                        // 重复出现的,追加 读卡器名字
                        tag             = (OneTag)uid_table[info.UID];
                        tag.ReaderName += "," + reader.Name;
                    }
                    else
                    {
                        // 首次出现
                        tag = new OneTag
                        {
                            Protocol   = info.Protocol,
                            ReaderName = reader.Name,
                            UID        = info.UID,
                            DSFID      = info.DsfID,
                            AntennaID  = info.AntennaID, // 2019/9/25
                            // InventoryInfo = info    // 有些冗余的字段
                        };

                        /*
                         * // testing
                         * tag.AntennaID = _currenAntenna;
                         * if (DateTime.Now - _lastTime > TimeSpan.FromSeconds(5))
                         * {
                         *  _currenAntenna++;
                         *  if (_currenAntenna > 50)
                         *      _currenAntenna = 1;
                         *  _lastTime = DateTime.Now;
                         * }
                         */

                        uid_table[info.UID] = tag;
                        tags.Add(tag);
                    }

                    if (StringUtil.IsInList("getTagInfo", style) &&
                        tag.TagInfo == null)
                    {
                        // TODO: 这里要利用 Hashtable 缓存
                        GetTagInfoResult result0 = Program.Rfid.GetTagInfo(reader.Name, info);
                        if (result0.Value == -1)
                        {
                            tag.TagInfo = null;
                            // TODO: 如何报错?写入操作历史?
                            // $"读取标签{info.UID}信息时出错:{result0.ToString()}"
                        }
                        else
                        {
                            tag.TagInfo = result0.TagInfo;
                        }
                    }
#if NO
                    GetTagInfoResult result0 = Program.Rfid.GetTagInfo(reader.Name, info);
                    if (result0.Value == -1)
                    {
                        // TODO: 如何报错?写入操作历史?
                        Program.MainForm.OutputText($"读取标签{info.UID}信息时出错:{result0.ToString()}", 2);
                        continue;
                    }

                    LogicChip chip = LogicChip.From(result0.TagInfo.Bytes,
                                                    (int)result0.TagInfo.BlockSize,
                                                    "" // result0.TagInfo.LockStatus
                                                    );
                    Element pii = chip.FindElement(ElementOID.PII);
                    if (pii == null)
                    {
                        Program.MainForm.Invoke((Action)(() =>
                        {
                            // 发送 UID
                            SendKeys.SendWait($"uid:{info.UID}\r");
                        }));
                    }
                    else
                    {
                        Program.MainForm.Invoke((Action)(() =>
                        {
                            // 发送 PII
                            SendKeys.SendWait($"pii:{pii.Text}\r");
                        }));
                    }
#endif
                }
            }

            return(new ListTagsResult {
                Results = tags
            });

#if NO
            InventoryResult result = new InventoryResult();
            List <OneTag>   tags   = new List <OneTag>();
            _lockTagList.EnterReadLock();
            try
            {
                foreach (OneTag tag in _tagList)
                {
                    if (reader_name == "*" || tag.ReaderName == reader_name)
                    {
                        tags.Add(tag);
                    }
                }
                return(new ListTagsResult {
                    Results = tags
                });
            }
            finally
            {
                _lockTagList.ExitReadLock();
            }
#endif
        }