You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
queuing_system/lib/home/app_provider.dart

290 lines
10 KiB
Dart

2 years ago
import 'dart:async';
import 'dart:developer';
import 'dart:io';
import 'package:connectivity/connectivity.dart';
import 'package:flutter/cupertino.dart';
2 years ago
import 'package:flutter_tts/flutter_tts.dart';
2 years ago
import 'package:just_audio/just_audio.dart';
import 'package:queuing_system/core/api.dart';
2 years ago
import 'package:queuing_system/core/response_models/call_config_model.dart';
import 'package:queuing_system/core/response_models/patient_ticket_model.dart';
2 years ago
import 'package:queuing_system/utils/call_by_voice.dart';
import 'package:queuing_system/utils/call_type.dart';
import 'package:queuing_system/utils/signalR_utils.dart';
class AppProvider extends ChangeNotifier {
AppProvider() {
startSignalHubConnection();
listenNetworkConnectivity();
2 years ago
listenAudioPlayerEvents();
2 years ago
}
SignalRHelper signalRHelper = SignalRHelper();
final AudioPlayer audioPlayer = AudioPlayer();
2 years ago
FlutterTts flutterTts = FlutterTts();
2 years ago
CallConfig patientCallConfigurations = CallConfig();
2 years ago
List<PatientTicketModel> patientTickets = [];
List<PatientTicketModel> isQueuePatients = [];
2 years ago
String currentDeviceIp = "";
bool isCallingInProgress = false;
bool isInternetConnectionAvailable = true;
2 years ago
bool isApiCallNeeded = false;
2 years ago
updateInternetConnection(bool value) {
isInternetConnectionAvailable = value;
notifyListeners();
}
Future<void> getCurrentIP() async {
final ips = await NetworkInterface.list(type: InternetAddressType.IPv4);
for (var interface in ips) {
if (interface.name == "eth0") {
for (var address in interface.addresses) {
currentDeviceIp = address.address;
notifyListeners();
}
}
if (interface.name == "wlan0") {
for (var address in interface.addresses) {
currentDeviceIp = address.address;
notifyListeners();
}
}
}
}
Future<void> startSignalHubConnection() async {
if (!signalRHelper.getConnectionState()) {
2 years ago
await getCurrentIP().whenComplete(() => signalRHelper.startSignalRConnection(
currentDeviceIp,
onUpdateAvailable: onPingReceived,
onConnect: onConnect,
onConnecting: onConnecting,
onDisconnect: onDisconnect,
));
2 years ago
}
}
Future<void> callPatientsAPI() async {
patientTickets.clear();
2 years ago
API.getCallRequestInfoByClinicInfo(currentDeviceIp, onSuccess: (waitingCalls, isQueuePatientsCalls, callConfigs) async {
2 years ago
patientCallConfigurations = callConfigs;
if (waitingCalls.length > patientCallConfigurations.screenMaxDisplayPatients) {
patientTickets = waitingCalls.sublist(0, patientCallConfigurations.screenMaxDisplayPatients);
} else {
patientTickets = waitingCalls;
}
isQueuePatients = isQueuePatientsCalls;
notifyListeners();
2 years ago
if (patientTickets.isNotEmpty) {
voiceCallPatientTicket(patientTickets.first);
updatePatientTicket(patientTickets.first);
2 years ago
}
2 years ago
}, onFailure: (error) {
log("Api call failed with this error: ${error.toString()}");
});
}
onPingReceived(data) async {
2 years ago
log("isCallingInProgress: $isCallingInProgress");
log("isApiCallNeeded: $isApiCallNeeded");
2 years ago
if (patientTickets.isNotEmpty) {
2 years ago
if (isCallingInProgress) {
isApiCallNeeded = true;
2 years ago
} else {
await callPatientsAPI();
}
} else {
await callPatientsAPI();
}
}
2 years ago
String getCallTypeText(PatientTicketModel ticket, CallConfig callConfig) {
2 years ago
final callType = ticket.getCallType();
switch (callType) {
case CallType.vitalSign:
return callConfig.vitalSignText;
case CallType.doctor:
return callConfig.doctorText;
case CallType.procedure:
return callConfig.procedureText;
case CallType.vaccination:
return callConfig.vaccinationText;
case CallType.nebulization:
return callConfig.nebulizationText;
default:
return callConfig.vitalSignText;
}
}
CallByVoice? voiceCaller;
PatientTicketModel currentPatient = PatientTicketModel();
2 years ago
2 years ago
voiceCallPatientTicket(PatientTicketModel patientTicket) async {
currentPatient = patientTicket;
2 years ago
isCallingInProgress = true;
2 years ago
log("Setting isCallingInProgress : $isCallingInProgress");
if (patientTicket.isToneReq && !patientTicket.isQueue) {
audioPlayer.setAsset("assets/tones/call_tone.mp3");
await audioPlayer.play();
await Future.delayed(const Duration(seconds: 3));
2 years ago
}
2 years ago
if (patientTicket.isVoiceReq && voiceCaller == null && !patientTicket.isQueue) {
final postVoice = getCallTypeText(patientTicket, patientCallConfigurations);
voiceCaller = CallByVoice(preVoice: "Ticket Number", ticketNo: patientTicket.queueNo.trim().toString(), postVoice: postVoice, lang: 'en', flutterTts: flutterTts);
await voiceCaller!.startCalling(patientTicket.queueNo.trim().toString() != patientTicket.callNoStr.trim().toString());
voiceCaller = null;
if (isQueuePatients.isNotEmpty) {
isQueuePatients.removeAt(0);
}
2 years ago
} else {
2 years ago
isCallingInProgress = false;
log("Setting isCallingInProgress : $isCallingInProgress");
if (isApiCallNeeded) {
log("I will start waiting!!");
Timer(Duration(seconds: patientCallConfigurations.concurrentCallDelaySec), () async {
await callPatientsAPI();
log("Called the API after waiting!");
isApiCallNeeded = false;
});
}
2 years ago
}
}
Future<void> listenAudioPlayerEvents() async {
audioPlayer.playerStateStream.listen((playerState) async {
2 years ago
if (playerState.processingState == ProcessingState.completed) {
2 years ago
// isCallingInProgress = false;
if (currentPatient.isVoiceReq) return;
if (isQueuePatients.isNotEmpty) {
log("isQueuePatients length : ${isQueuePatients.length}");
final length = isQueuePatients.length;
for (int i = 0; i < length; i++) {
await Future.delayed(Duration(seconds: patientCallConfigurations.concurrentCallDelaySec)).whenComplete(() async {
PatientTicketModel temp = PatientTicketModel();
if (patientTickets.isNotEmpty) {
temp = patientTickets.elementAt(0);
patientTickets.removeAt(0);
}
notifyListeners();
isQueuePatients.removeAt(0);
patientTickets.add(temp);
notifyListeners();
await voiceCallPatientTicket(patientTickets.first);
updatePatientTicket(patientTickets.first);
});
}
}
isCallingInProgress = false;
log("Setting isCallingInProgress : $isCallingInProgress");
if (isApiCallNeeded) {
log("I will start waiting!!");
Timer(Duration(seconds: patientCallConfigurations.concurrentCallDelaySec), () async {
await callPatientsAPI();
log("Called the API after waiting!");
isApiCallNeeded = false;
});
}
2 years ago
}
});
2 years ago
flutterTts.setStartHandler(() {
// isCallingInProgress = true;
});
flutterTts.setCompletionHandler(() async {
if (isQueuePatients.isNotEmpty) {
log("isQueuePatients length : ${isQueuePatients.length}");
final length = isQueuePatients.length;
for (int i = 0; i < length; i++) {
await Future.delayed(Duration(seconds: patientCallConfigurations.concurrentCallDelaySec)).whenComplete(() async {
PatientTicketModel temp = PatientTicketModel();
if (patientTickets.isNotEmpty) {
temp = patientTickets.elementAt(0);
patientTickets.removeAt(0);
}
notifyListeners();
isQueuePatients.removeAt(0);
patientTickets.add(temp);
notifyListeners();
await voiceCallPatientTicket(patientTickets.first);
updatePatientTicket(patientTickets.first);
2 years ago
});
}
}
isCallingInProgress = false;
log("Setting isCallingInProgress : $isCallingInProgress");
if (isApiCallNeeded) {
log("I will start waiting!!");
Timer(Duration(seconds: patientCallConfigurations.concurrentCallDelaySec), () async {
await callPatientsAPI();
log("Called the API after waiting!");
isApiCallNeeded = false;
});
}
});
2 years ago
}
2 years ago
// updatePatientTickets() {
// if (patientTickets.isNotEmpty) {
// List<Tickets> _ticketsToUpdate = patientTickets.where((t) => t.callUpdated == false).toList();
// API.callUpdateNotIsQueueRecordByIDAsync(currentDeviceIp, ticket: _ticketsToUpdate.first, onSuccess: (ticketsUpdated) {
// log("[${ticketsUpdated.length}] Tickets Updated: $ticketsUpdated");
// }, onFailure: (e) {
// log(" Tickets Update Failed with : ${e.toString()}");
// });
// }
// }
updatePatientTicket(PatientTicketModel patientTicket) {
2 years ago
if (!patientTicket.isQueue) {
API.callUpdateNotIsQueueRecordByIDAsync(currentDeviceIp, ticket: patientTicket, onSuccess: (ticketsUpdated) {
log("[${patientTicket.callNoStr}] Ticket Updated: $ticketsUpdated");
2 years ago
}, onFailure: (e) {
2 years ago
log(" Tickets Update ${patientTicket.callNoStr} Failed with Error : ${e.toString()}");
2 years ago
});
}
}
onConnect() {
log("SignalR: onConnect");
}
onDisconnect(exception) {
log("SignalR: onDisconnect");
signalRHelper.startSignalRConnection(currentDeviceIp, onUpdateAvailable: onPingReceived, onConnect: onConnect, onConnecting: onConnecting, onDisconnect: onDisconnect);
}
onConnecting() {
log("SignalR: onConnecting");
}
listenNetworkConnectivity() async {
Connectivity().onConnectivityChanged.listen((event) async {
switch (event) {
case ConnectivityResult.wifi:
updateInternetConnection(true);
await getCurrentIP();
if (signalRHelper.connection == null || signalRHelper.connection!.state != ConnectionState.active) {
signalRHelper.connection!.start();
}
break;
case ConnectivityResult.none:
updateInternetConnection(false);
signalRHelper.closeConnection();
break;
case ConnectivityResult.mobile:
break;
}
});
}
}