save work, not yet working
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
config.json
|
||||
src/udi/udi
|
||||
tmp/
|
||||
|
@ -8,11 +8,7 @@ RUN go build -a -installsuffix nocgo -o udi .
|
||||
|
||||
FROM scratch
|
||||
|
||||
ENV MQTT_BROKER ""
|
||||
ENV MQTT_USERNAME ""
|
||||
ENV MQTT_PASSWORD ""
|
||||
ENV MQTT_ENABLE_TLS ""
|
||||
ENV MQTT_SUBSCRIBE_TOPICS ""
|
||||
ENV UDI_CONF ""
|
||||
|
||||
COPY --from=builder /go/src/udi ./
|
||||
ENTRYPOINT ["./udi"]
|
||||
|
@ -13,9 +13,9 @@ type ConfigT struct {
|
||||
TlsEnable string `json:"tlsEnable"`
|
||||
} `json:"mqtt"`
|
||||
TopicMappings []struct {
|
||||
Topics []string `json:topics`
|
||||
Handler string `json:handler`
|
||||
} `json:"TopicMappings"`
|
||||
Topics []string `json:"topics"`
|
||||
Handler string `json:"handler"`
|
||||
} `json:"topicMappings"`
|
||||
Handlers []struct {
|
||||
Name string `json:"name"`
|
||||
DatabaseConnStr string `json:"databaseConnStr"`
|
||||
|
26
src/udi/config/example.json
Normal file
26
src/udi/config/example.json
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"mqtt": {
|
||||
"broker": "172.23.1.102:1883",
|
||||
"username": "",
|
||||
"password": "",
|
||||
"tlsEnable": "false"
|
||||
},
|
||||
"topicMappings": [
|
||||
{
|
||||
"topics": ["IoT/MBGW3/Measurement"],
|
||||
"handler": "IoT"
|
||||
}
|
||||
],
|
||||
"handlers": [
|
||||
{
|
||||
"name": "IoT",
|
||||
"databaseConnStr": "",
|
||||
"attributes": {
|
||||
}
|
||||
}
|
||||
],
|
||||
"archiver": {
|
||||
"dir": "/mnt/udi/archive"
|
||||
}
|
||||
}
|
||||
|
@ -3,11 +3,20 @@ package dispatcher
|
||||
import "log"
|
||||
import "time"
|
||||
import "os"
|
||||
import "fmt"
|
||||
import "net/url"
|
||||
import "encoding/json"
|
||||
import "udi/mqtt"
|
||||
import "udi/config"
|
||||
import "udi/handlers/handler"
|
||||
import "udi/handlers/ttn"
|
||||
import "udi/handlers/iot"
|
||||
|
||||
type archivingStruct struct {
|
||||
timestamp string `json:"timestamp"`
|
||||
topic string `json:"topic"`
|
||||
payload string `json:"payload"`
|
||||
}
|
||||
|
||||
var handlerMap map[string]handler.Handler = make(map[string]handler.Handler)
|
||||
var archiverChannel chan mqtt.Message = make(chan mqtt.Message, 100)
|
||||
@ -21,32 +30,56 @@ func InitDispatcher() {
|
||||
case "TTN":
|
||||
handlerMap[handlerEntry.Name] = ttn.NewTTNHandler()
|
||||
log.Printf("TTN initialized")
|
||||
case "IoT":
|
||||
handlerMap[handlerEntry.Name] = iot.NewIoTHandler()
|
||||
log.Printf("IoT initialized")
|
||||
default:
|
||||
log.Fatalf("Handler %s not found", handlerEntry.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func storeMessage(filename, item string) {
|
||||
file, err := os.OpenFile(archivingFilename, os.O_APPEND | os.O_CREATE | os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
log.Printf("Unable to open archiving file %s, message is not archived: %s", archivingFilename, err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
archivingString, err := json.Marshal(archivingItem)
|
||||
if err != nil {
|
||||
log.Printf("Unable to convert to json, message is not archived: %s", err)
|
||||
return
|
||||
}
|
||||
_, err := file.WriteString(string(message.Payload))
|
||||
if err != nil {
|
||||
log.Printf("Unable to write message, message is not archived: %s", err)
|
||||
return
|
||||
}
|
||||
log.Printf("Archiving message in file %s", archivingFilename)
|
||||
}
|
||||
|
||||
func archiver() {
|
||||
archivingRootDir := config.Config.Archiver.Dir
|
||||
currentArchivingDir := ""
|
||||
lastArchivingDir := ""
|
||||
var lastArchivingDir string
|
||||
|
||||
for {
|
||||
select {
|
||||
case _ = <- archiverChannel:
|
||||
case message := <- archiverChannel:
|
||||
currentTime := time.Now()
|
||||
currentDateStr := currentTime.Format("2006/01/02")
|
||||
currentDateStr := currentTime.Format("2006/01/02/15")
|
||||
currentArchivingDir := archivingRootDir + "/" + currentDateStr
|
||||
if currentArchivingDir != lastArchivingDir {
|
||||
err := os.MkdirAll(currentArchivingDir, 0755)
|
||||
if err := nil {
|
||||
log.Printf("Unable to create archiving dir %s", currentArchivingDir)
|
||||
if err != nil {
|
||||
log.Printf("Unable to create archiving dir %s: %s", currentArchivingDir, err)
|
||||
}
|
||||
lastArchivingDir = currentArchivingDir
|
||||
log.Printf("Archiving dir %s created", currentArchivingDir)
|
||||
}
|
||||
archivingFilename := currentArchivingDir + "/" + string(currentTime.Hour()) + "/"
|
||||
log.Printf("Archiving message")
|
||||
archivingFilename := fmt.Sprintf("%s/%s", currentArchivingDir, url.PathEscape(message.Topic))
|
||||
archivingItem := archivingStruct { currentTime.Format("2006-01-02 15:04:05"), message.Topic, string(message.Payload) }
|
||||
storeMessage(archivingFilename, archivingItem)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
23
src/udi/handlers/iot/iot.go
Normal file
23
src/udi/handlers/iot/iot.go
Normal file
@ -0,0 +1,23 @@
|
||||
package iot
|
||||
|
||||
import "log"
|
||||
|
||||
var idSeq int = 0
|
||||
|
||||
type IoTHandler struct {
|
||||
id int
|
||||
}
|
||||
|
||||
func NewIoTHandler() *IoTHandler {
|
||||
t := &IoTHandler {
|
||||
id: idSeq,
|
||||
}
|
||||
idSeq += 1
|
||||
return t
|
||||
}
|
||||
|
||||
func (self *IoTHandler) Handle(topic, payload string) {
|
||||
log.Printf("Handler IoT %d processing %s -> %s", self.id, topic, payload)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user