119 lines
5.0 KiB
C#
119 lines
5.0 KiB
C#
// Copyright (c) 2017 schick Informatik
|
|
// Description: Query NSW via HIS-Nord for free / taken message classes and visit id
|
|
|
|
using System;
|
|
using System.Collections.Specialized;
|
|
using System.Text;
|
|
using System.Net;
|
|
using System.Web;
|
|
|
|
using log4net;
|
|
using bsmd.database;
|
|
|
|
namespace bsmd.status
|
|
{
|
|
public class Status
|
|
{
|
|
|
|
private static readonly ILog _log = LogManager.GetLogger(typeof(bsmd.status.Status));
|
|
private readonly MessageCore _queryCore;
|
|
|
|
public Status(MessageCore core2Query) { _queryCore = core2Query; }
|
|
|
|
public void PerformQuery()
|
|
{
|
|
if (this._queryCore == null) return;
|
|
|
|
using (WebClient client = new WebClient())
|
|
{
|
|
|
|
try
|
|
{
|
|
|
|
string encodedLogin = HttpUtility.HtmlEncode(Properties.Settings.Default.login);
|
|
string encodedPW = HttpUtility.HtmlEncode(Properties.Settings.Default.password);
|
|
|
|
NameValueCollection postCollection = new NameValueCollection()
|
|
{
|
|
// ich glaube ein HTML Encoding braucht man hier nicht?
|
|
{ "login", encodedLogin },
|
|
{ "password", encodedPW },
|
|
{ "visitIdTransitId", _queryCore.IsTransit ? _queryCore.TransitId : _queryCore.VisitId },
|
|
{ "format", Properties.Settings.Default.format }
|
|
};
|
|
|
|
|
|
string uploadString = string.Format("login={0}&password={1}&visitIdTransitId={2}&format=xml", encodedLogin, encodedPW, _queryCore.IsTransit ? _queryCore.TransitId : _queryCore.VisitId);
|
|
_log.InfoFormat("Upload string: {0}", encodedLogin, encodedPW);
|
|
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
|
|
|
|
string resultString = client.UploadString(new Uri(Properties.Settings.Default.url), uploadString);
|
|
|
|
// Do some log output
|
|
|
|
//string resultString = Encoding.UTF8.GetString(resultData);
|
|
|
|
_log.InfoFormat("Status query result: {0}", resultString);
|
|
|
|
dataset result = null;
|
|
try
|
|
{
|
|
result = dataset.ReadStatus(resultString);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_log.ErrorFormat("Error parsing status request result: {0}", ex.ToString());
|
|
}
|
|
|
|
if (result != null)
|
|
{
|
|
// Update database with result values
|
|
|
|
if (result.NswResponse != null)
|
|
{
|
|
// three-way bool!
|
|
if (result.NswResponse.Cancelled != null)
|
|
_queryCore.Cancelled = result.NswResponse.Equals("Y");
|
|
else
|
|
_queryCore.Cancelled = null;
|
|
|
|
if (result.NswResponse.VisitIdOrTransitIdCancellable != null)
|
|
_queryCore.VisitIdOrTransitIdCancellable = result.NswResponse.VisitIdOrTransitIdCancellable.Equals("Y");
|
|
else
|
|
_queryCore.VisitIdOrTransitIdCancellable = null;
|
|
|
|
_queryCore.BlockedNotificationClasses = result.NswResponse.BlockedNotificationClasses;
|
|
_queryCore.OwnNotificationClasses = result.NswResponse.OwnNotificationClasses;
|
|
_queryCore.FreeNotificationClasses = result.NswResponse.FreeNotificationClasses;
|
|
_queryCore.StatusCheckErrorCode = result.NswResponse.ErrorCode;
|
|
string errorText = result.NswResponse.ErrorMessage;
|
|
if ((errorText != null) && (errorText.Length > 512)) errorText = errorText.Substring(0, 512);
|
|
_queryCore.StatusCheckErrorMessage = errorText;
|
|
|
|
}
|
|
else
|
|
{
|
|
if(result.LoginError != null)
|
|
{
|
|
_log.ErrorFormat("Login error: {0}", result.LoginError);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_log.Error("parsing result status failed");
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_log.ErrorFormat("Error uploading status request values: {0}", e.ToString());
|
|
_queryCore.StatusCheckErrorMessage = string.Format("Exception trying to reach HIS-Nord service: {0}", e.Message);
|
|
}
|
|
|
|
DBManager.Instance.Save(_queryCore);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|