141 lines
4.1 KiB
Dart
141 lines
4.1 KiB
Dart
import 'dart:convert';
|
|
import 'dart:html';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:http/http.dart';
|
|
import 'dart:developer';
|
|
|
|
Future<Map<String, dynamic>> readJson() async {
|
|
final data = await rootBundle.loadString('config/credentials.json');
|
|
var data_ = json.decode(data) as Map<String, dynamic>;
|
|
log("loaded json input $data_");
|
|
return data_;
|
|
}
|
|
|
|
/*Future<void> checkNameAvailability(String input) async {
|
|
// await cloudServiceAPI.loadConfig();
|
|
List<dynamic> devices = await cloudServiceAPI.getDevices();
|
|
for (Map<String, dynamic> selected in devices) {
|
|
if (selected["id"] == input) {
|
|
await showNameAvailabilityStatus(true);
|
|
return;
|
|
}
|
|
}
|
|
await showNameAvailabilityStatus(false);
|
|
}
|
|
|
|
Future<void> showNameAvailabilityStatus(bool status) async {
|
|
String statusText = status
|
|
? "die eingegebene ID ist verfügbar"
|
|
: "die eingegebene ID ist nicht verfügbar";
|
|
Fluttertoast.showToast(
|
|
msg: statusText,
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
gravity: ToastGravity.BOTTOM,
|
|
timeInSecForIosWeb: 2,
|
|
backgroundColor: Colors.grey[200],
|
|
textColor: Colors.black,
|
|
fontSize: 16.0);
|
|
}*/
|
|
|
|
class CloudServiceAPI {
|
|
static late final Map<String, dynamic> credentials;
|
|
static late Future loadJson;
|
|
static late String basicAuth;
|
|
static late String address;
|
|
static late String username;
|
|
static late String password;
|
|
static late Map<String, String> headers;
|
|
static bool initState = false;
|
|
static List<dynamic> devices = List<dynamic>.empty(growable: true);
|
|
|
|
CloudServiceAPI(){
|
|
if(!initState) {
|
|
loadJson = loadConfig();
|
|
initState = true;
|
|
}
|
|
}
|
|
|
|
Future loadConfig() async{
|
|
credentials = await readJson();
|
|
username = credentials['username'];
|
|
password = credentials['password'];
|
|
address = credentials['address'];
|
|
basicAuth = 'Basic ${base64.encode(utf8.encode('$username:$password'))}';
|
|
headers = {
|
|
'authorization': basicAuth,
|
|
'content-type': 'application/json',
|
|
'accept': 'application/json',
|
|
};
|
|
await updateDeviceList();
|
|
log("init Config $headers");
|
|
}
|
|
void reloadConfig() {
|
|
basicAuth = 'Basic ${base64.encode(utf8.encode('$username:$password'))}';
|
|
headers = {
|
|
'authorization': basicAuth,
|
|
'content-type': 'application/json',
|
|
'accept': 'application/json',
|
|
};
|
|
}
|
|
Future<List> getDevices() async {
|
|
Uri url = Uri.https(address, '/api/devices');
|
|
Response r = await get(url, headers: headers);
|
|
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 {
|
|
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 {
|
|
Uri url = Uri.https(address, '/api/app');
|
|
Response r = await get(url, headers: headers);
|
|
return json.decode(r.body) as Map<String, dynamic>;
|
|
}
|
|
Future<bool> createDevice(id, primaryThumbprint, secondaryThumbprint) async{
|
|
Uri url = Uri.https(address, '/api/devices');
|
|
Response r = await post(
|
|
url,
|
|
headers: headers,
|
|
body: jsonEncode(<String, String>{
|
|
'id': id,
|
|
'primaryThumbprint' : primaryThumbprint,
|
|
'secondaryThumbprint' : secondaryThumbprint
|
|
})
|
|
);
|
|
if (r.statusCode == 200){
|
|
return true;
|
|
}
|
|
debugPrint('Error createDevice: ${r.statusCode.toString()}');
|
|
return false;
|
|
}
|
|
String getAddress(){
|
|
return address;
|
|
}
|
|
String getUsername(){
|
|
return username;
|
|
}
|
|
String getPassword(){
|
|
return password;
|
|
}
|
|
void setAddress(String input){
|
|
address = input;
|
|
reloadConfig();
|
|
}
|
|
void setUsername(String input){
|
|
username = input;
|
|
reloadConfig();
|
|
}
|
|
void setPassword(String input){
|
|
password = input;
|
|
reloadConfig();
|
|
}
|
|
List<dynamic> getLoadedDevices(){
|
|
return devices;
|
|
}
|
|
} |