async Task ClearTagContent(ListViewItem item) { RfidChannel channel = StartRfidChannel( Program.MainForm.RfidCenterUrl, out string strError); if (channel == null) { strError = "StartRfidChannel() error"; goto ERROR1; } try { ItemInfo item_info = (ItemInfo)item.Tag; var old_tag_info = item_info.OneTag.TagInfo; var new_tag_info = old_tag_info.Clone(); // 制造一套空内容 { new_tag_info.AFI = 0; new_tag_info.DSFID = 0; new_tag_info.EAS = false; List <byte> bytes = new List <byte>(); for (int i = 0; i < new_tag_info.BlockSize * new_tag_info.MaxBlockCount; i++) { bytes.Add(0); } new_tag_info.Bytes = bytes.ToArray(); new_tag_info.LockStatus = ""; } var result = channel.Object.WriteTagInfo(item_info.OneTag.ReaderName, old_tag_info, new_tag_info); if (result.Value == -1) { strError = result.ErrorInfo; goto ERROR1; } await Task.Run(() => { GetTagInfo(item); }); return; } catch (Exception ex) { strError = "ClearTagContent() 出现异常: " + ex.Message; goto ERROR1; } finally { EndRfidChannel(channel); } ERROR1: this.Invoke((Action)(() => { ListViewUtil.ChangeItemText(item, COLUMN_PII, "error:" + strError); // 把 item 修改为红色背景,表示出错的状态 SetItemColor(item, "error"); })); }
void GetTagInfo(ListViewItem item) { ItemInfo item_info = (ItemInfo)item.Tag; OneTag tag = item_info.OneTag; RfidChannel channel = StartRfidChannel( Program.MainForm.RfidCenterUrl, out string strError); if (channel == null) { strError = "StartRfidChannel() error"; goto ERROR1; } try { GetTagInfoResult result = channel.Object.GetTagInfo("*", tag.UID); if (result.Value == -1) { strError = result.ErrorInfo; goto ERROR1; } tag.TagInfo = result.TagInfo; string hex_string = Element.GetHexString(result.TagInfo.Bytes, "4"); item_info.LogicChipItem = LogicChipItem.FromTagInfo(result.TagInfo); item_info.LogicChipItem.PropertyChanged += LogicChipItem_PropertyChanged; this.Invoke((Action)(() => { string pii = item_info.LogicChipItem.PrimaryItemIdentifier; // .FindElement(ElementOID.PII)?.Text; ListViewUtil.ChangeItemText(item, COLUMN_PII, pii); if (this.SelectedPII != null && pii == this.SelectedPII) { item.Font = new Font(item.Font, FontStyle.Bold); } })); return; } catch (Exception ex) { strError = "ListTags() 出现异常: " + ex.Message; goto ERROR1; } finally { EndRfidChannel(channel); } ERROR1: this.Invoke((Action)(() => { ListViewUtil.ChangeItemText(item, COLUMN_PII, "error:" + strError); // 把 item 修改为红色背景,表示出错的状态 SetItemColor(item, "error"); })); }
void ReleaseRfidChannel() { if (_rfidChannel != null) { EndRfidChannel(_rfidChannel); _rfidChannel = null; } }
async Task <bool> SaveTagContent(ListViewItem item) { ItemInfo item_info = (ItemInfo)item.Tag; if (item_info.LogicChipItem.Changed == false) { return(false); } RfidChannel channel = StartRfidChannel( Program.MainForm.RfidCenterUrl, out string strError); if (channel == null) { strError = "StartRfidChannel() error"; goto ERROR1; } try { var old_tag_info = item_info.OneTag.TagInfo; var new_tag_info = BuildNewTagInfo( old_tag_info, item_info.LogicChipItem); var result = channel.Object.WriteTagInfo(item_info.OneTag.ReaderName, old_tag_info, new_tag_info); if (result.Value == -1) { strError = result.ErrorInfo; goto ERROR1; } await Task.Run(() => { GetTagInfo(item); }); UpdateChanged(item_info.LogicChipItem); return(true); } catch (Exception ex) { strError = "SaveTagContent() 出现异常: " + ex.Message; goto ERROR1; } finally { EndRfidChannel(channel); } ERROR1: this.Invoke((Action)(() => { ListViewUtil.ChangeItemText(item, COLUMN_PII, "error:" + strError); // 把 item 修改为红色背景,表示出错的状态 SetItemColor(item, "error"); })); return(false); }
int LoadChipByUID( string reader_name, string uid, uint antenna_id, out TagInfo tag_info, out string strError) { strError = ""; tag_info = null; #if OLD_CODE RfidChannel channel = StartRfidChannel( Program.MainForm.RfidCenterUrl, out strError); if (channel == null) { strError = "StartRfidChannel() error"; return(-1); } #endif try { #if OLD_CODE var result = channel.Object.GetTagInfo( reader_name, uid); #else var result = RfidManager.GetTagInfo( reader_name, uid, antenna_id); #endif if (result.Value == -1) { strError = result.ErrorInfo; return(-1); } Debug.Assert(result.TagInfo != null, ""); tag_info = result.TagInfo; return(0); } catch (Exception ex) { strError = "GetTagInfo() 出现异常: " + ex.Message; return(-1); } finally { #if OLD_CODE EndRfidChannel(channel); #endif } }
void GetTagInfo(ListViewItem item) { OneTag tag = (OneTag)item.Tag; RfidChannel channel = StartRfidChannel( Program.MainForm.RfidCenterUrl, out string strError); if (channel == null) { strError = "StartRfidChannel() error"; goto ERROR1; } try { GetTagInfoResult result = channel.Object.GetTagInfo("*", tag.UID); if (result.Value == -1) { strError = result.ErrorInfo; goto ERROR1; } tag.TagInfo = result.TagInfo; LogicChip chip = LogicChip.From(result.TagInfo.Bytes, (int)result.TagInfo.BlockSize); this.Invoke((Action)(() => { string pii = chip.FindElement(ElementOID.PII)?.Text; ListViewUtil.ChangeItemText(item, COLUMN_PII, pii); if (pii == this.SelectedPII) { item.Font = new Font(item.Font, FontStyle.Bold); } })); return; } catch (Exception ex) { strError = "ListTags() 出现异常: " + ex.Message; goto ERROR1; } finally { EndRfidChannel(channel); } ERROR1: this.Invoke((Action)(() => { ListViewUtil.ChangeItemText(item, COLUMN_PII, "error:" + strError); // TODO: 把 item 修改为红色背景,表示出错的状态 })); }
// 装入以前的标签信息 // 如果读卡器上有多个标签,则出现对话框让从中选择一个。列表中和右侧 PII 相同的,优先被选定 // parameters: // adjust_right 是否自动调整右侧元素。即,把左侧的锁定状态元素覆盖到右侧。调整前要询问。如果不同意调整,可以放弃,然后改为放一个空白标签并装载保存 int LoadOldChip(bool adjust_right, out string strError) { strError = ""; if (string.IsNullOrEmpty(Program.MainForm.RfidCenterUrl)) { strError = "尚未配置 RFID 中心 URL"; return(-1); } RfidChannel channel = StartRfidChannel( Program.MainForm.RfidCenterUrl, out strError); if (channel == null) { return(-1); } try { ListTagsResult result = channel.Object.ListTags("*"); if (result.Value == -1) { strError = result.ErrorInfo; return(-1); } // 出现对话框让选择一个 SelectTagDialog dialog = new SelectTagDialog(); dialog.Tags = result.Results; dialog.ShowDialog(this); if (dialog.DialogResult == DialogResult.Cancel) { return(0); } // 装载标签详细信息 GetTagInfoResult result1 = channel.Object.GetTagInfo(dialog.SelectedTag.ReaderName, dialog.SelectedTag.UID); return(1); } catch (Exception ex) { strError = "出现异常: " + ex.Message; return(-1); } finally { EndRfidChannel(channel); } }
int SaveNewChip(out string strError) { strError = ""; #if OLD_CODE RfidChannel channel = StartRfidChannel( Program.MainForm.RfidCenterUrl, out strError); if (channel == null) { strError = "StartRfidChannel() error"; return(-1); } #endif try { TagInfo new_tag_info = LogicChipItem.ToTagInfo( _tagExisting.TagInfo, this.chipEditor_editing.LogicChipItem); #if OLD_CODE NormalResult result = channel.Object.WriteTagInfo( _tagExisting.ReaderName, _tagExisting.TagInfo, new_tag_info); #else NormalResult result = RfidManager.WriteTagInfo( _tagExisting.ReaderName, _tagExisting.TagInfo, new_tag_info); TagList.ClearTagTable(_tagExisting.UID); #endif if (result.Value == -1) { strError = result.ErrorInfo; return(-1); } return(0); } catch (Exception ex) { strError = "SaveNewChip() 出现异常: " + ex.Message; return(-1); } finally { #if OLD_CODE EndRfidChannel(channel); #endif } }
int SaveNewChip(out string strError) { strError = ""; RfidChannel channel = StartRfidChannel( Program.MainForm.RfidCenterUrl, out strError); if (channel == null) { strError = "StartRfidChannel() error"; return(-1); } try { #if NO TagInfo new_tag_info = _tagExisting.TagInfo.Clone(); new_tag_info.Bytes = this.chipEditor_editing.LogicChipItem.GetBytes( (int)(new_tag_info.MaxBlockCount * new_tag_info.BlockSize), (int)new_tag_info.BlockSize, LogicChip.GetBytesStyle.None, out string block_map); new_tag_info.LockStatus = block_map; #endif TagInfo new_tag_info = LogicChipItem.ToTagInfo( _tagExisting.TagInfo, this.chipEditor_editing.LogicChipItem); NormalResult result = channel.Object.WriteTagInfo( _tagExisting.ReaderName, _tagExisting.TagInfo, new_tag_info); if (result.Value == -1) { strError = result.ErrorInfo; return(-1); } return(0); } catch (Exception ex) { strError = "SaveNewChip() 出现异常: " + ex.Message; return(-1); } finally { EndRfidChannel(channel); } }
int LoadChipByUID( string reader_name, string uid, out TagInfo tag_info, out string strError) { strError = ""; tag_info = null; RfidChannel channel = StartRfidChannel( Program.MainForm.RfidCenterUrl, out strError); if (channel == null) { strError = "StartRfidChannel() error"; return(-1); } try { var result = channel.Object.GetTagInfo( reader_name, uid); if (result.Value == -1) { strError = result.ErrorInfo; return(-1); } tag_info = result.TagInfo; return(0); } catch (Exception ex) { strError = "GetTagInfo() 出现异常: " + ex.Message; return(-1); } finally { EndRfidChannel(channel); } }
int SaveNewChip(out string strError) { strError = ""; RfidChannel channel = StartRfidChannel( Program.MainForm.RfidCenterUrl, out strError); if (channel == null) { strError = "StartRfidChannel() error"; return(-1); } try { TagInfo new_tag_info = LogicChipItem.ToTagInfo( _tagExisting.TagInfo, this.chipEditor_editing.LogicChipItem); NormalResult result = channel.Object.WriteTagInfo( _tagExisting.ReaderName, _tagExisting.TagInfo, new_tag_info); if (result.Value == -1) { strError = result.ErrorInfo; return(-1); } return(0); } catch (Exception ex) { strError = "SaveNewChip() 出现异常: " + ex.Message; return(-1); } finally { EndRfidChannel(channel); } }
void InitialRfidChannel() { if (string.IsNullOrEmpty(Program.MainForm.RfidCenterUrl) == false) { _rfidChannel = StartRfidChannel( Program.MainForm.RfidCenterUrl, out string strError); if (_rfidChannel == null) { this.ShowMessageBox(strError); } #if NO // 马上检测一下通道是否可用 try { _rfidChannel.Object.ListReaders(); } catch (Exception ex) { this.ShowMessageBox("启动 RFID 设备时出错: " + ex.Message); } #endif } }
int SaveNewChip(out string strError) { strError = ""; #if OLD_CODE RfidChannel channel = StartRfidChannel( Program.MainForm.RfidCenterUrl, out strError); if (channel == null) { strError = "StartRfidChannel() error"; return(-1); } #endif try { #if NO TagInfo new_tag_info = _tagExisting.TagInfo.Clone(); new_tag_info.Bytes = this.chipEditor_editing.LogicChipItem.GetBytes( (int)(new_tag_info.MaxBlockCount * new_tag_info.BlockSize), (int)new_tag_info.BlockSize, LogicChip.GetBytesStyle.None, out string block_map); new_tag_info.LockStatus = block_map; #endif Debug.Assert(_tagExisting != null, ""); Debug.WriteLine("333 " + (_tagExisting.TagInfo != null ? "!=null" : "==null")); Debug.Assert(_tagExisting.TagInfo != null, ""); TagInfo new_tag_info = LogicChipItem.ToTagInfo( _tagExisting.TagInfo, this.chipEditor_editing.LogicChipItem); #if OLD_CODE NormalResult result = channel.Object.WriteTagInfo( _tagExisting.ReaderName, _tagExisting.TagInfo, new_tag_info); #else Debug.Assert(_tagExisting != null, ""); Debug.WriteLine("111 " + (_tagExisting.TagInfo != null ? "!=null" : "==null")); Debug.Assert(_tagExisting.TagInfo != null, ""); // 2019/9/30 Debug.Assert(_tagExisting.AntennaID == _tagExisting.TagInfo.AntennaID, $"2 _tagExisting.AntennaID({_tagExisting.AntennaID}) 应该 == _tagExisting.TagInfo.AntennaID({_tagExisting.TagInfo.AntennaID})"); NormalResult result = RfidManager.WriteTagInfo( _tagExisting.ReaderName, _tagExisting.TagInfo, new_tag_info); TagList.ClearTagTable(_tagExisting.UID); #endif if (result.Value == -1) { strError = result.ErrorInfo; return(-1); } return(0); } catch (Exception ex) { strError = "SaveNewChip() 出现异常: " + ex.Message; return(-1); } finally { #if OLD_CODE EndRfidChannel(channel); #endif } }
// 更新标签列表 bool UpdateChipList() { string strError = ""; if (string.IsNullOrEmpty(Program.MainForm.RfidCenterUrl)) { strError = "尚未配置 RFID 中心 URL"; goto ERROR1; } RfidChannel channel = StartRfidChannel( Program.MainForm.RfidCenterUrl, out strError); if (channel == null) { strError = "StartRfidChannel() error"; goto ERROR1; } try { ListTagsResult result = channel.Object.ListTags("*", null); if (result.Value == -1) { strError = result.ErrorInfo; goto ERROR1; } List <Task> tasks = new List <Task>(); bool is_empty = false; this.Invoke((Action)(() => { is_empty = this.listView1.Items.Count == 0; List <ListViewItem> items = new List <ListViewItem>(); foreach (OneTag tag in result.Results) { ListViewItem item = FindItem(this.listView1, tag.ReaderName, tag.UID); if (item == null) { item = new ListViewItem(tag.ReaderName); ListViewUtil.ChangeItemText(item, 1, tag.UID); item.Tag = tag; this.listView1.Items.Add(item); if (tag.TagInfo == null) { // 启动单独的线程去填充 .TagInfo tasks.Add(Task.Run(() => { GetTagInfo(item); })); } } items.Add(item); } // 交叉运算得到比 items 中多出来的 ListViewItem,删除它们 List <ListViewItem> delete_items = new List <ListViewItem>(); foreach (ListViewItem item in this.listView1.Items) { if (items.IndexOf(item) == -1) { delete_items.Add(item); } } foreach (ListViewItem item in delete_items) { this.listView1.Items.Remove(item); } })); // 再建立一个 task,等待 tasks 执行完以后,自动选定一个 item if (tasks.Count > 0) { Task.Run(() => { Task.WaitAll(tasks.ToArray()); this.Invoke((Action)(() => { // 首次填充,自动设好选定状态 if (is_empty) { SelectItem(this.SelectedPII); if (string.IsNullOrEmpty(this.SelectedPII) == false && this.AutoCloseDialog) { this.button_OK_Click(this, new EventArgs()); } } })); }); } return(true); } catch (Exception ex) { strError = "UpdateChipList() 出现异常: " + ExceptionUtil.GetDebugText(ex); goto ERROR1; } finally { EndRfidChannel(channel); } ERROR1: MessageBox.Show(this, strError); return(false); }
// 更新标签列表 bool UpdateChipList(bool show_messageBox) { int nRet = Interlocked.Increment(ref _inUpdate); try { if (nRet != 1) { return(false); } string strError = ""; if (string.IsNullOrEmpty(Program.MainForm.RfidCenterUrl)) { strError = "尚未配置 RFID 中心 URL"; goto ERROR1; } RfidChannel channel = StartRfidChannel( Program.MainForm.RfidCenterUrl, out strError); if (channel == null) { strError = "StartRfidChannel() error"; goto ERROR1; } try { ListTagsResult result = channel.Object.ListTags("*"); if (result.Value == -1) { strError = result.ErrorInfo; goto ERROR1; } List <Task> tasks = new List <Task>(); bool is_empty = false; this.Invoke((Action)(() => { is_empty = this.listView_tags.Items.Count == 0; List <ListViewItem> items = new List <ListViewItem>(); foreach (OneTag tag in result.Results) { ListViewItem item = FindItem(this.listView_tags, tag.ReaderName, tag.UID); if (item == null) { item = new ListViewItem(tag.ReaderName); ListViewUtil.ChangeItemText(item, 1, tag.UID); item.Tag = new ItemInfo { OneTag = tag }; this.listView_tags.Items.Add(item); if (tag.TagInfo == null) { // 启动单独的线程去填充 .TagInfo tasks.Add(Task.Run(() => { GetTagInfo(item); })); } } items.Add(item); } // 交叉运算得到比 items 中多出来的 ListViewItem,删除它们 List <ListViewItem> delete_items = new List <ListViewItem>(); foreach (ListViewItem item in this.listView_tags.Items) { if (items.IndexOf(item) == -1) { delete_items.Add(item); } } foreach (ListViewItem item in delete_items) { this.listView_tags.Items.Remove(item); } })); // 再建立一个 task,等待 tasks 执行完以后,自动选定一个 item if (tasks.Count > 0) { Task.Run(() => { Task.WaitAll(tasks.ToArray()); this.Invoke((Action)(() => { // 首次填充,自动设好选定状态 if (is_empty) { SelectItem(this.SelectedID != null ? this.SelectedID : this.SelectedPII); if (string.IsNullOrEmpty(this.SelectedPII) == false && this.AutoCloseDialog) { this.button_OK_Click(this, new EventArgs()); } } })); //this.Invoke((Action)(() => //{ FillEntityInfo(); //})); if (this._mode == "auto_fix_eas") { this.Invoke((Action)(() => { AutoFixEas(); })); } }); } return(true); } catch (RemotingException ex) { strError = "UpdateChipList() 出现异常: " + ex.Message; goto ERROR1; } catch (Exception ex) { strError = "UpdateChipList() 出现异常: " + ExceptionUtil.GetDebugText(ex); goto ERROR1; } finally { EndRfidChannel(channel); } ERROR1: if (show_messageBox) { this.ShowMessageBox(strError); } else { this.ShowMessage(strError, "red", true); } return(false); } finally { Interlocked.Decrement(ref _inUpdate); } }