mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-15 09:45:22 +00:00
Bug fixes 2
This commit is contained in:
@ -296,14 +296,6 @@ class OneGangBloc extends Bloc<OneGangEvent, OneGangState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// void toggleCreateSchedule() {
|
|
||||||
// emit(LoadingInitialState());
|
|
||||||
// createSchedule = !createSchedule;
|
|
||||||
// selectedDays.clear();
|
|
||||||
// selectedTime = DateTime.now();
|
|
||||||
// emit(UpdateCreateScheduleState(createSchedule));
|
|
||||||
// emit(ChangeSlidingSegmentState(value: 1));
|
|
||||||
// }
|
|
||||||
|
|
||||||
void toggleCreateSchedule(ToggleCreateScheduleEvent event, Emitter<OneGangState> emit) {
|
void toggleCreateSchedule(ToggleCreateScheduleEvent event, Emitter<OneGangState> emit) {
|
||||||
emit(LoadingInitialState());
|
emit(LoadingInitialState());
|
||||||
@ -358,7 +350,6 @@ class OneGangBloc extends Bloc<OneGangEvent, OneGangState> {
|
|||||||
for (var status in response['status']) {
|
for (var status in response['status']) {
|
||||||
statusModelList.add(StatusModel.fromJson(status));
|
statusModelList.add(StatusModel.fromJson(status));
|
||||||
}
|
}
|
||||||
// deviceStatus = TwoGangModel.fromJson(statusModelList);
|
|
||||||
deviceStatus = OneGangModel.fromJson(statusModelList);
|
deviceStatus = OneGangModel.fromJson(statusModelList);
|
||||||
groupOneGangList.add(GroupOneGangModel(
|
groupOneGangList.add(GroupOneGangModel(
|
||||||
deviceId: devicesList[i].uuid ?? '',
|
deviceId: devicesList[i].uuid ?? '',
|
||||||
|
@ -114,7 +114,7 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
|||||||
DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$threeGangId');
|
DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$threeGangId');
|
||||||
Stream<DatabaseEvent> stream = ref.onValue;
|
Stream<DatabaseEvent> stream = ref.onValue;
|
||||||
|
|
||||||
stream.listen((DatabaseEvent event) async {
|
stream.listen((DatabaseEvent event) async {
|
||||||
if (_timer != null) {
|
if (_timer != null) {
|
||||||
await Future.delayed(const Duration(seconds: 2));
|
await Future.delayed(const Duration(seconds: 2));
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
bool createSchedule = false;
|
bool createSchedule = false;
|
||||||
List<ScheduleModel> listSchedule = [];
|
List<ScheduleModel> listSchedule = [];
|
||||||
|
|
||||||
TwoGangBloc({required this.twoGangId, required this.switchCode}) : super(InitialState()) {
|
TwoGangBloc({required this.twoGangId, required this.switchCode})
|
||||||
|
: super(InitialState()) {
|
||||||
on<InitialEvent>(_fetchTwoGangStatus);
|
on<InitialEvent>(_fetchTwoGangStatus);
|
||||||
on<TwoGangUpdated>(_twoGangUpdated);
|
on<TwoGangUpdated>(_twoGangUpdated);
|
||||||
on<ChangeFirstSwitchStatusEvent>(_changeFirstSwitch);
|
on<ChangeFirstSwitchStatusEvent>(_changeFirstSwitch);
|
||||||
@ -65,13 +66,15 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
|
|
||||||
int selectedTabIndex = 0;
|
int selectedTabIndex = 0;
|
||||||
|
|
||||||
void toggleSelectedIndex(ToggleSelectedEvent event, Emitter<TwoGangState> emit) {
|
void toggleSelectedIndex(
|
||||||
|
ToggleSelectedEvent event, Emitter<TwoGangState> emit) {
|
||||||
emit(LoadingInitialState());
|
emit(LoadingInitialState());
|
||||||
selectedTabIndex = event.index;
|
selectedTabIndex = event.index;
|
||||||
emit(ChangeSlidingSegmentState(value: selectedTabIndex));
|
emit(ChangeSlidingSegmentState(value: selectedTabIndex));
|
||||||
}
|
}
|
||||||
|
|
||||||
void toggleCreateSchedule(ToggleCreateScheduleEvent event, Emitter<TwoGangState> emit) {
|
void toggleCreateSchedule(
|
||||||
|
ToggleCreateScheduleEvent event, Emitter<TwoGangState> emit) {
|
||||||
emit(LoadingInitialState());
|
emit(LoadingInitialState());
|
||||||
createSchedule = !createSchedule;
|
createSchedule = !createSchedule;
|
||||||
selectedDays.clear();
|
selectedDays.clear();
|
||||||
@ -79,7 +82,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
emit(UpdateCreateScheduleState(createSchedule));
|
emit(UpdateCreateScheduleState(createSchedule));
|
||||||
}
|
}
|
||||||
|
|
||||||
void _fetchTwoGangStatus(InitialEvent event, Emitter<TwoGangState> emit) async {
|
void _fetchTwoGangStatus(
|
||||||
|
InitialEvent event, Emitter<TwoGangState> emit) async {
|
||||||
emit(LoadingInitialState());
|
emit(LoadingInitialState());
|
||||||
try {
|
try {
|
||||||
var response = await DevicesAPI.getDeviceStatus(twoGangId);
|
var response = await DevicesAPI.getDeviceStatus(twoGangId);
|
||||||
@ -98,18 +102,21 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
|
|
||||||
_listenToChanges() {
|
_listenToChanges() {
|
||||||
try {
|
try {
|
||||||
DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$twoGangId');
|
DatabaseReference ref =
|
||||||
|
FirebaseDatabase.instance.ref('device-status/$twoGangId');
|
||||||
Stream<DatabaseEvent> stream = ref.onValue;
|
Stream<DatabaseEvent> stream = ref.onValue;
|
||||||
|
|
||||||
stream.listen((DatabaseEvent event) async {
|
stream.listen((DatabaseEvent event) async {
|
||||||
if (_timer != null) {
|
if (_timer != null) {
|
||||||
await Future.delayed(const Duration(seconds: 2));
|
await Future.delayed(const Duration(seconds: 2));
|
||||||
}
|
}
|
||||||
Map<dynamic, dynamic> usersMap = event.snapshot.value as Map<dynamic, dynamic>;
|
Map<dynamic, dynamic> usersMap =
|
||||||
|
event.snapshot.value as Map<dynamic, dynamic>;
|
||||||
List<StatusModel> statusList = [];
|
List<StatusModel> statusList = [];
|
||||||
|
|
||||||
usersMap['status'].forEach((element) {
|
usersMap['status'].forEach((element) {
|
||||||
statusList.add(StatusModel(code: element['code'], value: element['value']));
|
statusList
|
||||||
|
.add(StatusModel(code: element['code'], value: element['value']));
|
||||||
});
|
});
|
||||||
|
|
||||||
deviceStatus = TwoGangModel.fromJson(statusList);
|
deviceStatus = TwoGangModel.fromJson(statusList);
|
||||||
@ -124,7 +131,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
emit(UpdateState(twoGangModel: deviceStatus));
|
emit(UpdateState(twoGangModel: deviceStatus));
|
||||||
}
|
}
|
||||||
|
|
||||||
void _changeFirstSwitch(ChangeFirstSwitchStatusEvent event, Emitter<TwoGangState> emit) async {
|
void _changeFirstSwitch(
|
||||||
|
ChangeFirstSwitchStatusEvent event, Emitter<TwoGangState> emit) async {
|
||||||
emit(LoadingNewSate(twoGangModel: deviceStatus));
|
emit(LoadingNewSate(twoGangModel: deviceStatus));
|
||||||
try {
|
try {
|
||||||
deviceStatus.firstSwitch = !event.value;
|
deviceStatus.firstSwitch = !event.value;
|
||||||
@ -135,7 +143,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
|
|
||||||
_timer = Timer(const Duration(milliseconds: 500), () async {
|
_timer = Timer(const Duration(milliseconds: 500), () async {
|
||||||
final response = await DevicesAPI.controlDevice(
|
final response = await DevicesAPI.controlDevice(
|
||||||
DeviceControlModel(deviceId: twoGangId, code: 'switch_1', value: !event.value),
|
DeviceControlModel(
|
||||||
|
deviceId: twoGangId, code: 'switch_1', value: !event.value),
|
||||||
twoGangId);
|
twoGangId);
|
||||||
|
|
||||||
if (!response['success']) {
|
if (!response['success']) {
|
||||||
@ -147,7 +156,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _changeSecondSwitch(ChangeSecondSwitchStatusEvent event, Emitter<TwoGangState> emit) async {
|
void _changeSecondSwitch(
|
||||||
|
ChangeSecondSwitchStatusEvent event, Emitter<TwoGangState> emit) async {
|
||||||
emit(LoadingNewSate(twoGangModel: deviceStatus));
|
emit(LoadingNewSate(twoGangModel: deviceStatus));
|
||||||
try {
|
try {
|
||||||
deviceStatus.secondSwitch = !event.value;
|
deviceStatus.secondSwitch = !event.value;
|
||||||
@ -157,7 +167,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
}
|
}
|
||||||
_timer = Timer(const Duration(milliseconds: 500), () async {
|
_timer = Timer(const Duration(milliseconds: 500), () async {
|
||||||
final response = await DevicesAPI.controlDevice(
|
final response = await DevicesAPI.controlDevice(
|
||||||
DeviceControlModel(deviceId: twoGangId, code: 'switch_2', value: !event.value),
|
DeviceControlModel(
|
||||||
|
deviceId: twoGangId, code: 'switch_2', value: !event.value),
|
||||||
twoGangId);
|
twoGangId);
|
||||||
|
|
||||||
if (!response['success']) {
|
if (!response['success']) {
|
||||||
@ -180,11 +191,15 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
final response = await Future.wait([
|
final response = await Future.wait([
|
||||||
DevicesAPI.controlDevice(
|
DevicesAPI.controlDevice(
|
||||||
DeviceControlModel(
|
DeviceControlModel(
|
||||||
deviceId: twoGangId, code: 'switch_1', value: deviceStatus.firstSwitch),
|
deviceId: twoGangId,
|
||||||
|
code: 'switch_1',
|
||||||
|
value: deviceStatus.firstSwitch),
|
||||||
twoGangId),
|
twoGangId),
|
||||||
DevicesAPI.controlDevice(
|
DevicesAPI.controlDevice(
|
||||||
DeviceControlModel(
|
DeviceControlModel(
|
||||||
deviceId: twoGangId, code: 'switch_2', value: deviceStatus.secondSwitch),
|
deviceId: twoGangId,
|
||||||
|
code: 'switch_2',
|
||||||
|
value: deviceStatus.secondSwitch),
|
||||||
twoGangId),
|
twoGangId),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@ -207,11 +222,15 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
final response = await Future.wait([
|
final response = await Future.wait([
|
||||||
DevicesAPI.controlDevice(
|
DevicesAPI.controlDevice(
|
||||||
DeviceControlModel(
|
DeviceControlModel(
|
||||||
deviceId: twoGangId, code: 'switch_1', value: deviceStatus.firstSwitch),
|
deviceId: twoGangId,
|
||||||
|
code: 'switch_1',
|
||||||
|
value: deviceStatus.firstSwitch),
|
||||||
twoGangId),
|
twoGangId),
|
||||||
DevicesAPI.controlDevice(
|
DevicesAPI.controlDevice(
|
||||||
DeviceControlModel(
|
DeviceControlModel(
|
||||||
deviceId: twoGangId, code: 'switch_2', value: deviceStatus.secondSwitch),
|
deviceId: twoGangId,
|
||||||
|
code: 'switch_2',
|
||||||
|
value: deviceStatus.secondSwitch),
|
||||||
twoGangId),
|
twoGangId),
|
||||||
]);
|
]);
|
||||||
if (response.every((element) => !element['success'])) {
|
if (response.every((element) => !element['success'])) {
|
||||||
@ -232,7 +251,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
groupTwoGangList[i].secondSwitch = true;
|
groupTwoGangList[i].secondSwitch = true;
|
||||||
}
|
}
|
||||||
emit(UpdateGroupState(twoGangList: groupTwoGangList, allSwitches: true));
|
emit(UpdateGroupState(twoGangList: groupTwoGangList, allSwitches: true));
|
||||||
List<String> allDeviceIds = groupTwoGangList.map((device) => device.deviceId).toList();
|
List<String> allDeviceIds =
|
||||||
|
groupTwoGangList.map((device) => device.deviceId).toList();
|
||||||
|
|
||||||
final response1 = await DevicesAPI.deviceBatchController(
|
final response1 = await DevicesAPI.deviceBatchController(
|
||||||
code: 'switch_1',
|
code: 'switch_1',
|
||||||
@ -267,18 +287,19 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
|
|
||||||
emit(UpdateGroupState(twoGangList: groupTwoGangList, allSwitches: false));
|
emit(UpdateGroupState(twoGangList: groupTwoGangList, allSwitches: false));
|
||||||
|
|
||||||
List<String> allDeviceIds = groupTwoGangList.map((device) => device.deviceId).toList();
|
List<String> allDeviceIds =
|
||||||
|
groupTwoGangList.map((device) => device.deviceId).toList();
|
||||||
|
|
||||||
final response1 = await DevicesAPI.deviceBatchController(
|
final response1 = await DevicesAPI.deviceBatchController(
|
||||||
code: 'switch_1',
|
code: 'switch_1',
|
||||||
devicesUuid: allDeviceIds,
|
devicesUuid: allDeviceIds,
|
||||||
value: true,
|
value: false,
|
||||||
);
|
);
|
||||||
|
|
||||||
final response2 = await DevicesAPI.deviceBatchController(
|
final response2 = await DevicesAPI.deviceBatchController(
|
||||||
code: 'switch_2',
|
code: 'switch_2',
|
||||||
devicesUuid: allDeviceIds,
|
devicesUuid: allDeviceIds,
|
||||||
value: true,
|
value: false,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (response1['failedResults'].toString() != '[]' ||
|
if (response1['failedResults'].toString() != '[]' ||
|
||||||
@ -292,17 +313,20 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _changeSliding(ChangeSlidingSegment event, Emitter<TwoGangState> emit) async {
|
void _changeSliding(
|
||||||
|
ChangeSlidingSegment event, Emitter<TwoGangState> emit) async {
|
||||||
emit(ChangeSlidingSegmentState(value: event.value));
|
emit(ChangeSlidingSegmentState(value: event.value));
|
||||||
}
|
}
|
||||||
|
|
||||||
void _setCounterValue(SetCounterValue event, Emitter<TwoGangState> emit) async {
|
void _setCounterValue(
|
||||||
|
SetCounterValue event, Emitter<TwoGangState> emit) async {
|
||||||
emit(LoadingNewSate(twoGangModel: deviceStatus));
|
emit(LoadingNewSate(twoGangModel: deviceStatus));
|
||||||
int seconds = 0;
|
int seconds = 0;
|
||||||
try {
|
try {
|
||||||
seconds = event.duration.inSeconds;
|
seconds = event.duration.inSeconds;
|
||||||
final response = await DevicesAPI.controlDevice(
|
final response = await DevicesAPI.controlDevice(
|
||||||
DeviceControlModel(deviceId: twoGangId, code: event.deviceCode, value: seconds),
|
DeviceControlModel(
|
||||||
|
deviceId: twoGangId, code: event.deviceCode, value: seconds),
|
||||||
twoGangId);
|
twoGangId);
|
||||||
|
|
||||||
if (response['success'] ?? false) {
|
if (response['success'] ?? false) {
|
||||||
@ -327,7 +351,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _getCounterValue(GetCounterEvent event, Emitter<TwoGangState> emit) async {
|
void _getCounterValue(
|
||||||
|
GetCounterEvent event, Emitter<TwoGangState> emit) async {
|
||||||
emit(LoadingInitialState());
|
emit(LoadingInitialState());
|
||||||
try {
|
try {
|
||||||
add(GetScheduleEvent());
|
add(GetScheduleEvent());
|
||||||
@ -435,7 +460,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
deviceId: twoGangId,
|
deviceId: twoGangId,
|
||||||
);
|
);
|
||||||
List<dynamic> jsonData = response;
|
List<dynamic> jsonData = response;
|
||||||
listSchedule = jsonData.map((item) => ScheduleModel.fromJson(item)).toList();
|
listSchedule =
|
||||||
|
jsonData.map((item) => ScheduleModel.fromJson(item)).toList();
|
||||||
emit(InitialState());
|
emit(InitialState());
|
||||||
} on DioException catch (e) {
|
} on DioException catch (e) {
|
||||||
final errorData = e.response!.data;
|
final errorData = e.response!.data;
|
||||||
@ -446,12 +472,13 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
|
|
||||||
int? getTimeStampWithoutSeconds(DateTime? dateTime) {
|
int? getTimeStampWithoutSeconds(DateTime? dateTime) {
|
||||||
if (dateTime == null) return null;
|
if (dateTime == null) return null;
|
||||||
DateTime dateTimeWithoutSeconds =
|
DateTime dateTimeWithoutSeconds = DateTime(dateTime.year, dateTime.month,
|
||||||
DateTime(dateTime.year, dateTime.month, dateTime.day, dateTime.hour, dateTime.minute);
|
dateTime.day, dateTime.hour, dateTime.minute);
|
||||||
return dateTimeWithoutSeconds.millisecondsSinceEpoch ~/ 1000;
|
return dateTimeWithoutSeconds.millisecondsSinceEpoch ~/ 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future toggleRepeat(ToggleScheduleEvent event, Emitter<TwoGangState> emit) async {
|
Future toggleRepeat(
|
||||||
|
ToggleScheduleEvent event, Emitter<TwoGangState> emit) async {
|
||||||
try {
|
try {
|
||||||
emit(LoadingInitialState());
|
emit(LoadingInitialState());
|
||||||
final response = await DevicesAPI.changeSchedule(
|
final response = await DevicesAPI.changeSchedule(
|
||||||
@ -470,7 +497,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future deleteSchedule(DeleteScheduleEvent event, Emitter<TwoGangState> emit) async {
|
Future deleteSchedule(
|
||||||
|
DeleteScheduleEvent event, Emitter<TwoGangState> emit) async {
|
||||||
try {
|
try {
|
||||||
emit(LoadingInitialState());
|
emit(LoadingInitialState());
|
||||||
final response = await DevicesAPI.deleteSchedule(
|
final response = await DevicesAPI.deleteSchedule(
|
||||||
@ -490,7 +518,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _fetchTwoGangWizardStatus(InitialWizardEvent event, Emitter<TwoGangState> emit) async {
|
void _fetchTwoGangWizardStatus(
|
||||||
|
InitialWizardEvent event, Emitter<TwoGangState> emit) async {
|
||||||
emit(LoadingInitialState());
|
emit(LoadingInitialState());
|
||||||
try {
|
try {
|
||||||
devicesList = [];
|
devicesList = [];
|
||||||
@ -500,7 +529,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
HomeCubit.getInstance().selectedSpace?.id ?? '', '2G');
|
HomeCubit.getInstance().selectedSpace?.id ?? '', '2G');
|
||||||
|
|
||||||
for (int i = 0; i < devicesList.length; i++) {
|
for (int i = 0; i < devicesList.length; i++) {
|
||||||
var response = await DevicesAPI.getDeviceStatus(devicesList[i].uuid ?? '');
|
var response =
|
||||||
|
await DevicesAPI.getDeviceStatus(devicesList[i].uuid ?? '');
|
||||||
List<StatusModel> statusModelList = [];
|
List<StatusModel> statusModelList = [];
|
||||||
for (var status in response['status']) {
|
for (var status in response['status']) {
|
||||||
statusModelList.add(StatusModel.fromJson(status));
|
statusModelList.add(StatusModel.fromJson(status));
|
||||||
@ -523,15 +553,16 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
emit(UpdateGroupState(twoGangList: groupTwoGangList, allSwitches: allSwitchesOn));
|
emit(UpdateGroupState(
|
||||||
|
twoGangList: groupTwoGangList, allSwitches: allSwitchesOn));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
emit(FailedState(error: e.toString()));
|
emit(FailedState(error: e.toString()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _changeFirstWizardSwitch(
|
void _changeFirstWizardSwitch(ChangeFirstWizardSwitchStatusEvent event,
|
||||||
ChangeFirstWizardSwitchStatusEvent event, Emitter<TwoGangState> emit) async {
|
Emitter<TwoGangState> emit) async {
|
||||||
emit(LoadingNewSate(twoGangModel: deviceStatus));
|
emit(LoadingNewSate(twoGangModel: deviceStatus));
|
||||||
try {
|
try {
|
||||||
bool allSwitchesValue = true;
|
bool allSwitchesValue = true;
|
||||||
@ -544,9 +575,11 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
emit(UpdateGroupState(twoGangList: groupTwoGangList, allSwitches: allSwitchesValue));
|
emit(UpdateGroupState(
|
||||||
|
twoGangList: groupTwoGangList, allSwitches: allSwitchesValue));
|
||||||
|
|
||||||
List<String> allDeviceIds = groupTwoGangList.map((device) => device.deviceId).toList();
|
List<String> allDeviceIds =
|
||||||
|
groupTwoGangList.map((device) => device.deviceId).toList();
|
||||||
final response = await DevicesAPI.deviceBatchController(
|
final response = await DevicesAPI.deviceBatchController(
|
||||||
code: 'switch_1',
|
code: 'switch_1',
|
||||||
devicesUuid: allDeviceIds,
|
devicesUuid: allDeviceIds,
|
||||||
@ -561,8 +594,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _changeSecondWizardSwitch(
|
void _changeSecondWizardSwitch(ChangeSecondWizardSwitchStatusEvent event,
|
||||||
ChangeSecondWizardSwitchStatusEvent event, Emitter<TwoGangState> emit) async {
|
Emitter<TwoGangState> emit) async {
|
||||||
emit(LoadingNewSate(twoGangModel: deviceStatus));
|
emit(LoadingNewSate(twoGangModel: deviceStatus));
|
||||||
try {
|
try {
|
||||||
bool allSwitchesValue = true;
|
bool allSwitchesValue = true;
|
||||||
@ -574,9 +607,11 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
|||||||
allSwitchesValue = false;
|
allSwitchesValue = false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
List<String> allDeviceIds = groupTwoGangList.map((device) => device.deviceId).toList();
|
List<String> allDeviceIds =
|
||||||
|
groupTwoGangList.map((device) => device.deviceId).toList();
|
||||||
|
|
||||||
emit(UpdateGroupState(twoGangList: groupTwoGangList, allSwitches: allSwitchesValue));
|
emit(UpdateGroupState(
|
||||||
|
twoGangList: groupTwoGangList, allSwitches: allSwitchesValue));
|
||||||
|
|
||||||
final response = await DevicesAPI.deviceBatchController(
|
final response = await DevicesAPI.deviceBatchController(
|
||||||
code: 'switch_2',
|
code: 'switch_2',
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
|
import 'package:firebase_database/firebase_database.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
|
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
|
||||||
@ -34,7 +35,8 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
|||||||
List<ScheduleModel> listSchedule = [];
|
List<ScheduleModel> listSchedule = [];
|
||||||
DateTime? selectedTime = DateTime.now();
|
DateTime? selectedTime = DateTime.now();
|
||||||
|
|
||||||
WaterHeaterBloc({required this.whId, required this.switchCode}) : super(WHInitialState()) {
|
WaterHeaterBloc({required this.whId, required this.switchCode})
|
||||||
|
: super(WHInitialState()) {
|
||||||
on<WaterHeaterInitial>(_fetchWaterHeaterStatus);
|
on<WaterHeaterInitial>(_fetchWaterHeaterStatus);
|
||||||
on<WaterHeaterSwitch>(_changeFirstSwitch);
|
on<WaterHeaterSwitch>(_changeFirstSwitch);
|
||||||
on<SetCounterValue>(_setCounterValue);
|
on<SetCounterValue>(_setCounterValue);
|
||||||
@ -56,9 +58,11 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
|||||||
on<GroupAllOnEvent>(_groupAllOn);
|
on<GroupAllOnEvent>(_groupAllOn);
|
||||||
on<GroupAllOffEvent>(_groupAllOff);
|
on<GroupAllOffEvent>(_groupAllOff);
|
||||||
on<ToggleCreateCirculate>(_toggleCreateCirculate);
|
on<ToggleCreateCirculate>(_toggleCreateCirculate);
|
||||||
|
on<WaterHeaterUpdated>(_waterHeaterUpdated);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _fetchWaterHeaterStatus(WaterHeaterInitial event, Emitter<WaterHeaterState> emit) async {
|
void _fetchWaterHeaterStatus(
|
||||||
|
WaterHeaterInitial event, Emitter<WaterHeaterState> emit) async {
|
||||||
emit(WHLoadingState());
|
emit(WHLoadingState());
|
||||||
try {
|
try {
|
||||||
var response = await DevicesAPI.getDeviceStatus(whId);
|
var response = await DevicesAPI.getDeviceStatus(whId);
|
||||||
@ -70,15 +74,48 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
|||||||
statusModelList,
|
statusModelList,
|
||||||
);
|
);
|
||||||
emit(UpdateState(whModel: deviceStatus));
|
emit(UpdateState(whModel: deviceStatus));
|
||||||
Future.delayed(const Duration(milliseconds: 500));
|
_listenToChanges();
|
||||||
// _listenToChanges();
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
emit(WHFailedState(errorMessage: e.toString()));
|
emit(WHFailedState(errorMessage: e.toString()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _changeFirstSwitch(WaterHeaterSwitch event, Emitter<WaterHeaterState> emit) async {
|
_listenToChanges() {
|
||||||
|
try {
|
||||||
|
DatabaseReference ref =
|
||||||
|
FirebaseDatabase.instance.ref('device-status/$whId');
|
||||||
|
Stream<DatabaseEvent> stream = ref.onValue;
|
||||||
|
|
||||||
|
stream.listen((DatabaseEvent event) async {
|
||||||
|
if (_timer != null) {
|
||||||
|
await Future.delayed(const Duration(seconds: 2));
|
||||||
|
}
|
||||||
|
Map<dynamic, dynamic> usersMap =
|
||||||
|
event.snapshot.value as Map<dynamic, dynamic>;
|
||||||
|
List<StatusModel> statusList = [];
|
||||||
|
|
||||||
|
usersMap['status'].forEach((element) {
|
||||||
|
statusList
|
||||||
|
.add(StatusModel(code: element['code'], value: element['value']));
|
||||||
|
});
|
||||||
|
|
||||||
|
deviceStatus = WHModel.fromJson(statusList);
|
||||||
|
if (!isClosed) {
|
||||||
|
add(WaterHeaterUpdated());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (_) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
_waterHeaterUpdated(
|
||||||
|
WaterHeaterUpdated event, Emitter<WaterHeaterState> emit) async {
|
||||||
|
emit(WHLoadingState());
|
||||||
|
emit(UpdateState(whModel: deviceStatus));
|
||||||
|
}
|
||||||
|
|
||||||
|
void _changeFirstSwitch(
|
||||||
|
WaterHeaterSwitch event, Emitter<WaterHeaterState> emit) async {
|
||||||
emit(LoadingNewSate(whModel: deviceStatus));
|
emit(LoadingNewSate(whModel: deviceStatus));
|
||||||
try {
|
try {
|
||||||
deviceStatus.firstSwitch = !event.whSwitch;
|
deviceStatus.firstSwitch = !event.whSwitch;
|
||||||
@ -88,7 +125,10 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
|||||||
}
|
}
|
||||||
_timer = Timer(const Duration(milliseconds: 500), () async {
|
_timer = Timer(const Duration(milliseconds: 500), () async {
|
||||||
final response = await DevicesAPI.controlDevice(
|
final response = await DevicesAPI.controlDevice(
|
||||||
DeviceControlModel(deviceId: whId, code: 'switch_1', value: deviceStatus.firstSwitch),
|
DeviceControlModel(
|
||||||
|
deviceId: whId,
|
||||||
|
code: 'switch_1',
|
||||||
|
value: deviceStatus.firstSwitch),
|
||||||
whId);
|
whId);
|
||||||
|
|
||||||
if (!response['success']) {
|
if (!response['success']) {
|
||||||
@ -102,13 +142,16 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
|||||||
|
|
||||||
//=====================---------- timer ----------------------------------------
|
//=====================---------- timer ----------------------------------------
|
||||||
|
|
||||||
void _setCounterValue(SetCounterValue event, Emitter<WaterHeaterState> emit) async {
|
void _setCounterValue(
|
||||||
|
SetCounterValue event, Emitter<WaterHeaterState> emit) async {
|
||||||
emit(LoadingNewSate(whModel: deviceStatus));
|
emit(LoadingNewSate(whModel: deviceStatus));
|
||||||
int seconds = 0;
|
int seconds = 0;
|
||||||
try {
|
try {
|
||||||
seconds = event.duration.inSeconds;
|
seconds = event.duration.inSeconds;
|
||||||
final response = await DevicesAPI.controlDevice(
|
final response = await DevicesAPI.controlDevice(
|
||||||
DeviceControlModel(deviceId: whId, code: event.deviceCode, value: seconds), whId);
|
DeviceControlModel(
|
||||||
|
deviceId: whId, code: event.deviceCode, value: seconds),
|
||||||
|
whId);
|
||||||
|
|
||||||
if (response['success'] ?? false) {
|
if (response['success'] ?? false) {
|
||||||
if (event.deviceCode == 'countdown_1') {
|
if (event.deviceCode == 'countdown_1') {
|
||||||
@ -130,7 +173,8 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _getCounterValue(GetCounterEvent event, Emitter<WaterHeaterState> emit) async {
|
void _getCounterValue(
|
||||||
|
GetCounterEvent event, Emitter<WaterHeaterState> emit) async {
|
||||||
emit(WHLoadingState());
|
emit(WHLoadingState());
|
||||||
try {
|
try {
|
||||||
var response = await DevicesAPI.getDeviceStatus(whId);
|
var response = await DevicesAPI.getDeviceStatus(whId);
|
||||||
@ -220,7 +264,8 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
|||||||
deviceId: whId,
|
deviceId: whId,
|
||||||
);
|
);
|
||||||
List<dynamic> jsonData = response;
|
List<dynamic> jsonData = response;
|
||||||
listSchedule = jsonData.map((item) => ScheduleModel.fromJson(item)).toList();
|
listSchedule =
|
||||||
|
jsonData.map((item) => ScheduleModel.fromJson(item)).toList();
|
||||||
emit(WHInitialState());
|
emit(WHInitialState());
|
||||||
} on DioException catch (e) {
|
} on DioException catch (e) {
|
||||||
final errorData = e.response!.data;
|
final errorData = e.response!.data;
|
||||||
@ -231,12 +276,13 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
|||||||
|
|
||||||
int? getTimeStampWithoutSeconds(DateTime? dateTime) {
|
int? getTimeStampWithoutSeconds(DateTime? dateTime) {
|
||||||
if (dateTime == null) return null;
|
if (dateTime == null) return null;
|
||||||
DateTime dateTimeWithoutSeconds =
|
DateTime dateTimeWithoutSeconds = DateTime(dateTime.year, dateTime.month,
|
||||||
DateTime(dateTime.year, dateTime.month, dateTime.day, dateTime.hour, dateTime.minute);
|
dateTime.day, dateTime.hour, dateTime.minute);
|
||||||
return dateTimeWithoutSeconds.millisecondsSinceEpoch ~/ 1000;
|
return dateTimeWithoutSeconds.millisecondsSinceEpoch ~/ 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future toggleChange(ToggleScheduleEvent event, Emitter<WaterHeaterState> emit) async {
|
Future toggleChange(
|
||||||
|
ToggleScheduleEvent event, Emitter<WaterHeaterState> emit) async {
|
||||||
try {
|
try {
|
||||||
emit(WHLoadingState());
|
emit(WHLoadingState());
|
||||||
final response = await DevicesAPI.changeSchedule(
|
final response = await DevicesAPI.changeSchedule(
|
||||||
@ -254,7 +300,8 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future deleteSchedule(DeleteScheduleEvent event, Emitter<WaterHeaterState> emit) async {
|
Future deleteSchedule(
|
||||||
|
DeleteScheduleEvent event, Emitter<WaterHeaterState> emit) async {
|
||||||
try {
|
try {
|
||||||
emit(WHLoadingState());
|
emit(WHLoadingState());
|
||||||
final response = await DevicesAPI.deleteSchedule(
|
final response = await DevicesAPI.deleteSchedule(
|
||||||
@ -273,7 +320,8 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _toggleCreateCirculate(ToggleCreateCirculate event, Emitter<WaterHeaterState> emit) {
|
void _toggleCreateCirculate(
|
||||||
|
ToggleCreateCirculate event, Emitter<WaterHeaterState> emit) {
|
||||||
emit(WHLoadingState());
|
emit(WHLoadingState());
|
||||||
createCirculate = !createCirculate;
|
createCirculate = !createCirculate;
|
||||||
selectedDays.clear();
|
selectedDays.clear();
|
||||||
@ -281,13 +329,15 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
|||||||
emit(UpdateCreateScheduleState(createCirculate));
|
emit(UpdateCreateScheduleState(createCirculate));
|
||||||
}
|
}
|
||||||
|
|
||||||
void toggleSelectedIndex(ToggleSelectedEvent event, Emitter<WaterHeaterState> emit) {
|
void toggleSelectedIndex(
|
||||||
|
ToggleSelectedEvent event, Emitter<WaterHeaterState> emit) {
|
||||||
emit(WHLoadingState());
|
emit(WHLoadingState());
|
||||||
selectedTabIndex = event.index;
|
selectedTabIndex = event.index;
|
||||||
emit(ChangeSlidingSegmentState(value: selectedTabIndex));
|
emit(ChangeSlidingSegmentState(value: selectedTabIndex));
|
||||||
}
|
}
|
||||||
|
|
||||||
void toggleCreateSchedule(ToggleCreateScheduleEvent event, Emitter<WaterHeaterState> emit) {
|
void toggleCreateSchedule(
|
||||||
|
ToggleCreateScheduleEvent event, Emitter<WaterHeaterState> emit) {
|
||||||
emit(WHLoadingState());
|
emit(WHLoadingState());
|
||||||
createSchedule = !createSchedule;
|
createSchedule = !createSchedule;
|
||||||
selectedDays.clear();
|
selectedDays.clear();
|
||||||
@ -336,8 +386,8 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
|||||||
List<GroupWHModel> groupWaterHeaterList = [];
|
List<GroupWHModel> groupWaterHeaterList = [];
|
||||||
bool allSwitchesOn = true;
|
bool allSwitchesOn = true;
|
||||||
|
|
||||||
void _changeFirstWizardSwitch(
|
void _changeFirstWizardSwitch(ChangeFirstWizardSwitchStatusEvent event,
|
||||||
ChangeFirstWizardSwitchStatusEvent event, Emitter<WaterHeaterState> emit) async {
|
Emitter<WaterHeaterState> emit) async {
|
||||||
emit(LoadingNewSate(whModel: deviceStatus));
|
emit(LoadingNewSate(whModel: deviceStatus));
|
||||||
try {
|
try {
|
||||||
bool allSwitchesValue = true;
|
bool allSwitchesValue = true;
|
||||||
@ -349,7 +399,8 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
|||||||
allSwitchesValue = false;
|
allSwitchesValue = false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
emit(UpdateGroupState(twoGangList: groupWaterHeaterList, allSwitches: allSwitchesValue));
|
emit(UpdateGroupState(
|
||||||
|
twoGangList: groupWaterHeaterList, allSwitches: allSwitchesValue));
|
||||||
|
|
||||||
final response = await DevicesAPI.deviceBatchController(
|
final response = await DevicesAPI.deviceBatchController(
|
||||||
code: 'switch_1',
|
code: 'switch_1',
|
||||||
@ -364,7 +415,8 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _fetchWHWizardStatus(InitialWizardEvent event, Emitter<WaterHeaterState> emit) async {
|
void _fetchWHWizardStatus(
|
||||||
|
InitialWizardEvent event, Emitter<WaterHeaterState> emit) async {
|
||||||
emit(WHLoadingState());
|
emit(WHLoadingState());
|
||||||
try {
|
try {
|
||||||
devicesList = [];
|
devicesList = [];
|
||||||
@ -374,7 +426,8 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
|||||||
HomeCubit.getInstance().selectedSpace?.id ?? '', 'WH');
|
HomeCubit.getInstance().selectedSpace?.id ?? '', 'WH');
|
||||||
|
|
||||||
for (int i = 0; i < devicesList.length; i++) {
|
for (int i = 0; i < devicesList.length; i++) {
|
||||||
var response = await DevicesAPI.getDeviceStatus(devicesList[i].uuid ?? '');
|
var response =
|
||||||
|
await DevicesAPI.getDeviceStatus(devicesList[i].uuid ?? '');
|
||||||
List<StatusModel> statusModelList = [];
|
List<StatusModel> statusModelList = [];
|
||||||
for (var status in response['status']) {
|
for (var status in response['status']) {
|
||||||
statusModelList.add(StatusModel.fromJson(status));
|
statusModelList.add(StatusModel.fromJson(status));
|
||||||
@ -396,23 +449,27 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
|||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
emit(UpdateGroupState(twoGangList: groupWaterHeaterList, allSwitches: allSwitchesOn));
|
emit(UpdateGroupState(
|
||||||
|
twoGangList: groupWaterHeaterList, allSwitches: allSwitchesOn));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// emit(FailedState(error: e.toString()));
|
// emit(FailedState(error: e.toString()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _groupAllOn(GroupAllOnEvent event, Emitter<WaterHeaterState> emit) async {
|
void _groupAllOn(
|
||||||
|
GroupAllOnEvent event, Emitter<WaterHeaterState> emit) async {
|
||||||
emit(LoadingNewSate(whModel: deviceStatus));
|
emit(LoadingNewSate(whModel: deviceStatus));
|
||||||
try {
|
try {
|
||||||
for (int i = 0; i < groupWaterHeaterList.length; i++) {
|
for (int i = 0; i < groupWaterHeaterList.length; i++) {
|
||||||
groupWaterHeaterList[i].firstSwitch = true;
|
groupWaterHeaterList[i].firstSwitch = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
emit(UpdateGroupState(twoGangList: groupWaterHeaterList, allSwitches: true));
|
emit(UpdateGroupState(
|
||||||
|
twoGangList: groupWaterHeaterList, allSwitches: true));
|
||||||
|
|
||||||
List<String> allDeviceIds = groupWaterHeaterList.map((device) => device.deviceId).toList();
|
List<String> allDeviceIds =
|
||||||
|
groupWaterHeaterList.map((device) => device.deviceId).toList();
|
||||||
|
|
||||||
final response = await DevicesAPI.deviceBatchController(
|
final response = await DevicesAPI.deviceBatchController(
|
||||||
code: 'switch_1',
|
code: 'switch_1',
|
||||||
@ -430,15 +487,18 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _groupAllOff(GroupAllOffEvent event, Emitter<WaterHeaterState> emit) async {
|
void _groupAllOff(
|
||||||
|
GroupAllOffEvent event, Emitter<WaterHeaterState> emit) async {
|
||||||
emit(LoadingNewSate(whModel: deviceStatus));
|
emit(LoadingNewSate(whModel: deviceStatus));
|
||||||
try {
|
try {
|
||||||
for (int i = 0; i < groupWaterHeaterList.length; i++) {
|
for (int i = 0; i < groupWaterHeaterList.length; i++) {
|
||||||
groupWaterHeaterList[i].firstSwitch = false;
|
groupWaterHeaterList[i].firstSwitch = false;
|
||||||
}
|
}
|
||||||
emit(UpdateGroupState(twoGangList: groupWaterHeaterList, allSwitches: false));
|
emit(UpdateGroupState(
|
||||||
|
twoGangList: groupWaterHeaterList, allSwitches: false));
|
||||||
|
|
||||||
List<String> allDeviceIds = groupWaterHeaterList.map((device) => device.deviceId).toList();
|
List<String> allDeviceIds =
|
||||||
|
groupWaterHeaterList.map((device) => device.deviceId).toList();
|
||||||
|
|
||||||
final response = await DevicesAPI.deviceBatchController(
|
final response = await DevicesAPI.deviceBatchController(
|
||||||
code: 'switch_1',
|
code: 'switch_1',
|
||||||
|
Reference in New Issue
Block a user