From 84dfa474ee3518427f058ab38ceb3487e072d651 Mon Sep 17 00:00:00 2001 From: Simeon5566 Date: Wed, 2 Nov 2022 21:20:20 +0100 Subject: [PATCH] added Cloud Service, first draw --- config/credentials.json | 5 ++++ lib/cloud_service_api.dart | 58 ++++++++++++++++++++++++++++++++++++++ lib/cloud_service_ui.dart | 49 ++++++++++++++++++++++++++++++++ pubspec.yaml | 3 ++ 4 files changed, 115 insertions(+) create mode 100644 config/credentials.json create mode 100644 lib/cloud_service_api.dart create mode 100644 lib/cloud_service_ui.dart diff --git a/config/credentials.json b/config/credentials.json new file mode 100644 index 0000000..405c414 --- /dev/null +++ b/config/credentials.json @@ -0,0 +1,5 @@ +{ + "username": "...", + "password": "...", + "address": "..." +} \ No newline at end of file diff --git a/lib/cloud_service_api.dart b/lib/cloud_service_api.dart new file mode 100644 index 0000000..dd4f1dc --- /dev/null +++ b/lib/cloud_service_api.dart @@ -0,0 +1,58 @@ +import 'dart:convert'; +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:http/http.dart'; + + +Future> readJson() async { + final data = await rootBundle.loadString('config/credentials.json'); + var data_ = json.decode(data) as Map; + debugPrint(data_.toString()); + return data_; +} + +class CloudServiceAPI{ + late final Map credentials; + late Future loadJson; + late String basicAuth; + late String address; + late Map headers; + + CloudServiceAPI() { + loadJson = loadConfig(); + } + + Future loadConfig() async{ + credentials = await readJson(); + String username = credentials['username']; + String password = credentials['password']; + address = credentials['address']; + basicAuth = 'Basic ${base64.encode(utf8.encode('$username:$password'))}'; + headers = { + 'authorization': basicAuth, + 'content-type': 'application/json', + 'accept': 'application/json' + }; + } + Future getDevices() async { + var url = Uri.https(address, '/api/devices'); + Response r = await get(url, headers: headers); + return json.decode(r.body) as List; + } + Future> getDeviceInfo(deviceID) async { + var url = Uri.https(address, '/api/devices/$deviceID'); + Response r = await get(url, headers: headers); + return json.decode(r.body) as Map; + } + Future> getInformation() async { + var url = Uri.https(address, '/api/app'); + Response r = await get(url, headers: headers); + return json.decode(r.body) as Map; + } + Future createDevice( + id, + primaryThumbprint, + secondaryThumbprint) async{ + return false; + } +} \ No newline at end of file diff --git a/lib/cloud_service_ui.dart b/lib/cloud_service_ui.dart new file mode 100644 index 0000000..33b5ce4 --- /dev/null +++ b/lib/cloud_service_ui.dart @@ -0,0 +1,49 @@ +import 'dart:convert'; +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:http/http.dart' as http; +import 'package:http/http.dart'; +import 'cloud_service_api.dart'; +import 'sidebar.dart'; + + +class CloudService extends StatefulWidget { + const CloudService({Key? key}) : super(key: key); + + @override + State createState() => _CloudService(); + +} +Future> readJson() async { + final data = await rootBundle.loadString('config/credentials.json'); + var data_ = json.decode(data) as Map; + debugPrint(data_.toString()); + return data_; +} + +class _CloudService extends State{ + late final Map credentials; + final CloudServiceAPI cloudServiceAPI = CloudServiceAPI(); + + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text("Cloud Service"), + ), + body: Center( + child: TextButton( + onPressed: () async{ + var respond1 = await cloudServiceAPI.getDevices(); + debugPrint('Devices: ${respond1[0].toString()}'); + var respond2 = await cloudServiceAPI.getInformation(); + debugPrint('Information: ${respond2.toString()}'); + }, child: const Text("Example"), + ), + ), + drawer: const Sidebar(), + ); + } + +} \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index 9395086..13579f9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -38,6 +38,7 @@ dependencies: cupertino_icons: ^1.0.2 flutter_blue: ^0.8.0 shared_preferences: ^2.0.13 + http: ^0.13.5 dev_dependencies: @@ -66,6 +67,8 @@ flutter: # assets: # - images/a_dot_burr.jpeg # - images/a_dot_ham.jpeg + assets: + - config/credentials.json # An image asset can refer to one or more resolution-specific "variants", see # https://flutter.dev/assets-and-images/#resolution-aware