fix command endpoint

This commit is contained in:
2026-03-10 12:24:22 +01:00
parent bb666e8ab6
commit 3cbf936583
5 changed files with 44 additions and 85 deletions

View File

@@ -42,6 +42,7 @@ class ControlPanelViewModel extends Notifier<ControlPanelViewState> {
devices: devices,
selectedDevice: devices.first,
);
_selectedDeviceNotifier.setSelectedDevice(devices.first);
final positionLists = await Future.wait(
devices.map(

View File

@@ -13,67 +13,26 @@ class CommandsRemoteDatasourceImpl implements CommandsRemoteDatasource {
Future<void> send({
required SendCommandRequestModel request
}) async {
final response = await safeCall(
() => _repository.post<Map<String, dynamic>>(
'/commands',
body: request,
),
'Error in command',
);
print('RESPONSE:');
print(response.data);
try{
final response = await safeCall(
() => _repository.post<Map<String, dynamic>>(
'/commands',
body: request.toJson(),
),
'Error in command ${request.command}',
);
/*final data = response.data;
if (data == null || data.isEmpty) {
throw Exception('Empty response from /auth/login');
final data = response.data;
if (data == null || data.isEmpty) {
throw Exception('Empty response from command ${request.command}');
}
if (response.statusCode == 500) {
throw Exception('Server error from command ${request.command}');
}
// return CommandsResponseModel.fromJson(data);
} catch(e) {
return;
}
return CommandsResponseModel.fromJson(data);*/
}
@override
Future<void> twoFARequestCode({
required String token,
required String methodType,
}) =>
safeCall(
() => _repository.post<void>(
'/auth/2fa/request-code',
body: <String, dynamic>{'token': token, 'methodType': methodType},
),
'Error in twoFARequestCode',
);
@override
Future<void> twoFASendCode({
required String token,
required String code,
required String methodType,
}) =>
safeCall(
() => _repository.post<void>(
'/auth/twofa/login',
body: <String, dynamic>{
'token': token,
'code': code,
'methodType': methodType,
'rememberMe': true,
},
),
'Error in twoFASendCode',
);
@override
Future<bool> hasDevices() async {
final response = await safeCall(
() => _repository.get<Map<String, dynamic>>('/devices'),
'Error fetching devices',
);
final data = response.data;
if (data == null) return false;
final total = data['total'] as int? ?? 0;
return total > 0;
}
}

View File

@@ -6,12 +6,11 @@ part 'send_command_request_model.g.dart';
@freezed
abstract class SendCommandRequestModel with _$SendCommandRequestModel {
const factory SendCommandRequestModel({
required String deviceName,
required String device,
required String command,
Map<String, dynamic>? data,
}) = _SendCommandRequestModel;
factory SendCommandRequestModel.fromJson(Map<String, dynamic> json) =>
_$SendCommandRequestModelFromJson(json);
}

View File

@@ -15,7 +15,7 @@ T _$identity<T>(T value) => value;
/// @nodoc
mixin _$SendCommandRequestModel {
String get deviceName; String get command; Map<String, dynamic>? get data;
String get device; String get command; Map<String, dynamic>? get data;
/// Create a copy of SendCommandRequestModel
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@@ -28,16 +28,16 @@ $SendCommandRequestModelCopyWith<SendCommandRequestModel> get copyWith => _$Send
@override
bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is SendCommandRequestModel&&(identical(other.deviceName, deviceName) || other.deviceName == deviceName)&&(identical(other.command, command) || other.command == command)&&const DeepCollectionEquality().equals(other.data, data));
return identical(this, other) || (other.runtimeType == runtimeType&&other is SendCommandRequestModel&&(identical(other.device, device) || other.device == device)&&(identical(other.command, command) || other.command == command)&&const DeepCollectionEquality().equals(other.data, data));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType,deviceName,command,const DeepCollectionEquality().hash(data));
int get hashCode => Object.hash(runtimeType,device,command,const DeepCollectionEquality().hash(data));
@override
String toString() {
return 'SendCommandRequestModel(deviceName: $deviceName, command: $command, data: $data)';
return 'SendCommandRequestModel(device: $device, command: $command, data: $data)';
}
@@ -48,7 +48,7 @@ abstract mixin class $SendCommandRequestModelCopyWith<$Res> {
factory $SendCommandRequestModelCopyWith(SendCommandRequestModel value, $Res Function(SendCommandRequestModel) _then) = _$SendCommandRequestModelCopyWithImpl;
@useResult
$Res call({
String deviceName, String command, Map<String, dynamic>? data
String device, String command, Map<String, dynamic>? data
});
@@ -65,9 +65,9 @@ class _$SendCommandRequestModelCopyWithImpl<$Res>
/// Create a copy of SendCommandRequestModel
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline') @override $Res call({Object? deviceName = null,Object? command = null,Object? data = freezed,}) {
@pragma('vm:prefer-inline') @override $Res call({Object? device = null,Object? command = null,Object? data = freezed,}) {
return _then(_self.copyWith(
deviceName: null == deviceName ? _self.deviceName : deviceName // ignore: cast_nullable_to_non_nullable
device: null == device ? _self.device : device // ignore: cast_nullable_to_non_nullable
as String,command: null == command ? _self.command : command // ignore: cast_nullable_to_non_nullable
as String,data: freezed == data ? _self.data : data // ignore: cast_nullable_to_non_nullable
as Map<String, dynamic>?,
@@ -155,10 +155,10 @@ return $default(_that);case _:
/// }
/// ```
@optionalTypeArgs TResult maybeWhen<TResult extends Object?>(TResult Function( String deviceName, String command, Map<String, dynamic>? data)? $default,{required TResult orElse(),}) {final _that = this;
@optionalTypeArgs TResult maybeWhen<TResult extends Object?>(TResult Function( String device, String command, Map<String, dynamic>? data)? $default,{required TResult orElse(),}) {final _that = this;
switch (_that) {
case _SendCommandRequestModel() when $default != null:
return $default(_that.deviceName,_that.command,_that.data);case _:
return $default(_that.device,_that.command,_that.data);case _:
return orElse();
}
@@ -176,10 +176,10 @@ return $default(_that.deviceName,_that.command,_that.data);case _:
/// }
/// ```
@optionalTypeArgs TResult when<TResult extends Object?>(TResult Function( String deviceName, String command, Map<String, dynamic>? data) $default,) {final _that = this;
@optionalTypeArgs TResult when<TResult extends Object?>(TResult Function( String device, String command, Map<String, dynamic>? data) $default,) {final _that = this;
switch (_that) {
case _SendCommandRequestModel():
return $default(_that.deviceName,_that.command,_that.data);case _:
return $default(_that.device,_that.command,_that.data);case _:
throw StateError('Unexpected subclass');
}
@@ -196,10 +196,10 @@ return $default(_that.deviceName,_that.command,_that.data);case _:
/// }
/// ```
@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>(TResult? Function( String deviceName, String command, Map<String, dynamic>? data)? $default,) {final _that = this;
@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>(TResult? Function( String device, String command, Map<String, dynamic>? data)? $default,) {final _that = this;
switch (_that) {
case _SendCommandRequestModel() when $default != null:
return $default(_that.deviceName,_that.command,_that.data);case _:
return $default(_that.device,_that.command,_that.data);case _:
return null;
}
@@ -211,10 +211,10 @@ return $default(_that.deviceName,_that.command,_that.data);case _:
@JsonSerializable()
class _SendCommandRequestModel implements SendCommandRequestModel {
const _SendCommandRequestModel({required this.deviceName, required this.command, final Map<String, dynamic>? data}): _data = data;
const _SendCommandRequestModel({required this.device, required this.command, final Map<String, dynamic>? data}): _data = data;
factory _SendCommandRequestModel.fromJson(Map<String, dynamic> json) => _$SendCommandRequestModelFromJson(json);
@override final String deviceName;
@override final String device;
@override final String command;
final Map<String, dynamic>? _data;
@override Map<String, dynamic>? get data {
@@ -239,16 +239,16 @@ Map<String, dynamic> toJson() {
@override
bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is _SendCommandRequestModel&&(identical(other.deviceName, deviceName) || other.deviceName == deviceName)&&(identical(other.command, command) || other.command == command)&&const DeepCollectionEquality().equals(other._data, _data));
return identical(this, other) || (other.runtimeType == runtimeType&&other is _SendCommandRequestModel&&(identical(other.device, device) || other.device == device)&&(identical(other.command, command) || other.command == command)&&const DeepCollectionEquality().equals(other._data, _data));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType,deviceName,command,const DeepCollectionEquality().hash(_data));
int get hashCode => Object.hash(runtimeType,device,command,const DeepCollectionEquality().hash(_data));
@override
String toString() {
return 'SendCommandRequestModel(deviceName: $deviceName, command: $command, data: $data)';
return 'SendCommandRequestModel(device: $device, command: $command, data: $data)';
}
@@ -259,7 +259,7 @@ abstract mixin class _$SendCommandRequestModelCopyWith<$Res> implements $SendCom
factory _$SendCommandRequestModelCopyWith(_SendCommandRequestModel value, $Res Function(_SendCommandRequestModel) _then) = __$SendCommandRequestModelCopyWithImpl;
@override @useResult
$Res call({
String deviceName, String command, Map<String, dynamic>? data
String device, String command, Map<String, dynamic>? data
});
@@ -276,9 +276,9 @@ class __$SendCommandRequestModelCopyWithImpl<$Res>
/// Create a copy of SendCommandRequestModel
/// with the given fields replaced by the non-null parameter values.
@override @pragma('vm:prefer-inline') $Res call({Object? deviceName = null,Object? command = null,Object? data = freezed,}) {
@override @pragma('vm:prefer-inline') $Res call({Object? device = null,Object? command = null,Object? data = freezed,}) {
return _then(_SendCommandRequestModel(
deviceName: null == deviceName ? _self.deviceName : deviceName // ignore: cast_nullable_to_non_nullable
device: null == device ? _self.device : device // ignore: cast_nullable_to_non_nullable
as String,command: null == command ? _self.command : command // ignore: cast_nullable_to_non_nullable
as String,data: freezed == data ? _self._data : data // ignore: cast_nullable_to_non_nullable
as Map<String, dynamic>?,

View File

@@ -9,7 +9,7 @@ part of 'send_command_request_model.dart';
_SendCommandRequestModel _$SendCommandRequestModelFromJson(
Map<String, dynamic> json,
) => _SendCommandRequestModel(
deviceName: json['deviceName'] as String,
device: json['device'] as String,
command: json['command'] as String,
data: json['data'] as Map<String, dynamic>?,
);
@@ -17,7 +17,7 @@ _SendCommandRequestModel _$SendCommandRequestModelFromJson(
Map<String, dynamic> _$SendCommandRequestModelToJson(
_SendCommandRequestModel instance,
) => <String, dynamic>{
'deviceName': instance.deviceName,
'device': instance.device,
'command': instance.command,
'data': instance.data,
};