Support for two device types
This commit is contained in:
@@ -123,8 +123,11 @@ class _AddDeviceState extends State<AddDevice> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildScanResultTiles() {
|
Widget _buildScanResultTiles() {
|
||||||
// final res = _scanResults.where((r) => r.advertisementData.advName == "BlindMaster Device" && r.rssi > -55);
|
// Filter for both BlindMaster-C6 and BlindMaster Device
|
||||||
final res = _scanResults.where((r) => r.advertisementData.advName == "BlindMaster-C6");
|
final res = _scanResults.where((r) =>
|
||||||
|
r.advertisementData.advName == "BlindMaster-C6" ||
|
||||||
|
r.advertisementData.advName == "BlindMaster Device"
|
||||||
|
);
|
||||||
return (res.isNotEmpty)
|
return (res.isNotEmpty)
|
||||||
? ListView(
|
? ListView(
|
||||||
children: [
|
children: [
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ class DeviceSetup extends StatefulWidget {
|
|||||||
class _DeviceSetupState extends State<DeviceSetup> {
|
class _DeviceSetupState extends State<DeviceSetup> {
|
||||||
bool refreshing = false;
|
bool refreshing = false;
|
||||||
List<BluetoothService> _services = [];
|
List<BluetoothService> _services = [];
|
||||||
|
int maxPorts = 4; // Default to multi-port
|
||||||
|
|
||||||
List<Map<String, dynamic>> networks = [];
|
List<Map<String, dynamic>> networks = [];
|
||||||
|
|
||||||
@@ -60,6 +61,13 @@ class _DeviceSetupState extends State<DeviceSetup> {
|
|||||||
|
|
||||||
@override void initState() {
|
@override void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
// Detect device type from platform name
|
||||||
|
final deviceName = widget.device.platformName;
|
||||||
|
if (deviceName == "BlindMaster-C6") {
|
||||||
|
maxPorts = 1;
|
||||||
|
} else if (deviceName == "BlindMaster Device") {
|
||||||
|
maxPorts = 4;
|
||||||
|
}
|
||||||
initSetup();
|
initSetup();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -308,7 +316,12 @@ class _DeviceSetupState extends State<DeviceSetup> {
|
|||||||
Navigator.push(
|
Navigator.push(
|
||||||
context,
|
context,
|
||||||
MaterialPageRoute(
|
MaterialPageRoute(
|
||||||
builder: (context) => SetDeviceName(tokenEntryChar: tokenEntryChar, authConfirmChar: authConfirmChar, device: widget.device),
|
builder: (context) => SetDeviceName(
|
||||||
|
tokenEntryChar: tokenEntryChar,
|
||||||
|
authConfirmChar: authConfirmChar,
|
||||||
|
device: widget.device,
|
||||||
|
maxPorts: maxPorts,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
).then((_) {
|
).then((_) {
|
||||||
if (widget.device.isConnected) {
|
if (widget.device.isConnected) {
|
||||||
|
|||||||
@@ -11,10 +11,17 @@ import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
|||||||
import 'package:google_fonts/google_fonts.dart';
|
import 'package:google_fonts/google_fonts.dart';
|
||||||
|
|
||||||
class SetDeviceName extends StatefulWidget {
|
class SetDeviceName extends StatefulWidget {
|
||||||
const SetDeviceName({super.key, required this.tokenEntryChar, required this.authConfirmChar, required this.device});
|
const SetDeviceName({
|
||||||
|
super.key,
|
||||||
|
required this.tokenEntryChar,
|
||||||
|
required this.authConfirmChar,
|
||||||
|
required this.device,
|
||||||
|
required this.maxPorts,
|
||||||
|
});
|
||||||
final BluetoothCharacteristic tokenEntryChar;
|
final BluetoothCharacteristic tokenEntryChar;
|
||||||
final BluetoothCharacteristic authConfirmChar;
|
final BluetoothCharacteristic authConfirmChar;
|
||||||
final BluetoothDevice device;
|
final BluetoothDevice device;
|
||||||
|
final int maxPorts;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<SetDeviceName> createState() => _SetDeviceNameState();
|
State<SetDeviceName> createState() => _SetDeviceNameState();
|
||||||
@@ -57,7 +64,10 @@ class _SetDeviceNameState extends State<SetDeviceName> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future addDevice(String name) async {
|
Future addDevice(String name) async {
|
||||||
final payload = {'deviceName': name};
|
final payload = {
|
||||||
|
'deviceName': name,
|
||||||
|
'maxPorts': widget.maxPorts,
|
||||||
|
};
|
||||||
String? token;
|
String? token;
|
||||||
try {
|
try {
|
||||||
final tokenResponse = await securePost(payload, 'add_device');
|
final tokenResponse = await securePost(payload, 'add_device');
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import 'package:blind_master/BlindMasterResources/error_snackbar.dart';
|
|||||||
import 'package:blind_master/BlindMasterResources/secure_transmissions.dart';
|
import 'package:blind_master/BlindMasterResources/secure_transmissions.dart';
|
||||||
import 'package:blind_master/BlindMasterScreens/addingDevices/add_device.dart';
|
import 'package:blind_master/BlindMasterScreens/addingDevices/add_device.dart';
|
||||||
import 'package:blind_master/BlindMasterScreens/individualControl/device_screen.dart';
|
import 'package:blind_master/BlindMasterScreens/individualControl/device_screen.dart';
|
||||||
|
import 'package:blind_master/BlindMasterScreens/individualControl/peripheral_screen.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
class DevicesMenu extends StatefulWidget {
|
class DevicesMenu extends StatefulWidget {
|
||||||
@@ -32,9 +33,11 @@ class _DevicesMenuState extends State<DevicesMenu> {
|
|||||||
final body = json.decode(response.body) as Map<String, dynamic>;
|
final body = json.decode(response.body) as Map<String, dynamic>;
|
||||||
final names = body['devices'] as List;
|
final names = body['devices'] as List;
|
||||||
final ids = body['device_ids'] as List;
|
final ids = body['device_ids'] as List;
|
||||||
|
final maxPortsList = body['max_ports'] as List;
|
||||||
devices = List.generate(names.length, (i) => {
|
devices = List.generate(names.length, (i) => {
|
||||||
'id': ids[i],
|
'id': ids[i],
|
||||||
'name': names[i],
|
'name': names[i],
|
||||||
|
'max_ports': maxPortsList[i],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
@@ -103,13 +106,52 @@ class _DevicesMenuState extends State<DevicesMenu> {
|
|||||||
leading: const Icon(Icons.blinds),
|
leading: const Icon(Icons.blinds),
|
||||||
title: Text(device['name']),
|
title: Text(device['name']),
|
||||||
trailing: const Icon(Icons.arrow_forward_ios_rounded),
|
trailing: const Icon(Icons.arrow_forward_ios_rounded),
|
||||||
onTap: () {
|
onTap: () async {
|
||||||
|
final maxPorts = device['max_ports'] as int;
|
||||||
|
if (maxPorts == 1) {
|
||||||
|
// Single-port device (C6): Get the single peripheral and navigate directly
|
||||||
|
try {
|
||||||
|
final payload = {"deviceId": device['id']};
|
||||||
|
final response = await secureGet('peripheral_list', queryParameters: payload);
|
||||||
|
if (response != null && response.statusCode == 200) {
|
||||||
|
final body = json.decode(response.body) as Map<String, dynamic>;
|
||||||
|
final ids = body['peripheral_ids'] as List;
|
||||||
|
final portNums = body['port_nums'] as List;
|
||||||
|
if (ids.isNotEmpty && mounted) {
|
||||||
|
Navigator.push(
|
||||||
|
context,
|
||||||
|
MaterialPageRoute(
|
||||||
|
builder: (context) => PeripheralScreen(
|
||||||
|
peripheralId: ids[0],
|
||||||
|
deviceId: device['id'],
|
||||||
|
peripheralNum: portNums[0],
|
||||||
|
deviceName: device['name'],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
).then((_) { getDevices(); });
|
||||||
|
} else {
|
||||||
|
// No peripheral found, show error
|
||||||
|
if (mounted) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text('No peripheral configured for this device'))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
if (mounted) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(errorSnackbar(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Multi-port device: Navigate to device screen
|
||||||
Navigator.push(
|
Navigator.push(
|
||||||
context,
|
context,
|
||||||
MaterialPageRoute(
|
MaterialPageRoute(
|
||||||
builder: (context) => DeviceScreen(deviceId: device['id']),
|
builder: (context) => DeviceScreen(deviceId: device['id']),
|
||||||
),
|
),
|
||||||
).then((_) { getDevices(); });
|
).then((_) { getDevices(); });
|
||||||
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
Reference in New Issue
Block a user