save work, not yet working
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
config.json
|
config.json
|
||||||
src/udi/udi
|
src/udi/udi
|
||||||
|
tmp/
|
||||||
|
@ -8,11 +8,7 @@ RUN go build -a -installsuffix nocgo -o udi .
|
|||||||
|
|
||||||
FROM scratch
|
FROM scratch
|
||||||
|
|
||||||
ENV MQTT_BROKER ""
|
ENV UDI_CONF ""
|
||||||
ENV MQTT_USERNAME ""
|
|
||||||
ENV MQTT_PASSWORD ""
|
|
||||||
ENV MQTT_ENABLE_TLS ""
|
|
||||||
ENV MQTT_SUBSCRIBE_TOPICS ""
|
|
||||||
|
|
||||||
COPY --from=builder /go/src/udi ./
|
COPY --from=builder /go/src/udi ./
|
||||||
ENTRYPOINT ["./udi"]
|
ENTRYPOINT ["./udi"]
|
||||||
|
@ -13,9 +13,9 @@ type ConfigT struct {
|
|||||||
TlsEnable string `json:"tlsEnable"`
|
TlsEnable string `json:"tlsEnable"`
|
||||||
} `json:"mqtt"`
|
} `json:"mqtt"`
|
||||||
TopicMappings []struct {
|
TopicMappings []struct {
|
||||||
Topics []string `json:topics`
|
Topics []string `json:"topics"`
|
||||||
Handler string `json:handler`
|
Handler string `json:"handler"`
|
||||||
} `json:"TopicMappings"`
|
} `json:"topicMappings"`
|
||||||
Handlers []struct {
|
Handlers []struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
DatabaseConnStr string `json:"databaseConnStr"`
|
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 "log"
|
||||||
import "time"
|
import "time"
|
||||||
import "os"
|
import "os"
|
||||||
|
import "fmt"
|
||||||
|
import "net/url"
|
||||||
|
import "encoding/json"
|
||||||
import "udi/mqtt"
|
import "udi/mqtt"
|
||||||
import "udi/config"
|
import "udi/config"
|
||||||
import "udi/handlers/handler"
|
import "udi/handlers/handler"
|
||||||
import "udi/handlers/ttn"
|
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 handlerMap map[string]handler.Handler = make(map[string]handler.Handler)
|
||||||
var archiverChannel chan mqtt.Message = make(chan mqtt.Message, 100)
|
var archiverChannel chan mqtt.Message = make(chan mqtt.Message, 100)
|
||||||
@ -21,32 +30,56 @@ func InitDispatcher() {
|
|||||||
case "TTN":
|
case "TTN":
|
||||||
handlerMap[handlerEntry.Name] = ttn.NewTTNHandler()
|
handlerMap[handlerEntry.Name] = ttn.NewTTNHandler()
|
||||||
log.Printf("TTN initialized")
|
log.Printf("TTN initialized")
|
||||||
|
case "IoT":
|
||||||
|
handlerMap[handlerEntry.Name] = iot.NewIoTHandler()
|
||||||
|
log.Printf("IoT initialized")
|
||||||
default:
|
default:
|
||||||
log.Fatalf("Handler %s not found", handlerEntry.Name)
|
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() {
|
func archiver() {
|
||||||
archivingRootDir := config.Config.Archiver.Dir
|
archivingRootDir := config.Config.Archiver.Dir
|
||||||
currentArchivingDir := ""
|
var lastArchivingDir string
|
||||||
lastArchivingDir := ""
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case _ = <- archiverChannel:
|
case message := <- archiverChannel:
|
||||||
currentTime := time.Now()
|
currentTime := time.Now()
|
||||||
currentDateStr := currentTime.Format("2006/01/02")
|
currentDateStr := currentTime.Format("2006/01/02/15")
|
||||||
currentArchivingDir := archivingRootDir + "/" + currentDateStr
|
currentArchivingDir := archivingRootDir + "/" + currentDateStr
|
||||||
if currentArchivingDir != lastArchivingDir {
|
if currentArchivingDir != lastArchivingDir {
|
||||||
err := os.MkdirAll(currentArchivingDir, 0755)
|
err := os.MkdirAll(currentArchivingDir, 0755)
|
||||||
if err := nil {
|
if err != nil {
|
||||||
log.Printf("Unable to create archiving dir %s", currentArchivingDir)
|
log.Printf("Unable to create archiving dir %s: %s", currentArchivingDir, err)
|
||||||
}
|
}
|
||||||
lastArchivingDir = currentArchivingDir
|
lastArchivingDir = currentArchivingDir
|
||||||
|
log.Printf("Archiving dir %s created", currentArchivingDir)
|
||||||
}
|
}
|
||||||
archivingFilename := currentArchivingDir + "/" + string(currentTime.Hour()) + "/"
|
archivingFilename := fmt.Sprintf("%s/%s", currentArchivingDir, url.PathEscape(message.Topic))
|
||||||
log.Printf("Archiving message")
|
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