Merge branch 'simeon' into justin

This commit is contained in:
Simeon Messerschmidt 2023-01-04 17:30:30 +01:00 committed by GitHub
commit b31dee4799
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 24 deletions

View File

@ -1,6 +1,5 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:developer'; import 'dart:developer';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart'; import 'package:fluttertoast/fluttertoast.dart';
@ -24,6 +23,7 @@ class CloudServiceAPI {
static String _password = ""; static String _password = "";
static late Map<String, String> _headers; static late Map<String, String> _headers;
static bool _initState = false; static bool _initState = false;
static List<dynamic> devices = List<dynamic>.empty(growable: true);
CloudServiceAPI() { CloudServiceAPI() {
if (!_initState) { if (!_initState) {
@ -44,6 +44,7 @@ class CloudServiceAPI {
'accept': 'application/json', 'accept': 'application/json',
}; };
log("init Config $_headers"); log("init Config $_headers");
await updateDeviceList();
} }
Future<void> reloadConfig() async { Future<void> reloadConfig() async {
@ -64,7 +65,6 @@ class CloudServiceAPI {
List<DeviceModel> remoteObjectsList = remoteObjectsMap.toList(); List<DeviceModel> remoteObjectsList = remoteObjectsMap.toList();
return remoteObjectsList; return remoteObjectsList;
} }
Future<DeviceInfoModel> getDeviceInfo(String deviceID) async { Future<DeviceInfoModel> getDeviceInfo(String deviceID) async {
Uri url = Uri.https(_address, '/api/devices/$deviceID'); Uri url = Uri.https(_address, '/api/devices/$deviceID');
Response response = await get(url, headers: _headers); Response response = await get(url, headers: _headers);
@ -72,6 +72,15 @@ class CloudServiceAPI {
DeviceInfoModel deviceInfoModel = DeviceInfoModel.fromJson(jsonObject); DeviceInfoModel deviceInfoModel = DeviceInfoModel.fromJson(jsonObject);
return deviceInfoModel; return deviceInfoModel;
} }
Future updateDeviceList() async {
devices = await getDevices();
debugPrint("devices: ${devices.elementAt(0)}");
}
Future<Map<String, dynamic>> getDeviceInfo(deviceID) async {
Uri url = Uri.https(address, '/api/devices/$deviceID');
Response r = await get(url, headers: headers);
return json.decode(r.body) as Map<String, dynamic>;
}
Future<Map<String, dynamic>> getInformation() async { Future<Map<String, dynamic>> getInformation() async {
Uri url = Uri.https(_address, '/api/app'); Uri url = Uri.https(_address, '/api/app');
@ -148,4 +157,7 @@ class CloudServiceAPI {
_password = input; _password = input;
reloadConfig(); reloadConfig();
} }
List<dynamic> getLoadedDevices(){
return devices;
}
} }

View File

@ -25,7 +25,19 @@ class CloudService extends StatefulWidget {
class _CloudService extends State<CloudService> { class _CloudService extends State<CloudService> {
late final Map<String, dynamic> credentials; late final Map<String, dynamic> credentials;
CloudServiceAPI cloudServiceAPI = CloudServiceAPI(); CloudServiceAPI cloudServiceAPI = CloudServiceAPI();
static SharedPreferences? preferencesInstance; int _selectedIndex = 0;
void _onItemTapped(int index){
setState(() {
_selectedIndex = index;
if(_selectedIndex == 0){
cloudServiceAPI.updateDeviceList();
}
if(_selectedIndex == 1){
// implementation missing
}
});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -33,27 +45,44 @@ class _CloudService extends State<CloudService> {
appBar: AppBar( appBar: AppBar(
title: const Text("Cloud Service"), title: const Text("Cloud Service"),
), ),
body: Center( body: Column(
child: TextButton( children: <Widget>[
onPressed: () async { Expanded(child: ListView.separated(
// List<BluetoothObjectRemote> respond1 = await cloudServiceAPI.getDevices(); itemCount: cloudServiceAPI.getLoadedDevices().length,
// debugPrint('Devices: ${respond1[0].toString()}'); itemBuilder: (BuildContext context, int index) {
// debugPrint('Devices: ${respond1[0].toString()}'); return Container(
height: 100,
//dynamic respond2 = await cloudServiceAPI.getInformation(); color: Colors.lightBlueAccent[100],
//debugPrint('Information: ${respond2.toString()}'); child: Center(child: Text("Id: ${cloudServiceAPI.getLoadedDevices()[index]['id']} \n"
"endpoint: ${cloudServiceAPI.getLoadedDevices()[index]['endpoint']} \n"
// dynamic respond3 = await cloudServiceAPI.createDevice('1', 'asdas', 'sdwe1'); "status: ${cloudServiceAPI.getLoadedDevices()[index]['endpoint']} \n"
// debugPrint('CreateDevice: ${respond3.toString()}');*/ "connectionState: ${cloudServiceAPI.getLoadedDevices()[index]['connectionState']} \n"
"lastActivityTime: ${cloudServiceAPI.getLoadedDevices()[index]['lastActivityTime']}"),),
//dynamic respond4 = await cloudServiceAPI.getDeviceInfo("PFC200V3-430EB3"); );
//debugPrint('Information: ${respond4.toString()}'); }, separatorBuilder: (BuildContext context, int index) => const Divider(
thickness: 0,
//bool respond5 = await cloudServiceAPI.createDevice("PFC200V3-430EB2", "60987668030DF277AD7706D7C1FA683F37230D202A80EE5135B05EF928E3BF88", ""); indent: 0,
//debugPrint('Information: ${respond5.toString()}'); height: 5,
}, color: Colors.white,
child: const Text("Example"), ),)
)
],
), ),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: const<BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.refresh),
label: 'Reload',
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
label: 'Search',
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.lightBlueAccent,
onTap: _onItemTapped,
), ),
drawer: const Sidebar(), drawer: const Sidebar(),
); );