mirror of
https://bitbucket.org/myhomie/myhomie_app.git
synced 2025-12-06 09:01:20 +00:00
246 lines
7.1 KiB
Dart
246 lines
7.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:mqtt_client/mqtt_client.dart';
|
|
import 'package:mqtt_client/mqtt_server_client.dart';
|
|
import 'package:mycoreapi/api.dart';
|
|
import 'package:myhomie_app/client.dart';
|
|
|
|
import 'constants.dart';
|
|
|
|
void main() {
|
|
String initialRoute;
|
|
|
|
initialRoute = '/home';
|
|
|
|
final MyApp myApp = MyApp(
|
|
initialRoute: initialRoute,
|
|
//context: localContext,
|
|
);
|
|
|
|
runApp(myApp);
|
|
}
|
|
|
|
class MyApp extends StatefulWidget {
|
|
final String initialRoute;
|
|
//final Context context;
|
|
MyApp({this.initialRoute});
|
|
|
|
// This widget is the root of your application.
|
|
@override
|
|
_MyAppState createState() => _MyAppState();
|
|
}
|
|
|
|
class _MyAppState extends State<MyApp> {
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
debugShowCheckedModeBanner: false,
|
|
title: 'MyHomie App Demo',
|
|
initialRoute: widget.initialRoute,
|
|
/*supportedLocales: [
|
|
const Locale('en', 'US'),
|
|
//const Locale('fr', 'FR'),
|
|
],*/
|
|
theme: ThemeData(
|
|
primarySwatch: Colors.blue,
|
|
scaffoldBackgroundColor: kBackgroundColor,
|
|
//fontFamily: "Vollkorn",
|
|
textTheme: TextTheme(bodyText1: TextStyle(color: kBodyTextColor)),
|
|
visualDensity: VisualDensity.adaptivePlatformDensity,
|
|
),
|
|
routes: {
|
|
'/home': (context) => MyHomePage(title: 'MyHomie App Demo Home Page')
|
|
}
|
|
);
|
|
}
|
|
}
|
|
|
|
class MyHomePage extends StatefulWidget {
|
|
MyHomePage({Key key, this.title}) : super(key: key);
|
|
final String title;
|
|
|
|
@override
|
|
_MyHomePageState createState() => _MyHomePageState();
|
|
}
|
|
|
|
class _MyHomePageState extends State<MyHomePage> {
|
|
int _counter = 0;
|
|
//final MqttServerClient client = MqttServerClient.withPort('192.168.31.140', 'flutter_client', 1883); // TODO Add switch button or check online connexion if local broker available
|
|
//final MqttServerClient client = MqttServerClient.withPort('myhomie.be', 'flutter_client00', 1883); // TODO ONLINE
|
|
final clientAPI = Client();
|
|
|
|
void _incrementCounter() {
|
|
setState(() {
|
|
_counter++;
|
|
|
|
/*print("client.connectionStatus !!! ==" + client.connectionStatus.toString());
|
|
if (client.connectionStatus.state == MqttConnectionState.connected) {
|
|
const pubTopic = 'topic/test';
|
|
final builder = MqttClientPayloadBuilder();
|
|
builder.addString('Hello MQTT');
|
|
client.publishMessage(pubTopic, MqttQos.atLeastOnce, builder.payload);
|
|
}*/
|
|
});
|
|
}
|
|
|
|
void authenticateTRY() async {
|
|
print("try auth.. ");
|
|
var isError = true;
|
|
try {
|
|
LoginDTO loginDTO = new LoginDTO(email: "test@email.be", password: "kljqsdkljqsd");
|
|
TokenDTO token = await clientAPI.authenticationApi.authenticationAuthenticateWithJson(loginDTO);
|
|
print("Token ??");
|
|
print(token.accessToken);
|
|
setAccessToken(token.accessToken);
|
|
isError = false;
|
|
}
|
|
catch (e) {
|
|
print("error auth");
|
|
}
|
|
|
|
if (!isError) {
|
|
UserInfoDetailDTO user = await clientAPI.userApi.userGet("604a33639b4a377a413045b9");
|
|
print("user values ??");
|
|
print(user.email);
|
|
print(user.id);
|
|
|
|
List<RoomSummaryDTO> rooms = await clientAPI.roomApi.roomGetAll(user.id);
|
|
print("number of rooms " + rooms.length.toString());
|
|
rooms.forEach((element) {
|
|
print(element);
|
|
});
|
|
|
|
List<ProviderDTO> providers = await clientAPI.providerApi.providerGetAll(user.id);
|
|
print("number of providers " + providers.length.toString());
|
|
providers.forEach((element) {
|
|
print(element);
|
|
});
|
|
|
|
List<DeviceSummaryDTO> devices = await clientAPI.deviceApi.deviceGetAll(user.id);
|
|
print("number of devices " + devices.length.toString());
|
|
/*devices.forEach((element) {
|
|
print(element);
|
|
});*/
|
|
|
|
List<AutomationDTO> automations = await clientAPI.automationApi.automationGetAll(user.id);
|
|
print("number of automations " + automations.length.toString());
|
|
automations.forEach((element) {
|
|
print(element);
|
|
});
|
|
|
|
List<GroupSummaryDTO> groups = await clientAPI.groupApi.groupGetAll(user.id);
|
|
print("number of groups " + groups.length.toString());
|
|
groups.forEach((element) {
|
|
print(element);
|
|
});
|
|
}
|
|
}
|
|
|
|
// connection succeeded
|
|
void onConnected() {
|
|
print('Connected !!!!!!!!!!!! ----------------------------------');
|
|
|
|
/*client.updates.listen((List<MqttReceivedMessage<MqttMessage>> c) {
|
|
final MqttPublishMessage message = c[0].payload;
|
|
final payload = MqttPublishPayload.bytesToStringAsString(message.payload.message);
|
|
print('Received message:$payload from topic: ${c[0].topic}>');
|
|
});*/
|
|
}
|
|
|
|
void setAccessToken(String accessToken) {
|
|
clientAPI.apiApi.authentications.forEach((key, auth) {
|
|
if (auth is OAuth) {
|
|
auth.accessToken = accessToken;
|
|
}
|
|
});
|
|
}
|
|
|
|
// unconnected
|
|
void onDisconnected() {
|
|
print('Disconnected');
|
|
}
|
|
|
|
// subscribe to topic succeeded
|
|
void onSubscribed(String topic) {
|
|
print('Subscribed topic: $topic');
|
|
}
|
|
|
|
// subscribe to topic failed
|
|
void onSubscribeFail(String topic) {
|
|
print('Failed to subscribe $topic');
|
|
}
|
|
|
|
// unsubscribe succeeded
|
|
void onUnsubscribed(String topic) {
|
|
print('Unsubscribed topic: $topic');
|
|
}
|
|
|
|
Future<MqttServerClient> connect() async {
|
|
/*client.logging(on: false);
|
|
client.keepAlivePeriod = 20;
|
|
client.onDisconnected = onDisconnected;
|
|
client.onConnected = onConnected;
|
|
client.onSubscribed = onSubscribed;*/
|
|
|
|
/// Security context
|
|
/*SecurityContext context = new SecurityContext()
|
|
..useCertificateChain('path/to/my_cert.pem')
|
|
..usePrivateKey('path/to/my_key.pem', password: 'key_password')
|
|
..setClientAuthorities('path/to/client.crt', password: 'password');*/
|
|
//client.setProtocolV31();
|
|
//client.secure = true;
|
|
//client.securityContext = context;
|
|
|
|
final connMessage = MqttConnectMessage()
|
|
.authenticateAs('thomas', 'MyCore,1') // TODO ONLINE
|
|
/*.keepAliveFor(60)
|
|
.withWillTopic('willtopic')
|
|
.withWillMessage('Will message')*/
|
|
.withClientIdentifier("TESSST")
|
|
.startClean();
|
|
//.withWillQos(MqttQos.atLeastOnce);
|
|
//client.secure = true;
|
|
/*client.connectionMessage = connMessage;
|
|
client.autoReconnect = true;
|
|
try {
|
|
await client.connect();
|
|
} catch (e) {
|
|
print('Exception: $e');
|
|
client.disconnect();
|
|
}
|
|
|
|
client.subscribe("#", MqttQos.atLeastOnce);
|
|
*/
|
|
//return client;
|
|
return null;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
connect();
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(widget.title),
|
|
),
|
|
body: Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
Text(
|
|
'You have pushed the button this many times:',
|
|
),
|
|
Text(
|
|
'$_counter',
|
|
style: Theme.of(context).textTheme.headline4,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
onPressed: authenticateTRY,
|
|
tooltip: 'Increment',
|
|
child: Icon(Icons.add),
|
|
),
|
|
);
|
|
}
|
|
} |