From 4f90fdbc4029ec3f0eca315c76f0aafe1a3df414 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=ED=9D=AC=EC=84=B1?= Date: Mon, 15 Dec 2025 10:32:46 +0900 Subject: [PATCH] =?UTF-8?q?=EB=B0=B0=ED=84=B0=EB=A6=AC,=20=EC=8A=B5?= =?UTF-8?q?=EB=8F=84,=20=EC=98=A8=EB=8F=84,=20=EC=A0=84=EC=95=95=20?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=84=B0=20=EC=B6=9C=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/home_screen_content.dart | 75 +++++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 6 deletions(-) diff --git a/lib/home_screen_content.dart b/lib/home_screen_content.dart index 9db0b76..bcf5809 100644 --- a/lib/home_screen_content.dart +++ b/lib/home_screen_content.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:flutter/material.dart'; import 'package:flutter_map/flutter_map.dart'; import 'package:latlong2/latlong.dart'; @@ -44,13 +46,14 @@ class _HomeScreenContentState extends State { } // 25.12.03 지은 추가 끝 - void test() async { + Future?> test() async { final data = await _api.getTimeseries(target: "*", limit: 200); if (data != null) { print("받은 데이터 길이: ${data['samples'].length}"); print(data['samples'][0]); // 첫 번째 데이터 출력 } + return data; } int _selectedImageIndex = 0; @@ -69,9 +72,48 @@ class _HomeScreenContentState extends State { 'FAN': false, }; + double? _battPct; + double? _volt; + double? _temp; + double? _humi; + Timer? _timer; + + @override + void initState() { + super.initState(); + _loadBattPct(); + _timer = Timer.periodic(const Duration(seconds: 2), (_) => _loadBattPct()); + } + + @override + void dispose() { + _timer?.cancel(); + super.dispose(); + } + + Future _loadBattPct() async { + final data = await test(); // Future → Map으로 resolve + + if (data == null) return; + + // samples[0]에서 batt_pct 값 읽기 + final battPct = data['samples'][0]['batt_pct']; + final volt = data['samples'][0]['pv_volt']; + final temp = data['samples'][0]['temp']; + final humi = data['samples'][0]['humi']; + + setState(() { + // 숫자/문자 둘 다 안전하게 처리 + _battPct = battPct.toDouble(); + _volt = volt.toDouble(); + _temp = temp.toDouble(); + _humi = humi.toDouble(); + }); + } + + @override Widget build(BuildContext context) { - test(); return Container( color: _pageBackgroundColor, child: Column( @@ -283,11 +325,20 @@ class _HomeScreenContentState extends State { painter: _BatteryArcPainter( backgroundColor: _accentContainerColor, color: _mainBlueColor, - percentage: 1.0, + percentage: _battPct!.floor()/100 , ), ), ), - Text('86', style: TextStyle(fontSize: 24, fontWeight: FontWeight.w600, color: _mainTextColor)), + Text( + _battPct == null + ? '--' // 아직 로딩 전 + : _battPct!.floor().toString(), + style: TextStyle( + fontSize: 24, + fontWeight: FontWeight.w600, + color: _mainTextColor, + ), + ), ], ), ), @@ -513,9 +564,21 @@ class _HomeScreenContentState extends State { child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - _buildSensorInfoRow(Icons.water_drop_outlined, '습도: 60%', '습도'), + _buildSensorInfoRow( + Icons.water_drop_outlined, + _humi == null + ? '--' // 아직 로딩 전 + : '습도: '+_humi!.floor().toString()+'%', + '습도' + ), const SizedBox(height: 24), - _buildSensorInfoRow(Icons.thermostat, '온도: 24.5℃', '온도'), + _buildSensorInfoRow( + Icons.thermostat, + _temp == null + ? '--' // 아직 로딩 전 + : '온도: '+_temp!.floor().toString()+'℃', + '온도' + ), ], ), ),