Compare commits
8 Commits
master
...
pu/Improve
Author | SHA1 | Date | |
---|---|---|---|
30c66efffd | |||
78241d8e5a | |||
8e95443548 | |||
dc5d066698 | |||
e02af3c353 | |||
5045802242 | |||
6f319cf6dd | |||
f21e4ff1d6 |
2
build.sh
2
build.sh
@ -52,7 +52,7 @@ build_linux_arm() {
|
||||
echo -e "\t\t${GREEN}... done${NC}"
|
||||
|
||||
echo -e "\nstipping arm binary:"
|
||||
strip -o ${TARGET}-linux-arm-stripped ${APP}-linux-arm
|
||||
strip -o ${APP}-linux-arm-stripped ${APP}-linux-arm
|
||||
echo -e "\t\t${GREEN}... done${NC}"
|
||||
}
|
||||
|
||||
|
10
env/env.go
vendored
10
env/env.go
vendored
@ -26,6 +26,14 @@ func (env *Env) DevicesIndex(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
for _, device := range devices {
|
||||
fmt.Fprintf(w, "%s, %s, %s, £%.2f\n", device.ID, device.MAC, device.SN, device.LastMsg)
|
||||
fmt.Fprintf(w, "%s, %s, %s, %s, %s, %s, %s\n",
|
||||
device.TOPIC,
|
||||
device.CustomerID,
|
||||
device.DeviceID,
|
||||
device.ProjectName,
|
||||
device.MAC,
|
||||
device.SN,
|
||||
device.LastMsg,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
3
main.go
3
main.go
@ -84,7 +84,8 @@ func main() {
|
||||
|
||||
mqtt.Setup(env)
|
||||
mqtt.Connect()
|
||||
go mqtt.Listen()
|
||||
go mqtt.Listen("/ATB/#")
|
||||
go mqtt.Listen("ATB/#")
|
||||
|
||||
http.HandleFunc("/devices", env.DevicesIndex)
|
||||
go http.ListenAndServe(":3000", nil)
|
||||
|
@ -24,7 +24,13 @@ func NewDB(dataSourceName string) (*DB, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sqlStmt := "CREATE TABLE IF NOT EXISTS devices (id TEXT not null primary key, mac TEXT, sn TEXT, lastMsg TEXT);"
|
||||
sqlStmt := `CREATE TABLE IF NOT EXISTS devices (topic TEXT not null primary key,
|
||||
customerID TEXT,
|
||||
deviceID TEXT,
|
||||
projectName TEXT,
|
||||
mac TEXT,
|
||||
sn TEXT,
|
||||
lastMsg TIMESTAMP DEFAULT CURRENT_TIMESTAMP);`
|
||||
_, err = db.Exec(sqlStmt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -2,13 +2,17 @@ package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Device struct {
|
||||
ID string
|
||||
MAC string
|
||||
SN string
|
||||
LastMsg string
|
||||
TOPIC string
|
||||
CustomerID string
|
||||
DeviceID string
|
||||
ProjectName string
|
||||
MAC string
|
||||
SN string
|
||||
LastMsg string
|
||||
}
|
||||
|
||||
func (db *DB) AllDevices() ([]*Device, error) {
|
||||
@ -21,28 +25,58 @@ func (db *DB) AllDevices() ([]*Device, error) {
|
||||
devices := make([]*Device, 0)
|
||||
for rows.Next() {
|
||||
device := new(Device)
|
||||
err := rows.Scan(&device.ID, &device.MAC, &device.SN, &device.LastMsg)
|
||||
err := rows.Scan(&device.TOPIC, &device.CustomerID, &device.DeviceID, &device.ProjectName, &device.MAC, &device.SN, &device.LastMsg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
device.ProjectName, err = db.GetProjectName(device.CustomerID)
|
||||
devices = append(devices, device)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return devices, nil
|
||||
}
|
||||
|
||||
func (db *DB) InsertDevice(device *Device) error {
|
||||
sqlStmt := `INSERT OR REPLACE INTO devices (id, mac, sn, lastMsg)
|
||||
VALUES($1, $2, $3, $4);`
|
||||
_, err := db.Exec(sqlStmt, device.ID, device.MAC, device.SN, device.LastMsg)
|
||||
|
||||
timeStamp := time.Now().Format(time.RFC3339)
|
||||
// DEBUG
|
||||
fmt.Printf("Timestamp: %s\n", timeStamp)
|
||||
|
||||
sqlStmt := `INSERT OR REPLACE INTO devices (topic, customerID, deviceID, projectName, mac, sn)
|
||||
VALUES($1, $2, $3, $4, $5, $6);`
|
||||
_, err := db.Exec(sqlStmt, device.TOPIC, device.CustomerID, device.DeviceID, device.ProjectName, device.MAC, device.SN)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// DEBUG
|
||||
fmt.Printf("Sucessfully inserted device with ID %s to database\n", device.ID)
|
||||
fmt.Printf("Sucessfully inserted device with ID %s to database\n", device.TOPIC)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *DB) GetProjectName(projectNumber string) (string, error) {
|
||||
|
||||
projectNameRows, err := db.Query("SELECT projectName FROM projects WHERE projectNumber = $1", projectNumber)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer projectNameRows.Close()
|
||||
|
||||
projectName := ""
|
||||
for projectNameRows.Next() {
|
||||
err := projectNameRows.Scan(&projectName)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
if err = projectNameRows.Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return projectName, err
|
||||
|
||||
}
|
||||
|
28
mqtt/mqtt.go
28
mqtt/mqtt.go
@ -40,7 +40,7 @@ func Connect() {
|
||||
fmt.Println("connected to broker, topic = ", topic)
|
||||
}
|
||||
|
||||
func Listen() {
|
||||
func Listen(topic string) {
|
||||
if !client.IsConnected() {
|
||||
log.Fatal("Client is not connected")
|
||||
return
|
||||
@ -82,8 +82,6 @@ func Setup(environment *env.Env) {
|
||||
//fmt.Println("customer: ", *customer)
|
||||
//fmt.Println("device: ", *device)
|
||||
|
||||
topic = "/ATB/#"
|
||||
|
||||
opts = createClientOptions(ev.Config.BrokerClientID, uri)
|
||||
|
||||
}
|
||||
@ -124,10 +122,15 @@ var subscriptionHandler = func(client MQTT.Client, msg MQTT.Message) {
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// create ID from topic
|
||||
regexpWithType := "\\/ATB\\/[A-Z]+\\/[0-9]+\\/[0-9]+\\/mo"
|
||||
regexpDefault := "\\/ATB\\/[0-9]+\\/[0-9]+\\/[0-9]+\\/[0-9]+\\/mo"
|
||||
regexpWithType := "ATB\\/[A-Z]+\\/[\\-]*[0-9]+\\/[\\-]*[0-9]+\\/mo"
|
||||
regexpDefault := "ATB\\/[0-9]+\\/[\\-]*[0-9]+\\/[\\-]*[0-9]+\\/[0-9]+\\/mo"
|
||||
|
||||
topicSlice := strings.Split(msg.Topic(), "/")
|
||||
|
||||
// remove a possible empty element (-> handle '/ATB/#' and 'ATB/#'
|
||||
if (len(topicSlice[0]) == 0) {
|
||||
topicSlice = topicSlice[1:]
|
||||
}
|
||||
|
||||
matchedWithType, err := regexp.MatchString(regexpWithType, msg.Topic())
|
||||
if err != nil {
|
||||
@ -138,13 +141,16 @@ var subscriptionHandler = func(client MQTT.Client, msg MQTT.Message) {
|
||||
log.Printf("ERROR: matchedDefault: %s", err.Error())
|
||||
}
|
||||
|
||||
var customerID string
|
||||
var deviceID string
|
||||
if matchedWithType {
|
||||
fmt.Printf("Topic matched regexpWithType\n")
|
||||
deviceID = topicSlice[2] + "_" + topicSlice[3] + "_" + topicSlice[4]
|
||||
customerID = topicSlice[2]
|
||||
deviceID = topicSlice[3]
|
||||
} else if matchedDefault {
|
||||
fmt.Printf("Topic matched regexpDefault\n")
|
||||
deviceID = topicSlice[2] + "_" + topicSlice[3] + "_" + topicSlice[4] + "_" + topicSlice[5]
|
||||
customerID = topicSlice[1]
|
||||
deviceID = topicSlice[4]
|
||||
} else {
|
||||
log.Printf("ERROR: no matching topic: %s", msg.Topic())
|
||||
return
|
||||
@ -153,7 +159,11 @@ var subscriptionHandler = func(client MQTT.Client, msg MQTT.Message) {
|
||||
fmt.Printf("Generated deviceID = %s\n", deviceID)
|
||||
|
||||
var device models.Device
|
||||
device.ID = deviceID
|
||||
device.TOPIC = msg.Topic()
|
||||
device.CustomerID = customerID
|
||||
device.DeviceID = deviceID
|
||||
device.ProjectName = "projectName t.b.d."
|
||||
|
||||
device.MAC = "t.b.d."
|
||||
device.SN = "t.b.d."
|
||||
device.LastMsg = "t.b.d"
|
||||
@ -174,7 +184,7 @@ var subscriptionHandler = func(client MQTT.Client, msg MQTT.Message) {
|
||||
fmt.Printf("Device MAC = %s\n", device.MAC)
|
||||
fmt.Printf("Device SN = %s\n", device.SN)
|
||||
|
||||
// TODO: store this device in database
|
||||
// store this device in database
|
||||
err = ev.DB.InsertDevice(&device)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
|
Loading…
Reference in New Issue
Block a user