Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds basic topic integration and a topic column to the data view #23

Merged
merged 1 commit into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Wifi_sqlwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ def on_message(client, userdata, msg):
writeToDb(theTime, p["DeviceID"], p["MessageID"], p["Payload"], p["path"],p["hops"],p["duckType"])
return

def writeToDb(theTime, duckId, messageId, payload, path, hops, duckType):
def writeToDb(theTime, duckId, topic, messageId, payload, path, hops, duckType):
conn = sqlite3.connect(dbFile)
c = conn.cursor()
print ("Writing to db...")
try:
c.execute("INSERT INTO clusterData VALUES (?,?,?,?,?,?,?)", (theTime, duckId, messageId, payload, path, hops, duckType))
c.execute("INSERT INTO clusterData VALUES (?,?,?,?,?,?,?,?)", (theTime, duckId, topic, messageId, payload, path, hops, duckType))
conn.commit()
conn.close()
except Error as e:
Expand All @@ -50,7 +50,7 @@ def writeToDb(theTime, duckId, messageId, payload, path, hops, duckType):

try:
db = sqlite3.connect(dbFile)
db.cursor().execute("CREATE TABLE IF NOT EXISTS clusterData (timestamp datetime, duck_id TEXT, message_id TEXT, payload TEXT, path TEXT, hops INT, duck_type INT)")
db.cursor().execute("CREATE TABLE IF NOT EXISTS clusterData (timestamp datetime, duck_id TEXT, topic TEXT, message_id TEXT, payload TEXT, path TEXT, hops INT, duck_type INT)")
db.commit()
db.close()
except Error as e:
Expand Down
Binary file modified data.db
Binary file not shown.
8 changes: 4 additions & 4 deletions routes/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function closeDB() {
function getAllData() {
return new Promise((resolve, reject) => {
openDB();
let sql = 'SELECT timestamp, duck_id, message_id, payload, path, hops, duck_type FROM clusterData ORDER BY timestamp DESC'
let sql = 'SELECT timestamp, duck_id, topic, message_id, payload, path, hops, duck_type FROM clusterData ORDER BY timestamp DESC'
console.log(sql)
db.all(sql, (err, rows) => {
if (err) {
Expand All @@ -46,7 +46,7 @@ function getAllData() {
function getDataByDuckId(duckId) {
return new Promise((resolve, reject) => {
openDB();
let sql = 'SELECT timestamp, duck_id, message_id, payload, path, hops, duck_type FROM clusterData WHERE duck_Id = ?'
let sql = 'SELECT timestamp, duck_id, topic, message_id, payload, path, hops, duck_type FROM clusterData WHERE duck_Id = ?'

db.all(sql, [duckId], (err, rows) => {
if (err) {
Expand Down Expand Up @@ -77,7 +77,7 @@ function getUniqueDucks() {
function getLastCount(count) {
return new Promise((resolve, reject) => {
openDB();
let sql = 'SELECT timestamp, duck_id, message_id, payload, path, hops, duck_type FROM clusterData DESC LIMIT ?'
let sql = 'SELECT timestamp, duck_id, topic, message_id, payload, path, hops, duck_type FROM clusterData DESC LIMIT ?'

db.all(sql, [count], (err, rows) => {
if (err) {
Expand All @@ -92,7 +92,7 @@ function getLastCount(count) {
function getDuckPlusData() {
return new Promise((resolve, reject) => {
openDB();
let sql = 'SELECT timestamp, duck_id, message_id, payload, path, hops, duck_type FROM ( SELECT ROW_NUMBER() OVER ( PARTITION BY duck_id ORDER BY timestamp DESC ) RowNum, timestamp, duck_id, message_id, payload, path, hops, duck_type FROM clusterData ) WHERE RowNum = 1;'
let sql = 'SELECT timestamp, duck_id, topic, message_id, payload, path, hops, duck_type FROM ( SELECT ROW_NUMBER() OVER ( PARTITION BY duck_id ORDER BY timestamp DESC ) RowNum, timestamp, duck_id, topic, message_id, payload, path, hops, duck_type FROM clusterData ) WHERE RowNum = 1;'

db.all(sql, (err, rows) => {
if (err) {
Expand Down
30 changes: 13 additions & 17 deletions serial_sqlwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,34 @@
ser = serial.Serial('/dev/ttyUSB0',115200)


def writeToDb(theTime, duckId, messageId, payload, path, hops, duckType):
def writeToDb(theTime, duckId, topic, messageId, payload, path, hops, duckType):
conn = sqlite3.connect(dbFile)
c = conn.cursor()
print ("Writing to db...")
try:
c.execute("INSERT INTO clusterData VALUES (?,?,?,?,?,?,?)", (theTime, duckId, messageId, payload, path, hops, duckType))
c.execute("INSERT INTO clusterData VALUES (?,?,?,?,?,?,?,?)", (theTime, duckId, topic, messageId, payload, path, hops, duckType))
conn.commit()
conn.close()
except Error as e:
print(e)


try:
db = sqlite3.connect(dbFile)
db.cursor().execute("CREATE TABLE IF NOT EXISTS clusterData (timestamp datetime, duck_id TEXT, topic TEXT, message_id TEXT, payload TEXT, path TEXT, hops INT, duck_type INT)")
db.commit()
db.close()
except Error as e:
print(e)

while True:
theTime = strftime("%Y-%m-%d %Hs:%M:%S", gmtime())
theTime = strftime("%Y-%m-%d %H:%M:%S", gmtime())
payload = ser.readline()
prstrip = payload.rstrip().decode('utf8')
if len(prstrip) >0:
print(prstrip)
try:
p = json.loads(prstrip)
writeToDb(theTime, p["DeviceID"], p["MessageID"], p["Payload"], p["path"],p["hops"],p["duckType"])
writeToDb(theTime, p["DeviceID"], p["topic"], p["MessageID"], p["Payload"], p["path"],p["hops"],p["duckType"])
except:
print(prstrip)
print("Invalid Packet")





try:
db = sqlite3.connect(dbFile)
db.cursor().execute("CREATE TABLE IF NOT EXISTS clusterData (timestamp datetime, duck_id TEXT, message_id TEXT, payload TEXT, path TEXT, hops INT, duck_type INT)")
db.commit()
db.close()
except Error as e:
print(e)
2 changes: 2 additions & 0 deletions views/data.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<thead>
<th>DuckID</th>
<th>Timestamp</th>
<th>Topic</th>
<th>Message ID</th>
<th>Path</th>
<th>Payload</th>
Expand All @@ -25,6 +26,7 @@
<tr>
<td><%= duck.duck_id %></td>
<td><%= duck.timestamp %></td>
<td><%= duck.topic %></td>
<td><%= duck.message_id %></td>
<td><%= duck.path %></td>
<td><%= duck.payload %></td>
Expand Down