it works yippee (depending on what node sends it data the script will send it to the corresponding table with automatic node numbering)
This commit is contained in:
@@ -6,15 +6,11 @@ import json
|
||||
sensorNodeArray = []
|
||||
enqueteNodeArray = []
|
||||
|
||||
async def processSensorNodeData(data):
|
||||
async def processSensorNodeData(data, nodeID):
|
||||
try:
|
||||
mydb = dbLogin()
|
||||
cursor = mydb.cursor()
|
||||
|
||||
# MACDataReading = mydb.cursor()
|
||||
# MACDataReading.execute("SELECT MAC FROM Node")
|
||||
|
||||
print('some_response')
|
||||
query = "INSERT INTO `Measurement` (NodeID, Type, Value) VALUES (%s, %s, %s)"
|
||||
processedData = json.loads(data)
|
||||
|
||||
@@ -23,18 +19,8 @@ async def processSensorNodeData(data):
|
||||
processedeCO2 = (processedData['eCO2'])
|
||||
processedTVOC = (processedData['TVOC'])
|
||||
processedMAC = (processedData['node'])
|
||||
MACTuple = (processedMAC,)
|
||||
|
||||
# MACDataFetching = MACDataReading.fetchall()
|
||||
# MACArray = list(MACDataFetching)
|
||||
|
||||
|
||||
# if MACTuple not in MACArray:
|
||||
# addingNode = "INSERT INTO `Node` (MAC) VALUES (%s)"
|
||||
# cursor.execute(addingNode, MACTuple)
|
||||
# mydb.commit()
|
||||
|
||||
pushingDataArray = [(1, "Temp", processedTemp), (1, "Humi", processedHumi), (1, "eCO2", processedeCO2), (1, "TVOC", processedTVOC)]
|
||||
pushingDataArray = [(nodeID, "Temp", processedTemp), (nodeID, "Humi", processedHumi), (nodeID, "eCO2", processedeCO2), (nodeID, "TVOC", processedTVOC)]
|
||||
for i in pushingDataArray:
|
||||
print(query ,i)
|
||||
cursor.execute(query, i)
|
||||
@@ -63,15 +49,19 @@ async def receive_data():
|
||||
else:
|
||||
type = 'enquete'
|
||||
|
||||
|
||||
|
||||
await getNodeInfo('sensor')
|
||||
await getNodeInfo('enquete')
|
||||
|
||||
print(str(sensorNodeArray))
|
||||
print(sensorNodeArray)
|
||||
|
||||
if str(macAdress) in sensorNodeArray:
|
||||
await processSensorNodeData(data)
|
||||
elif str(macAdress) in enqueteNodeArray:
|
||||
await processEnqueteNodeData(data)
|
||||
if macAdress in sensorNodeArray:
|
||||
nodeID = await getNodeID(macAdress)
|
||||
await processSensorNodeData(data, nodeID)
|
||||
elif macAdress in enqueteNodeArray:
|
||||
nodeID = await getNodeID(macAdress)
|
||||
await processEnqueteNodeData(data, nodeID)
|
||||
else:
|
||||
await newNode(macAdress, type)
|
||||
except websockets.ConnectionClosedError as e:
|
||||
@@ -100,6 +90,9 @@ def dbLogin():
|
||||
return mydb
|
||||
|
||||
async def getNodeInfo(type):
|
||||
global sensorNodeArray
|
||||
global enqueteNodeArray
|
||||
|
||||
nodeInfoArray = []
|
||||
|
||||
id = (type,)
|
||||
@@ -113,6 +106,9 @@ async def getNodeInfo(type):
|
||||
nodeInfoArray.append(item)
|
||||
print(nodeInfoArray)
|
||||
|
||||
cursor.close()
|
||||
mydb.close()
|
||||
|
||||
if type == 'sensor':
|
||||
sensorNodeArray = nodeInfoArray
|
||||
print(sensorNodeArray)
|
||||
@@ -123,6 +119,17 @@ async def getNodeInfo(type):
|
||||
return enqueteNodeArray
|
||||
|
||||
|
||||
async def getNodeID(macAdress):
|
||||
id = (macAdress,)
|
||||
mydb = dbLogin()
|
||||
cursor = mydb.cursor()
|
||||
cursor.execute("""SELECT nodeID FROM Node WHERE MAC = %s""", id)
|
||||
data = cursor.fetchall()
|
||||
|
||||
nodeID = data[0][0]
|
||||
|
||||
return nodeID
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user