import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:managerapi/api.dart'; import 'package:mqtt_client/mqtt_server_client.dart'; import 'package:provider/provider.dart'; import 'package:tablet_app/Components/Buttons/rounded_button.dart'; import 'package:tablet_app/Components/loading.dart'; import 'package:tablet_app/Components/rounded_input_field.dart'; import 'package:tablet_app/Helpers/DatabaseHelper.dart'; import 'package:tablet_app/Helpers/MQTTHelper.dart'; import 'package:tablet_app/Models/tabletContext.dart'; import 'package:tablet_app/Screens/MainView/dropDown_configuration.dart'; import 'package:tablet_app/Screens/MainView/main_view.dart'; import 'package:tablet_app/app_context.dart'; import 'package:tablet_app/client.dart'; import 'package:tablet_app/constants.dart'; import 'dart:io'; import 'package:unique_identifier/unique_identifier.dart'; class ConfigViewWidget extends StatefulWidget { ConfigViewWidget(); @override _ConfigViewWidget createState() => _ConfigViewWidget(); } class _ConfigViewWidget extends State { Size sizeScreen = new Size(1080.0, 1920.0); // Tablet resolution String url; //DEV "http://192.168.31.96" bool configOk = false; @override Widget build(BuildContext context) { final appContext = Provider.of(context); SystemChrome.setEnabledSystemUIOverlays([]); Size size = MediaQuery.of(context).size; TabletAppContext tabletAppContext = appContext.getContext(); if (tabletAppContext != null) { if (tabletAppContext.host != null) url = tabletAppContext.host; } return Scaffold( body: Container( height: size.height, width: size.width, color: kBackgroundGrey, child: Center( child: Container( height: size.height * 0.85, width: size.width * 0.9, child: configOk ? FutureBuilder( future: getConfigurations(appContext), builder: (context, AsyncSnapshot snapshot) { if (snapshot.connectionState == ConnectionState.done) { return Center( child: Container( decoration: BoxDecoration( borderRadius: BorderRadius.circular(29), color: kBackgroundLight, ), height: size.height*0.3, width: size.width*0.3, child: Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.center, children: [ Padding( padding: const EdgeInsets.only(left: 15, right: 15, top: 15, bottom: 5), child: Text( "Choisir une configuration", style: new TextStyle( fontSize: 25 ), ), ), DropDownConfig( configurations: snapshot.data, onChange: (ConfigurationDTO configurationOut) async { // CREATE DEVICE REQUEST await createDevice(configurationOut, appContext); Navigator.pushAndRemoveUntil( context, MaterialPageRoute( builder: (context) { return MainViewWidget(); }, ), (Route route) => false // For pushAndRemoveUntil ); }, ), ], ), ), ); } else if (snapshot.connectionState == ConnectionState.none) { return Text("No data"); } else { return Center( child: Container( height: size.height * 0.2, child: Loading() ) ); } } ) : Center( child: Container( decoration: BoxDecoration( borderRadius: BorderRadius.circular(29), color: kBackgroundLight, ), height: size.height*0.3, width: size.width*0.3, child: Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.center, children: [ Padding( padding: const EdgeInsets.all(8.0), child: Text( "Entrer l'url du manager", style: new TextStyle( fontSize: 25 ), ), ), Padding( padding: const EdgeInsets.only(left: 50, right: 50), child: RoundedInputField( hintText: "URL", initialValue: url, onChanged: (value) { setState(() { url = value; }); }, icon: Icons.language ), ), RoundedButton( text: "OK", fontSize: 25, press: () async { var client = Client(url); var isOk = await isValidApi(client); if (isOk) { Fluttertoast.showToast( msg: "Connecté au manager", toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, timeInSecForIosWeb: 1, backgroundColor: Colors.lightGreen, textColor: Colors.white, fontSize: 16.0 ); TabletAppContext tabletAppContext = new TabletAppContext(); tabletAppContext.host = url; tabletAppContext.clientAPI = client; var identifier = await UniqueIdentifier.serial; tabletAppContext.clientMQTT = new MqttServerClient(url.replaceAll('http://', ''),'tablet_app_'+identifier); setState(() { appContext.setContext(tabletAppContext); configOk = true; }); } else { Fluttertoast.showToast( msg: "L'url ne correspond pas à un manager", toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, timeInSecForIosWeb: 1, backgroundColor: Colors.redAccent, textColor: Colors.white, fontSize: 16.0 ); } }, ) ], ), ), ), ), ), ) ); } Future createDevice(ConfigurationDTO configurationDTO, dynamic appContext) async { TabletAppContext tabletAppContext = appContext.getContext(); DeviceDetailDTO newDevice = new DeviceDetailDTO(); newDevice.configurationId = configurationDTO.id; newDevice.configuration = configurationDTO.label; newDevice.connected = true; newDevice.identifier = await UniqueIdentifier.serial; newDevice.ipAddressWLAN = await getIP(true); newDevice.ipAddressETH = await getIP(false); newDevice.name = newDevice.ipAddressWLAN; print(newDevice); DeviceDetailDTO device = await tabletAppContext.clientAPI.deviceApi.deviceCreate(newDevice); if (device != null) { // STORE IT LOCALLY !! TabletAppContext tabletAppContext = appContext.getContext(); tabletAppContext.id = device.identifier; tabletAppContext.deviceId = device.id; tabletAppContext.host = url; tabletAppContext.language = "FR"; // By Default tabletAppContext.configuration = configurationDTO; appContext.setContext(tabletAppContext); // STORE IT LOCALLY (SQLite) TabletAppContext localContext = await DatabaseHelper.instance.getData(); if (localContext != null) { // Check if sql DB exist await DatabaseHelper.instance.update(tabletAppContext); } else { await DatabaseHelper.instance.insert(tabletAppContext); } Fluttertoast.showToast( msg: "La tablette a bien été créée", toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, timeInSecForIosWeb: 1, backgroundColor: Colors.lightGreen, textColor: Colors.white, fontSize: 16.0 ); } else { Fluttertoast.showToast( msg: "Une erreur est survenue lors de la création de la tablette", toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, timeInSecForIosWeb: 1, backgroundColor: Colors.deepOrangeAccent, textColor: Colors.white, fontSize: 16.0 ); } } isValidApi(Client client) async { print("TEST URL"); try { var configs = await client.configurationApi.configurationGet(); return configs != null; } catch (ex) { return false; } } } Future getIP(bool isWLAN) async { for (var interface in await NetworkInterface.list()) { print('== Interface: ${interface.name} =='); if (interface.name == "wlan0" && isWLAN) { // wifi return interface.addresses.first.address; } if (interface.name == "eth0" && !isWLAN) { // wired return interface.addresses.first.address; } } return null; } boxDecoration(SectionDTO section) { return BoxDecoration( color: kBackgroundLight, shape: BoxShape.rectangle, borderRadius: BorderRadius.circular(30.0), image: new DecorationImage( fit: BoxFit.cover, colorFilter: new ColorFilter.mode(Colors.black.withOpacity(0.5), BlendMode.dstATop), image: new NetworkImage( section.imageSource, ), ), boxShadow: [ BoxShadow( color: kBackgroundSecondGrey, spreadRadius: 0.5, blurRadius: 5, offset: Offset(0, 1.5), // changes position of shadow ), ], ); } Future> getConfigurations(dynamic appContext) async { List configurations = await appContext.getContext().clientAPI.configurationApi.configurationGet(); print("number of configurations " + configurations.length.toString()); configurations.forEach((element) { print(element); }); return configurations; }