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()+'℃', + '온도' + ), ], ), ),