import 'dart:convert'; import 'dart:developer'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:http/http.dart'; import '../schemas/device_info_model.dart'; import '../schemas/device_model.dart'; Future> readJson() async { final String data = await rootBundle.loadString('config/credentials.json'); final Map dataMap = json.decode(data); log("loaded json input $dataMap"); return dataMap; } class CloudServiceAPI { static late final Map credentials; static String _basicAuth = ""; static String _address = ""; static String _username = ""; static String _password = ""; static late Map _headers; static bool _initState = false; static List devices = List.empty(growable: true); CloudServiceAPI() { if (!_initState) { 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', }; log("init Config $_headers"); await updateDeviceList(); } Future reloadConfig() async { _basicAuth = 'Basic ${base64.encode(utf8.encode('$_username:$_password'))}'; _headers = { 'authorization': _basicAuth, 'content-type': 'application/json', 'accept': 'application/json', }; } Future> getDevices() async { Uri url = Uri.https(_address, '/api/devices'); Response responnse = await get(url, headers: _headers); dynamic jsonObject = json.decode(responnse.body); Iterable remoteObjectsIterable = Iterable.castFrom(jsonObject); Iterable remoteObjectsMap = remoteObjectsIterable.map((data) => DeviceModel.fromJson(data)); List remoteObjectsList = remoteObjectsMap.toList(); return remoteObjectsList; } Future getDeviceInfo(String deviceID) async { Uri url = Uri.https(_address, '/api/devices/$deviceID'); Response response = await get(url, headers: _headers); dynamic jsonObject = json.decode(response.body); DeviceInfoModel deviceInfoModel = DeviceInfoModel.fromJson(jsonObject); return deviceInfoModel; } Future updateDeviceList() async { devices = await getDevices(); debugPrint("devices: ${devices.elementAt(0)}"); } Future> 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; } Future> getInformation() async { Uri url = Uri.https(_address, '/api/app'); Response response = await get(url, headers: _headers); return json.decode(response.body) as Map; } Future createDevice(String id, String primaryThumbprint, String secondaryThumbprint) async { Uri url = Uri.https(_address, '/api/devices'); Response response = await post(url, headers: _headers, body: jsonEncode({ 'id': id, 'primaryThumbprint': primaryThumbprint, 'secondaryThumbprint': secondaryThumbprint })); if (response.statusCode == 200) { return true; } debugPrint('Error createDevice: ${response.statusCode.toString()}'); return false; } Future checkNameAvailability(String inputID) async { // await cloudServiceAPI.loadConfig(); List devices = await getDevices(); for (DeviceModel selected in devices) { if (selected.id == inputID) { debugPrint("ID 1: ${selected.id}"); debugPrint("ID 2: $inputID"); await showNameAvailabilityStatus(false); return false; } } await showNameAvailabilityStatus(true); return true; } Future 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); } String get address { return _address; } String get username { return _username; } String get password { return _password; } set address(String input) { _address = input; reloadConfig(); } set username(String input) { _username = input; reloadConfig(); } set password(String input) { _password = input; reloadConfig(); } List getLoadedDevices(){ return devices; } }