You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/IoTCenter/Api/InfluxController.cs

55 lines
1.8 KiB

using Infrastructure.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace IoTCenter.Api
{
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]/[action]")]
[ApiController]
public class InfluxController : ControllerBase
{
private readonly IConfiguration _cfg;
private readonly IHttpClientFactory _httpClientFactory;
public InfluxController(IConfiguration cfg, IHttpClientFactory httpClientFactory)
{
this._cfg = cfg;
this._httpClientFactory = httpClientFactory;
}
[HttpPost]
public async Task<IActionResult> Query()
{
var server = this._cfg["influxdb:url"];
var url = $"{server}/query";
var data = new Dictionary<string, string>();
foreach (var item in this.Request.Form)
{
try
{
data.Add(item.Key, this.FromBase64(this.FromBase64(item.Value.ToString())));
}
catch (Exception ex)
{
ex.PrintStack();
data.Add(item.Key, item.Value.ToString());
}
}
using var content = new FormUrlEncodedContent(data);
var client = _httpClientFactory.CreateClient();
var result = await client.PostAsync(url, content).Result.Content.ReadAsStringAsync();
return Content(result, "application/json");
}
private string FromBase64(string value)
{
return Encoding.UTF8.GetString(Convert.FromBase64String(value));
}
}
}