123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- using Applications.UniversalApi_WebConsole_App.Models;
- using Edge.Core;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Mvc.ApiExplorer;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Logging;
- using Newtonsoft.Json;
- using RestSharp;
- namespace Applications.UniversalApi_WebConsole_App.Controllers
- {
- public class HomeController : Controller
- {
- private readonly ILogger<HomeController> _logger;
- JsonSerializerSettings _jsonSetting =
- new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore };
- private readonly IApiDescriptionGroupCollectionProvider _apiDescriptionsProvider;
- private readonly string _apiBaseAddress;
- private string _checkLicenseUrl;
- private string _verifyLicenseUrl;
- private readonly string _serviceDiscoveryUrl;
- public HomeController(ILogger<HomeController> logger,
- IApiDescriptionGroupCollectionProvider apiDescriptionsProvider,
- IConfiguration configuration)
- {
-
- _logger = logger;
- _apiDescriptionsProvider = apiDescriptionsProvider;
- _apiBaseAddress = configuration.GetValue<string>("BaseUrl");
- _checkLicenseUrl = configuration.GetValue<string>("CheckLicenseUrl");
- _verifyLicenseUrl = configuration.GetValue<string>("VerifyLicenseUrl");
- _serviceDiscoveryUrl = configuration.GetValue<string>("ServiceDiscoveryUrl");
- }
- public async Task<IActionResult> Index()
- {
- var applicableApiDescriptions = _apiDescriptionsProvider.ApiDescriptionGroups.Items
- .SelectMany(group => group.Items);
- if (await CheckLicense() == true)
- {
- return View("Index", _apiDescriptionsProvider.ApiDescriptionGroups.Items
- .SelectMany(group => group.Items));
- }
- else
- {
- return View("InputLicense", Licensing.Instance().GetMacAddress(_logger));
- }
- }
- 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 });
- }
- public async Task<IActionResult> InputLicense()
- {
- return View("InputLicense");
- }
- public async Task<IActionResult> SaveLicense(string license)
- {
- if (await LicenseVerify(license) == true)
- {
- return RedirectToAction(nameof(Index), _apiDescriptionsProvider.ApiDescriptionGroups.Items.SelectMany(group => group.Items));
- }
- else
- {
- return View("InputLicense");
- }
- }
- private async Task<bool> CheckLicense()
- {
- var result = false;
- var path = await getServiceUrl("webapi", "CheckLicense", "LicensingApp");
-
- if (path != string.Empty)
- {
- _checkLicenseUrl = path;
- }
- try
- {
- var restClient = new RestClient(_apiBaseAddress);
- RestRequest request = new RestRequest(_checkLicenseUrl, Method.POST);
- request.AddHeader("Content-Type", "application/json");
- request.AddJsonBody("");
- request.RequestFormat = DataFormat.Json;
- var response = await restClient.ExecuteTaskAsync(request);
- if (response.StatusCode != System.Net.HttpStatusCode.OK)
- {
- _logger.LogInformation($"CheckLicense {_checkLicenseUrl} with Status code: " + response.StatusCode);
- }
- else
- {
- result = JsonConvert.DeserializeObject<bool>(response.Content);
- }
- }
- catch (Exception _)
- {
- }
- return result;
- }
- private async Task<bool> LicenseVerify(string license)
- {
- var result = false;
- try
- {
- var path = await getServiceUrl("webapi", "VerifyLicense", "LicensingApp");
- if (path != string.Empty)
- {
- _verifyLicenseUrl = path;
- }
- var restClient = new RestClient(_apiBaseAddress);
- RestRequest request = new RestRequest(_verifyLicenseUrl, Method.POST);
- request.AddHeader("Content-Type", "application/json");
- request.AddJsonBody(license);
- request.RequestFormat = DataFormat.Json;
- var response = await restClient.ExecuteTaskAsync(request);
- if (response.StatusCode != System.Net.HttpStatusCode.OK)
- {
- _logger.LogInformation($"LicenseVerify {_verifyLicenseUrl} with Status code: " + response.StatusCode);
- }
- else
- {
- result = JsonConvert.DeserializeObject<bool>(response.Content);
- }
- }
-
- catch (Exception _)
- {
-
- }
- return result;
- }
- /// <summary>
- /// serviceType canbe "webapi"/"localMqtt"/"remotemqtt"
- /// </summary>
- /// <param name="serviceType"></param>
- /// <param name="apiName"></param>
- /// <param name="tag"></param>
- /// <returns></returns>
- private async Task<string> getServiceUrl(string serviceType,string apiName,string tag)
- {
- var result = string.Empty;
- object[] requestBody = new object[]{ serviceType, new object[] { tag} };
- try
- {
- var restClient = new RestClient(_apiBaseAddress);
- RestRequest request = new RestRequest(_serviceDiscoveryUrl, Method.POST);
- request.AddHeader("Content-Type", "application/json");
- request.AddJsonBody(requestBody);
- request.RequestFormat = DataFormat.Json;
- var response = await restClient.ExecuteTaskAsync(request);
- if (response.StatusCode != System.Net.HttpStatusCode.OK)
- {
- _logger.LogInformation($"serviceDiscovery {_serviceDiscoveryUrl} with Status code: " + response.StatusCode);
- }
- else
- {
- var data = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(response.Content);
- if (data.Count > 0)
- {
- foreach (var d in data)
- {
- if (d["ApiName"].ToString() == apiName)
- {
- result = d["Path"].ToString().Substring(1);
- break;
- }
- }
- }
- }
- }
- catch (Exception _)
- {
- }
- return result;
- }
- }
- }
|