Files
rog_app/lib/services/external_service.dart

280 lines
9.6 KiB
Dart
Raw Normal View History

2022-10-19 08:53:17 +05:30
import 'dart:io';
2022-11-05 22:02:21 +05:30
import 'package:get/get.dart';
2022-10-19 08:53:17 +05:30
import 'package:http/http.dart' as http;
2022-11-14 22:22:41 +05:30
import 'package:intl/intl.dart';
import 'package:rogapp/model/rog.dart';
2023-01-31 10:14:23 +05:30
import 'package:rogapp/pages/destination/destination_controller.dart';
2022-11-05 22:02:21 +05:30
import 'package:rogapp/pages/index/index_controller.dart';
2022-11-14 22:22:41 +05:30
import 'package:rogapp/utils/database_helper.dart';
2022-10-19 08:53:17 +05:30
import 'dart:convert';
import '../utils/const.dart';
2022-11-14 22:22:41 +05:30
//
// Rog type 0- start 1- checkin 2- goal
//
2022-10-19 08:53:17 +05:30
class ExternalService {
static final ExternalService _instance = ExternalService._internal();
factory ExternalService(){
return _instance;
}
ExternalService._internal();
2022-11-14 22:22:41 +05:30
String getFormatedTime(DateTime datetime){
return DateFormat('yyyy-MM-dd HH:mm:ss').format(datetime);
}
2022-11-05 22:02:21 +05:30
Future<Map<String, dynamic>> StartRogaining() async {
final IndexController indexController = Get.find<IndexController>();
2022-11-14 22:22:41 +05:30
2023-09-03 23:37:41 +05:30
Map<String, dynamic> res = {};
2022-11-14 22:22:41 +05:30
2023-09-03 23:37:41 +05:30
int userId = indexController.currentUser[0]["user"]["id"];
2022-11-05 22:02:21 +05:30
//print("--- Pressed -----");
2023-09-03 23:37:41 +05:30
String team = indexController.currentUser[0]["user"]['team_name'];
2022-11-05 22:02:21 +05:30
//print("--- _team : ${_team}-----");
2023-09-03 23:37:41 +05:30
String eventCode = indexController.currentUser[0]["user"]["event_code"];
2022-11-05 22:02:21 +05:30
2023-01-12 12:32:08 +05:30
if(indexController.connectionStatusName != "wifi" && indexController.connectionStatusName != "mobile"){
2022-11-14 22:22:41 +05:30
DatabaseHelper db = DatabaseHelper.instance;
2023-09-03 23:37:41 +05:30
Rog rog = Rog(
2022-11-14 22:22:41 +05:30
id:1,
2023-09-03 23:37:41 +05:30
team_name: team,
event_code : eventCode,
user_id: userId,
2022-11-14 22:22:41 +05:30
cp_number: -1,
checkintime: DateTime.now().toUtc().microsecondsSinceEpoch,
image: null,
rog_action_type: 0
);
2023-09-03 23:37:41 +05:30
db.insertRogaining(rog);
2022-11-14 22:22:41 +05:30
}
else {
String url = 'https://natnats.mobilous.com/start_from_rogapp';
2023-09-03 23:37:41 +05:30
print('++++++++$url');
2022-11-14 22:22:41 +05:30
final http.Response response = await http.post(
Uri.parse(url),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
2023-09-03 23:37:41 +05:30
'team_name': team,
'event_code': eventCode
2022-11-14 22:22:41 +05:30
}),
);
2022-10-19 08:53:17 +05:30
2022-11-14 22:22:41 +05:30
print("---- start rogianing api status ---- ${response.statusCode}");
2022-11-05 22:02:21 +05:30
2022-11-14 22:22:41 +05:30
if (response.statusCode == 200) {
2023-09-03 23:37:41 +05:30
res = json.decode(utf8.decode(response.bodyBytes));
print('----_res : $res ----');
2022-11-14 22:22:41 +05:30
}
2022-10-19 08:53:17 +05:30
}
2023-09-03 23:37:41 +05:30
return res;
2022-10-19 08:53:17 +05:30
}
2023-09-03 23:37:41 +05:30
Future<Map<String, dynamic>> makeCheckpoint(int userId, String token, String checkinTime, String teamname, int cp, String eventcode, String imageurl) async {
Map<String, dynamic> res = {};
2022-10-19 08:53:17 +05:30
String url = 'https://natnats.mobilous.com/checkin_from_rogapp';
2023-09-03 23:37:41 +05:30
print('++++++++$url');
2022-11-14 22:22:41 +05:30
final IndexController indexController = Get.find<IndexController>();
2022-10-19 08:53:17 +05:30
2022-11-05 22:02:21 +05:30
if(imageurl != null){
2023-01-12 12:32:08 +05:30
if(indexController.connectionStatusName != "wifi" && indexController.connectionStatusName != "mobile"){
2022-11-14 22:22:41 +05:30
DatabaseHelper db = DatabaseHelper.instance;
2023-09-03 23:37:41 +05:30
Rog rog = Rog(
2022-11-14 22:22:41 +05:30
id:1,
team_name: teamname,
event_code : eventcode,
2023-09-03 23:37:41 +05:30
user_id: userId,
2022-11-14 22:22:41 +05:30
cp_number: cp,
checkintime: DateTime.now().toUtc().microsecondsSinceEpoch,
image: imageurl,
rog_action_type: 1,
2022-11-05 22:02:21 +05:30
);
2023-09-03 23:37:41 +05:30
db.insertRogaining(rog);
2022-11-14 22:22:41 +05:30
}
else {
2023-09-03 23:37:41 +05:30
String serverUrl = ConstValues.currentServer();
String url1 = "$serverUrl/api/checkinimage/";
final im1Bytes = File(imageurl).readAsBytesSync();
2022-11-14 22:22:41 +05:30
String im1_64 = base64Encode(im1Bytes);
2022-11-05 22:02:21 +05:30
2022-11-14 22:22:41 +05:30
final http.Response response = await http.post(
Uri.parse(url1),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
2023-09-03 23:37:41 +05:30
'Authorization': 'Token $token'
2022-11-14 22:22:41 +05:30
},
// 'id', 'user', 'goalimage', 'goaltime', 'team_name', 'event_code','cp_number'
body: jsonEncode(<String, String>{
2023-09-03 23:37:41 +05:30
'user' : userId.toString(),
2022-11-14 22:22:41 +05:30
'team_name': teamname,
'event_code': eventcode,
'checkinimage' : im1_64,
2023-09-03 23:37:41 +05:30
'checkintime' : checkinTime,
2022-11-14 22:22:41 +05:30
'cp_number' : cp.toString()
}),
);
2022-11-05 22:02:21 +05:30
2023-09-03 23:37:41 +05:30
res = json.decode(utf8.decode(response.bodyBytes));
2022-11-05 22:02:21 +05:30
2023-09-03 23:37:41 +05:30
print("-----@@@@@ $res -----");
2022-11-14 22:22:41 +05:30
if(response.statusCode == 201){
//print('---- toekn is ${token} -----');
final http.Response response2 = await http.post(
Uri.parse(url),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'team_name': teamname,
'cp_number': cp.toString(),
'event_code': eventcode,
2023-09-03 23:37:41 +05:30
'image': res["checkinimage"].toString().replaceAll('http://localhost:8100', 'http://rogaining.sumasen.net')
2022-11-14 22:22:41 +05:30
}),
);
print("--- checnin response ${response2.statusCode}----");
if (response2.statusCode == 200) {
2023-09-03 23:37:41 +05:30
res = json.decode(utf8.decode(response2.bodyBytes));
print('----checkin res _res : $res ----');
2022-11-14 22:22:41 +05:30
}
}
}
2022-11-05 22:02:21 +05:30
}
else{
2022-11-14 22:22:41 +05:30
if(indexController.connectionStatusName != "wifi" || indexController.connectionStatusName != "mobile"){
DatabaseHelper db = DatabaseHelper.instance;
2023-09-03 23:37:41 +05:30
Rog rog = Rog(
2022-11-14 22:22:41 +05:30
id:1,
team_name: teamname,
event_code : eventcode,
2023-09-03 23:37:41 +05:30
user_id: userId,
2022-11-14 22:22:41 +05:30
cp_number: cp,
checkintime: DateTime.now().toUtc().microsecondsSinceEpoch,
image: null,
rog_action_type: 1,
);
2023-09-03 23:37:41 +05:30
db.insertRogaining(rog);
2022-11-14 22:22:41 +05:30
}
else {
2022-11-05 22:02:21 +05:30
final http.Response response3 = await http.post(
Uri.parse(url),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'team_name': teamname,
'cp_number': cp.toString(),
'event_code': eventcode,
'image': ""
}),
);
print("--- checnin response ${response3.statusCode}----");
if (response3.statusCode == 200) {
2023-09-03 23:37:41 +05:30
res = json.decode(utf8.decode(response3.bodyBytes));
print('----checkin res _res : $res ----');
2022-11-05 22:02:21 +05:30
}
2022-11-14 22:22:41 +05:30
}
2022-10-19 08:53:17 +05:30
}
2023-09-03 23:37:41 +05:30
return res;
2022-10-19 08:53:17 +05:30
}
2022-11-05 22:02:21 +05:30
2022-10-19 08:53:17 +05:30
2023-09-03 23:37:41 +05:30
Future<Map<String, dynamic>> makeGoal(int userId, String token, String teamname, String image, String goalTime, String eventcode) async {
Map<String, dynamic> res2 = {};
2022-10-19 08:53:17 +05:30
2022-11-14 22:22:41 +05:30
final IndexController indexController = Get.find<IndexController>();
2023-01-31 10:14:23 +05:30
final DestinationController destinationController = Get.find<DestinationController>();
2022-11-14 22:22:41 +05:30
2023-01-31 10:14:23 +05:30
//if(indexController.connectionStatusName != "wifi" && indexController.connectionStatusName != "mobile"){
2022-11-14 22:22:41 +05:30
DatabaseHelper db = DatabaseHelper.instance;
2023-09-03 23:37:41 +05:30
Rog rog = Rog(
2022-11-14 22:22:41 +05:30
id:1,
team_name: teamname,
event_code : eventcode,
2023-09-03 23:37:41 +05:30
user_id: userId,
2022-11-14 22:22:41 +05:30
cp_number: -1,
checkintime: DateTime.now().toUtc().microsecondsSinceEpoch,
image: image,
rog_action_type: 1,
);
2023-09-03 23:37:41 +05:30
db.insertRogaining(rog);
2023-01-31 10:14:23 +05:30
// }
// else{
2023-09-03 23:37:41 +05:30
String serverUrl = ConstValues.currentServer();
String url1 = "$serverUrl/api/goalimage/";
final im1Bytes = File(image).readAsBytesSync();
2022-11-14 22:22:41 +05:30
String im1_64 = base64Encode(im1Bytes);
final http.Response response = await http.post(
Uri.parse(url1),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
2023-09-03 23:37:41 +05:30
'Authorization': 'Token $token'
2022-11-14 22:22:41 +05:30
},
// 'id', 'user', 'goalimage', 'goaltime', 'team_name', 'event_code','cp_number'
body: jsonEncode(<String, String>{
2023-09-03 23:37:41 +05:30
'user' : userId.toString(),
2022-11-14 22:22:41 +05:30
'team_name': teamname,
'event_code': eventcode,
2023-09-03 23:37:41 +05:30
'goaltime' : goalTime,
2022-11-14 22:22:41 +05:30
'goalimage' : im1_64,
'cp_number' : "-1"
}),
2022-10-19 08:53:17 +05:30
);
2022-11-14 22:22:41 +05:30
String url = 'https://natnats.mobilous.com/goal_from_rogapp';
2023-09-03 23:37:41 +05:30
print('++++++++$url');
2022-11-14 22:22:41 +05:30
if (response.statusCode == 201) {
2023-09-03 23:37:41 +05:30
Map<String, dynamic> res = json.decode(utf8.decode(response.bodyBytes));
print('----_res : $res ----');
print('---- image url ${res["goalimage"]} ----');
2022-11-14 22:22:41 +05:30
final http.Response response2 = await http.post(
Uri.parse(url),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'team_name': teamname,
'event_code': eventcode,
2023-09-03 23:37:41 +05:30
'goal_time' : goalTime,
'image' : res["goalimage"].toString().replaceAll('http://localhost:8100', 'http://rogaining.sumasen.net')
2022-11-14 22:22:41 +05:30
}
),
);
2023-09-03 23:37:41 +05:30
print('----- response2 is $response2 --------');
2022-11-14 22:22:41 +05:30
if (response2.statusCode == 200) {
2023-09-03 23:37:41 +05:30
res2 = json.decode(utf8.decode(response2.bodyBytes));
2022-11-14 22:22:41 +05:30
}
}
2023-01-31 10:14:23 +05:30
//}
destinationController.resetRogaining();
2023-09-03 23:37:41 +05:30
return res2;
2022-10-19 08:53:17 +05:30
}
static Future<Map<String, dynamic>> usersEventCode(String teamcode, String password) async {
2023-09-03 23:37:41 +05:30
Map<String, dynamic> res = {};
String url = "https://natnats.mobilous.com/check_event_code?zekken_number=$teamcode&password=$password";
print('++++++++$url');
2022-10-19 08:53:17 +05:30
final http.Response response = await http.get(
Uri.parse(url),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
}
);
if (response.statusCode == 200) {
2023-09-03 23:37:41 +05:30
res = json.decode(utf8.decode(response.bodyBytes));
2022-10-19 08:53:17 +05:30
}
2023-09-03 23:37:41 +05:30
return res;
2022-10-19 08:53:17 +05:30
}
}