semesterprojekt-bluetooth-p.../lib/objects/cloud_service_api.dart

152 lines
4.5 KiB
Dart

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<Map<String, dynamic>> readJson() async {
final String data = await rootBundle.loadString('config/credentials.json');
final Map<String, dynamic> dataMap = json.decode(data);
log("loaded json input $dataMap");
return dataMap;
}
class CloudServiceAPI {
static late final Map<String, dynamic> credentials;
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;
CloudServiceAPI() {
if (!_initState) {
loadConfig();
_initState = true;
}
}
Future<void> 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");
}
Future<void> reloadConfig() async {
_basicAuth = 'Basic ${base64.encode(utf8.encode('$_username:$_password'))}';
_headers = {
'authorization': _basicAuth,
'content-type': 'application/json',
'accept': 'application/json',
};
}
Future<List<DeviceModel>> getDevices() async {
Uri url = Uri.https(_address, '/api/devices');
Response responnse = await get(url, headers: _headers);
dynamic jsonObject = json.decode(responnse.body);
Iterable<dynamic> remoteObjectsIterable = Iterable.castFrom(jsonObject);
Iterable<DeviceModel> remoteObjectsMap = remoteObjectsIterable.map((data) => DeviceModel.fromJson(data));
List<DeviceModel> remoteObjectsList = remoteObjectsMap.toList();
return remoteObjectsList;
}
Future<DeviceInfoModel> 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<Map<String, dynamic>> getInformation() async {
Uri url = Uri.https(_address, '/api/app');
Response response = await get(url, headers: _headers);
return json.decode(response.body) as Map<String, dynamic>;
}
Future<bool> createDevice(String id, String primaryThumbprint, String secondaryThumbprint) async {
Uri url = Uri.https(_address, '/api/devices');
Response response = await post(url,
headers: _headers,
body: jsonEncode(<String, String>{
'id': id,
'primaryThumbprint': primaryThumbprint,
'secondaryThumbprint': secondaryThumbprint
}));
if (response.statusCode == 200) {
return true;
}
debugPrint('Error createDevice: ${response.statusCode.toString()}');
return false;
}
Future<bool> checkNameAvailability(String inputID) async {
// await cloudServiceAPI.loadConfig();
List<DeviceModel> 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<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);
}
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();
}
}