refactor color system to be separate from models, also move errors out of models.
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -0,0 +1,369 @@
|
||||
package color
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.aiterp.net/lucifer/new-server/internal/lerrors"
|
||||
"github.com/lucasb-eyer/go-colorful"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Color struct {
|
||||
RGB *RGB `json:"rgb,omitempty"`
|
||||
HS *HueSat `json:"hs,omitempty"`
|
||||
K *int `json:"k,omitempty"`
|
||||
XY *XY `json:"xy,omitempty"`
|
||||
}
|
||||
|
||||
func (col *Color) IsHueSat() bool {
|
||||
return col.HS != nil
|
||||
}
|
||||
|
||||
func (col *Color) IsHueSatKelvin() bool {
|
||||
return col.HS != nil && col.K != nil
|
||||
}
|
||||
|
||||
func (col *Color) IsKelvin() bool {
|
||||
return col.K != nil
|
||||
}
|
||||
|
||||
func (col *Color) IsEmpty() bool {
|
||||
return *col == Color{}
|
||||
}
|
||||
|
||||
func (col *Color) SetK(k int) {
|
||||
*col = Color{K: &k}
|
||||
}
|
||||
|
||||
func (col *Color) SetXY(xy XY) {
|
||||
*col = Color{XY: &xy}
|
||||
}
|
||||
|
||||
// ToRGB tries to copy the color to an RGB color. If it's already RGB, it will be plainly copied, but HS
|
||||
// will be returned. If ok is false, then no copying has occurred and cv2 will be blank.
|
||||
func (col *Color) ToRGB() (col2 Color, ok bool) {
|
||||
if col.RGB != nil {
|
||||
rgb := *col.RGB
|
||||
col2 = Color{RGB: &rgb}
|
||||
ok = true
|
||||
} else if col.HS != nil {
|
||||
rgb := col.HS.ToRGB()
|
||||
col2 = Color{RGB: &rgb}
|
||||
ok = true
|
||||
} else if col.XY != nil {
|
||||
rgb := col.XY.ToRGB()
|
||||
col2 = Color{RGB: &rgb}
|
||||
ok = true
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (col *Color) ToHS() (col2 Color, ok bool) {
|
||||
if col.HS != nil {
|
||||
hs := *col.HS
|
||||
col2 = Color{HS: &hs}
|
||||
ok = true
|
||||
} else if col.RGB != nil {
|
||||
hs := col.RGB.ToHS()
|
||||
col2 = Color{HS: &hs}
|
||||
ok = true
|
||||
} else if col.XY != nil {
|
||||
hs := col.XY.ToHS()
|
||||
col2 = Color{HS: &hs}
|
||||
ok = true
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (col *Color) ToHSK() (col2 Color, ok bool) {
|
||||
k := 4000
|
||||
|
||||
if col.HS != nil {
|
||||
hs := *col.HS
|
||||
col2 = Color{HS: &hs}
|
||||
|
||||
if col.K != nil {
|
||||
k = *col.K
|
||||
}
|
||||
col2.K = &k
|
||||
|
||||
ok = true
|
||||
} else if col.RGB != nil {
|
||||
hs := col.RGB.ToHS()
|
||||
col2 = Color{HS: &hs}
|
||||
col2.K = &k
|
||||
ok = true
|
||||
} else if col.XY != nil {
|
||||
hs := col.XY.ToHS()
|
||||
col2 = Color{HS: &hs}
|
||||
col2.K = &k
|
||||
ok = true
|
||||
} else if col.K != nil {
|
||||
k = *col.K
|
||||
col2.HS = &HueSat{Hue: 0, Sat: 0}
|
||||
col2.K = &k
|
||||
ok = true
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// ToXY tries to copy the color to an XY color.
|
||||
func (col *Color) ToXY() (col2 Color, ok bool) {
|
||||
if col.XY != nil {
|
||||
xy := *col.XY
|
||||
col2 = Color{XY: &xy}
|
||||
ok = true
|
||||
} else if col.HS != nil {
|
||||
xy := col.HS.ToXY()
|
||||
col2 = Color{XY: &xy}
|
||||
ok = true
|
||||
} else if col.RGB != nil {
|
||||
xy := col.RGB.ToXY()
|
||||
col2 = Color{XY: &xy}
|
||||
ok = true
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (col *Color) Interpolate(other Color, fac float64) Color {
|
||||
// Special case for kelvin values.
|
||||
if col.IsKelvin() && other.IsKelvin() {
|
||||
k1 := *col.K
|
||||
k2 := *col.K
|
||||
k3 := k1 + int(float64(k2-k1)*fac)
|
||||
return Color{K: &k3}
|
||||
}
|
||||
|
||||
// Get the colorful values.
|
||||
cvCF := col.colorful()
|
||||
otherCF := other.colorful()
|
||||
|
||||
// Blend and normalize
|
||||
blended := cvCF.BlendLuv(otherCF, fac)
|
||||
blendedHue, blendedSat, _ := blended.Hsv()
|
||||
blendedHs := HueSat{Hue: blendedHue, Sat: blendedSat}
|
||||
|
||||
// Convert to the first's type
|
||||
switch col.Kind() {
|
||||
case "rgb":
|
||||
rgb := blendedHs.ToRGB()
|
||||
return Color{RGB: &rgb}
|
||||
case "xy":
|
||||
xy := blendedHs.ToXY()
|
||||
return Color{XY: &xy}
|
||||
default:
|
||||
return Color{HS: &blendedHs}
|
||||
}
|
||||
}
|
||||
|
||||
func (col *Color) Kind() string {
|
||||
switch {
|
||||
case col.RGB != nil:
|
||||
return "rgb"
|
||||
case col.XY != nil:
|
||||
return "xy"
|
||||
case col.HS != nil && col.K != nil:
|
||||
return "hsk"
|
||||
case col.HS != nil:
|
||||
return "hs"
|
||||
case col.K != nil:
|
||||
return "k"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func (col *Color) String() string {
|
||||
switch {
|
||||
case col.RGB != nil:
|
||||
return fmt.Sprintf("rgb:%.3f,%.3f,%.3f", col.RGB.Red, col.RGB.Green, col.RGB.Blue)
|
||||
case col.XY != nil:
|
||||
return fmt.Sprintf("xy:%.4f,%.4f", col.XY.X, col.XY.Y)
|
||||
case col.HS != nil && col.K != nil:
|
||||
return fmt.Sprintf("hsk:%.4f,%.3f,%d", col.HS.Hue, col.HS.Sat, *col.K)
|
||||
case col.HS != nil:
|
||||
return fmt.Sprintf("hs:%.4f,%.3f", col.HS.Hue, col.HS.Sat)
|
||||
case col.K != nil:
|
||||
return fmt.Sprintf("k:%d", *col.K)
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func (col *Color) colorful() colorful.Color {
|
||||
switch {
|
||||
case col.HS != nil:
|
||||
return colorful.Hsv(col.HS.Hue, col.HS.Sat, 1)
|
||||
case col.RGB != nil:
|
||||
return colorful.Color{R: col.RGB.Red, G: col.RGB.Green, B: col.RGB.Blue}
|
||||
case col.XY != nil:
|
||||
return colorful.Xyy(col.XY.X, col.XY.Y, 0.5)
|
||||
default:
|
||||
return colorful.Color{R: 255, B: 255, G: 255}
|
||||
}
|
||||
}
|
||||
|
||||
func Parse(raw string) (col Color, err error) {
|
||||
if raw == "" {
|
||||
return
|
||||
}
|
||||
|
||||
tokens := strings.SplitN(raw, ":", 2)
|
||||
if len(tokens) != 2 {
|
||||
err = lerrors.ErrBadInput
|
||||
return
|
||||
}
|
||||
|
||||
switch tokens[0] {
|
||||
case "kelvin", "k":
|
||||
{
|
||||
parsedPart, err := strconv.Atoi(tokens[1])
|
||||
if err != nil {
|
||||
err = lerrors.ErrBadInput
|
||||
break
|
||||
}
|
||||
|
||||
col.K = &parsedPart
|
||||
}
|
||||
|
||||
case "xy":
|
||||
{
|
||||
parts := strings.Split(tokens[1], ",")
|
||||
if len(parts) < 2 {
|
||||
err = lerrors.ErrUnknownColorFormat
|
||||
return
|
||||
}
|
||||
|
||||
x, err1 := strconv.ParseFloat(parts[0], 64)
|
||||
y, err2 := strconv.ParseFloat(parts[1], 64)
|
||||
if err1 != nil || err2 != nil {
|
||||
err = lerrors.ErrBadInput
|
||||
break
|
||||
}
|
||||
|
||||
col.XY = &XY{x, y}
|
||||
}
|
||||
|
||||
case "hs":
|
||||
{
|
||||
parts := strings.Split(tokens[1], ",")
|
||||
if len(parts) < 2 {
|
||||
err = lerrors.ErrUnknownColorFormat
|
||||
return
|
||||
}
|
||||
|
||||
part1, err1 := strconv.ParseFloat(parts[0], 64)
|
||||
part2, err2 := strconv.ParseFloat(parts[1], 64)
|
||||
if err1 != nil || err2 != nil {
|
||||
err = lerrors.ErrBadInput
|
||||
break
|
||||
}
|
||||
|
||||
col.HS = &HueSat{Hue: part1, Sat: part2}
|
||||
}
|
||||
|
||||
case "hsk":
|
||||
{
|
||||
parts := strings.Split(tokens[1], ",")
|
||||
if len(parts) < 3 {
|
||||
err = lerrors.ErrUnknownColorFormat
|
||||
return
|
||||
}
|
||||
|
||||
part1, err1 := strconv.ParseFloat(parts[0], 64)
|
||||
part2, err2 := strconv.ParseFloat(parts[1], 64)
|
||||
part3, err3 := strconv.Atoi(parts[2])
|
||||
if err1 != nil || err2 != nil || err3 != nil {
|
||||
err = lerrors.ErrBadInput
|
||||
break
|
||||
}
|
||||
|
||||
col.HS = &HueSat{Hue: part1, Sat: part2}
|
||||
col.K = &part3
|
||||
}
|
||||
|
||||
case "rgb":
|
||||
{
|
||||
if strings.HasPrefix(tokens[1], "#") {
|
||||
hex := tokens[1][1:]
|
||||
if !validHex(hex) {
|
||||
err = lerrors.ErrBadInput
|
||||
break
|
||||
}
|
||||
|
||||
if len(hex) == 6 {
|
||||
col.RGB = &RGB{
|
||||
Red: float64(hex2num(hex[0:2])) / 255.0,
|
||||
Green: float64(hex2num(hex[2:4])) / 255.0,
|
||||
Blue: float64(hex2num(hex[4:6])) / 255.0,
|
||||
}
|
||||
} else if len(hex) == 3 {
|
||||
col.RGB = &RGB{
|
||||
Red: float64(hex2digit(hex[0])) / 15.0,
|
||||
Green: float64(hex2digit(hex[1])) / 15.0,
|
||||
Blue: float64(hex2digit(hex[2])) / 15.0,
|
||||
}
|
||||
} else {
|
||||
err = lerrors.ErrUnknownColorFormat
|
||||
return
|
||||
}
|
||||
} else {
|
||||
parts := strings.Split(tokens[1], ",")
|
||||
if len(parts) < 3 {
|
||||
err = lerrors.ErrUnknownColorFormat
|
||||
return
|
||||
}
|
||||
|
||||
part1, err1 := strconv.ParseFloat(parts[0], 64)
|
||||
part2, err2 := strconv.ParseFloat(parts[1], 64)
|
||||
part3, err3 := strconv.ParseFloat(parts[2], 64)
|
||||
if err1 != nil || err2 != nil || err3 != nil {
|
||||
err = lerrors.ErrBadInput
|
||||
break
|
||||
}
|
||||
|
||||
col.RGB = &RGB{Red: part1, Green: part2, Blue: part3}
|
||||
}
|
||||
|
||||
normalizedRGB := col.RGB.ToHS().ToRGB()
|
||||
col.RGB = &normalizedRGB
|
||||
}
|
||||
|
||||
default:
|
||||
err = lerrors.ErrUnknownColorFormat
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func validHex(h string) bool {
|
||||
for _, ch := range h {
|
||||
if !((ch >= 'a' && ch <= 'f') || (ch >= '0' || ch <= '9')) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func hex2num(s string) int {
|
||||
v := 0
|
||||
for _, h := range s {
|
||||
v *= 16
|
||||
v += hex2digit(byte(h))
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
func hex2digit(h byte) int {
|
||||
if h >= 'a' && h <= 'f' {
|
||||
return 10 + int(h-'a')
|
||||
} else {
|
||||
return int(h - '0')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package color
|
||||
|
||||
import "github.com/lucasb-eyer/go-colorful"
|
||||
|
||||
type HueSat struct {
|
||||
Hue float64 `json:"hue"`
|
||||
Sat float64 `json:"sat"`
|
||||
}
|
||||
|
||||
func (hs HueSat) ToXY() XY {
|
||||
return hs.ToRGB().ToXY()
|
||||
}
|
||||
|
||||
func (hs HueSat) ToRGB() RGB {
|
||||
c := colorful.Hsv(hs.Hue, hs.Sat, 1)
|
||||
return RGB{Red: c.R, Green: c.G, Blue: c.B}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package color
|
||||
|
||||
import "github.com/lucasb-eyer/go-colorful"
|
||||
|
||||
type RGB struct {
|
||||
Red float64 `json:"red"`
|
||||
Green float64 `json:"green"`
|
||||
Blue float64 `json:"blue"`
|
||||
}
|
||||
|
||||
func (rgb RGB) AtIntensity(intensity float64) RGB {
|
||||
hue, sat, _ := colorful.Color{R: rgb.Red, G: rgb.Green, B: rgb.Blue}.Hsv()
|
||||
hsv2 := colorful.Hsv(hue, sat, intensity)
|
||||
return RGB{Red: hsv2.R, Green: hsv2.G, Blue: hsv2.B}
|
||||
}
|
||||
|
||||
func (rgb RGB) ToHS() HueSat {
|
||||
hue, sat, _ := colorful.Color{R: rgb.Red, G: rgb.Green, B: rgb.Blue}.Hsv()
|
||||
return HueSat{Hue: hue, Sat: sat}
|
||||
}
|
||||
|
||||
func (rgb RGB) ToXY() XY {
|
||||
x, y, z := (colorful.Color{R: rgb.Red, G: rgb.Green, B: rgb.Blue}).Xyz()
|
||||
|
||||
return XY{
|
||||
X: x / (x + y + z),
|
||||
Y: y / (x + y + z),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
package color
|
||||
|
||||
import (
|
||||
"github.com/lucasb-eyer/go-colorful"
|
||||
"math"
|
||||
)
|
||||
|
||||
const eps = 0.0001
|
||||
const epsSquare = eps * eps
|
||||
|
||||
type Gamut struct {
|
||||
Red XY `json:"red"`
|
||||
Green XY `json:"green"`
|
||||
Blue XY `json:"blue"`
|
||||
}
|
||||
|
||||
func (cg *Gamut) side(x1, y1, x2, y2, x, y float64) float64 {
|
||||
return (y2-y1)*(x-x1) + (-x2+x1)*(y-y1)
|
||||
}
|
||||
|
||||
func (cg *Gamut) naiveContains(color XY) bool {
|
||||
x, y := color.X, color.Y
|
||||
x1, y1 := cg.Red.X, cg.Red.Y
|
||||
x2, y2 := cg.Green.X, cg.Green.Y
|
||||
x3, y3 := cg.Blue.X, cg.Blue.Y
|
||||
|
||||
checkSide1 := cg.side(x1, y1, x2, y2, x, y) < 0
|
||||
checkSide2 := cg.side(x2, y2, x3, y3, x, y) < 0
|
||||
checkSide3 := cg.side(x3, y3, x1, y1, x, y) < 0
|
||||
|
||||
return checkSide1 && checkSide2 && checkSide3
|
||||
}
|
||||
|
||||
func (cg *Gamut) getBounds() (xMin, xMax, yMin, yMax float64) {
|
||||
x1, y1 := cg.Red.X, cg.Red.Y
|
||||
x2, y2 := cg.Green.X, cg.Green.Y
|
||||
x3, y3 := cg.Blue.X, cg.Blue.Y
|
||||
|
||||
xMin = math.Min(x1, math.Min(x2, x3)) - eps
|
||||
xMax = math.Max(x1, math.Max(x2, x3)) + eps
|
||||
yMin = math.Min(y1, math.Min(y2, y3)) - eps
|
||||
yMax = math.Max(y1, math.Max(y2, y3)) + eps
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (cg *Gamut) isInBounds(color XY) bool {
|
||||
x, y := color.X, color.Y
|
||||
xMin, xMax, yMin, yMax := cg.getBounds()
|
||||
|
||||
return !(x < xMin || xMax < x || y < yMin || yMax < y)
|
||||
}
|
||||
|
||||
func (cg *Gamut) distanceSquarePointToSegment(x1, y1, x2, y2, x, y float64) float64 {
|
||||
sqLength1 := (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)
|
||||
dotProduct := ((x-x1)*(x2-x1) + (y-y1)*(y2-y1)) / sqLength1
|
||||
if dotProduct < 0 {
|
||||
return (x-x1)*(x-x1) + (y-y1)*(y-y1)
|
||||
} else if dotProduct <= 1 {
|
||||
sqLength2 := (x1-x)*(x1-x) + (y1-y)*(y1-y)
|
||||
return sqLength2 - dotProduct*dotProduct*sqLength1
|
||||
} else {
|
||||
return (x-x2)*(x-x2) + (y-y2)*(y-y2)
|
||||
}
|
||||
}
|
||||
|
||||
func (cg *Gamut) atTheEdge(color XY) bool {
|
||||
x, y := color.X, color.Y
|
||||
x1, y1 := cg.Red.X, cg.Red.Y
|
||||
x2, y2 := cg.Green.X, cg.Green.Y
|
||||
x3, y3 := cg.Blue.X, cg.Blue.Y
|
||||
|
||||
if cg.distanceSquarePointToSegment(x1, y1, x2, y2, x, y) <= epsSquare {
|
||||
return true
|
||||
}
|
||||
if cg.distanceSquarePointToSegment(x2, y2, x3, y3, x, y) <= epsSquare {
|
||||
return true
|
||||
}
|
||||
if cg.distanceSquarePointToSegment(x3, y3, x1, y1, x, y) <= epsSquare {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (cg *Gamut) Contains(color XY) bool {
|
||||
if cg == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
return cg.isInBounds(color) && (cg.naiveContains(color) || cg.atTheEdge(color))
|
||||
}
|
||||
|
||||
func (cg *Gamut) Conform(color XY) XY {
|
||||
if cg.Contains(color) {
|
||||
return color
|
||||
}
|
||||
|
||||
var best *XY
|
||||
|
||||
xMin, xMax, yMin, yMax := cg.getBounds()
|
||||
|
||||
for x := xMin; x < xMax; x += 0.001 {
|
||||
for y := yMin; y < yMax; y += 0.001 {
|
||||
color2 := XY{X: x, Y: y}
|
||||
|
||||
if cg.Contains(color2) {
|
||||
if best == nil || color.DistanceTo(color2) < color.DistanceTo(*best) {
|
||||
best = &color2
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if best == nil {
|
||||
centerX := (cg.Red.X + cg.Green.X + cg.Blue.X) / 3
|
||||
centerY := (cg.Red.Y + cg.Green.Y + cg.Blue.Y) / 3
|
||||
|
||||
stepX := (centerX - color.X) / 5000
|
||||
stepY := (centerY - color.Y) / 5000
|
||||
|
||||
for !cg.Contains(color) {
|
||||
color.X += stepX
|
||||
color.Y += stepY
|
||||
}
|
||||
|
||||
return color
|
||||
}
|
||||
|
||||
for x := best.X - 0.001; x < best.X+0.001; x += 0.0002 {
|
||||
for y := best.Y - 0.001; y < best.Y+0.001; y += 0.0002 {
|
||||
color2 := XY{X: x, Y: y}
|
||||
|
||||
if cg.atTheEdge(color2) {
|
||||
if best == nil || color.DistanceTo(color2) < color.DistanceTo(*best) {
|
||||
best = &color2
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for x := best.X - 0.0001; x < best.X+0.0001; x += 0.00003 {
|
||||
for y := best.Y - 0.0001; y < best.Y+0.0001; y += 0.00003 {
|
||||
color2 := XY{X: x, Y: y}
|
||||
|
||||
if cg.atTheEdge(color2) {
|
||||
if best == nil || color.DistanceTo(color2) < color.DistanceTo(*best) {
|
||||
best = &color2
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return *best
|
||||
}
|
||||
|
||||
type XY struct {
|
||||
X float64 `json:"x"`
|
||||
Y float64 `json:"y"`
|
||||
}
|
||||
|
||||
func (xy XY) ToRGB() RGB {
|
||||
h, s, _ := colorful.Xyy(xy.X, xy.Y, 0.5).Hsv()
|
||||
c := colorful.Hsv(h, s, 1)
|
||||
return RGB{Red: c.R, Green: c.G, Blue: c.B}
|
||||
}
|
||||
|
||||
func (xy XY) ToHS() HueSat {
|
||||
h, s, _ := colorful.Xyy(xy.X, xy.Y, 0.5).Hsv()
|
||||
return HueSat{Hue: h, Sat: s}
|
||||
}
|
||||
|
||||
func (xy XY) DistanceTo(other XY) float64 {
|
||||
return math.Sqrt(math.Pow(xy.X-other.X, 2) + math.Pow(xy.Y-other.Y, 2))
|
||||
}
|
||||
|
||||
func (xy XY) Round() XY {
|
||||
return XY{
|
||||
X: math.Round(xy.X*10000) / 10000,
|
||||
Y: math.Round(xy.Y*10000) / 10000,
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.aiterp.net/lucifer/new-server/internal/lerrors"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"golang.org/x/sync/errgroup"
|
||||
"io"
|
||||
@@ -159,7 +160,7 @@ func (b *Bridge) ForgetDevice(ctx context.Context, device models.Device) error {
|
||||
}
|
||||
b.mu.Unlock()
|
||||
if !found {
|
||||
return models.ErrNotFound
|
||||
return lerrors.ErrNotFound
|
||||
}
|
||||
|
||||
// Delete light from bridge
|
||||
@@ -237,7 +238,7 @@ func (b *Bridge) getToken(ctx context.Context) (string, error) {
|
||||
return "", errLinkButtonNotPressed
|
||||
}
|
||||
if result[0].Success == nil {
|
||||
return "", models.ErrUnexpectedResponse
|
||||
return "", lerrors.ErrUnexpectedResponse
|
||||
}
|
||||
|
||||
return result[0].Success.Username, nil
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"git.aiterp.net/lucifer/new-server/internal/lerrors"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -22,7 +23,7 @@ type Driver struct {
|
||||
func (d *Driver) SearchBridge(ctx context.Context, address, _ string, dryRun bool) ([]models.Bridge, error) {
|
||||
if address == "" {
|
||||
if !dryRun {
|
||||
return nil, models.ErrAddressOnlyDryRunnable
|
||||
return nil, lerrors.ErrAddressOnlyDryRunnable
|
||||
}
|
||||
|
||||
res, err := http.Get("https://discovery.meethue.com")
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.aiterp.net/lucifer/new-server/internal/color"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"golang.org/x/sync/errgroup"
|
||||
"log"
|
||||
@@ -283,7 +284,6 @@ func (b *Bridge) MakeCongruent(ctx context.Context) (int, error) {
|
||||
} else if xyColor, ok := device.State.Color.ToXY(); ok && light.Color != nil {
|
||||
xy := light.Color.Gamut.Conform(*xyColor.XY).Round()
|
||||
if xy.DistanceTo(light.Color.XY) > 0.0009 || (light.ColorTemperature != nil && light.ColorTemperature.Mirek != nil) {
|
||||
log.Println(xyColor.String(), device.State.Color.String())
|
||||
update.ColorXY = &xy
|
||||
changed = true
|
||||
}
|
||||
@@ -428,7 +428,7 @@ func (b *Bridge) GenerateDevices() []models.Device {
|
||||
}
|
||||
if light.Color != nil {
|
||||
if device.State.Color.IsEmpty() {
|
||||
device.State.Color = models.ColorValue{
|
||||
device.State.Color = color.Color{
|
||||
XY: &light.Color.XY,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"git.aiterp.net/lucifer/new-server/internal/lerrors"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
@@ -52,7 +52,7 @@ func (c *Client) Register(ctx context.Context) (string, error) {
|
||||
}
|
||||
}
|
||||
if result[0].Success == nil {
|
||||
return "", models.ErrUnexpectedResponse
|
||||
return "", lerrors.ErrUnexpectedResponse
|
||||
}
|
||||
|
||||
c.token = result[0].Success.Username
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"git.aiterp.net/lucifer/new-server/internal/color"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -152,9 +152,9 @@ type LightDimming struct {
|
||||
}
|
||||
|
||||
type LightColor struct {
|
||||
Gamut models.ColorGamut `json:"gamut"`
|
||||
GamutType string `json:"gamut_type"`
|
||||
XY models.ColorXY `json:"xy"`
|
||||
Gamut color.Gamut `json:"gamut"`
|
||||
GamutType string `json:"gamut_type"`
|
||||
XY color.XY `json:"xy"`
|
||||
}
|
||||
|
||||
type LightCT struct {
|
||||
@@ -182,7 +182,7 @@ type LightAlert struct {
|
||||
type ResourceUpdate struct {
|
||||
Name *string
|
||||
Power *bool
|
||||
ColorXY *models.ColorXY
|
||||
ColorXY *color.XY
|
||||
Brightness *float64
|
||||
Mirek *int
|
||||
TransitionDuration *time.Duration
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"git.aiterp.net/lucifer/new-server/internal/lerrors"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"net/http"
|
||||
"sync"
|
||||
@@ -19,7 +20,7 @@ type Driver struct {
|
||||
func (d *Driver) SearchBridge(ctx context.Context, address, token string, dryRun bool) ([]models.Bridge, error) {
|
||||
if address == "" {
|
||||
if !dryRun {
|
||||
return nil, models.ErrAddressOnlyDryRunnable
|
||||
return nil, lerrors.ErrAddressOnlyDryRunnable
|
||||
}
|
||||
|
||||
res, err := http.Get("https://discovery.meethue.com")
|
||||
|
||||
@@ -2,6 +2,8 @@ package lifx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.aiterp.net/lucifer/new-server/internal/color"
|
||||
"git.aiterp.net/lucifer/new-server/internal/lerrors"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"log"
|
||||
"sync"
|
||||
@@ -20,7 +22,7 @@ type Bridge struct {
|
||||
func (b *Bridge) StartSearch(ctx context.Context) error {
|
||||
c := b.getClient()
|
||||
if c == nil {
|
||||
return models.ErrBridgeRunningRequired
|
||||
return lerrors.ErrBridgeRunningRequired
|
||||
}
|
||||
|
||||
_, err := c.HorribleBroadcast(ctx, &GetService{})
|
||||
@@ -129,9 +131,9 @@ func (b *Bridge) Run(ctx context.Context, debug bool) error {
|
||||
|
||||
for {
|
||||
target, seq, payload, err := client.Recv(time.Millisecond * 200)
|
||||
if err == models.ErrInvalidPacketSize || err == models.ErrPayloadTooShort || err == models.ErrUnrecognizedPacketType {
|
||||
if err == lerrors.ErrInvalidPacketSize || err == lerrors.ErrPayloadTooShort || err == lerrors.ErrUnrecognizedPacketType {
|
||||
log.Println("LIFX udp socket received something weird:", err)
|
||||
} else if err != nil && err != models.ErrReadTimeout {
|
||||
} else if err != nil && err != lerrors.ErrReadTimeout {
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
@@ -168,8 +170,8 @@ func (b *Bridge) Run(ctx context.Context, debug bool) error {
|
||||
if state.deviceState == nil {
|
||||
state.deviceState = &models.DeviceState{
|
||||
Power: p.On,
|
||||
Color: models.ColorValue{
|
||||
HS: &models.ColorHS{
|
||||
Color: color.Color{
|
||||
HS: &color.HueSat{
|
||||
Hue: p.Hue,
|
||||
Sat: p.Sat,
|
||||
},
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"git.aiterp.net/lucifer/new-server/internal/lerrors"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net"
|
||||
@@ -215,7 +215,7 @@ func (c *Client) Recv(timeout time.Duration) (target string, seq uint8, payload
|
||||
}
|
||||
if err != nil {
|
||||
if netErr, ok := err.(*net.OpError); ok && netErr.Timeout() {
|
||||
err = models.ErrReadTimeout
|
||||
err = lerrors.ErrReadTimeout
|
||||
return
|
||||
}
|
||||
|
||||
@@ -224,7 +224,7 @@ func (c *Client) Recv(timeout time.Duration) (target string, seq uint8, payload
|
||||
|
||||
packet := Packet(c.buf[:n])
|
||||
if n < 2 || packet.Size() != n && packet.Protocol() != 1024 {
|
||||
err = models.ErrInvalidAddress
|
||||
err = lerrors.ErrInvalidAddress
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package lifx
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"git.aiterp.net/lucifer/new-server/internal/lerrors"
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
@@ -60,7 +60,7 @@ func (p *Packet) SetTarget(v string) error {
|
||||
return err
|
||||
}
|
||||
if len(addr) != 6 {
|
||||
return models.ErrInvalidAddress
|
||||
return lerrors.ErrInvalidAddress
|
||||
}
|
||||
|
||||
copy((*p)[8:], addr)
|
||||
@@ -133,7 +133,7 @@ func (p *Packet) Payload() (res Payload, err error) {
|
||||
res = &SetLightPower{}
|
||||
err = res.Decode((*p)[36:])
|
||||
default:
|
||||
err = models.ErrUnrecognizedPacketType
|
||||
err = lerrors.ErrUnrecognizedPacketType
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -3,7 +3,7 @@ package lifx
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"git.aiterp.net/lucifer/new-server/internal/lerrors"
|
||||
"math"
|
||||
"time"
|
||||
)
|
||||
@@ -82,7 +82,7 @@ type StateHostFirmware struct {
|
||||
|
||||
func (p *StateHostFirmware) Decode(data []byte) error {
|
||||
if len(data) < 20 {
|
||||
return models.ErrPayloadTooShort
|
||||
return lerrors.ErrPayloadTooShort
|
||||
}
|
||||
|
||||
ts := int64(binary.LittleEndian.Uint64(data[0:8]))
|
||||
@@ -123,7 +123,7 @@ type StateVersion struct {
|
||||
|
||||
func (p *StateVersion) Decode(data []byte) error {
|
||||
if len(data) < 8 {
|
||||
return models.ErrPayloadTooShort
|
||||
return lerrors.ErrPayloadTooShort
|
||||
}
|
||||
|
||||
p.Vendor = binary.LittleEndian.Uint32(data[0:4])
|
||||
@@ -207,7 +207,7 @@ type SetColor struct {
|
||||
|
||||
func (p *SetColor) Decode(data []byte) error {
|
||||
if len(data) < 13 {
|
||||
return models.ErrPayloadTooShort
|
||||
return lerrors.ErrPayloadTooShort
|
||||
}
|
||||
|
||||
hue := binary.LittleEndian.Uint16(data[1:3])
|
||||
@@ -263,7 +263,7 @@ type SetLightPower struct {
|
||||
|
||||
func (p *SetLightPower) Decode(data []byte) error {
|
||||
if len(data) < 6 {
|
||||
return models.ErrPayloadTooShort
|
||||
return lerrors.ErrPayloadTooShort
|
||||
}
|
||||
|
||||
level := binary.LittleEndian.Uint16(data[0:2])
|
||||
@@ -314,7 +314,7 @@ func (p *StateService) String() string {
|
||||
|
||||
func (p *StateService) Decode(data []byte) error {
|
||||
if len(data) < 5 {
|
||||
return models.ErrPayloadTooShort
|
||||
return lerrors.ErrPayloadTooShort
|
||||
}
|
||||
|
||||
p.Service = int(data[0])
|
||||
@@ -357,7 +357,7 @@ func (p *LightState) String() string {
|
||||
|
||||
func (p *LightState) Decode(data []byte) error {
|
||||
if len(data) < 52 {
|
||||
return models.ErrPayloadTooShort
|
||||
return lerrors.ErrPayloadTooShort
|
||||
}
|
||||
|
||||
hue := binary.LittleEndian.Uint16(data[0:2])
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package lifx
|
||||
|
||||
import (
|
||||
"git.aiterp.net/lucifer/new-server/internal/color"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"math"
|
||||
"time"
|
||||
@@ -38,7 +39,7 @@ func (s *State) generateUpdate() []Payload {
|
||||
|
||||
c, ok := s.deviceState.Color.ToHSK()
|
||||
if !ok {
|
||||
c, _ = models.ParseColorValue("hsk:0,0,4000")
|
||||
c, _ = color.Parse("hsk:0,0,4000")
|
||||
}
|
||||
|
||||
l := s.lightState
|
||||
@@ -76,16 +77,16 @@ func (s *State) handleAck(seq uint8) {
|
||||
prevLabel = s.lightState.Label
|
||||
}
|
||||
|
||||
color, ok := s.deviceState.Color.ToHSK()
|
||||
c, ok := s.deviceState.Color.ToHSK()
|
||||
if !ok {
|
||||
color, _ = models.ParseColorValue("hsk:0,0,4000")
|
||||
c, _ = color.Parse("hsk:0,0,4000")
|
||||
}
|
||||
|
||||
s.lightState = &LightState{
|
||||
Hue: color.HS.Hue,
|
||||
Sat: color.HS.Sat,
|
||||
Hue: c.HS.Hue,
|
||||
Sat: c.HS.Sat,
|
||||
Bri: s.deviceState.Intensity,
|
||||
Kelvin: *color.K,
|
||||
Kelvin: *c.K,
|
||||
On: s.deviceState.Power,
|
||||
Label: prevLabel,
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"crypto/sha1"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.aiterp.net/lucifer/new-server/internal/lerrors"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"io"
|
||||
"log"
|
||||
@@ -163,9 +164,9 @@ func (b *bridge) command(ctx context.Context, command string, payload interface{
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return models.ErrCannotForwardRequest
|
||||
return lerrors.ErrCannotForwardRequest
|
||||
} else if res.StatusCode != 200 {
|
||||
return models.ErrIncorrectToken
|
||||
return lerrors.ErrIncorrectToken
|
||||
}
|
||||
|
||||
if target == nil {
|
||||
@@ -174,7 +175,7 @@ func (b *bridge) command(ctx context.Context, command string, payload interface{
|
||||
|
||||
err = json.NewDecoder(res.Body).Decode(&target)
|
||||
if err != nil {
|
||||
return models.ErrUnexpectedResponse
|
||||
return lerrors.ErrUnexpectedResponse
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -190,27 +191,27 @@ func (b *bridge) authenticate(ctx context.Context) error {
|
||||
Password: b.password,
|
||||
})
|
||||
if err != nil {
|
||||
return models.ErrMissingToken
|
||||
return lerrors.ErrMissingToken
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", accountEndpoint + "login", bytes.NewReader(body))
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", accountEndpoint+"login", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return models.ErrMissingToken
|
||||
return lerrors.ErrMissingToken
|
||||
}
|
||||
|
||||
addDefaultHeaders(req)
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return models.ErrCannotForwardRequest
|
||||
return lerrors.ErrCannotForwardRequest
|
||||
} else if res.StatusCode != 200 {
|
||||
return models.ErrIncorrectToken
|
||||
return lerrors.ErrIncorrectToken
|
||||
}
|
||||
|
||||
var resBody authResBody
|
||||
err = json.NewDecoder(res.Body).Decode(&resBody)
|
||||
if err != nil {
|
||||
return models.ErrBridgeSearchFailed
|
||||
return lerrors.ErrBridgeSearchFailed
|
||||
}
|
||||
|
||||
log.Printf("Mill: Authenticated as %s", resBody.NickName)
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.aiterp.net/lucifer/new-server/internal/color"
|
||||
"git.aiterp.net/lucifer/new-server/internal/lerrors"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"github.com/lucasb-eyer/go-colorful"
|
||||
"io"
|
||||
@@ -70,7 +72,7 @@ func (b *bridge) Devices() []models.Device {
|
||||
UserProperties: nil,
|
||||
State: models.DeviceState{
|
||||
Power: panel.ColorRGBA[3] == 0,
|
||||
Color: models.ColorValue{RGB: &models.ColorRGB{
|
||||
Color: color.Color{RGB: &color.RGB{
|
||||
Red: rgb.R,
|
||||
Green: rgb.G,
|
||||
Blue: rgb.B,
|
||||
@@ -140,9 +142,9 @@ func (b *bridge) Overview(ctx context.Context) (*Overview, error) {
|
||||
|
||||
switch res.StatusCode {
|
||||
case 400, 403, 500, 503:
|
||||
return nil, models.ErrUnexpectedResponse
|
||||
return nil, lerrors.ErrUnexpectedResponse
|
||||
case 401:
|
||||
return nil, models.ErrIncorrectToken
|
||||
return nil, lerrors.ErrIncorrectToken
|
||||
}
|
||||
|
||||
overview := Overview{}
|
||||
@@ -333,7 +335,7 @@ func (b *bridge) updateEffect(ctx context.Context) error {
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != 204 {
|
||||
return models.ErrUnexpectedResponse
|
||||
return lerrors.ErrUnexpectedResponse
|
||||
}
|
||||
|
||||
b.mu.Lock()
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.aiterp.net/lucifer/new-server/internal/lerrors"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"net/http"
|
||||
"sync"
|
||||
@@ -30,7 +31,7 @@ func (d *Driver) SearchBridge(ctx context.Context, address, _ string, dryRun boo
|
||||
return nil, err
|
||||
}
|
||||
if deviceInfo.ModelNumber == "" {
|
||||
return nil, models.ErrUnexpectedResponse
|
||||
return nil, lerrors.ErrUnexpectedResponse
|
||||
}
|
||||
|
||||
token := ""
|
||||
@@ -47,7 +48,7 @@ func (d *Driver) SearchBridge(ctx context.Context, address, _ string, dryRun boo
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != 200 {
|
||||
return nil, models.ErrBridgeSearchFailed
|
||||
return nil, lerrors.ErrBridgeSearchFailed
|
||||
}
|
||||
|
||||
tokenResponse := TokenResponse{}
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
package drivers
|
||||
|
||||
import "git.aiterp.net/lucifer/new-server/models"
|
||||
import (
|
||||
"git.aiterp.net/lucifer/new-server/internal/lerrors"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
)
|
||||
|
||||
type DriverMap map[models.DriverKind]models.Driver
|
||||
|
||||
func (m DriverMap) Provide(kind models.DriverKind) (models.Driver, error) {
|
||||
if m[kind] == nil {
|
||||
return nil, models.ErrUnknownDriver
|
||||
return nil, lerrors.ErrUnknownDriver
|
||||
}
|
||||
|
||||
return m[kind], nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package lerrors
|
||||
|
||||
import "errors"
|
||||
|
||||
var ErrNotFound = errors.New("not found")
|
||||
var ErrInvalidName = errors.New("invalid name")
|
||||
var ErrBadInput = errors.New("bad input")
|
||||
var ErrBadColor = errors.New("bad color")
|
||||
var ErrInternal = errors.New("internal")
|
||||
var ErrUnknownColorFormat = errors.New("unknown color format")
|
||||
var ErrUnknownDriver = errors.New("unknown driver")
|
||||
|
||||
var ErrMissingToken = errors.New("driver is missing authentication information")
|
||||
var ErrIncorrectToken = errors.New("driver is not accepting authentication information")
|
||||
var ErrUnexpectedResponse = errors.New("driver api returned unexpected response (wrong driver selected?)")
|
||||
var ErrBridgeSearchFailed = errors.New("bridge search failed")
|
||||
var ErrAddressOnlyDryRunnable = errors.New("this address may only be used for a dry run")
|
||||
var ErrCannotForwardRequest = errors.New("driver is not able to forward requests")
|
||||
var ErrCannotForget = errors.New("forget not supported on this bridge type")
|
||||
|
||||
var ErrInvalidAddress = errors.New("invalid mac address")
|
||||
var ErrPayloadTooShort = errors.New("payload too short")
|
||||
var ErrInvalidPacketSize = errors.New("invalid packet size")
|
||||
var ErrReadTimeout = errors.New("read timeout")
|
||||
var ErrUnrecognizedPacketType = errors.New("packet type not recognized")
|
||||
var ErrBridgeRunningRequired = errors.New("this operation cannot be performed when bridge is not running")
|
||||
|
||||
var ErrSceneInvalidInterval = errors.New("scene interval must be 0 (=disabled) or greater")
|
||||
var ErrSceneNoRoles = errors.New("scene cannot have zero rules")
|
||||
var ErrSceneRoleNoStates = errors.New("scene rule has no states")
|
||||
var ErrSceneRoleUnsupportedOrdering = errors.New("scene rule has an unsupported ordering")
|
||||
var ErrSceneRoleUnknownEffect = errors.New("scene rule has an unknown effect")
|
||||
var ErrSceneRoleUnknownPowerMode = errors.New("scene rule has an unknown power mode")
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"git.aiterp.net/lucifer/new-server/internal/color"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
sq "github.com/Masterminds/squirrel"
|
||||
"github.com/jmoiron/sqlx"
|
||||
@@ -375,7 +376,7 @@ func (r *DeviceRepo) populate(ctx context.Context, records []deviceRecord) ([]mo
|
||||
|
||||
for _, state := range states {
|
||||
if state.DeviceID == record.ID {
|
||||
color, _ := models.ParseColorValue(state.Color)
|
||||
color, _ := color.Parse(state.Color)
|
||||
|
||||
device.State = models.DeviceState{
|
||||
Power: state.Power,
|
||||
|
||||
@@ -2,6 +2,7 @@ package mysql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.aiterp.net/lucifer/new-server/internal/color"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
@@ -85,7 +86,7 @@ func (c *ColorPresetRepo) Delete(ctx context.Context, preset *models.ColorPreset
|
||||
func (c *ColorPresetRepo) fromRecords(records ...presetRecord) []models.ColorPreset {
|
||||
newList := make([]models.ColorPreset, len(records), len(records))
|
||||
for i, record := range records {
|
||||
color, _ := models.ParseColorValue(record.Value)
|
||||
color, _ := color.Parse(record.Value)
|
||||
|
||||
newList[i] = models.ColorPreset{
|
||||
ID: record.ID,
|
||||
|
||||
@@ -2,16 +2,16 @@ package mysql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"git.aiterp.net/lucifer/new-server/internal/lerrors"
|
||||
"log"
|
||||
)
|
||||
|
||||
func dbErr(err error) error {
|
||||
if err == sql.ErrNoRows {
|
||||
return models.ErrNotFound
|
||||
return lerrors.ErrNotFound
|
||||
} else if err != nil {
|
||||
log.Printf("Internal error: %s", err.Error())
|
||||
return models.ErrInternal
|
||||
return lerrors.ErrInternal
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user