using Microsoft.AspNetCore.Mvc; using Modbus.Net; using Modbus.Net.Modbus; using System.Diagnostics; using TripleAdd.Models; namespace TripleAdd.Controllers { public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILogger logger) { _logger = logger; } private static BaseUtility utility; private static BaseMachine machine; public ActionResult Index() { return RedirectToAction("Machine"); } public async Task Machine() { var data = await GetMachineEnter(); ViewBag.Method = "SetMachine"; return View("Index", data); } public async Task Utility() { var data = await GetUtilityEnter(); ViewBag.Method = "SetUtility"; return View("Index", data); } private async Task GetUtilityEnter() { if (utility == null) { utility = new ModbusUtility(ModbusType.Tcp, "192.168.0.172", 2, 0); utility.AddressTranslator = new AddressTranslatorModbus(); } object[] getNum = await utility.GetDatasAsync("4X 1", new KeyValuePair(typeof(ushort), 4)); ushort[] getNumUshorts = BigEndianValueHelper.Instance.ObjectArrayToDestinationArray(getNum); return SetValue(getNumUshorts); } private async Task GetMachineEnter() { if (machine == null) { machine = new ModbusMachine("1", ModbusType.Tcp, "192.168.3.10", new List() { new AddressUnit() {Id = "1", Area = "4X", Address = 1, CommunicationTag = "Add1", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, new AddressUnit() {Id = "2", Area = "4X", Address = 2, CommunicationTag = "Add2", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, new AddressUnit() {Id = "3", Area = "4X", Address = 3, CommunicationTag = "Add3", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, new AddressUnit() {Id = "4", Area = "4X", Address = 4, CommunicationTag = "Ans", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, }, 2, 0); machine.AddressCombiner = new AddressCombinerContinus(machine.AddressTranslator, 100000); machine.AddressCombinerSet = new AddressCombinerContinus(machine.AddressTranslator, 100000); } var resultFormat = (await machine.GetDatasAsync(MachineDataType.CommunicationTag)).MapGetValuesToSetValues(); return SetValue(new ushort[4] { (ushort)resultFormat["Add1"], (ushort)resultFormat["Add2"], (ushort)resultFormat["Add3"], (ushort)resultFormat["Ans"] }); } private TripleAddViewModel SetValue(ushort[] getNum) { return new TripleAddViewModel() { Add1 = getNum[0], Add2 = getNum[1], Add3 = getNum[2], Ans = getNum[3], }; } [HttpPost] public async Task SetUtility(TripleAddViewModel model) { ushort add1 = model.Add1, add2 = model.Add2, add3 = model.Add3; var ans = await await utility.SetDatasAsync("4X 1", new object[] { add1, add2, add3 }).ContinueWith(async p => await GetUtilityEnter()); return Json(ans); } [HttpPost] public async Task SetMachine(TripleAddViewModel model) { ushort add1 = model.Add1, add2 = model.Add2, add3 = model.Add3; var setDic = new Dictionary { { "Add1", add1 }, { "Add2", add2 }, { "Add3", add3 } }; var ans = await await machine.SetDatasAsync(MachineDataType.CommunicationTag, setDic).ContinueWith(async p => await GetMachineEnter()); return Json(ans); } public IActionResult Privacy() { return View(); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } } }