Device Handler Documentation
This code defines a DeviceHandler
struct that handles various device-related HTTP requests. It uses the Echo framework for routing and handling HTTP requests and responses.
DeviceHandler
struct
type DeviceHandler struct {
medahandler.MedaHandler
cfg config.Config
devices map[machinescanner.MachineBrand]attendancemachine.AttendanceMachine
}
medahandler.MedaHandler
: Embeds theMedaHandler
struct from themedahandler
package.cfg config.Config
: Configuration settings.devices map[machinescanner.MachineBrand]attendancemachine.AttendanceMachine
: A map of attendance machines by their brand.
Functions
NewDeviceHandler
func NewDeviceHandler(
cfg config.Config,
deviceRevo attendancemachine.AttendanceMachine,
deviceHik attendancemachine.AttendanceMachine,
deviceDahua attendancemachine.AttendanceMachine,
) medahandler.MedaHandlerContract
Creates a new DeviceHandler
instance.
BindRouter
func (h *DeviceHandler) BindRouter(pbx *core.ServeEvent) error
Binds the handler's routes to the Echo router.
ScanDevice
func (h *DeviceHandler) ScanDevice(c echo.Context) error
Scans for devices on the network and returns the result as a JSON response.
UpsertDevice
func (h *DeviceHandler) UpsertDevice(c echo.Context) error
Upserts (inserts or updates) device information and returns the result as a JSON response.
GetDeviceInfo
func (h *DeviceHandler) GetDeviceInfo(c echo.Context) error
Gets device information and returns the result as a JSON response.
SetDateTime
func (h *DeviceHandler) SetDateTime(c echo.Context) error
Sets the date and time on a device and returns the result as a JSON response.
Example Usage
Initializing the Handler
package main
import (
"fio-backend/config"
"fio-backend/handler"
"fio-backend/utils/attendance_machine"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"
)
func main() {
// Initialize Echo
e := echo.New()
// Initialize PocketBase
pb := pocketbase.New()
// Load configuration
cfg := config.LoadConfig()
// Initialize attendance machines
deviceRevo := attendance_machine.NewRevoMachine(cfg)
deviceHik := attendance_machine.NewHikMachine(cfg)
deviceDahua := attendance_machine.NewDahuaMachine(cfg)
// Initialize the handler
deviceHandler := handler.NewDeviceHandler(cfg, deviceRevo, deviceHik, deviceDahua)
// Bind routes
pb.OnBeforeServe().Add(func(e *core.ServeEvent) error {
return deviceHandler.BindRouter(e)
})
// Start the server
e.Logger.Fatal(e.Start(":8080"))
}
Making Requests
Scan Devices
curl -X GET http://localhost:8080/api/v1/device/scan
Upsert Device
curl -X POST http://localhost:8080/api/v1/device -d '{
"devices": [
{
"id": "device123",
"name": "Device 123",
"brand": "revo",
"ip": "192.168.1.100",
"port": 5005,
"username": "admin",
"password": "password"
}
]
}'
Get Device Info
curl -X GET http://localhost:8080/api/v1/device/info -d '{
"brand": "revo",
"ip": "192.168.1.100",
"port": 5005,
"username": "admin",
"password": "password"
}'
Set Device Date and Time
curl -X POST http://localhost:8080/api/v1/device/device123/set-datetime -d '{
"device_id": "device123",
"date_time": "2023-01-01T12:00:00Z"
}'
This documentation provides an overview of the DeviceHandler
struct, its functions, and example usage. By following the example, you can initialize the handler, bind routes, and make requests to the various endpoints.