140 lines
4.9 KiB
Go
140 lines
4.9 KiB
Go
package helper
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"users_management/m/model/dto/res"
|
|
"users_management/m/model/entity"
|
|
"users_management/m/utils/service"
|
|
)
|
|
|
|
func ConvertToDeviceDetailsResponses(devices []entity.DeviceDetails, geocoder service.GeocodingService) ([]res.DeviceDetailsResponse, error) {
|
|
var responses []res.DeviceDetailsResponse
|
|
|
|
for _, device := range devices {
|
|
response, err := ConvertToDeviceDetailsResponse(device, geocoder)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
responses = append(responses, response)
|
|
}
|
|
|
|
return responses, nil
|
|
}
|
|
|
|
func ConvertToDeviceDetailsResponse(device entity.DeviceDetails, geocoder service.GeocodingService) (res.DeviceDetailsResponse, error) {
|
|
// Get address
|
|
address := ""
|
|
if geocoder != nil {
|
|
addr, err := geocoder.GetAddressFromCoordinates(device.Latitude, device.Longitude)
|
|
if err != nil {
|
|
log.Printf("Geocoding error for device %s: %v", device.DeviceCode, err)
|
|
address = fmt.Sprintf("Coordinates: %.6f, %.6f", device.Latitude, device.Longitude)
|
|
} else {
|
|
address = addr
|
|
}
|
|
}
|
|
|
|
// Get all backbones connected to this device
|
|
allBackbones := device.GetAllBackbones()
|
|
backboneInfos := make([]res.BackboneConnectionInfo, 0)
|
|
for _, backbone := range allBackbones {
|
|
isStartDevice := backbone.DeviceStartID == device.ID
|
|
connectedTo := ""
|
|
|
|
if isStartDevice && backbone.DeviceEnd.DeviceCode != "" {
|
|
connectedTo = backbone.DeviceEnd.DeviceCode
|
|
} else if !isStartDevice && backbone.DeviceStart.DeviceCode != "" {
|
|
connectedTo = backbone.DeviceStart.DeviceCode
|
|
}
|
|
|
|
info := res.BackboneConnectionInfo{
|
|
ID: backbone.ID,
|
|
BackboneCode: backbone.BackboneCode,
|
|
CoreAmount: backbone.CoreAmount,
|
|
IsStartDevice: isStartDevice,
|
|
ConnectedTo: connectedTo,
|
|
}
|
|
backboneInfos = append(backboneInfos, info)
|
|
}
|
|
|
|
// Get all fishbones connected to this device
|
|
allFishbones := device.GetAllFishbones()
|
|
fishboneInfos := make([]res.FishboneConnectionInfo, 0)
|
|
for _, fishbone := range allFishbones {
|
|
isStartDevice := fishbone.DeviceStartID == device.ID
|
|
connectedTo := ""
|
|
|
|
if isStartDevice && fishbone.DeviceEnd.DeviceCode != "" {
|
|
connectedTo = fishbone.DeviceEnd.DeviceCode
|
|
} else if !isStartDevice && fishbone.DeviceStart.DeviceCode != "" {
|
|
connectedTo = fishbone.DeviceStart.DeviceCode
|
|
}
|
|
|
|
info := res.FishboneConnectionInfo{
|
|
ID: fishbone.ID,
|
|
FishboneCode: fishbone.FishboneCode,
|
|
CoreAmount: fishbone.CoreAmount,
|
|
BackboneCode: fishbone.Backbone.BackboneCode,
|
|
IsStartDevice: isStartDevice,
|
|
ConnectedTo: connectedTo,
|
|
}
|
|
fishboneInfos = append(fishboneInfos, info)
|
|
}
|
|
|
|
// Convert tower connections - safely handle nullable ExternalTower
|
|
towerInfos := make([]res.TowerConnectionDetail, 0)
|
|
for _, tower := range device.Towers {
|
|
distance := calculateDistance(device.Latitude, device.Longitude, tower.Latitude, tower.Longitude)
|
|
|
|
// Safely handle nullable ExternalTower field
|
|
var externalTower *bool
|
|
if tower.ExternalTower != nil {
|
|
externalTower = tower.ExternalTower
|
|
} // If tower.ExternalTower is nil, externalTower remains nil
|
|
|
|
// Safely handle nullable ImageURL
|
|
var imageURL *string
|
|
if tower.ImageURL != "" {
|
|
imageURL = &tower.ImageURL
|
|
}
|
|
|
|
info := res.TowerConnectionDetail{
|
|
ID: tower.ID,
|
|
TowerCode: tower.TowerCode,
|
|
Distance: distance,
|
|
ExternalTower: externalTower, // This will be null if tower.ExternalTower is nil
|
|
ImageURL: imageURL,
|
|
}
|
|
towerInfos = append(towerInfos, info)
|
|
}
|
|
|
|
response := res.DeviceDetailsResponse{
|
|
ID: device.ID,
|
|
DeviceCode: device.DeviceCode,
|
|
DeviceType: string(device.DeviceType),
|
|
Address: address,
|
|
Longitude: device.Longitude,
|
|
Latitude: device.Latitude,
|
|
Status: string(device.Status),
|
|
PortAmount: device.PortAmount,
|
|
PortUsed: device.DevicePort.PortUsed,
|
|
PortAvailable: device.DevicePort.PortAvailable,
|
|
Region: device.Region,
|
|
Province: device.Province,
|
|
City: device.City,
|
|
District: device.District,
|
|
ImageURL: device.ImageURL,
|
|
Backbones: backboneInfos,
|
|
Fishbones: fishboneInfos,
|
|
Towers: towerInfos,
|
|
CreatedAt: device.CreatedAt,
|
|
UpdatedAt: device.UpdatedAt,
|
|
}
|
|
|
|
return response, nil
|
|
}
|
|
|
|
|
|
|
|
// ... rest of helper functions remain the same
|