2026-07-15 17:33:55 +08:00
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"modelRT/constants"
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-29 15:12:05 +08:00
|
|
|
// MeasurementTypeString converts measurement.type from PostgreSQL to the
|
|
|
|
|
// electric-element type stored in the Redis data-object hash.
|
|
|
|
|
func MeasurementTypeString(measurementType int16) (string, error) {
|
|
|
|
|
switch measurementType {
|
|
|
|
|
case constants.MeasurementTypeTelemetry:
|
|
|
|
|
return "TM", nil
|
|
|
|
|
case constants.MeasurementTypeTelesignal:
|
|
|
|
|
return "TS", nil
|
|
|
|
|
case constants.MeasurementTypeTelecommand:
|
|
|
|
|
return "TC", nil
|
|
|
|
|
case constants.MeasurementTypeTeleadjusting:
|
|
|
|
|
return "TA", nil
|
|
|
|
|
case constants.MeasurementTypeSetpoint:
|
|
|
|
|
return "SP", nil
|
|
|
|
|
default:
|
|
|
|
|
return "", fmt.Errorf("unsupported measurement type %d", measurementType)
|
2026-07-15 17:33:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func integerJSONValue(value any) (int, error) {
|
|
|
|
|
switch typed := value.(type) {
|
|
|
|
|
case int:
|
|
|
|
|
return typed, nil
|
|
|
|
|
case int8:
|
|
|
|
|
return int(typed), nil
|
|
|
|
|
case int16:
|
|
|
|
|
return int(typed), nil
|
|
|
|
|
case int32:
|
|
|
|
|
return int(typed), nil
|
|
|
|
|
case int64:
|
|
|
|
|
return int(typed), nil
|
|
|
|
|
case float32:
|
|
|
|
|
converted := int(typed)
|
|
|
|
|
if typed != float32(converted) {
|
|
|
|
|
return 0, fmt.Errorf("expected integer, got %v", typed)
|
|
|
|
|
}
|
|
|
|
|
return converted, nil
|
|
|
|
|
case float64:
|
|
|
|
|
converted := int(typed)
|
|
|
|
|
if typed != float64(converted) {
|
|
|
|
|
return 0, fmt.Errorf("expected integer, got %v", typed)
|
|
|
|
|
}
|
|
|
|
|
return converted, nil
|
|
|
|
|
default:
|
|
|
|
|
return 0, fmt.Errorf("expected integer, got %T", value)
|
|
|
|
|
}
|
|
|
|
|
}
|