public static QrTestResult Create( string pQrCode, TypeOfSign pSignType, bool pStructureOk, bool pUriOk, bool pSpacesOk, bool pDistrictOk, OnlineStatus pIsOnline, bool pHasCoordinates, bool pIsDuplicate, string pMessage, bool pHasIssue) { var mRV = new QrTestResult(); mRV.QrCode = pQrCode; mRV.SignType = pSignType; mRV.UriOk = pUriOk; mRV.StructureOk = pStructureOk; mRV.DistrictOk = pDistrictOk; mRV.SpacesOk = pSpacesOk; mRV.IsOnline = pIsOnline; mRV.HasCoordinates = pHasCoordinates; mRV.IsDuplicate = pIsDuplicate; mRV.HasIssue = pHasIssue; mRV.Messages.Add(pMessage); return(mRV); }
public static QrTestResult TestQRCode(this string qrCode, string districtsShapefile, bool checkOnline = true, double xCoord = double.NaN, double yCoord = double.NaN, bool checkForDuplicates = false) { var qrTestResult = new QrTestResult(); if (QRCodes == null && checkForDuplicates == true) { QRCodes = new List <string>(); } if (String.IsNullOrEmpty(qrCode)) { qrTestResult.AddMessage("No QR-code to test: " + qrCode); return(qrTestResult); } if (Districts == null) { qrTestResult.AddMessage("Loading districts from Shapefile (once per session): " + districtsShapefile); Districts = Shapefile.OpenFile(districtsShapefile); } string mBaseURL, mMunicipalityAbbreviation, mAreaAbbreviation = "", mRoadID, mSignType, mAUSNumber; qrTestResult.AddMessage("Decoded QR code content: " + qrCode); qrTestResult.QrCode = qrCode; var mRegex = Regex.Match(qrCode, @"http://([a-z\.]*)/([A-Za-z]*)/([A-Za-z]*)/([0-9]*)/([a-zA-Z0-9]*)/([0-9]*)$"); if (mRegex.Groups.Count != 7) { qrTestResult.AddMessage("Error: QR code has wrong structure for ANS sign: " + (mRegex.Groups.Count - 1) + " parts, should be 6"); qrTestResult.StructureOk = false; qrTestResult.HasIssue = true; qrTestResult.SignType = QrTestResult.TypeOfSign.Unknown; } else { qrTestResult.AddMessage("Info: QR code has correct structure for ANS sign"); qrTestResult.StructureOk = true; qrTestResult.SignType = QrTestResult.TypeOfSign.AddressUnitNumber; mBaseURL = mRegex.Groups[1].ToString(); mMunicipalityAbbreviation = mRegex.Groups[2].ToString(); mAreaAbbreviation = mRegex.Groups[3].ToString(); mRoadID = mRegex.Groups[4].ToString(); mSignType = mRegex.Groups[5].ToString(); mAUSNumber = mRegex.Groups[6].ToString(); } // Check for existence of QR-code in processed batch if (checkForDuplicates == true && QRCodes.Contains(qrCode.Trim().ToLower())) { qrTestResult.AddMessage("QR-code already exists (duplicate): " + qrCode); qrTestResult.IsDuplicate = true; qrTestResult.HasIssue = true; } else { QRCodes.Add(qrCode.Trim().ToLower()); qrTestResult.IsDuplicate = false; } // Check for correct start of QR code if (!qrCode.StartsWith("http://myabudhabi.net/adm")) { qrTestResult.AddMessage("Error: base URL or municipality"); qrTestResult.UriOk = false; qrTestResult.HasIssue = true; } else { qrTestResult.AddMessage("Info: URL part is ok"); qrTestResult.UriOk = true; } // Check if codes contains spaces if (qrCode.Contains(" ")) { qrTestResult.AddMessage("Error: QR Code contains spaces"); qrTestResult.SpacesOk = false; qrTestResult.HasIssue = true; } else { qrTestResult.AddMessage("Info: Contains no spaces"); qrTestResult.SpacesOk = true; } // Check if code exists on myabudhabi.net MyAbuDhabiNetResponse mResponse = null; if (checkOnline) { if (HasInternetConnection()) { mResponse = qrCode.DownloadLandingPage().Data as MyAbuDhabiNetResponse; if (mResponse == null || mResponse.status != "success") { qrTestResult.AddMessage("Notice: The QR-code does not exist on myabudhabi.net"); qrTestResult.IsOnline = QrTestResult.OnlineStatus.Unavailable; qrTestResult.HasIssue = true; } else { qrTestResult.AddMessage(String.Format("Info: Exists on myabudhabi.net (Longitude: {0}/Latitude: {1}){2}", mResponse.x, mResponse.y, Environment.NewLine)); qrTestResult.IsOnline = QrTestResult.OnlineStatus.Available; } } else { qrTestResult.AddMessage("Either the computer is not connected to the Internet or there is a problem with the connection"); qrTestResult.IsOnline = QrTestResult.OnlineStatus.Unknown; } } else { qrTestResult.IsOnline = QrTestResult.OnlineStatus.Unknown; } // Check if inside district Point mPointGeom = null; if (xCoord != double.NaN || yCoord != double.NaN) { mPointGeom = new Point(xCoord, yCoord); qrTestResult.HasCoordinates = true; } else if (mResponse != null) { double mX, mY; if (!double.TryParse(mResponse.x, out mX) || !double.TryParse(mResponse.y, out mY)) { qrTestResult.AddMessage("Notice: Coordinate values could not be parsed to a number or records on myabudhabi.net does not contain coordinates"); qrTestResult.HasCoordinates = false; qrTestResult.HasIssue = true; } else { var mPoints = new double[] { mX, mY }; ProjectionInfo pSRSFrom = KnownCoordinateSystems.Geographic.World.WGS1984; ProjectionInfo pSRSTo = KnownCoordinateSystems.Projected.World.WebMercator; Reproject.ReprojectPoints(mPoints, new double[] { 0 }, pSRSFrom, pSRSTo, 0, 1); mPointGeom = new Point(mPoints[0], mPoints[1]); qrTestResult.HasCoordinates = true; } } if (mPointGeom != null) { foreach (Feature mFeature in Districts.Features) { if (mFeature.Contains(mPointGeom)) { if (mFeature.DataRow["DISTRICTABB"].ToString().ToLower().Trim() == mAreaAbbreviation.ToLower()) { qrTestResult.AddMessage(String.Format("Info: District abbreviation is correct according to coordinates: {0} ({1}){2}", mFeature.DataRow["NAMELATIN"].ToString(), mFeature.DataRow["DISTRICTABB"].ToString().ToLower(), Environment.NewLine)); qrTestResult.DistrictOk = true; } else { qrTestResult.AddMessage(String.Format("Error: District abbreviation is wrong according to coordinates; it is: {1} but should be {0}{2}", mFeature.DataRow["DISTRICTABB"].ToString().ToLower(), mAreaAbbreviation, Environment.NewLine)); qrTestResult.DistrictOk = false; qrTestResult.HasIssue = true; } break; } } } return(qrTestResult); }