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 { /// /// 取得設備最新一筆資料 /// public class OntimeDeviceRawDataController : MyBaseApiController { private readonly IFrontendRepository frontendRepository; public OntimeDeviceRawDataController(IFrontendRepository frontendRepository) { this.frontendRepository = frontendRepository; } [HttpPost] [Route("api/OntimeRawData/GetAllRawData")] public async Task>>> GetAllRawData(PostOntimeRawData post) { ApiResult> apiResult = new ApiResult>(jwt_str); if (!jwtlife) { apiResult.Code = "5000"; return BadRequest(apiResult); } try { List ontimeAggregateRawDatas = new List(); 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(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() }; 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); } } }