internal IResponse CreateResponse(byte[] frame) { byte functionCode = frame[1]; IResponse response = null; // IModbusMessage response; // check for slave exception response else create message from frame if (functionCode > Modbus.ExceptionOffset) { response = new ExceptionResponse(frame); } else if (functionCode == 3) { response = new Response3(frame); } else if (functionCode == 6) { response = new Response6(frame); } else if (functionCode == 0x14) { response = new Response14(frame); } //if (CheckFrame && !ChecksumsMatch(response, frame)) //{ // // string msg = $"Checksums failed to match {string.Join(", ", response.MessageFrame)} != {string.Join(", ", frame)}"; // string msg = $"Checksums failed to match"; // // Logger.Warning(msg); // throw new IOException(msg); //} return(response); }
public async Task MapAsync(Response1 response1, Response3 response3) { await Mapper <Response1, Response3> .MapAsync(response1, response3) .With(r1 => r1.ConsumerID, (r3, consumerId) => r3.IDNumber = consumerId) //Calculated field .With(r1 => r1.AvgNoOfPurchasesPerMonth * r1.PeriodInMonths, (r3, total) => r3.TotalPurchases = total) //Using Switch .Switch(r1 => r1.PeriodInMonths) .Case(periodInMonths => periodInMonths > 0 && periodInMonths <= 3, (r3, periodInMonths) => r3.Period = "Quarter") .Case(periodInMonths => periodInMonths > 3 && periodInMonths <= 6, (r3, periodInMonths) => r3.Period = "Half") .Case(periodInMonths => periodInMonths > 6 && periodInMonths <= 9, (r3, periodInMonths) => r3.Period = "Three Quarter") .Case(periodInMonths => periodInMonths > 9 && periodInMonths <= 12, (r3, periodInMonths) => r3.Period = "Year") .Else((r3, periodInMonths) => r3.Period = "Unknown") .End() //Switch mapping with case mappings .Switch(r1 => r1.InsuranceType) .CaseMap(insuranceType => insuranceType == InsuranceType.MutualFund, //Mapping source InsuranceMutualFund to destination InsuranceInfo using Map //You can debug using using Debugger.Break() mapper => { Debugger.Break(); mapper.With(r1 => r1.InsuranceMutualFund, (r3, insuranceMutualFund) => r3.InsuranceInfo = insuranceMutualFund, Map); } ) .CaseMap(insuranceType => insuranceType == InsuranceType.Superannuation, //Mapping source InsuranceSuperannuation to destination InsuranceInfo using Map mapper => mapper.With(r1 => r1.InsuranceSuperannuation, (r3, insuranceSuperannuation) => r3.InsuranceInfo = insuranceSuperannuation, Map) ) //Mapping source InsuranceEmployment to destination InsuranceInfo using Map by default .ElseMap(mapper => mapper.With(r1 => r1.InsuranceEmployment, (r3, insuranceEmployment) => r3.InsuranceInfo = insuranceEmployment, Map)) .End() //Mapping List .With(r1 => r1.BankingInfos, (r3, bankingInfos) => r3.BankingInformation = bankingInfos, Map) //Mapping Dictionary .With(r1 => r1.EmploymentCodes, (r3, employmentCodes) => r3.LabourCodes = employmentCodes, Map, val => val) //Using another map - When Details1 is not null then map Details1 to Details3 using another map .When(r1 => r1.Details != null, mapper => mapper.With(r1 => r1.Details, (r3, details3) => r3.Details = details3, Map)) //Using another map - When Fund1 is not null then map Fund1 to Fund3 using another map .When(r1 => r1.MutualFund != null, mapper => mapper.With(r1 => r1.MutualFund, (r3, fund3) => r3.Fund = fund3, Map)) .Exec(); }
public async Task MyMapper_Async_Test() { var dob = DateTime.Now; IResponseMapper mapper = new ResponseMapper(); Response1 source = new Response1 { ConsumerID = 123, Name = "XYZ", AvgNoOfPurchasesPerMonth = 10, PeriodInMonths = 12, Details = new Details1() { DOB = dob, IsDisabled = true }, MutualFund = new Fund1 { BankIdNo = 10, Name = "XYZ", FundId = 1, Address = new Address1 { StreetNo = "123N", State = new State1 { Name = "Victoria", Abbr = "VIC" }, Country = new Country1() { Name = "Australia", Abbr = "AU" } }, FundKeys = new Dictionary <string, string>() { { "ABN", "123456" }, { "TFN", "9876543" } } }, InsuranceMutualFund = new InsuranceMutualFund { MutualFundNumber = "123", TaxNo = "456" }, InsuranceSuperannuation = new InsuranceSuperannuation { SuperannuationNumber = "789", TaxFileNumber = "456" }, InsuranceEmployment = new InsuranceEmployment { EmploymentNumber = "678", TaxNumber = "456" }, InsuranceType = Entities.InsuranceType.Employment, BankingInfos = new List <BankingInfo1>() { new BankingInfo1 { AccountName = "ABC", AccountNo = "1" }, new BankingInfo1 { AccountName = "XYZ", AccountNo = "2" } }, EmploymentCodes = new Dictionary <EmploymentCode, string>() { { EmploymentCode.E, "Engineer" } } }; //Mapping source to new destination var destination = await mapper.MapAsync(source); Assert.IsTrue(destination.IDNumber == source.ConsumerID); Assert.IsTrue(destination.Name == source.Name); Assert.IsTrue(destination.TotalPurchases == source.AvgNoOfPurchasesPerMonth * source.PeriodInMonths); Assert.IsTrue(destination.Details.DateOfBirth == source.Details.DOB); Assert.IsTrue(destination.Details.IsHandicapped == source.Details.IsDisabled); Assert.IsTrue(source.PeriodInMonths == 12); Assert.IsTrue(destination.Period == "Year"); Assert.IsTrue(source.InsuranceType == InsuranceType.Employment); Assert.IsTrue(destination.InsuranceInfo.MembershipNo == source.InsuranceEmployment.EmploymentNumber); Assert.IsTrue(destination.InsuranceInfo.TaxNumber == source.InsuranceEmployment.TaxNumber); Assert.IsTrue(destination.Fund.BankIdNo == source.MutualFund.BankIdNo); Assert.IsTrue(destination.Fund.FundId == source.MutualFund.FundId); Assert.IsTrue(destination.Fund.Name == source.MutualFund.Name); Assert.IsTrue(destination.Fund.Address.StreetNo == source.MutualFund.Address.StreetNo); Assert.IsTrue(destination.Fund.Address.State.Name == source.MutualFund.Address.State.Name); Assert.IsTrue(destination.Fund.Address.State.Abbr == source.MutualFund.Address.State.Abbr); Assert.IsTrue(destination.Fund.Address.Country.Name == source.MutualFund.Address.Country.Name); Assert.IsTrue(destination.Fund.Address.Country.Abbr == source.MutualFund.Address.Country.Abbr); Assert.IsTrue(destination.Fund.FundKeys["ABN"] == source.MutualFund.FundKeys["ABN"]); Assert.IsTrue(destination.Fund.FundKeys["TFN"] == source.MutualFund.FundKeys["TFN"]); Assert.IsTrue(destination.BankingInformation.Count == source.BankingInfos.Count); Assert.IsTrue(destination.BankingInformation[0].AccountName == source.BankingInfos[0].AccountName); Assert.IsTrue(destination.BankingInformation[0].AccountNumber == source.BankingInfos[0].AccountNo); Assert.IsTrue(destination.BankingInformation[1].AccountName == source.BankingInfos[1].AccountName); Assert.IsTrue(destination.BankingInformation[1].AccountNumber == source.BankingInfos[1].AccountNo); Assert.IsTrue(destination.LabourCodes.Count == source.EmploymentCodes.Count); Assert.IsTrue(destination.LabourCodes[LabourCode.ENG] == source.EmploymentCodes[EmploymentCode.E]); //Mapping source to existing destination Response3 destination_1 = new Response3() { Existing = "Mapping to existing destination object" }; await mapper.MapAsync(source, destination_1); Assert.IsTrue(destination.IDNumber == source.ConsumerID); Assert.IsTrue(destination.Name == source.Name); Assert.IsTrue(destination_1.Existing == "Mapping to existing destination object"); Assert.IsTrue(destination.TotalPurchases == source.AvgNoOfPurchasesPerMonth * source.PeriodInMonths); Assert.IsTrue(destination.Details.DateOfBirth == source.Details.DOB); Assert.IsTrue(destination.Details.IsHandicapped == source.Details.IsDisabled); Assert.IsTrue(source.PeriodInMonths == 12); Assert.IsTrue(destination.Period == "Year"); Assert.IsTrue(source.InsuranceType == InsuranceType.Employment); Assert.IsTrue(destination.InsuranceInfo.MembershipNo == source.InsuranceEmployment.EmploymentNumber); Assert.IsTrue(destination.InsuranceInfo.TaxNumber == source.InsuranceEmployment.TaxNumber); Assert.IsTrue(destination.Fund.BankIdNo == source.MutualFund.BankIdNo); Assert.IsTrue(destination.Fund.FundId == source.MutualFund.FundId); Assert.IsTrue(destination.Fund.Name == source.MutualFund.Name); Assert.IsTrue(destination.Fund.Address.StreetNo == source.MutualFund.Address.StreetNo); Assert.IsTrue(destination.Fund.Address.State.Name == source.MutualFund.Address.State.Name); Assert.IsTrue(destination.Fund.Address.State.Abbr == source.MutualFund.Address.State.Abbr); Assert.IsTrue(destination.Fund.Address.Country.Name == source.MutualFund.Address.Country.Name); Assert.IsTrue(destination.Fund.Address.Country.Abbr == source.MutualFund.Address.Country.Abbr); Assert.IsTrue(destination.Fund.FundKeys["ABN"] == source.MutualFund.FundKeys["ABN"]); Assert.IsTrue(destination.Fund.FundKeys["TFN"] == source.MutualFund.FundKeys["TFN"]); Assert.IsTrue(destination.BankingInformation.Count == source.BankingInfos.Count); Assert.IsTrue(destination.BankingInformation[0].AccountName == source.BankingInfos[0].AccountName); Assert.IsTrue(destination.BankingInformation[0].AccountNumber == source.BankingInfos[0].AccountNo); Assert.IsTrue(destination.BankingInformation[1].AccountName == source.BankingInfos[1].AccountName); Assert.IsTrue(destination.BankingInformation[1].AccountNumber == source.BankingInfos[1].AccountNo); Assert.IsTrue(destination.LabourCodes.Count == source.EmploymentCodes.Count); Assert.IsTrue(destination.LabourCodes[LabourCode.ENG] == source.EmploymentCodes[EmploymentCode.E]); //Mapping source to existing destination (this) Response4 source_2 = new Response4() { IDNumber = "XYZ", AccountNumber = "123" }; Response5 destination_2 = new Response5(); await destination_2.MapAsync(source_2); Assert.IsTrue(destination_2.IDNumber == source_2.IDNumber); Assert.IsTrue(destination_2.AccNo == source_2.AccountNumber); }
public ActionResult Index() { HttpWebRequest WR = WebRequest.CreateHttp("https://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1"); WR.UserAgent = ".NET Framework Test Client"; HttpWebResponse Response3; try { Response3 = (HttpWebResponse)WR.GetResponse(); } catch (WebException e) { ViewBag.Error = "Exception"; ViewBag.ErrorDescription = e.Message; return(View()); } if (Response3.StatusCode != HttpStatusCode.OK) { ViewBag.Error = Response3.StatusCode; ViewBag.ErrorDescription = Response3.StatusDescription; return(View()); } StreamReader reader = new StreamReader(Response3.GetResponseStream()); string CardData = reader.ReadToEnd(); try { JObject JsonData = JObject.Parse(CardData); ViewBag.DeckId = JsonData["deck_id"]; ViewBag.Remain = JsonData["remaining"]; //Session["x"] = ViewBag.DeckId; //string sess = (string)Session["x"]; //id = (string)JsonData["deck_id"]; //var domain = "https://deckofcardsapi.com/api/deck/"; //string id = ViewBag.DeckId; //var end = "/draw /? count = 5"; //string full = $"{domain}{id}{end}"; //$"{domain}{ViewBag.DeckId}{end}" //HttpCookie myCookie = new HttpCookie("url"); id = ViewBag.DeckId; url = $"https://deckofcardsapi.com/api/deck/{ViewBag.DeckId}/draw/?count=5"; Response.Cookies["Link"].Value = url; Response.Cookies["DeckIdCookie"].Value = ViewBag.DeckId; Response.Cookies["Link"].Expires = DateTime.Now.AddMinutes(2); Response.Cookies["DeckIdCookie"].Expires = DateTime.Now.AddMinutes(2); //myCookie.Expires = DateTime.Now.AddHours(12); //HttpResponse.AppendCookie(myCookie); var input = Request.Cookies["Link"] != null ? Request.Cookies["Link"].Value : String.Empty; //if for some reason it starts throwing null exception reference again, just paste in url above and replace input and it will work again HttpWebRequest WR2 = WebRequest.CreateHttp($"{input}"); WR2.UserAgent = ".NET Framework Test Client"; HttpWebResponse Response2; try { Response2 = (HttpWebResponse)WR2.GetResponse(); } catch (WebException ex) { ViewBag.Error2 = "Exception"; ViewBag.ErrorDescription2 = ex.Message; return(View()); } if (Response2.StatusCode != HttpStatusCode.OK) { ViewBag.Error2 = Response2.StatusCode; ViewBag.ErrorDescription2 = Response2.StatusDescription; return(View()); } StreamReader reader2 = new StreamReader(Response2.GetResponseStream()); string Card2Data = reader2.ReadToEnd(); try { JObject Json2Data = JObject.Parse(Card2Data); ViewBag.Cards = /*(JObject)*/ Json2Data["cards"]; //ViewBag.Cards1 = (JObject)Json2Data["cards"][0]; //ViewBag.Cards2 = (JObject)Json2Data["cards"][1]; //ViewBag.Cards3 = (JObject)Json2Data["cards"][2]; //ViewBag.Cards4 = (JObject)Json2Data["cards"][3]; //ViewBag.Cards5 = (JObject)Json2Data["cards"][4]; //ViewBag.Images1 = JsonData["cards"][0]["image"]; //ViewBag.Images2 = JsonData["cards"][1]["image"]; //ViewBag.Images3 = JsonData["cards"][2]["image"]; //ViewBag.Images4 = JsonData["cards"][3]["image"]; //ViewBag.Images5 = JsonData["cards"][4]["image"]; //ViewBag.Value1 = JsonData["cards"][0]["value"]; //ViewBag.Value2 = JsonData["cards"][1]["value"]; //ViewBag.Value3 = JsonData["cards"][2]["value"]; //ViewBag.Value4 = JsonData["cards"][3]["value"]; //ViewBag.Value5 = JsonData["cards"][4]["value"]; //ViewBag.Suit1 = JsonData["cards"][0]["suit"]; //ViewBag.Suit2 = JsonData["cards"][1]["suit"]; //ViewBag.Suit3 = JsonData["cards"][2]["suit"]; //ViewBag.Suit4 = JsonData["cards"][3]["suit"]; //ViewBag.Suit5 = JsonData["cards"][4]["suit"]; } catch (Exception ex) { ViewBag.Error2 = "JSON Issue"; ViewBag.ErrorDescription2 = ex.Message; return(View()); } } catch (Exception e) { ViewBag.Error = "JSON Issue"; ViewBag.ErrorDescription = e.Message; return(View()); } return(View()); }