Compare commits

..

No commits in common. "pu/ImproveDatabase" and "master" have entirely different histories.

6 changed files with 22 additions and 81 deletions

View File

@ -52,7 +52,7 @@ build_linux_arm() {
echo -e "\t\t${GREEN}... done${NC}"
echo -e "\nstipping arm binary:"
strip -o ${APP}-linux-arm-stripped ${APP}-linux-arm
strip -o ${TARGET}-linux-arm-stripped ${APP}-linux-arm
echo -e "\t\t${GREEN}... done${NC}"
}

10
env/env.go vendored
View File

@ -26,14 +26,6 @@ func (env *Env) DevicesIndex(w http.ResponseWriter, r *http.Request) {
return
}
for _, device := range devices {
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,
)
fmt.Fprintf(w, "%s, %s, %s, £%.2f\n", device.ID, device.MAC, device.SN, device.LastMsg)
}
}

View File

@ -84,8 +84,7 @@ func main() {
mqtt.Setup(env)
mqtt.Connect()
go mqtt.Listen("/ATB/#")
go mqtt.Listen("ATB/#")
go mqtt.Listen()
http.HandleFunc("/devices", env.DevicesIndex)
go http.ListenAndServe(":3000", nil)

View File

@ -24,13 +24,7 @@ func NewDB(dataSourceName string) (*DB, error) {
return nil, err
}
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);`
sqlStmt := "CREATE TABLE IF NOT EXISTS devices (id TEXT not null primary key, mac TEXT, sn TEXT, lastMsg TEXT);"
_, err = db.Exec(sqlStmt)
if err != nil {
return nil, err

View File

@ -2,17 +2,13 @@ package models
import (
"fmt"
"time"
)
type Device struct {
TOPIC string
CustomerID string
DeviceID string
ProjectName string
MAC string
SN string
LastMsg string
ID string
MAC string
SN string
LastMsg string
}
func (db *DB) AllDevices() ([]*Device, error) {
@ -25,58 +21,28 @@ func (db *DB) AllDevices() ([]*Device, error) {
devices := make([]*Device, 0)
for rows.Next() {
device := new(Device)
err := rows.Scan(&device.TOPIC, &device.CustomerID, &device.DeviceID, &device.ProjectName, &device.MAC, &device.SN, &device.LastMsg)
err := rows.Scan(&device.ID, &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 {
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)
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)
if err != nil {
return err
}
// DEBUG
fmt.Printf("Sucessfully inserted device with ID %s to database\n", device.TOPIC)
fmt.Printf("Sucessfully inserted device with ID %s to database\n", device.ID)
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
}

View File

@ -40,7 +40,7 @@ func Connect() {
fmt.Println("connected to broker, topic = ", topic)
}
func Listen(topic string) {
func Listen() {
if !client.IsConnected() {
log.Fatal("Client is not connected")
return
@ -82,6 +82,8 @@ func Setup(environment *env.Env) {
//fmt.Println("customer: ", *customer)
//fmt.Println("device: ", *device)
topic = "/ATB/#"
opts = createClientOptions(ev.Config.BrokerClientID, uri)
}
@ -122,15 +124,10 @@ 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 {
@ -141,16 +138,13 @@ 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")
customerID = topicSlice[2]
deviceID = topicSlice[3]
deviceID = topicSlice[2] + "_" + topicSlice[3] + "_" + topicSlice[4]
} else if matchedDefault {
fmt.Printf("Topic matched regexpDefault\n")
customerID = topicSlice[1]
deviceID = topicSlice[4]
deviceID = topicSlice[2] + "_" + topicSlice[3] + "_" + topicSlice[4] + "_" + topicSlice[5]
} else {
log.Printf("ERROR: no matching topic: %s", msg.Topic())
return
@ -159,11 +153,7 @@ var subscriptionHandler = func(client MQTT.Client, msg MQTT.Message) {
fmt.Printf("Generated deviceID = %s\n", deviceID)
var device models.Device
device.TOPIC = msg.Topic()
device.CustomerID = customerID
device.DeviceID = deviceID
device.ProjectName = "projectName t.b.d."
device.ID = deviceID
device.MAC = "t.b.d."
device.SN = "t.b.d."
device.LastMsg = "t.b.d"
@ -184,7 +174,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)
// store this device in database
// TODO: store this device in database
err = ev.DB.InsertDevice(&device)
if err != nil {
fmt.Println(err.Error())