mr-wolf-gb/traccar

Traccar GPS服务器

1.0.0 2024-02-28 09:56 UTC

This package is auto-updated.

Last update: 2024-09-10 01:46:34 UTC


README

Latest Version on Packagist Total Downloads Latest Unstable Version License PHP Version Require

此Laravel包作为无缝集成工具,使开发者能够轻松通过其强大的API与Traccar服务器交互。Traccar,一个强大的GPS跟踪平台,比以往任何时候都更容易访问,因为这个包简化了Laravel应用程序与Traccar服务器之间的通信,提供了广泛的功能和功能。无论是检索实时位置数据、管理设备还是利用高级跟踪功能,此包都简化了过程,提高了Laravel项目的效率和可扩展性。

目录

🔧所需PHP版本

安装

您可以通过composer安装此包

composer require mr-wolf-gb/traccar

您可以发布配置和迁移

php artisan vendor:publish --provider="MrWolfGb\Traccar\TraccarServiceProvider"

设置traccar服务器信息[.env文件]

TRACCAR_BASE_URL="https://:8082/"
TRACCAR_SOCKET_URL="ws://:8082/api/socket"
TRACCAR_USERNAME="admin@traccar.local"
TRACCAR_PASSWORD="password"
TRACCAR_TOKEN="RzBFAiEA84hXSL6uV6FQyBX0_Ds1a6NMcSC..."
# token required only when using fetch session informations

✨功能和用法

多用户和服务器

// by default, it uses .env credentials else you can set it manually
// Inject service as public variable in Controller
public function __construct(public TraccarService $traccarService)
{
    $this->traccarService->setEmail("user1@traccar.local");
    $this->traccarService->setPassword("password");
    $this->traccarService->setBaseUrl("https://:8082/");
    $this->traccarService->setToken("ws://:8082/api/socket");
}
// or inject directly in specific method
public function index(TraccarService $traccarService)
{
    //...
}

可用资源

  • 服务器

模型 : 服务器模型

public function index(TraccarService $traccarService)
{
    $serverRepo = $traccarService->serverRepository();
    // Get server information
    $srv = $serverRepo->fetchServerInformation();
    // Update server information
    $serverRepo->updateServerInformation(server: $srv);
}
  • 会话

模型 : 会话模型

public function index(TraccarService $traccarService)
{
    $sessionRepo = $traccarService->sessionRepository();
    // Create new session 
    $session = $sessionRepo->createNewSession();
    // Get connected session [Require user Token in configuration]
    $session = $sessionRepo->fetchSessionInformation();
    // Close session
    $sessionRepo->closeSession();
}
  • 用户

模型 : 用户模型

public function index(TraccarService $traccarService)
{
    $userRepo = $traccarService->userRepository();
    // Get list of users
    $list = $userRepo->fetchListUsers();
    // Create new user
    $user = $userRepo->createUser(
        name: 'test', 
        email: 'test@test.local', 
        password: 'test'
    );
    // Create new user with Model : MrWolfGb\Traccar\Models\User
    $user = $userRepo->createNewUser(new User([
        'name' => 'test',
        'email' => 'test@test.local',
        'password' => 'test',
    ]));
    // Update user
    $user = $userRepo->updateUser(user: $user);
    // Delete user : int|User $user
    $userRepo->deleteUser(user: $user);
    // Assign user to device : int|User $user, int|Device $device
    $userRepo->assignUserDevice(user: 1, device: 1);
    // Remove user from device : int|User $user, int|Device $device
    $userRepo->removeUserDevice(user: 1, device: 1);
}

模型 : 组模型

public function index(TraccarService $traccarService)
{
    $groupRepo = $traccarService->groupRepository();
    // Get list of groups
    $list = $groupRepo->fetchListGroups();
    // Create new group
    $group = $groupRepo->createGroup(name: 'test-group');
    // Create new group with Model : MrWolfGb\Traccar\Models\Group
    $group = $groupRepo->createNewGroup(group: new Group(['name' => 'test']));
    // Update group
    $user = $groupRepo->updateGroup(group: $group);
    // Delete group : int|Group $group
    $groupRepo->deleteGroup(group: $group);
}
  • 设备

模型 : 设备模型

