added device list to cloud service screen
This commit is contained in:
parent
7ffe408d44
commit
4db201624c
|
@ -1,4 +1,5 @@
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
import 'dart:html';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:http/http.dart';
|
import 'package:http/http.dart';
|
||||||
|
@ -46,6 +47,7 @@ class CloudServiceAPI {
|
||||||
static late String password;
|
static late 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) {
|
||||||
|
@ -65,6 +67,7 @@ class CloudServiceAPI {
|
||||||
'content-type': 'application/json',
|
'content-type': 'application/json',
|
||||||
'accept': 'application/json',
|
'accept': 'application/json',
|
||||||
};
|
};
|
||||||
|
await updateDeviceList();
|
||||||
log("init Config $headers");
|
log("init Config $headers");
|
||||||
}
|
}
|
||||||
void reloadConfig() {
|
void reloadConfig() {
|
||||||
|
@ -80,6 +83,10 @@ class CloudServiceAPI {
|
||||||
Response r = await get(url, headers: headers);
|
Response r = await get(url, headers: headers);
|
||||||
return json.decode(r.body) as List<dynamic>;
|
return json.decode(r.body) as List<dynamic>;
|
||||||
}
|
}
|
||||||
|
Future updateDeviceList() async {
|
||||||
|
devices = await getDevices();
|
||||||
|
debugPrint("devices: ${devices.elementAt(0)}");
|
||||||
|
}
|
||||||
Future<Map<String, dynamic>> getDeviceInfo(deviceID) async {
|
Future<Map<String, dynamic>> getDeviceInfo(deviceID) async {
|
||||||
Uri url = Uri.https(address, '/api/devices/$deviceID');
|
Uri url = Uri.https(address, '/api/devices/$deviceID');
|
||||||
Response r = await get(url, headers: headers);
|
Response r = await get(url, headers: headers);
|
||||||
|
@ -128,4 +135,7 @@ class CloudServiceAPI {
|
||||||
password = input;
|
password = input;
|
||||||
reloadConfig();
|
reloadConfig();
|
||||||
}
|
}
|
||||||
|
List<dynamic> getLoadedDevices(){
|
||||||
|
return devices;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -14,18 +14,25 @@ class CloudService extends StatefulWidget {
|
||||||
State<CloudService> createState() => _CloudService();
|
State<CloudService> createState() => _CloudService();
|
||||||
|
|
||||||
}
|
}
|
||||||
Future<Map<String, dynamic>> readJson() async {
|
|
||||||
final data = await rootBundle.loadString('config/credentials.json');
|
|
||||||
var data_ = json.decode(data) as Map<String, dynamic>;
|
|
||||||
debugPrint(data_.toString());
|
|
||||||
return data_;
|
|
||||||
}
|
|
||||||
|
|
||||||
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,17 +40,45 @@ 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(
|
||||||
var respond1 = await cloudServiceAPI.getDevices();
|
itemCount: cloudServiceAPI.getLoadedDevices().length,
|
||||||
debugPrint('Devices: ${respond1.toString()}');
|
itemBuilder: (BuildContext context, int index) {
|
||||||
var respond2 = await cloudServiceAPI.getInformation();
|
return Container(
|
||||||
debugPrint('Information: ${respond2.toString()}');
|
height: 100,
|
||||||
var respond3 = await cloudServiceAPI.createDevice('1', 'asdas', 'sdwe1');
|
color: Colors.lightBlueAccent[100],
|
||||||
debugPrint('CreateDevice: ${respond3.toString()}');
|
child: Center(child: Text("Id: ${cloudServiceAPI.getLoadedDevices()[index]['id']} \n"
|
||||||
}, child: const Text("Example"),
|
"endpoint: ${cloudServiceAPI.getLoadedDevices()[index]['endpoint']} \n"
|
||||||
|
"status: ${cloudServiceAPI.getLoadedDevices()[index]['endpoint']} \n"
|
||||||
|
"connectionState: ${cloudServiceAPI.getLoadedDevices()[index]['connectionState']} \n"
|
||||||
|
"lastActivityTime: ${cloudServiceAPI.getLoadedDevices()[index]['lastActivityTime']}"),),
|
||||||
|
);
|
||||||
|
}, separatorBuilder: (BuildContext context, int index) => const Divider(
|
||||||
|
thickness: 0,
|
||||||
|
indent: 0,
|
||||||
|
height: 5,
|
||||||
|
color: Colors.white,
|
||||||
|
),)
|
||||||
|
)
|
||||||
|
],
|
||||||
),
|
),
|
||||||
|
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(),
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue