Browse Source

switched to scribble as database

master
Giovanni Harting 1 year ago
parent
commit
010f7a89fb
2 changed files with 45 additions and 38 deletions
  1. +44
    -33
      ledd.go
  2. +1
    -5
      ledd.yaml

+ 44
- 33
ledd.go View File

@@ -2,8 +2,9 @@ package main

import (
"encoding/binary"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"encoding/json"
"github.com/nanobox-io/golang-scribble"
"math"
"net"
"os"
"os/signal"
@@ -27,7 +28,7 @@ const LOG_CLIENTS = "CH"
// STRUCTS

type Config struct {
Name string
Name string
Daemon struct {
Frontend struct {
Host string
@@ -92,11 +93,11 @@ type LEDManager struct {
// GLOBAL VARS

var log = logging.MustGetLogger("LedD")
var backManager = BackendManager{}
var clientManager = ClientManager{}
var ledManager = LEDManager{}
var LEDCollection = &mgo.Collection{}
var config = Config{}
var backManager BackendManager
var clientManager ClientManager
var ledManager LEDManager
var db *scribble.Driver
var config Config

// SOCKET SETUP

@@ -141,20 +142,30 @@ func (manager *LEDManager) start() {
}

if _, ok := manager.leds[led.Name]; ok {
log.Warningf("[%s] Can't add LED: already existent! (%s)", LOG_CLIENTS, led.Name)
log.Warningf("[%s] Can't add LED: already in LEDM! (%s)", LOG_CLIENTS, led.Name)
continue
}

var dbLED LED
err := db.Read("led", led.Name, &dbLED)
if err == nil {
// log.Warningf("[%s] LED already in DB! (%s)", LOG_CLIENTS, led.Name)
} else if os.IsNotExist(err) {
err = db.Write("led", led.Name, led)
if err != nil {
log.Warning("[%s] Error while adding LED to database: %s", LOG_BACKEND, err)
}
} else {
log.Warning("[%s] Error while checking database for LED: %s", LOG_BACKEND, err)
}

manager.leds[led.Name] = led
go manager.color(led)
err := LEDCollection.Insert(led)
if err != nil {
log.Warning("[%s] Error while adding LED to database: %s", LOG_BACKEND, err)
}
case led := <-manager.remove:
if _, ok := manager.leds[led.Name]; ok {
log.Debugf("[%s] Request to remove %s", led.Backend, led.Name)
LEDCollection.RemoveAll(bson.M{"name": led.Name})
err := db.Delete("led", led.Name)
check(err)
delete(manager.leds, led.Name)
}
case color := <-manager.broadcast:
@@ -180,9 +191,12 @@ func (manager *LEDManager) color(led *LED) {
cMap := make(map[int32]int32)

if !color.IsValid() {
log.Warningf("[%s] Got invalid HCL->RGB color, clamping!", led.Name)
color = color.Clamped()
}

log.Debugf("[%s] New color: \x1b[38;2;%d;%d;%dm%s\x1b[0m", led.Name, int(math.Round(color.R*255)), int(math.Round(color.G*255)), int(math.Round(color.B*255)), color.Hex())

cMap[led.Channel[0]] = int32(color.R * float64(backend.resolution))
cMap[led.Channel[1]] = int32(color.G * float64(backend.resolution))
cMap[led.Channel[2]] = int32(color.B * float64(backend.resolution))
@@ -391,10 +405,8 @@ func (manager *ClientManager) receive(client *Client) {
break
}
if length > 0 {
//log.Debugf("[%s] Read %d bytes", client.socket.RemoteAddr(), length)

for i := 0; i < length; {
msgLen := int(binary.BigEndian.Uint32(message[i:i+4]))
msgLen := int(binary.BigEndian.Uint32(message[i : i+4]))

// log.Debugf("[%s] Reading protobuf after %d (len=%d)", client.socket.RemoteAddr(), i+4, msgLen)

@@ -437,14 +449,14 @@ func (manager *ClientManager) receive(client *Client) {
break
}

nLED := &LED{
nLED := LED{
Name: msg.MAddLed.Name,
Channel: msg.MAddLed.Channel,
Backend: backend.name,
color: make(chan colorful.Color, 20),
}

ledManager.add <- nLED
ledManager.add <- &nLED
case *ledd.ClientWrapperMessage_MSetLed:
leds := clientMsg.Leds

@@ -530,7 +542,6 @@ func prepareProtobuf(data []byte) []byte {
}

// MAIN

func main() {
killSignals := make(chan os.Signal, 1)
signal.Notify(killSignals, syscall.SIGINT, syscall.SIGTERM)
@@ -543,11 +554,8 @@ func main() {
err = yaml.Unmarshal(content, &config)
check(err)

session, err := mgo.Dial(fmt.Sprintf("%s:%d", config.Mongodb.Host, config.Mongodb.Port))
db, err = scribble.New("db", nil)
check(err)
defer session.Close()

LEDCollection = session.DB(config.Mongodb.Database).C("led")

backManager = BackendManager{
backends: make(map[string]*Backend),
@@ -555,7 +563,6 @@ func main() {
register: make(chan *Backend, 10),
unregister: make(chan *Backend, 10),
}

go backManager.start()

clientManager = ClientManager{
@@ -564,7 +571,6 @@ func main() {
register: make(chan *Client, 10),
unregister: make(chan *Client, 10),
}

go clientManager.start()

ledManager = LEDManager{
@@ -573,17 +579,22 @@ func main() {
add: make(chan *LED, 10),
remove: make(chan *LED, 10),
}

go ledManager.start()

var dbLEDs = make([]LED, 0)
err = LEDCollection.Find(nil).All(&dbLEDs)
if err != nil {
log.Notice("Failed to load LEDs from db. If there should be LEDs, check db connection")
leds, err := db.ReadAll("led")
if os.IsNotExist(err) {
log.Infof("No DB found.")
} else {
check(err)
}

for _, l := range dbLEDs {
ledManager.add <- &l
for _, ledJson := range leds {
led := LED{}
err = json.Unmarshal([]byte(ledJson), &led)
check(err)

led.color = make(chan colorful.Color, 20)
ledManager.add <- &led
}

backendThread, err := setupSocket(config.Daemon.Backend.Host, config.Daemon.Backend.Port, LOG_BACKEND, true)


+ 1
- 5
ledd.yaml View File

@@ -5,8 +5,4 @@ daemon:
port: 5635
backend:
host: ""
port: 5640
mongodb:
host: "127.0.0.1"
port: 27017
database: "ledd"
port: 5640

Loading…
Cancel
Save