mirror of
https://git.rezel.net/LudoTech/traque.git
synced 2026-04-11 00:30:19 +02:00
Modify folder structure
This commit is contained in:
48
mobile/traque-app/src/services/backgroundLocationTask.js
Normal file
48
mobile/traque-app/src/services/backgroundLocationTask.js
Normal file
@@ -0,0 +1,48 @@
|
||||
// Expo
|
||||
import { defineTask, isTaskRegisteredAsync } from "expo-task-manager";
|
||||
import * as Location from 'expo-location';
|
||||
// Services
|
||||
import { emitUpdatePosition } from "./socketEmitter";
|
||||
// Constants
|
||||
import { TASKS, LOCATION_PARAMETERS } from "../constants";
|
||||
|
||||
|
||||
// Task
|
||||
|
||||
defineTask(TASKS.BACKGROUND_LOCATION, async ({ data, error }) => {
|
||||
if (error) {
|
||||
console.error(error);
|
||||
return;
|
||||
}
|
||||
if (data) {
|
||||
const { locations } = data;
|
||||
if (locations.length == 0) {
|
||||
console.log("No location measured.");
|
||||
return;
|
||||
}
|
||||
const { latitude, longitude } = locations[0].coords;
|
||||
emitUpdatePosition([latitude, longitude]);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// Functions
|
||||
|
||||
export const getLocationAuthorization = async () => {
|
||||
const { status : statusForeground } = await Location.requestForegroundPermissionsAsync();
|
||||
const { status : statusBackground } = await Location.requestBackgroundPermissionsAsync();
|
||||
return statusForeground == "granted" && statusBackground == "granted";
|
||||
};
|
||||
|
||||
export const startLocationTracking = async () => {
|
||||
if (!(await getLocationAuthorization())) return;
|
||||
if (await isTaskRegisteredAsync(TASKS.BACKGROUND_LOCATION)) return;
|
||||
console.log("Location tracking started.");
|
||||
await Location.startLocationUpdatesAsync(TASKS.BACKGROUND_LOCATION, LOCATION_PARAMETERS.SERVER);
|
||||
};
|
||||
|
||||
export const stopLocationTracking = async () => {
|
||||
if (!await isTaskRegisteredAsync(TASKS.BACKGROUND_LOCATION)) return;
|
||||
console.log("Location tracking stopped.");
|
||||
await Location.stopLocationUpdatesAsync(TASKS.BACKGROUND_LOCATION);
|
||||
};
|
||||
32
mobile/traque-app/src/services/imageService.js
Normal file
32
mobile/traque-app/src/services/imageService.js
Normal file
@@ -0,0 +1,32 @@
|
||||
// Constants
|
||||
import { SERVER_URL } from "../constants";
|
||||
|
||||
export const uploadTeamImage = async (id, imageUri) => {
|
||||
if (!imageUri || !id) return;
|
||||
|
||||
const data = new FormData();
|
||||
data.append('file', {
|
||||
uri: imageUri,
|
||||
name: 'photo.jpg',
|
||||
type: 'image/jpeg',
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await fetch(`${SERVER_URL}/upload?team=${id}`, {
|
||||
method: 'POST',
|
||||
body: data,
|
||||
});
|
||||
|
||||
if (!response.ok) throw new Error("Échec de l'upload");
|
||||
return await response.blob();
|
||||
} catch (error) {
|
||||
console.error("Erreur uploadImage :", error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const enemyImage = (id) => {
|
||||
if (!id) return require('../assets/images/missing_image.jpg');
|
||||
|
||||
return {uri: `${SERVER_URL}/photo/enemy?team=${id}`};
|
||||
};
|
||||
8
mobile/traque-app/src/services/socket.js
Normal file
8
mobile/traque-app/src/services/socket.js
Normal file
@@ -0,0 +1,8 @@
|
||||
// Socket
|
||||
import { io } from "socket.io-client";
|
||||
// Constants
|
||||
import { SOCKET_URL } from "../constants";
|
||||
|
||||
export const socket = io(SOCKET_URL, {
|
||||
path: "/back/socket.io"
|
||||
});
|
||||
44
mobile/traque-app/src/services/socketEmitter.js
Normal file
44
mobile/traque-app/src/services/socketEmitter.js
Normal file
@@ -0,0 +1,44 @@
|
||||
// Services
|
||||
import { socket } from "./socket";
|
||||
|
||||
export const emitLogin = (password, callback) => {
|
||||
if (!socket?.connected) return;
|
||||
console.log("Try to log in with :", password);
|
||||
socket.emit("login", password, callback);
|
||||
};
|
||||
|
||||
export const emitLogout = () => {
|
||||
if (!socket?.connected) return;
|
||||
console.log("Logout.");
|
||||
socket.emit("logout");
|
||||
};
|
||||
|
||||
export const emitSendPosition = () => {
|
||||
if (!socket?.connected) return;
|
||||
console.log("Reveal position.");
|
||||
socket.emit("send_position");
|
||||
};
|
||||
|
||||
export const emitUpdatePosition = (location) => {
|
||||
if (!socket?.connected) return;
|
||||
console.log("Update position :", location);
|
||||
socket.emit("update_position", location);
|
||||
};
|
||||
|
||||
export const emitCapture = (captureCode, callback) => {
|
||||
if (!socket?.connected) return;
|
||||
console.log("Try to capture :", captureCode);
|
||||
socket.emit("capture", captureCode, callback);
|
||||
};
|
||||
|
||||
export const emitBattery = (level) => {
|
||||
if (!socket?.connected) return;
|
||||
console.log("Send battery level.");
|
||||
socket.emit('battery_update', level);
|
||||
};
|
||||
|
||||
export const emitDeviceInfo = (infos) => {
|
||||
if (!socket?.connected) return;
|
||||
console.log("Send device infos.");
|
||||
socket.emit('device_info', infos);
|
||||
};
|
||||
Reference in New Issue
Block a user