/// <summary> /// 删除消息 /// </summary> /// <param name="records"></param> /// <param name="strGroupName"></param> /// <returns> /// true 成功 /// false 失败 /// </returns> bool DeleteMessage(List <MessageRecord> records) { List <MessageRecord> delete_records = new List <MessageRecord>(); foreach (MessageRecord source in records) { MessageRecord record = new MessageRecord(); // 2016-6-20 jane 不需要传group参数 record.groups = dp2WeiXinService.C_Group_PatronNotity.Split(new char[] { ',' }); record.id = source.id; delete_records.Add(record); } string strError = ""; // CancellationToken cancel_token = new CancellationToken(); try { MessageConnection connection = this.Channels.GetConnectionTaskAsync( this.Url, dp2WeiXinService.C_ConnName_TraceMessage).Result; SetMessageRequest param = new SetMessageRequest("expire", "dontNotifyMe", delete_records);//records);这里应该用delete_records吧,用records好像也没错 CancellationToken cancel_token = new CancellationToken(); SetMessageResult result = connection.SetMessageTaskAsync(param, new TimeSpan(0, 1, 0), cancel_token).Result; if (result.Value == -1) { strError = result.ErrorInfo; goto ERROR1; } } catch (AggregateException ex) { strError = MessageConnection.GetExceptionText(ex); goto ERROR1; } catch (Exception ex) { strError = ex.Message; goto ERROR1; } return(true); ERROR1: dp2WeiXinService.Instance.WriteErrorLog("DeleteMessage() error : " + strError); //this.WriteLog("DeleteMessage() error : " + strError, dp2WeiXinService.C_LogLevel_1); return(false); }
// 从 dp2mserver 获得消息 // 每次最多获得 100 条 List <MessageRecord> GetMessage() { string strError = ""; CancellationToken cancel_token = new CancellationToken(); string id = Guid.NewGuid().ToString(); GetMessageRequest request = new GetMessageRequest(id, "", this.GroupName, // "" 表示默认群组 "", "", // strTimeRange, "", // sort "", //id "", // subject 0, 100); try { MessageConnection connection = this.Channels.GetConnectionTaskAsync( this.Url, "").Result; #if NO GetMessageResult result = connection.GetMessage( request, new TimeSpan(0, 1, 0), cancel_token).Result; #endif GetMessageResult result = connection.GetMessage( request, new TimeSpan(0, 1, 0), cancel_token); if (result.Value == -1) { goto ERROR1; } return(result.Results); } catch (AggregateException ex) { strError = MessageConnection.GetExceptionText(ex); goto ERROR1; } catch (Exception ex) { strError = ex.Message; goto ERROR1; } ERROR1: WriteErrorLog("GetMessage() error: " + strError); return(new List <MessageRecord>()); }
// return: // false 出错 // true 成功 bool SendMessage( MessageConnection connection, string strGroupName, string[] texts) { string strError = ""; List <MessageRecord> records = new List <MessageRecord>(); foreach (string text in texts) { MessageRecord record = new MessageRecord(); record.groups = strGroupName.Split(new char[] { ',' }); record.creator = ""; // 服务器会自己填写 record.data = text; record.format = "text"; record.type = "message"; record.thread = ""; record.expireTime = new DateTime(0); // 表示永远不失效 records.Add(record); } try { SetMessageRequest param = new SetMessageRequest("create", "", records); SetMessageResult result = connection.SetMessageTaskAsync(param, new CancellationToken()).Result; if (result.Value == -1) { strError = result.ErrorInfo; goto ERROR1; } return(true); } catch (AggregateException ex) { strError = MessageConnection.GetExceptionText(ex); goto ERROR1; } catch (Exception ex) { strError = ex.Message; goto ERROR1; } ERROR1: this.Invoke((Action)(() => MessageBox.Show(this, strError))); return(false); }
// 从 dp2mserver 获得消息 // 每次最多获得 100 条 List <MessageRecord> GetMessage() { string strError = ""; CancellationToken cancel_token = new CancellationToken(); string subjectCondition = ""; string id = Guid.NewGuid().ToString(); GetMessageRequest request = new GetMessageRequest(id, "", this.GroupName, // "" 表示默认群组 "", "", // strTimeRange, "publishTime|asc", //sortCondition 按发布时间正序排 "", subjectCondition, 0, 100); try { MessageConnection connection = this.Channels.GetConnectionTaskAsync( this.Url, dp2WeiXinService.C_ConnName_TraceMessage).Result; GetMessageResult result = connection.GetMessage(request, new TimeSpan(0, 1, 0), cancel_token); if (result.Value == -1) { goto ERROR1; } return(result.Results); } catch (AggregateException ex) { strError = MessageConnection.GetExceptionText(ex); goto ERROR1; } catch (Exception ex) { strError = ex.Message; goto ERROR1; } ERROR1: dp2WeiXinService.Instance.WriteErrorLog("GetMessage() error: " + strError); //this.WriteLog("GetMessage() error: " + strError,dp2WeiXinService.C_LogLevel_1); return(new List <MessageRecord>()); }
void DeleteMessage(List <MessageRecord> records, string strGroupName) { List <MessageRecord> delete_records = new List <MessageRecord>(); foreach (MessageRecord source in records) { MessageRecord record = new MessageRecord(); record.groups = strGroupName.Split(new char[] { ',' }); record.id = source.id; delete_records.Add(record); } string strError = ""; // CancellationToken cancel_token = new CancellationToken(); try { MessageConnection connection = this.Channels.GetConnectionTaskAsync( this.Url, "").Result; SetMessageRequest param = new SetMessageRequest("expire", "dontNotifyMe", records); SetMessageResult result = connection.SetMessageTaskAsync(param, new CancellationToken()).Result; if (result.Value == -1) { goto ERROR1; } } catch (AggregateException ex) { strError = MessageConnection.GetExceptionText(ex); goto ERROR1; } catch (Exception ex) { strError = ex.Message; goto ERROR1; } return; ERROR1: WriteErrorLog("DeleteMessage() error : " + strError); }
void DoGetPatronInfo() { string strError = ""; EnableControls(false); try { CancellationToken cancel_token = new CancellationToken(); string id = Guid.NewGuid().ToString(); SearchRequest request = new SearchRequest(id, "getPatronInfo", "", this.textBox_getReaderInfo_queryWord.Text, "", "", this.textBox_getReaderInfo_formatList.Text, 1); try { Task <MessageConnection> task = this._channels.GetConnectionAsync( this.textBox_config_messageServerUrl.Text, this.textBox_getReaderInfo_remoteUserName.Text); SearchResult result = task.ContinueWith <SearchResult>((antecendent) => { if (antecendent.IsFaulted == true) { throw antecendent.Exception; } MessageConnection connection = task.Result; return(connection.SearchAsync( this.textBox_getReaderInfo_remoteUserName.Text, request, new TimeSpan(0, 1, 0), cancel_token).Result); }).Result; this.Invoke(new Action(() => { if (result.ResultCount == 0) { this.textBox_getReaderInfo_results.Text = "没有找到"; } else { this.textBox_getReaderInfo_results.Text = ToString(result); } })); } catch (AggregateException ex) { strError = MessageConnection.GetExceptionText(ex); goto ERROR1; } catch (Exception ex) { strError = ex.Message; goto ERROR1; } return; } finally { EnableControls(true); } ERROR1: this.Invoke((Action)(() => MessageBox.Show(this, strError))); }
async void DoGetInfo() { string strError = ""; if (string.IsNullOrEmpty(this.comboBox_getInfo_method.Text) == true) { strError = "尚未指定方法"; goto ERROR1; } EnableControls(false); try { CancellationToken cancel_token = new CancellationToken(); string id = Guid.NewGuid().ToString(); SearchRequest request = new SearchRequest(id, this.comboBox_getInfo_method.Text, "", this.textBox_getInfo_queryWord.Text, "", "", "", this.textBox_getInfo_formatList.Text, 1, 0, -1); try { MessageConnection connection = await this._channels.GetConnectionAsync( this.textBox_config_messageServerUrl.Text, this.textBox_getInfo_remoteUserName.Text); SearchResult result = await connection.SearchAsync( this.textBox_getInfo_remoteUserName.Text, request, new TimeSpan(0, 1, 0), cancel_token); this.Invoke(new Action(() => { if (result.ResultCount == 0) { SetTextString(this.webBrowser1, "没有找到"); } else { SetTextString(this.webBrowser1, ToString(result)); } })); } catch (AggregateException ex) { strError = MessageConnection.GetExceptionText(ex); goto ERROR1; } catch (Exception ex) { strError = ex.Message; goto ERROR1; } return; } finally { EnableControls(true); } ERROR1: this.Invoke((Action)(() => MessageBox.Show(this, strError))); }
private long GetItemInfo(string dp2mserverUrl, string remoteUserName, string biblioPath, out string itemList, out string strError) { itemList = ""; strError = ""; CancellationToken cancel_token = new CancellationToken(); string id = "2-item"; // Guid.NewGuid().ToString(); SearchRequest request = new SearchRequest(id, null, // TODO "getItemInfo", "entity", biblioPath, "", "", "", "opac", 10, 0, -1); try { MessageConnection connection = this._channels.GetConnectionTaskAsync( dp2mserverUrl, remoteUserName).Result; SearchResult result = null; try { result = connection.SearchTaskAsync( remoteUserName, request, new TimeSpan(0, 1, 0), cancel_token).Result; } catch (Exception ex) { strError = "检索出错:[SearchAsync异常]" + ex.Message; return(-1); } if (result.ResultCount == -1) { strError = "检索出错:" + result.ErrorInfo; return(-1); } if (result.ResultCount == 0) { strError = "未命中"; return(0); } for (int i = 0; i < result.Records.Count; i++) { string xml = result.Records[i].Data; XmlDocument dom = new XmlDocument(); dom.LoadXml(xml); string strBarcode = DomUtil.GetElementText(dom.DocumentElement, "barcode"); string strRefID = DomUtil.GetElementText(dom.DocumentElement, "refID"); // 册条码号 string strViewBarcode = ""; if (string.IsNullOrEmpty(strBarcode) == false) { strViewBarcode = strBarcode; } else { strViewBarcode = "refID:" + strRefID; //"@refID:" } itemList += strViewBarcode + "\n"; } return(result.Records.Count); } catch (AggregateException ex) { strError = MessageConnection.GetExceptionText(ex); goto ERROR1; } catch (Exception ex) { strError = ex.Message; goto ERROR1; } ERROR1: return(-1); }
private int GetBiblioSummary(string dp2mserverUrl, string remoteUserName, string biblioPath, out string summary, out string strError) { summary = ""; strError = ""; CancellationToken cancel_token = new CancellationToken(); string id = Guid.NewGuid().ToString(); SearchRequest request = new SearchRequest(id, null, // TODO: "getBiblioInfo", "<全部>", biblioPath, "", "", "", "summary", 1, 0, -1); try { MessageConnection connection = this._channels.GetConnectionTaskAsync( dp2mserverUrl, remoteUserName).Result; SearchResult result = connection.SearchTaskAsync( remoteUserName, request, new TimeSpan(0, 1, 0), cancel_token).Result; if (result.ResultCount == -1) { strError = "检索出错:" + result.ErrorInfo; return(-1); } if (result.ResultCount == 0) { strError = "未命中"; return(0); } summary = result.Records[0].Data; return(1); } catch (AggregateException ex) { strError = MessageConnection.GetExceptionText(ex); goto ERROR1; } catch (Exception ex) { strError = ex.Message; goto ERROR1; } ERROR1: return(-1); }
/* * 微信公众号新图书馆dp2mserver账号 * 命名:weixin_图书馆英文或中文简称(如weixin_cctb,weixin_tjsyzx) * 权限:getPatronInfo,searchBiblio,searchPatron,bindPatron,getBiblioInfo,getBiblioSummary,getItemInfo,circulation,getUserInfo,getRes * 义务:空 * 单位:图书馆名称 * 群组:gn:_lib_bb * gn:_lib_book * gn:_lib_homePage * * === * 新图书馆安装dp2capo时创建的dp2mserver账号 * 命名:capo_图书馆英文或中文简称(如capo_cctb,capo_tjsyzx) * 权限:空 * 义务:getPatronInfo,searchBiblio,searchPatron,bindPatron,getBiblioInfo,getBiblioSummary,getItemInfo,circulation,getUserInfo,getRes * 单位:图书馆名称 * 群组:gn:_patronNotify|-n * */ async Task <bool> CreateCapoUser() { string strError = ""; EnableControls(false); try { using (MessageConnectionCollection _channels = new MessageConnectionCollection()) { _channels.Login += _channels_LoginSupervisor; MessageConnection connection = await _channels.GetConnectionAsyncLite( this.textBox_url.Text, "supervisor"); // 记忆用过的超级用户名和密码 this.ManagerUserName = connection.UserName; this.ManagerPassword = connection.Password; CancellationToken cancel_token = _cancel.Token; string id = Guid.NewGuid().ToString(); string strDepartment = InputDlg.GetInput( this, "图书馆名", "请指定图书馆名: ", "", this.Font); if (strDepartment == null) { return(false); } bool bEanbleWebCall = false; this.Invoke(new Action(() => { DialogResult temp_result = MessageBox.Show(this, "是否允许 webCall (通过 dp2Router 访问 dp2library)?", "安装 dp2Capo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (temp_result == System.Windows.Forms.DialogResult.Yes) { bEanbleWebCall = true; } })); List <User> users = new List <User>(); User user = new User(); user.userName = this.textBox_userName.Text; user.password = this.textBox_password.Text; user.rights = ""; // TODO: 看看除了 weixin_xxx 以外是否还有其他请求者需要许可 user.duty = ":weixinclient|" + MakeWeixinUserName(this.textBox_userName.Text) + ",getPatronInfo,searchBiblio,searchPatron,bindPatron,getBiblioInfo,getBiblioSummary,getItemInfo,circulation,getUserInfo,getRes,getSystemParameter"; if (bEanbleWebCall) { user.duty += ",webCall:router"; } user.groups = new string[] { "gn:_patronNotify|-n" }; user.department = strDepartment; user.binding = "ip:[current]"; user.comment = "dp2Capo 专用账号"; users.Add(user); MessageResult result = await connection.SetUsersAsyncLite("create", users, new TimeSpan(0, 1, 0), cancel_token); if (result.Value == -1) { strError = "创建用户 '" + this.textBox_userName.Text + "' 时出错: " + result.ErrorInfo; goto ERROR1; } return(true); } } catch (MessageException ex) { if (ex.ErrorCode == "Unauthorized") { strError = "以用户名 '" + ex.UserName + "' 登录时, 用户名或密码不正确"; this.ManagerUserName = ""; this.ManagerPassword = ""; goto ERROR1; } if (ex.ErrorCode == "HttpRequestException") { strError = "dp2MServer URL 不正确,或 dp2MServer 尚未启动"; goto ERROR1; } strError = ex.Message; goto ERROR1; } catch (AggregateException ex) { strError = MessageConnection.GetExceptionText(ex); goto ERROR1; } catch (Exception ex) { strError = ex.Message; goto ERROR1; } finally { EnableControls(true); } ERROR1: this.Invoke(new Action(() => { MessageBox.Show(this, strError); })); return(false); }
async Task <bool> DetectUser() { string strError = ""; EnableControls(false); try { using (MessageConnectionCollection _channels = new MessageConnectionCollection()) { _channels.Login += _channels_Login; MessageConnection connection = await _channels.GetConnectionAsyncLite( this.textBox_url.Text, ""); CancellationToken cancel_token = _cancel.Token; string id = Guid.NewGuid().ToString(); GetMessageRequest request = new GetMessageRequest(id, "", "gn:<default>", // "" 表示默认群组 "", "", "", "", "", 0, 1); GetMessageResult result = await connection.GetMessageAsyncLite( request, new TimeSpan(0, 1, 0), cancel_token); if (result.Value == -1) { strError = "检测用户时出错: " + result.ErrorInfo; goto ERROR1; } return(true); } } catch (MessageException ex) { if (ex.ErrorCode == "Unauthorized") { strError = "以用户名 '" + ex.UserName + "' 登录时, 用户名或密码不正确"; goto ERROR1; } if (ex.ErrorCode == "HttpRequestException") { strError = "dp2MServer URL 不正确,或 dp2MServer 尚未启动"; goto ERROR1; } strError = ex.Message; goto ERROR1; } catch (AggregateException ex) { strError = MessageConnection.GetExceptionText(ex); goto ERROR1; } catch (Exception ex) { strError = ex.Message; goto ERROR1; } finally { EnableControls(true); } ERROR1: this.Invoke(new Action(() => { MessageBox.Show(this, strError); })); return(false); }
int _inGetMessage = 0; // 防止因为 ConnectionStateChange 事件导致重入 // 装载已经存在的消息记录 async void DoLoadMessage(string strGroupName, string strTimeRange, bool bClearAll) { if (_inGetMessage > 0) { return; } _inGetMessage++; try { string strError = ""; if (Program.MainForm == null) { return; } // TODO: 如果当前 Connection 尚未连接,则要促使它连接,然后重试 load if (Program.MainForm.MessageHub.IsConnected == false) { if (_redoLoadMesssageCount < 5) { AddErrorLine("当前点对点连接尚未建立。重试操作中 ..."); Program.MainForm.MessageHub.Connect(); Thread.Sleep(5000); _redoLoadMesssageCount++; Task.Factory.StartNew(() => DoLoadMessage(strGroupName, strTimeRange, bClearAll)); return; } else { AddErrorLine("当前点对点连接尚未建立。停止重试。消息装载失败。"); _redoLoadMesssageCount = 0; // 以后再调用本函数,就重新计算重试次数 return; } } if (bClearAll) { this.Invoke((Action)(() => this.ClearHtml())); _lastMessage = null; } this.Invoke((Action)(() => this.HtmlWaiting(this.webBrowser1, "正在获取消息,请等待 ..."))); EnableControls(false); try { CancellationToken cancel_token = new CancellationToken(); string id = Guid.NewGuid().ToString(); GetMessageRequest request = new GetMessageRequest(id, strGroupName, // "<default>" 表示默认群组 "", strTimeRange, 0, -1); try { MessageResult result = await Program.MainForm.MessageHub.GetMessageAsync( request, FillMessage, new TimeSpan(0, 1, 0), cancel_token); #if NO this.Invoke(new Action(() => { SetTextString(this.webBrowser1, ToString(result)); })); #endif if (result.Value == -1) { //strError = result.ErrorInfo; //goto ERROR1; this.AddErrorLine(result.ErrorInfo); } } catch (AggregateException ex) { strError = MessageConnection.GetExceptionText(ex); goto ERROR1; } catch (Exception ex) { strError = ex.Message; goto ERROR1; } return; } finally { EnableControls(true); this.Invoke((Action)(() => this.HtmlWaiting(this.webBrowser1, ""))); } ERROR1: this.Invoke((Action)(() => MessageBox.Show(this, strError))); } finally { _inGetMessage--; } }
async Task <bool> CreateRouterUser() { string strError = ""; EnableControls(false); try { using (MessageConnectionCollection _channels = new MessageConnectionCollection()) { _channels.Login += _channels_LoginSupervisor; MessageConnection connection = await _channels.GetConnectionAsyncLite( this.textBox_url.Text, "supervisor"); // 记忆用过的超级用户名和密码 this.ManagerUserName = connection.UserName; this.ManagerPassword = connection.Password; CancellationToken cancel_token = _cancel.Token; string id = Guid.NewGuid().ToString(); List <User> users = new List <User>(); User user = new User(); user.userName = this.textBox_userName.Text; user.password = this.textBox_password.Text; user.rights = "webCall"; user.duty = ""; user.groups = null; user.comment = "dp2Router 专用账号"; user.binding = "ip:[current]"; users.Add(user); MessageResult result = await connection.SetUsersAsyncLite("create", users, new TimeSpan(0, 1, 0), cancel_token); if (result.Value == -1) { strError = "创建用户 '" + this.textBox_userName.Text + "' 时出错: " + result.ErrorInfo; goto ERROR1; } return(true); } } catch (MessageException ex) { if (ex.ErrorCode == "Unauthorized") { strError = "以用户名 '" + ex.UserName + "' 登录时, 用户名或密码不正确"; this.ManagerUserName = ""; this.ManagerPassword = ""; goto ERROR1; } if (ex.ErrorCode == "HttpRequestException") { strError = "dp2MServer URL 不正确,或 dp2MServer 尚未启动"; goto ERROR1; } strError = ex.Message; goto ERROR1; } catch (AggregateException ex) { strError = MessageConnection.GetExceptionText(ex); goto ERROR1; } catch (Exception ex) { strError = ex.Message; goto ERROR1; } finally { EnableControls(true); } ERROR1: this.Invoke(new Action(() => { MessageBox.Show(this, strError); })); return(false); }
// 继续填充余下的命中记录 async Task <NormalResult> FillRestRecordsAsync( P2PConnection connection, string remoteUserName, string resultsetName, int start, CancellationToken token) { try { // string remoteUserName = this.comboBox_query_shelfAccount.Text; while (token.IsCancellationRequested == false) { SearchRequest request = new SearchRequest(Guid.NewGuid().ToString(), null, // loginInfo, "searchHistory", // "searchBiblio", "", // "dbNameList", "!getResult", "", // this.comboBox_query_from.Text, "", // this.comboBox_query_matchStyle.Text, resultsetName, "json", -1, start, 10); var result = await connection.SearchAsyncLite(remoteUserName, request, TimeSpan.FromSeconds(60), token); if (result.ResultCount == -1) { return(new NormalResult { Value = -1, ErrorInfo = result.ErrorInfo }); } if (result.ResultCount == 0) { // strError = "没有命中"; return(new NormalResult { Value = -1, ErrorInfo = "检索没有命中结果。无法获得结果" }); } if (start >= result.ResultCount) { break; } FillRecords(result.Records, false, token); start += result.Records.Count; if (start >= result.ResultCount) { break; } } return(new NormalResult()); } catch (AggregateException ex) { return(new NormalResult { Value = -1, ErrorInfo = MessageConnection.GetExceptionText(ex) }); } catch (Exception ex) { return(new NormalResult { Value = -1, ErrorInfo = ex.Message }); } }
private async void button_search_Click(object sender, EventArgs e) { string strError = ""; _cancelSearch = new CancellationTokenSource(); CancellationToken token = _cancelSearch.Token; EnableSearchButtons(false); try { this.listView_records.Items.Clear(); var get_result = await ConnectionPool.OpenConnectionAsync(this.comboBox_query_myAccount.Text); if (get_result.Value == -1) { strError = get_result.ErrorInfo; goto ERROR1; } var connection = get_result.Connection; string resultsetName = "default"; string remoteUserName = this.comboBox_query_shelfAccount.Text; SearchRequest request = new SearchRequest(Guid.NewGuid().ToString(), null, // loginInfo, "searchHistory", // "searchBiblio", "dbNameList", this.textBox_query_word.Text, this.comboBox_query_from.Text, this.comboBox_query_matchStyle.Text, resultsetName, "json", -1, 0, 10); var result = await connection.SearchAsyncLite(remoteUserName, request, TimeSpan.FromSeconds(60), token); if (result.ResultCount == -1) { strError = result.ErrorInfo; goto ERROR1; } if (result.ResultCount == 0) { strError = "没有命中"; goto ERROR1; } FillRecords(result.Records, true, token); if (result.Records.Count < result.ResultCount) { var fill_result = await FillRestRecordsAsync( connection, remoteUserName, resultsetName, result.Records.Count, token); } return; } catch (AggregateException ex) { strError = MessageConnection.GetExceptionText(ex); goto ERROR1; } catch (Exception ex) { strError = ExceptionUtil.GetDebugText(ex); // ex.Message; goto ERROR1; } finally { EnableSearchButtons(true); } ERROR1: MessageBox.Show(this, strError); }