public function index(TraccarService $traccarService)
{
    $deviceRepo = $traccarService->deviceRepository();
    // Get list of devices
    $list = $deviceRepo->fetchListDevices();
    // Get user devices
    $list = $deviceRepo->getUserDevices(userId: 1);
    // Get device by id
    $device = $deviceRepo->getDeviceById(deviceId: 1);
    // Get device by uniqueId
    $device = $deviceRepo->getDeviceByUniqueId(uniqueId: 123456);
    // Create new device
    $device = $deviceRepo->createDevice(name: 'test', uniqueId: '123456789');
    // Create new device with Model : MrWolfGb\Traccar\Models\Device
    $device = $deviceRepo->createNewDevice(device: new Device([
        'name' => 'test-device',
        'uniqueId' => '123456789-d1-device',
    ]));
    // Update device
    $device = $deviceRepo->updateDevice(device: $device);
    // Delete device : int|Device $device
    $deviceRepo->deleteDevice(device: $device);
    // Update total distance and hours
    $deviceRepo->updateTotalDistanceAndHoursOfDevice(device: $device, totalDistance: 100, hours: 10);
    // Assign device to geofence : int|Device $device, int|Geofence $geofence
    $deviceRepo->assignDeviceGeofence(device: $device, geofence: $geofence);
    // Remove device from geofence : int|Device $device, int|Geofence $geofence
    $deviceRepo->removeDeviceGeofence(device: $device, geofence: $geofence);
    // Assign device to notification : int|Device $device, int|Notification $notification
    $deviceRepo->assignDeviceNotification(device: $device, notification: $notification);
    // Remove device from notification : int|Device $device, int|Notification $notification
    $deviceRepo->removeDeviceNotification(device: $device, notification: $notification);
}
  • 地理围栏

模型 : 地理围栏模型

public function index(TraccarService $traccarService)
{
    $geofenceRepo = $traccarService->geofenceRepository();
    // Get list of geofences
    $list = $geofenceRepo->fetchListGeofences();
    // Get geofence
    $geofence = $geofenceRepo->createGeofence(
        name: 'test-geofence', 
        area: 'POLYGON ((34.55602185173028 -18.455295134508617, 37.67183427726626 -18.13110040602976, 34.98211925933252 -14.500119447061167, 34.55602185173028 -18.455295134508617))',
        description: 'test'
    );
    // Create new geofence with Model : MrWolfGb\Traccar\Models\Geofence
    $geofence = $geofenceRepo->createNewGeofence( geofence: new Geofence([
        'name' => 'test-geofence', 
        'area' => 'LINESTRING (38.06472440318089 -26.49821693459276, 38.4968396008517 -24.64860674974679, 37.297972401178825 -23.72380165732423, 38.099388220592346 -23.37149495544884)',
        'description' => 'test'
    ]));
    // Update geofence
    $geofence = $geofenceRepo->updateGeofence(geofence: $geofence);
    // Delete geofence : int|Geofence $geofence
    $geofenceRepo->deleteGeofence(geofence: $geofence);
}
  • 通知

模型 : 通知模型

public function index(TraccarService $traccarService)
{
    $notificationRepo = $traccarService->notificationRepository();
    // Get list of notifications
    $list = $notificationRepo->fetchListNotifications();
    // Create new notification
    $notification = $notificationRepo->createNotification(
        type: 'alarm', 
        notificators: ['web'], 
        always: true
    );
    // Create new notification with Model : MrWolfGb\Traccar\Models\Notification
    $notification = $notificationRepo->createNewNotification(new Notification([
        'type' => NotificationType::ALARM->value,
        'notificator' => implode(',', [
            NotificatorType::WEB->value, 
            NotificatorType::COMMAND->value
        ]),
        'always' => false,
        'commandId' => 1, // required if notificator is/contains command
    ]));
    // Update notification
    $notification = $notificationRepo->updateNotification(notification: $notification);
    // Delete notification : int|Notification $notification
    $notificationRepo->deleteNotification(notification: $notification);
    // Get notification types from Traccar server
    $list = $notificationRepo->fetchNotificationTypes();
    // Send test notification
    $notificationRepo->sendTestNotification(
        type: NotificationType::MEDIA->value, 
        notificator: NotificatorType::WEB->value
    );
}
  • 位置

