2023-04-13 08:45:19 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:flutter/services.dart';
|
2023-04-11 06:08:10 +00:00
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
import 'package:location/location.dart';
|
2023-04-13 08:45:19 +00:00
|
|
|
import 'package:platform_device_id/platform_device_id.dart';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'package:unit2/utils/request.dart';
|
|
|
|
import '../../model/sos/session.dart';
|
|
|
|
import '../../utils/urls.dart';
|
2023-04-11 06:08:10 +00:00
|
|
|
|
|
|
|
class SosService{
|
|
|
|
static final SosService _instance = SosService();
|
|
|
|
static SosService get instance => _instance;
|
|
|
|
DateFormat todayFormat = DateFormat("yMdHms");
|
|
|
|
Future<LocationData?> getUserLocation() async {
|
|
|
|
Location location = Location();
|
|
|
|
LocationData? locationData;
|
|
|
|
bool serviceEnabled = false;
|
|
|
|
PermissionStatus permissionGranted;
|
2023-04-13 08:45:19 +00:00
|
|
|
|
|
|
|
serviceEnabled = await location.serviceEnabled();
|
2023-04-11 06:08:10 +00:00
|
|
|
if (!serviceEnabled) {
|
|
|
|
serviceEnabled = await location.requestService();
|
|
|
|
if (!serviceEnabled) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
permissionGranted = await location.hasPermission();
|
|
|
|
if (permissionGranted == PermissionStatus.denied) {
|
|
|
|
permissionGranted = await location.requestPermission();
|
|
|
|
if (permissionGranted != PermissionStatus.granted) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2023-04-13 08:45:19 +00:00
|
|
|
try{
|
2023-04-11 06:08:10 +00:00
|
|
|
LocationData newLocationData = await location.getLocation();
|
|
|
|
locationData = newLocationData;
|
|
|
|
return locationData;
|
2023-04-13 08:45:19 +00:00
|
|
|
}catch(e){
|
|
|
|
throw e.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Future<String> initPlatformState() async {
|
|
|
|
String? deviceId;
|
|
|
|
try {
|
|
|
|
deviceId = await PlatformDeviceId.getDeviceId;
|
|
|
|
} on PlatformException {
|
|
|
|
deviceId = 'Failed to get deviceId.';
|
|
|
|
}
|
|
|
|
return deviceId!;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<SessionData> requestSos({required LocationData location, required String mobile1,
|
|
|
|
String? mobile2, required String msg, required String requestedDate}) async {
|
|
|
|
var body;
|
|
|
|
String deviceId = await initPlatformState();
|
|
|
|
String sessionToken =
|
|
|
|
deviceId.toString() + todayFormat.format(DateTime.now());
|
|
|
|
String path = Url.instance.sosRequest();
|
|
|
|
SessionData? sessionData;
|
|
|
|
|
|
|
|
if(mobile2 != null){
|
|
|
|
body = {
|
|
|
|
"mobile_id": deviceId.toString(),
|
|
|
|
"mobile_gps":
|
|
|
|
"${location.latitude.toString()},${location.longitude.toString()}",
|
|
|
|
"mobile1": mobile1,
|
|
|
|
"mobile2": mobile2,
|
|
|
|
"respondent_message": msg,
|
|
|
|
"session_token": sessionToken,
|
|
|
|
"pushedbutton_date": requestedDate
|
|
|
|
};
|
|
|
|
}else{
|
|
|
|
body = {
|
|
|
|
"mobile_id": deviceId.toString(),
|
|
|
|
"mobile_gps":
|
|
|
|
"${location.latitude.toString()},${location.longitude.toString()}",
|
|
|
|
"mobile1": mobile1,
|
|
|
|
"respondent_message": msg,
|
|
|
|
"session_token": sessionToken,
|
|
|
|
"pushedbutton_date": requestedDate
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, String> headers = {
|
|
|
|
'Content-Type': 'application/json; charset=UTF-8',
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
http.Response response = await Request.instance.postRequest(path: path,body: body,headers: headers);
|
|
|
|
if (response.statusCode == 201) {
|
|
|
|
var body = jsonDecode(response.body);
|
|
|
|
SessionData newSessionData = SessionData.fromJson(body['data']);
|
|
|
|
sessionData = newSessionData;
|
|
|
|
}
|
|
|
|
|
|
|
|
}on Error catch (e) {
|
|
|
|
throw (e.toString());
|
|
|
|
}
|
|
|
|
return sessionData!;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<SessionData> checkAcknowledgement(String sessionToken) async {
|
|
|
|
String path = Url.instance.sosRequest();
|
|
|
|
SessionData? sessionData;
|
|
|
|
try {
|
|
|
|
http.Response response = await Request.instance.getRequest(path: path,param:{"session_token":sessionToken} );
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
var body = jsonDecode(response.body);
|
|
|
|
SessionData newSessionData = SessionData.fromJson(body['data'][0]);
|
|
|
|
|
|
|
|
sessionData = newSessionData;
|
|
|
|
|
|
|
|
}
|
|
|
|
} on Error
|
|
|
|
catch (e) {
|
|
|
|
throw (e.toString());
|
|
|
|
}
|
|
|
|
return sessionData!;
|
2023-04-11 06:08:10 +00:00
|
|
|
}
|
|
|
|
}
|