ibms-dome/FrontendWebApi/ApiControllers/OntimeDeviceRawDataController.cs

91 lines
3.6 KiB
C#
Raw Normal View History

2022-10-14 16:08:54 +08:00
using FrontendWebApi.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Repository.FrontendRepository.Interface;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace FrontendWebApi.ApiControllers
{
/// <summary>
/// 取得設備最新一筆資料
/// </summary>
public class OntimeDeviceRawDataController : MyBaseApiController<OntimeDeviceRawDataController>
{
private readonly IFrontendRepository frontendRepository;
public OntimeDeviceRawDataController(IFrontendRepository frontendRepository)
{
this.frontendRepository = frontendRepository;
}
[HttpPost]
[Route("api/OntimeRawData/GetAllRawData")]
public async Task<ActionResult<ApiResult<List<OntimeAggregateRawData>>>> GetAllRawData(PostOntimeRawData post)
{
ApiResult<List<OntimeAggregateRawData>> apiResult = new ApiResult<List<OntimeAggregateRawData>>(jwt_str);
if (!jwtlife)
{
apiResult.Code = "5000";
return BadRequest(apiResult);
}
try
{
List<OntimeAggregateRawData> ontimeAggregateRawDatas = new List<OntimeAggregateRawData>();
var sqlRawData = $@"SELECT * FROM ontime_device_rawdata WHERE building_guid = @building_guid AND main_system_guid = @main_system_guid AND sub_system_guid = @sub_system_guid";
//Stopwatch stopWatch = new Stopwatch();
//stopWatch.Start();
var ontimeRawDatas = await frontendRepository.GetAllAsync<OntimeRawData>(sqlRawData, post);
//stopWatch.Stop();
//Logger.LogInformation("【OnTimeDeviceRawDataJob】【效能檢驗】[取得所有RawData花費時間]{0} 毫秒", stopWatch.ElapsedMilliseconds);
//stopWatch.Reset();
//stopWatch.Start();
var ontimeRawDatas_groupby_device_number = ontimeRawDatas.GroupBy(x => x.device_number).ToList();
foreach (var ontimeRawData_groupby_device_number in ontimeRawDatas_groupby_device_number)
{
OntimeAggregateRawData ontimeAggregateRawData = new OntimeAggregateRawData()
{
device_number = ontimeRawData_groupby_device_number.Key,
points = new List<Point>()
};
foreach(var ontimeRawData in ontimeRawData_groupby_device_number)
{
Point point = new Point()
{
name = ontimeRawData.name,
value = ontimeRawData.value,
is_bool = ontimeRawData.is_bool
};
ontimeAggregateRawData.points.Add(point);
}
ontimeAggregateRawDatas.Add(ontimeAggregateRawData);
}
//stopWatch.Stop();
//Logger.LogInformation("【OnTimeDeviceRawDataJob】【效能檢驗】[RawData資料整理花費時間]{0} 毫秒", stopWatch.ElapsedMilliseconds);
apiResult.Data = ontimeAggregateRawDatas;
apiResult.Code = "0000";
}
catch (Exception exception)
{
apiResult.Code = "9999";
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
return Ok(apiResult);
}
return Ok(apiResult);
}
}
}