模型 : 位置模型

public function index(TraccarService $traccarService)
{
    $positionRepo = $traccarService->positionRepository();
    // Get list of positions
    $list = $positionRepo->fetchListPositions(
        from: now()->subHours(1), 
        to: now(), 
        id: [1, 2, 3] // optional
    );
    // Delete positions of device : int|Device $device
    $positionRepo->deletePositions(
        device: 1, 
        from: now()->subHours(1), 
        to: now()
    );
    // OsmAnd
    $positionRepo->OsmAnd(uniqueId: "1234-d1", temperature: "21.5", abc: "def");
}
  • 事件

模型 : 事件模型

public function index(TraccarService $traccarService)
{
    // Get specific event details
    $event = $traccarService->eventRepository()->fetchEventInformation(eventID: 1);
}
  • 司机

模型 : 司机模型

public function index(TraccarService $traccarService)
{
    $driverRepo = $traccarService->driverRepository();
    // Get list of drivers
    $list = $driverRepo->fetchListDrivers();
    // Create new driver
    $driver = $driverRepo->createDriver(
        name: 'test-driver',
        uniqueId: '123456789-d1-driver'
    );
    // Create new driver with Model : MrWolfGb\Traccar\Models\Driver
    $driver = $driverRepo->createNewDriver( new Driver([
      'name' => 'test-driver',
      'uniqueId' => '123456789-d1-driver'
    ]));
    // Update driver
    $driver = $driverRepo->updateDriver(driver: $driver);
    // Delete driver : int|Driver $driver
    $driverRepo->deleteDriver(driver: $driver);
}
  • 报告

模型 : 报告模型

public function index(TraccarService $traccarService)
{
    $reportRepo = $traccarService->reportRepository();
    // Get route report for specific device
    $list = $reportRepo->reportRoute(
        from:  now()->subHours(value: 3),
        to: now(),
        deviceId: 1
    );
    // Get events report
    $list = $reportRepo->reportEvents(
        from:  now()->subHours(value: 3),
        to: now(),
        deviceId: 1,
        type: 'engine' // optional, by default 'allEvents'
    );
    // Get summary report
    $list = $reportRepo->reportSummary(
        from:  now()->subHours(value: 3),
        to: now(),
        deviceId: [1,2],
        //groupId: [1,2], // optional
        //daily: true // optional
    );
    // Get trips report
    $list = $reportRepo->reportTrips(
        from:  now()->subHours(value: 3),
        to: now(),
        deviceId: 1
    );
    // Get stops report
    $list = $reportRepo->reportStops(
        from:  now()->subHours(value: 3),
        to: now(),
        deviceId: 1
    );
    // Get combined report
    $list = $reportRepo->reportCombined(
        from:  now()->subHours(value: 3),
        to: now(),
        deviceId: [1,2],
        //groupId: [1,2], // optional
    );
}

命令

此命令使用已发布的迁移将设备存储在本地数据库中。

php artisan traccar:sync

或者

php artisan traccar:sync-devices

使用php监听Traccar WebSocket

如果您想使用php监听Traccar WebSocket,可以查看文件中的示例: WsListenCommand

Traccar自定义服务器

这个版本是原始TRACCAR仓库的分支,旨在添加一些有用的功能,如

  1. Websocket可以从外部主机(应用)访问
  2. api/session/check-sid?sid=[SESSION_ID]检查会话是否仍然有效

此版本Traccar的功能

中间件

TraccarSession是一个中间件,它将Traccar用户的会话ID添加到视图中。

// route web.php
Route::get('/', [HomeController::class, 'index'])->middleware('TraccarSession');
// blade view
const socket = new WebSocket("{{config('traccar.websocket_url')}}?session={{$traccarSessionId}}");
socket.onerror = (error) => {
    console.log('socket error: ', error)
}
socket.onmessage = function (event) {
    var data = JSON.parse(event.data);
    console.log('socket message : ', data)
}

测试

composer test

变更日志

请参阅变更日志以获取有关最近更改的更多信息。

贡献

请参阅CONTRIBUTING以获取详细信息。

致谢

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。