Created
November 30, 2018 09:43
-
-
Save SLAAMER/02a22fb0be5b2a290eea526f1141b589 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mqtt import MQTT | |
import requests | |
import time | |
import json | |
import re | |
import uuid | |
import os | |
rest = 5 | |
# /dsd/dispenser/:id/dispense/:productId | |
urlDispenser = "http://dsd-api.herokuapp.com/api/v1/dispenser/" | |
dispenserId = 4 | |
inventory = "" | |
topic = "/" | |
try: | |
macString = ':'.join(re.findall('..', '%012x' % uuid.getnode())).upper() | |
payload = dict(mac=macString) | |
# consultar mi existencia | |
response = requests.post(urlDispenser+"auto", data=payload) | |
if(response.status_code == 200): | |
print("Response from " + urlDispenser) | |
dispenserId = response.json()['dispenser']['id'] | |
print(dispenserId) | |
# Listen to my topic to dispense | |
topic = "/dsd/dispenser/" + str(dispenserId) | |
master = MQTT() | |
while True: | |
# consultar mi inventario | |
urlInventory = urlDispenser + str(dispenserId) + "/item/" | |
response = requests.get(urlInventory) | |
if(response.status_code == 200): | |
print("Response from " + urlInventory) | |
inventory = response.json()['items'] | |
master.publish(topic, str(inventory)) | |
else: | |
print('Connection lost with the dispenser') | |
time.sleep(rest) | |
except Exception as e: | |
print("An ERROR ocurred: "+str(e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment