mirror of
https://bitbucket.org/myhomie/myhomie_app.git
synced 2025-12-06 00:51:19 +00:00
231 lines
7.2 KiB
Dart
231 lines
7.2 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/rendering.dart';
|
|
import 'package:mqtt_client/mqtt_client.dart';
|
|
import 'package:mqtt_client/mqtt_server_client.dart';
|
|
import 'package:myhomie_app/Components/Custom_Navigation_Bar/custom_app_bar.dart';
|
|
import 'package:myhomie_app/Components/Custom_Navigation_Bar/custom_bottom_navigation_bar.dart';
|
|
import 'package:myhomie_app/Components/Custom_Navigation_Bar/custom_nav_item.dart';
|
|
import 'package:myhomie_app/Helpers/MQTTHelper.dart';
|
|
import 'package:myhomie_app/Screens/Debug/DebugPage.dart';
|
|
import 'package:myhomie_app/Screens/Main/Automations/automations.dart';
|
|
import 'package:myhomie_app/app_context.dart';
|
|
import 'package:myhomie_app/constants.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
import '../../Models/homieContext.dart';
|
|
import 'Energy/energy.dart';
|
|
import 'Home/home.dart';
|
|
import 'Profile/profile.dart';
|
|
import 'Security/security.dart';
|
|
import 'index.dart';
|
|
|
|
PageController pageController = PageController(initialPage: 0);
|
|
int currentIndex = 0;
|
|
|
|
class MainPage extends StatefulWidget {
|
|
MainPage({Key key}) : super(key: key);
|
|
|
|
@override
|
|
_MainPageState createState() => _MainPageState();
|
|
}
|
|
|
|
class _MainPageState extends State<MainPage> {
|
|
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
|
|
|
|
setPage() {
|
|
setState(() {
|
|
pageController.jumpToPage(currentIndex);
|
|
});
|
|
}
|
|
|
|
//final MqttServerClient client = MqttServerClient.withPort('myhomie.be', 'flutter_client00', 1883); // TODO ONLINE
|
|
|
|
/*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);
|
|
}
|
|
});
|
|
}*/
|
|
|
|
// 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}>');
|
|
});
|
|
}
|
|
|
|
// 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) {
|
|
final appContext = Provider.of<AppContext>(context);
|
|
Size size = MediaQuery.of(context).size;
|
|
|
|
if (!MQTTHelper.instance.isInstantiated)
|
|
{
|
|
print("MQTT NOT INSTANTIATED");
|
|
MQTTHelper.instance.connect(appContext);
|
|
}
|
|
|
|
HomieAppContext homieAppContext = appContext.getContext();
|
|
|
|
return ChangeNotifierProvider<Index>(
|
|
create: (_) => Index(0, appContext.getContext()),
|
|
child: Scaffold(
|
|
extendBody: true,
|
|
appBar: CustomAppBar(),
|
|
body: PageView(
|
|
physics: NeverScrollableScrollPhysics(),
|
|
controller: pageController,
|
|
children: <Widget>[
|
|
HomeScreen(),
|
|
SecurityScreen(),
|
|
AutomationsScreen(),
|
|
EnergyScreen(),
|
|
ProfileScreen(),
|
|
],
|
|
),
|
|
drawer: Drawer(
|
|
child: ListView(
|
|
// Important: Remove any padding from the ListView.
|
|
padding: EdgeInsets.zero,
|
|
children: [
|
|
const DrawerHeader(
|
|
decoration: BoxDecoration(
|
|
color: Colors.blue,
|
|
),
|
|
child: Text('Drawer Header'),
|
|
),
|
|
ListTile(
|
|
title: const Text('Debug screen'),
|
|
onTap: () {
|
|
Navigator.pop(context);
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) {
|
|
return DebugPage();
|
|
},
|
|
)
|
|
);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
backgroundColor: kMainColor,
|
|
onPressed: () {
|
|
var message = {
|
|
"userId": homieAppContext.userId,
|
|
"width": size.width,
|
|
"height": size.height,
|
|
"action": "button0"
|
|
};
|
|
|
|
const pubTopic = 'rpiZero/test';
|
|
final builder = MqttClientPayloadBuilder();
|
|
|
|
builder.addString(jsonEncode(message));
|
|
|
|
homieAppContext.clientMQTT.publishMessage(pubTopic, MqttQos.atLeastOnce, builder.payload);
|
|
|
|
var message2 = {
|
|
"state": "toggle"
|
|
};
|
|
|
|
const pubTopic2 = 'zigbee2mqtt/GU10Bureau/set';
|
|
final builder2 = MqttClientPayloadBuilder();
|
|
|
|
builder2.addString(jsonEncode(message2));
|
|
|
|
homieAppContext.clientMQTT.publishMessage(pubTopic2, MqttQos.atLeastOnce, builder2.payload);
|
|
/*Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) {
|
|
return DebugPage();
|
|
},
|
|
)
|
|
)*/
|
|
},
|
|
child: CustomNavItem(setPage: setPage, icon: NavItemIcon.home, id: 0),
|
|
),
|
|
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
|
|
bottomNavigationBar: CustomBottomNavigationBar(),
|
|
),
|
|
);
|
|
}
|
|
} |