mirror of
https://bitbucket.org/FransoletThomas/tablet-app.git
synced 2025-12-06 00:21:19 +00:00
Fix mqtt delete issue + show config if no config + update device etc
This commit is contained in:
parent
5a6624531f
commit
f95b0832b8
@ -18,7 +18,8 @@ class MQTTHelper {
|
||||
void onConnected(dynamic appContext) {
|
||||
print('Connected !!!!!!!!!!!! ----------------------------------');
|
||||
TabletAppContext tabletAppContext = appContext.getContext();
|
||||
tabletAppContext.clientMQTT.updates.listen((List<MqttReceivedMessage<MqttMessage>> c) {
|
||||
|
||||
tabletAppContext.clientMQTT.updates.listen((List<MqttReceivedMessage<MqttMessage>> c) async {
|
||||
final MqttPublishMessage message = c[0].payload;
|
||||
final payload = MqttPublishPayload.bytesToStringAsString(message.payload.message);
|
||||
print('Received message:$payload from topic: ${c[0].topic}');
|
||||
@ -31,17 +32,34 @@ class MQTTHelper {
|
||||
var configId = c[0].topic.split('/')[1];
|
||||
if (configId != null) {
|
||||
// Check if tablet config
|
||||
if (tabletAppContext.configuration.id == configId) {
|
||||
// refresh config
|
||||
try {
|
||||
PlayerMessageDTO playerMessage = PlayerMessageDTO.fromJson(jsonDecode(payload));
|
||||
var isConfigChanged = playerMessage.configChanged != null ? playerMessage.configChanged : false;
|
||||
if (isConfigChanged) {
|
||||
updateConfig(appContext);
|
||||
print("Check if tablet config");
|
||||
print(tabletAppContext.configuration);
|
||||
if (tabletAppContext.configuration != null) {
|
||||
if (tabletAppContext.configuration.id == configId) {
|
||||
// refresh config
|
||||
try {
|
||||
PlayerMessageDTO playerMessage = PlayerMessageDTO.fromJson(jsonDecode(payload));
|
||||
var isConfigChanged = playerMessage.configChanged != null ? playerMessage.configChanged : false;
|
||||
var isDeleted = playerMessage.isDeleted != null ? playerMessage.isDeleted : false;
|
||||
print(isDeleted);
|
||||
if (isDeleted) {
|
||||
// Clear all
|
||||
print("isDeleted");
|
||||
tabletAppContext.configuration = null;
|
||||
appContext.setContext(tabletAppContext);
|
||||
// UPDATE IT LOCALLY (SQLite)
|
||||
await DatabaseHelper.instance.update(tabletAppContext);
|
||||
|
||||
} else {
|
||||
if (isConfigChanged) {
|
||||
updateConfig(appContext);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch(e) {
|
||||
print('Error = $e');
|
||||
}
|
||||
}
|
||||
catch(e) {
|
||||
print('Error = $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -39,6 +39,6 @@ class TabletAppContext with ChangeNotifier{
|
||||
// Implement toString to make it easier to see information about
|
||||
@override
|
||||
String toString() {
|
||||
return 'TabletAppContext{id: $id, deviceId: $deviceId, selectedConfiguration: $configuration, language: $language, host: $host}';
|
||||
return 'TabletAppContext{id: $id, deviceId: $deviceId, configuration: $configuration, language: $language, host: $host}';
|
||||
}
|
||||
}
|
||||
@ -9,6 +9,7 @@ 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';
|
||||
@ -29,7 +30,7 @@ class ConfigViewWidget extends StatefulWidget {
|
||||
|
||||
class _ConfigViewWidget extends State<ConfigViewWidget> {
|
||||
Size sizeScreen = new Size(1080.0, 1920.0); // Tablet resolution
|
||||
String url = "http://192.168.31.96"; //DEV "http://192.168.31.96"
|
||||
String url; //DEV "http://192.168.31.96"
|
||||
bool configOk = false;
|
||||
|
||||
@override
|
||||
@ -37,6 +38,11 @@ class _ConfigViewWidget extends State<ConfigViewWidget> {
|
||||
final appContext = Provider.of<AppContext>(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,
|
||||
@ -75,7 +81,7 @@ class _ConfigViewWidget extends State<ConfigViewWidget> {
|
||||
configurations: snapshot.data,
|
||||
onChange: (ConfigurationDTO configurationOut) async {
|
||||
// CREATE DEVICE REQUEST
|
||||
createDevice(configurationOut, appContext);
|
||||
await createDevice(configurationOut, appContext);
|
||||
|
||||
Navigator.pushAndRemoveUntil(
|
||||
context,
|
||||
@ -128,6 +134,7 @@ class _ConfigViewWidget extends State<ConfigViewWidget> {
|
||||
padding: const EdgeInsets.only(left: 50, right: 50),
|
||||
child: RoundedInputField(
|
||||
hintText: "URL",
|
||||
initialValue: url,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
url = value;
|
||||
@ -210,7 +217,13 @@ class _ConfigViewWidget extends State<ConfigViewWidget> {
|
||||
appContext.setContext(tabletAppContext);
|
||||
|
||||
// STORE IT LOCALLY (SQLite)
|
||||
await DatabaseHelper.instance.insert(tabletAppContext);
|
||||
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",
|
||||
|
||||
@ -5,6 +5,7 @@ import 'package:managerapi/api.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:tablet_app/Components/loading.dart';
|
||||
import 'package:tablet_app/Helpers/MQTTHelper.dart';
|
||||
import 'package:tablet_app/Screens/Configuration/config_view.dart';
|
||||
import 'package:tablet_app/Screens/Map/map_context.dart';
|
||||
import 'package:tablet_app/Screens/Map/map_view.dart';
|
||||
import 'package:tablet_app/Models/map-marker.dart';
|
||||
@ -191,7 +192,8 @@ class _MainViewWidget extends State<MainViewWidget> {
|
||||
builder: (context, AsyncSnapshot<dynamic> snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.done) {
|
||||
if (snapshot.data == null) {
|
||||
return Text("ERROR");
|
||||
// Show select config
|
||||
return Text("");
|
||||
}
|
||||
else {
|
||||
return GridView.builder(
|
||||
@ -261,11 +263,23 @@ class _MainViewWidget extends State<MainViewWidget> {
|
||||
|
||||
Future<List<SectionDTO>> getSections(dynamic appContext) async {
|
||||
TabletAppContext tabletAppContext = await appContext.getContext();
|
||||
print("GET SECTIONS");
|
||||
print(tabletAppContext.toString());
|
||||
List<SectionDTO> sections = await tabletAppContext.clientAPI.sectionApi.sectionGetFromConfiguration(tabletAppContext.configuration.id);
|
||||
sections.sort((a, b) => a.order.compareTo(b.order));
|
||||
return sections;
|
||||
try {
|
||||
List<SectionDTO> sections = await tabletAppContext.clientAPI.sectionApi.sectionGetFromConfiguration(tabletAppContext.configuration.id);
|
||||
sections.sort((a, b) => a.order.compareTo(b.order));
|
||||
return sections;
|
||||
} catch (e) {
|
||||
print(e);
|
||||
print("IN CATCH");
|
||||
Navigator.pushAndRemoveUntil(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) {
|
||||
return ConfigViewWidget();
|
||||
},
|
||||
),
|
||||
(Route<dynamic> route) => false // For pushAndRemoveUntil
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -26,6 +26,11 @@ void main() async {
|
||||
// Get config from manager
|
||||
DeviceDetailDTO device = await localContext.clientAPI.deviceApi.deviceGetDetail(localContext.deviceId);
|
||||
localContext.configuration.id = device.configurationId;
|
||||
if (device.configurationId == null) {
|
||||
print("device.configurationId == null");
|
||||
localContext.configuration = null;
|
||||
isConfig = false;
|
||||
}
|
||||
} else {
|
||||
print("NO LOCAL DB !");
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user