Support for two device types
This commit is contained in:
@@ -123,8 +123,11 @@ class _AddDeviceState extends State<AddDevice> {
|
||||
}
|
||||
|
||||
Widget _buildScanResultTiles() {
|
||||
// final res = _scanResults.where((r) => r.advertisementData.advName == "BlindMaster Device" && r.rssi > -55);
|
||||
final res = _scanResults.where((r) => r.advertisementData.advName == "BlindMaster-C6");
|
||||
// Filter for both BlindMaster-C6 and BlindMaster Device
|
||||
final res = _scanResults.where((r) =>
|
||||
r.advertisementData.advName == "BlindMaster-C6" ||
|
||||
r.advertisementData.advName == "BlindMaster Device"
|
||||
);
|
||||
return (res.isNotEmpty)
|
||||
? ListView(
|
||||
children: [
|
||||
|
||||
@@ -46,6 +46,7 @@ class DeviceSetup extends StatefulWidget {
|
||||
class _DeviceSetupState extends State<DeviceSetup> {
|
||||
bool refreshing = false;
|
||||
List<BluetoothService> _services = [];
|
||||
int maxPorts = 4; // Default to multi-port
|
||||
|
||||
List<Map<String, dynamic>> networks = [];
|
||||
|
||||
@@ -60,6 +61,13 @@ class _DeviceSetupState extends State<DeviceSetup> {
|
||||
|
||||
@override void 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();
|
||||
}
|
||||
|
||||
@@ -308,7 +316,12 @@ class _DeviceSetupState extends State<DeviceSetup> {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => SetDeviceName(tokenEntryChar: tokenEntryChar, authConfirmChar: authConfirmChar, device: widget.device),
|
||||
builder: (context) => SetDeviceName(
|
||||
tokenEntryChar: tokenEntryChar,
|
||||
authConfirmChar: authConfirmChar,
|
||||
device: widget.device,
|
||||
maxPorts: maxPorts,
|
||||
),
|
||||
)
|
||||
).then((_) {
|
||||
if (widget.device.isConnected) {
|
||||
|
||||
@@ -11,10 +11,17 @@ import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
||||
import 'package:google_fonts/google_fonts.dart';
|
||||
|
||||
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 authConfirmChar;
|
||||
final BluetoothDevice device;
|
||||
final int maxPorts;
|
||||
|
||||
@override
|
||||
State<SetDeviceName> createState() => _SetDeviceNameState();
|
||||
@@ -57,7 +64,10 @@ class _SetDeviceNameState extends State<SetDeviceName> {
|
||||
}
|
||||
|
||||
Future addDevice(String name) async {
|
||||
final payload = {'deviceName': name};
|
||||
final payload = {
|
||||
'deviceName': name,
|
||||
'maxPorts': widget.maxPorts,
|
||||
};
|
||||
String? token;
|
||||
try {
|
||||
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/BlindMasterScreens/addingDevices/add_device.dart';
|
||||
import 'package:blind_master/BlindMasterScreens/individualControl/device_screen.dart';
|
||||
import 'package:blind_master/BlindMasterScreens/individualControl/peripheral_screen.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class DevicesMenu extends StatefulWidget {
|
||||
@@ -32,9 +33,11 @@ class _DevicesMenuState extends State<DevicesMenu> {
|
||||
final body = json.decode(response.body) as Map<String, dynamic>;
|
||||
final names = body['devices'] as List;
|
||||
final ids = body['device_ids'] as List;
|
||||
final maxPortsList = body['max_ports'] as List;
|
||||
devices = List.generate(names.length, (i) => {
|
||||
'id': ids[i],
|
||||
'name': names[i],
|
||||
'max_ports': maxPortsList[i],
|
||||
});
|
||||
}
|
||||
} catch(e) {
|
||||
@@ -103,13 +106,52 @@ class _DevicesMenuState extends State<DevicesMenu> {
|
||||
leading: const Icon(Icons.blinds),
|
||||
title: Text(device['name']),
|
||||
trailing: const Icon(Icons.arrow_forward_ios_rounded),
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => DeviceScreen(deviceId: device['id']),
|
||||
),
|
||||
).then((_) { getDevices(); });
|
||||
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(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => DeviceScreen(deviceId: device['id']),
|
||||
),
|
||||
).then((_) { getDevices(); });
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user