mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
WIP Mqtt_to_Http : 3DPrinterMessages DONE !
This commit is contained in:
parent
06ce2e6f82
commit
3a5f526b49
@ -4,7 +4,9 @@ import signal
|
|||||||
import time
|
import time
|
||||||
import json
|
import json
|
||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
# Init variables
|
||||||
broker="localhost"
|
broker="localhost"
|
||||||
#username="oilkfgjy"
|
#username="oilkfgjy"
|
||||||
#password="lEyZb90q49Rf"
|
#password="lEyZb90q49Rf"
|
||||||
@ -14,35 +16,78 @@ API_ENDPOINT = 'http://192.168.0.17:3000/api/iot/0'
|
|||||||
|
|
||||||
delay = 5000
|
delay = 5000
|
||||||
|
|
||||||
mqttc = mqtt.Client('Mqtt_To_Http_Interceptor')
|
bufferJsonPrinter = []
|
||||||
#mqttc.username_pw_set(username, password)
|
headersVar = {}
|
||||||
|
|
||||||
mqttc.connect(broker, 1883)
|
def getToken():
|
||||||
|
r = requests.post(TOKEN_URL)
|
||||||
|
global headersVar
|
||||||
|
|
||||||
TOKEN_URL = 'http://192.168.0.17:3000/api/token?username=Thomas&password=MonsieurMagic'
|
if r.status_code == 200 :
|
||||||
|
|
||||||
r = requests.post(TOKEN_URL)
|
|
||||||
|
|
||||||
if r.status_code == 200 :
|
|
||||||
token = r.text
|
token = r.text
|
||||||
print('we have a token!')
|
print('we have a token!')
|
||||||
|
headersVar = {'Authorization': "Bearer " + token, 'Content-Type': "application/json", 'Accept': "application/json"}
|
||||||
hed = {'Authorization': "Bearer " + token +"f", 'Content-Type': "application/json"}
|
print(headersVar)
|
||||||
print(hed)
|
else :
|
||||||
|
print("Error in getToken function")
|
||||||
|
|
||||||
def on_message(mosq, obj, msg):
|
def on_message(mosq, obj, msg):
|
||||||
print(msg.topic)
|
print("Topic = " + msg.topic)
|
||||||
|
global bufferJsonPrinter
|
||||||
|
global headers
|
||||||
|
|
||||||
if msg.topic == '3DPrinterSensors' :
|
if msg.topic == '3DPrinterSensors' :
|
||||||
print(str(msg.payload, "utf-8"))
|
msgPayload = str(msg.payload, "utf-8")
|
||||||
print(API_ENDPOINT)
|
print("3DPrinterMessage = PayLoad : " + msgPayload)
|
||||||
|
|
||||||
# Add increment and test = if data length >= 10, push to DB via Http
|
# Add increment and test = if data length >= 10, push to DB via Http
|
||||||
# Add Verification on token.. if status code == 401 forbidden then get token and call again the service
|
# Add Verification on token.. if status code == 401 forbidden then get token and call again the service
|
||||||
|
validData = False;
|
||||||
|
|
||||||
jsonData = json.dumps([{'Time': '14/04/19', 'Temperature': 26.5, 'Pressure': 1004.3, 'Smoke': 100}])
|
try :
|
||||||
anotherR = requests.post(API_ENDPOINT,data=jsonData, headers=hed)
|
msgPayload = '{"Time":"'+ str(datetime.datetime.now()) + '",'+ msgPayload[1:]
|
||||||
print (anotherR.text)
|
bufferJsonPrinter.append(msgPayload)
|
||||||
print(anotherR.status_code)
|
#print("bufferJsonPrinter= " + bufferJsonPrinter["result"])
|
||||||
|
#item_dict = json.loads(bufferJsonPrinter)
|
||||||
|
#print("BufferJsonPrinter= " + json.dumps(bufferJsonPrinter))
|
||||||
|
print("3DPrinterMessage = NumberOfElement already collected : " + str(len(bufferJsonPrinter)))
|
||||||
|
validData = True;
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
print("3DPrinterMessage = Got an error in parsing message")
|
||||||
|
|
||||||
|
if validData and len(bufferJsonPrinter) == 10:
|
||||||
|
print("I'm in !")
|
||||||
|
# Request
|
||||||
|
#jsonData1 = json.dumps([{'Time': '14/04/19', 'Temperature': 26.5, 'Pressure': 1004.3, 'Smoke': 100},{'Time': '14/04/19', 'Temperature': 26.5, 'Pressure': 1004.3, 'Smoke': 100}])
|
||||||
|
|
||||||
|
try:
|
||||||
|
jsonData = bufferJsonPrinter
|
||||||
|
#print('jsonData1 : ' + jsonData1)
|
||||||
|
#print('jsonData : ' + str(jsonData).replace("'",""))
|
||||||
|
printerRequest = requests.post(API_ENDPOINT,data=str(jsonData).replace("'",""), headers=headersVar)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
print("3DPrinterMessage = error in post request")
|
||||||
|
|
||||||
|
|
||||||
|
while printerRequest.status_code == 401:
|
||||||
|
getToken()
|
||||||
|
time.sleep(20)
|
||||||
|
print("3DPrinterMessage = Forbidden error : Trying to send again the request with new token")
|
||||||
|
#jsonData = json.dumps([{'Time': '14/04/19', 'Temperature': 26.5, 'Pressure': 1004.3, 'Smoke': 100}])
|
||||||
|
printerRequest = requests.post(API_ENDPOINT,data=bufferJsonPrinter["result"], headers=headersVar)
|
||||||
|
|
||||||
|
if printerRequest.status_code == 404:
|
||||||
|
print("3DPrinterMessage = The server is unreachable (404)")
|
||||||
|
if printerRequest.status_code == 400:
|
||||||
|
print("3DPrinterMessage = Bad request (400)")
|
||||||
|
elif printerRequest.status_code == 200 or printerRequest.status_code == 201:
|
||||||
|
print("3DPrinterMessage = I sent the infos ! Everything is ok :) !")
|
||||||
|
bufferJsonPrinter = []
|
||||||
|
else:
|
||||||
|
print(str(printerRequest.status_code))
|
||||||
|
print("3DPrinterMessage = Unexpected Error : status code")
|
||||||
|
|
||||||
# Extracting data in json format
|
# Extracting data in json format
|
||||||
#data = r.json()
|
#data = r.json()
|
||||||
@ -66,6 +111,16 @@ def on_message(mosq, obj, msg):
|
|||||||
else:
|
else:
|
||||||
print("Unknown topic")
|
print("Unknown topic")
|
||||||
|
|
||||||
|
########## MAIN CODE ##########
|
||||||
|
|
||||||
|
mqttc = mqtt.Client('Mqtt_To_Http_Interceptor')
|
||||||
|
#mqttc.username_pw_set(username, password)
|
||||||
|
|
||||||
|
mqttc.connect(broker, 1883)
|
||||||
|
|
||||||
|
TOKEN_URL = 'http://192.168.0.17:3000/api/token?username=Thomas&password=MonsieurMagic'
|
||||||
|
|
||||||
|
getToken()
|
||||||
|
|
||||||
mqttc.on_message=on_message
|
mqttc.on_message=on_message
|
||||||
#####
|
#####
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user