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/core/api.dart

164 lines
7.0 KiB
Dart

import 'dart:developer';
import 'dart:io';
import 'package:queuing_system/core/base/base_app_client.dart';
import 'package:queuing_system/core/config/config.dart';
import 'package:queuing_system/core/response_models/call_config_model.dart';
import 'package:queuing_system/core/response_models/patient_ticket_model.dart';
import 'package:queuing_system/core/response_models/prayers_widget_model.dart';
import 'package:queuing_system/core/response_models/rss_feed_model.dart';
import 'package:queuing_system/core/response_models/weathers_widget_model.dart';
import 'package:queuing_system/core/response_models/widgets_config_model.dart';
import 'package:queuing_system/core/response_models/test_patients.dart';
const _getCallRequestInfoByClinicInfo = "/GetCallRequestInfo_ByIP";
const _callUpdateNotIsQueueRecordByIDAsync = "/CallRequest_QueueUpdate";
const _waitingAreaScreenConfigGet = "/WaitingAreaScreen_Config_Get";
const _weatherForecastGetBy5Days = "/WeatherForecast_GetBy5Days";
const _prayerTimeToday = "/PrayerTime_Today";
const _rssFeedGet = "/RssFeed_Get";
class MyHttpOverrides extends HttpOverrides {
@override
HttpClient createHttpClient(SecurityContext? context) {
return super.createHttpClient(context)..badCertificateCallback = (X509Certificate cert, String host, int port) => true;
}
}
class API {
static getCallRequestInfoByClinicInfo(String deviceIp,
{required Function(List<PatientTicketModel>, List<PatientTicketModel>, CallConfig callConfig) onSuccess, required Function(dynamic) onFailure}) async {
final body = {"ipAdress": deviceIp, "apiKey": apiKey};
bool isDevMode = false;
if (isDevMode) {
final Map<String, dynamic> response = testPatientsData["data"] as Map<String, dynamic>;
CallConfig callConfig = CallConfig.fromJson(response["callConfig"]);
var callPatients = (response["callPatients"] as List).map((j) => PatientTicketModel.fromJson(j)).toList().where((element) => element.callType != 0).toList();
var isQueuePatients = callPatients.where((element) => (element.isQueue == false && element.callType != 0)).toList();
log("callPatients: ${callPatients.toString()}");
log("isQueuePatients: ${isQueuePatients.toString()}");
onSuccess(callPatients.reversed.toList(), isQueuePatients.reversed.toList(), callConfig);
return;
}
BaseAppClient.post(_getCallRequestInfoByClinicInfo,
body: body,
onSuccess: (apiResp, status) {
if (status == 200) {
final response = apiResp["data"];
CallConfig callConfig = CallConfig.fromJson(response["callConfig"]);
var callPatients = (response["callPatients"] as List).map((j) => PatientTicketModel.fromJson(j)).toList().where((element) => element.callType != 0).toList();
var isQueuePatients = callPatients.where((element) => (element.isQueue == false && element.callType != 0)).toList();
callPatients.sort((a, b) => a.editedOnTimeStamp.compareTo(b.editedOnTimeStamp));
isQueuePatients.sort((a, b) => a.editedOnTimeStamp.compareTo(b.editedOnTimeStamp));
log("callPatients: ${callPatients.toString()}");
log("isQueuePatients: ${isQueuePatients.toString()}");
onSuccess(callPatients.reversed.toList(), isQueuePatients.reversed.toList(), callConfig);
} else {
onFailure(apiResp);
}
},
onFailure: (error, status) => onFailure(error));
}
static callUpdateNotIsQueueRecordByIDAsync(
String deviceIp, {
required PatientTicketModel ticket,
required Function(List<PatientTicketModel>) onSuccess,
required Function(dynamic) onFailure,
}) async {
List<PatientTicketModel> _ticketsUpdated = [];
// for (var ticket in tickets) {
final body = {"id": ticket.id, "apiKey": apiKey, "ipAddress": deviceIp, "callType": ticket.callType};
await BaseAppClient.post(_callUpdateNotIsQueueRecordByIDAsync,
body: body,
onSuccess: (response, status) {
if (status == 200) {
ticket.callUpdated = true;
_ticketsUpdated.add(ticket);
}
log("here response: $response");
},
onFailure: (error, status) => onFailure(error));
// }
if (_ticketsUpdated.isNotEmpty) {
onSuccess(_ticketsUpdated);
} else {
onFailure(false);
}
}
static Future<WidgetsConfigModel> getWidgetConfigsFromServer(String deviceIp, {required Function(dynamic) onFailure}) async {
final body = {"ipAddress": deviceIp};
WidgetsConfigModel widgetsConfigModel = WidgetsConfigModel();
await BaseAppClient.post(_waitingAreaScreenConfigGet,
body: body,
onSuccess: (response, status) {
if (status == 200 && response["data"] != null) {
widgetsConfigModel = (response["data"] as List).map((e) => WidgetsConfigModel.fromJson(e)).toList().first;
}
},
onFailure: (error, status) => log("error: ${error.toString()}"));
return widgetsConfigModel;
}
static Future<WeathersWidgetModel?> getWeatherDetailsFromServer(String cityId, {required Function(dynamic) onFailure}) async {
final body = {"cityID": cityId};
WeathersWidgetModel weathersWidgetModel = WeathersWidgetModel();
await BaseAppClient.post(_weatherForecastGetBy5Days,
body: body,
onSuccess: (response, status) {
if (status == 200 && response["data"] != null) {
weathersWidgetModel = (response["data"] as List).map((e) => WeathersWidgetModel.fromJson(e)).toList().first;
}
},
onFailure: (error, status) => log("error: ${error.toString()}"));
return weathersWidgetModel;
}
static Future<PrayersWidgetModel?> getPrayerDetailsFromServer({
required double latitude,
required double longitude,
required Function(dynamic) onFailure,
}) async {
final body = {"latitude": latitude, "longitude": longitude};
PrayersWidgetModel currentPrayersWidgetModel = PrayersWidgetModel();
await BaseAppClient.post(_prayerTimeToday,
body: body,
onSuccess: (response, status) {
if (status == 200 && response["data"] != null) {
final prayersWidgetModel = (response["data"] as List).map((e) => PrayersWidgetModel.fromJson(e)).toList().first;
currentPrayersWidgetModel = prayersWidgetModel;
}
},
onFailure: (error, status) => log("error: ${error.toString()}"));
return currentPrayersWidgetModel;
}
static Future<RssFeedModel?> getRssFeedDetailsFromServer({
required int languageId,
required Function(dynamic) onFailure,
}) async {
final body = {"languageID": languageId};
RssFeedModel rssFeedModel = RssFeedModel();
await BaseAppClient.post(_rssFeedGet,
body: body,
onSuccess: (response, status) {
if (status == 200 && response["data"] != null) {
final rssFeed = (response["data"] as List).map((e) => RssFeedModel.fromJson(e)).toList().first;
rssFeedModel = rssFeed;
}
},
onFailure: (error, status) => log("error: ${error.toString()}"));
return rssFeedModel;
}
}