-
Notifications
You must be signed in to change notification settings - Fork 11
/
sql.go
132 lines (117 loc) · 2.93 KB
/
sql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"./mbus"
"database/sql"
"encoding/hex"
"fmt"
_ "github.com/go-sql-driver/mysql"
"log"
"strconv"
"time"
)
var db *sql.DB
// Initializes the DB
func setupDB() {
db, _ = sql.Open("mysql", *DBUser+":"+*DBPass+"@/"+*DBName+"?parseTime=true&loc=Local")
}
// Add a frame to the database
func addFrameToDB(frame *mbus.Frame) {
stmt, err := db.Prepare("INSERT INTO sniffedFrames (value) VALUES(?)")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
stmt.Exec(frame.Value)
// Try to update the frame in the muc table
stmt, err = db.Prepare("UPDATE muc SET `value`=?,`timestamp`=? WHERE address=?")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
stmt.Exec(frame.Value, time.Now(), frame.Identification())
// Try to insert the frame in the muc table
// this won't work if there is already one as the column is defined unique
stmt, err = db.Prepare("INSERT INTO muc (`value`, `address`, `key`) VALUES(?, ?, ?)")
if err != nil {
log.Fatal(err)
}
var key string
if frame.Address() == "440000570C37" {
key = "CAFEBABE123456789ABCDEF0CAFEBABE"
} else {
key = ""
}
defer stmt.Close()
stmt.Exec(frame.Value, frame.Identification(), key)
}
// Add key to MUC ID
func addKeyToID(ID int, key string) {
stmt, err := db.Prepare("UPDATE muc SET `key`=? WHERE `ID`=?")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
stmt.Exec(key, ID)
}
// Get all muc frames
func getMucFrames() []mbus.Frame {
rows, err := db.Query("SELECT `ID`, `value`, `timestamp`, `key` FROM muc ORDER BY timestamp DESC")
if err != nil {
log.Fatal(err)
}
// Fetch rows
var frames []mbus.Frame
for rows.Next() {
var ID int
var value string
var timestamp time.Time
var key string
rows.Scan(&ID, &value, ×tamp, &key)
frame, _ := mbus.NewFrame(value)
frame.Time = timestamp
frame.ID = ID
frame.Key = key
frames = append(frames, *frame)
}
return frames
}
// Get new frames
func getNewFrames(lastFrame int) ([]mbus.Frame, int) {
rows, err := db.Query("SELECT * FROM sniffedFrames WHERE id > " + strconv.Itoa(lastFrame))
if err != nil {
log.Fatal(err)
}
// Fetch rows
lastRow := lastFrame
var frames []mbus.Frame
for rows.Next() {
var ID int
var value string
var timestamp time.Time
rows.Scan(&ID, &value, ×tamp)
if ID != 0 {
frame, _ := mbus.NewFrame(value)
hexbyte, _ := hex.DecodeString(fmt.Sprintf("%s", value))
frame.Hexified = fmt.Sprintf("% X", fmt.Sprintf("%s", hexbyte))
frame.Time = timestamp
frame.ID = ID
frames = append(frames, *frame)
lastRow = ID
}
}
return frames, lastRow
}
// Truncates the SQL table
func truncateTable() {
db.Query("TRUNCATE TABLE sniffedFrames")
db.Query("TRUNCATE TABLE muc")
}
// Insert new frame into SQL database
func addNewFrame(value string, timestamp time.Time) {
stmt, err := db.Prepare("INSERT INTO sniffedFrames (value, timestamp) VALUES(?, ?)")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
stmt.Exec(value, timestamp)
}