Initial Commit
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
env/*
|
||||||
|
*.pyc
|
||||||
10
app.yaml
Normal file
10
app.yaml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
runtime: python
|
||||||
|
entrypoint: python mqtt_reader.py
|
||||||
|
env: flex
|
||||||
|
|
||||||
|
manual_scaling:
|
||||||
|
instances: 1
|
||||||
|
resources:
|
||||||
|
cpu: 1
|
||||||
|
memory_gb: 0.5
|
||||||
|
disk_size_gb: 10
|
||||||
32
firebase_interface.py
Normal file
32
firebase_interface.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
"""Sample of wrapper for meshify data to firebase."""
|
||||||
|
|
||||||
|
import time
|
||||||
|
from firebase import firebase
|
||||||
|
|
||||||
|
BASE_URL = 'https://pocloud-ff2c9.firebaseio.com'
|
||||||
|
fbdb = firebase.FirebaseApplication(BASE_URL, None)
|
||||||
|
|
||||||
|
def set_firebase_channel(deviceId, device_type, channel, value, timestamp):
|
||||||
|
"""Sets the value of a firebase channel."""
|
||||||
|
if timestamp == 0:
|
||||||
|
timestamp = time.time()
|
||||||
|
channel_value = {
|
||||||
|
'name': channel,
|
||||||
|
'value': value,
|
||||||
|
'timestamp': timestamp
|
||||||
|
}
|
||||||
|
|
||||||
|
channels_url = "/devices/{}/{}/channels/{}".format(deviceId, device_type, channel)
|
||||||
|
chan_result = fbdb.patch(channels_url, channel_value,
|
||||||
|
connection=None,
|
||||||
|
params={'print': 'pretty'},
|
||||||
|
headers={'X_FANCY_HEADER': 'VERY FANCY'})
|
||||||
|
print(chan_result)
|
||||||
|
|
||||||
|
|
||||||
|
history_url = "/devices/{}/{}/history/{}".format(deviceId, device_type, channel)
|
||||||
|
result = fbdb.post(history_url, channel_value,
|
||||||
|
connection=None,
|
||||||
|
params={'print': 'pretty'},
|
||||||
|
headers={'X_FANCY_HEADER': 'VERY FANCY'})
|
||||||
|
print(result)
|
||||||
37
mqtt_reader.py
Normal file
37
mqtt_reader.py
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import paho.mqtt.client as paho
|
||||||
|
from firebase_interface import set_firebase_channel
|
||||||
|
import json
|
||||||
|
|
||||||
|
# The callback for when the client receives a CONNACK response from the server.
|
||||||
|
def on_connect(client, userdata, flags, rc):
|
||||||
|
print("Connected with result code "+str(rc))
|
||||||
|
|
||||||
|
# Subscribing in on_connect() means that if we lose the connection and
|
||||||
|
# reconnect then subscriptions will be renewed.
|
||||||
|
client.subscribe("meshify/db/194/#")
|
||||||
|
|
||||||
|
# The callback for when a PUBLISH message is received from the server.
|
||||||
|
def on_message(client, userdata, msg):
|
||||||
|
print(msg.topic+" "+str(msg.payload))
|
||||||
|
payload = json.loads(msg.payload)
|
||||||
|
topic = msg.topic.split("/")
|
||||||
|
device_id = topic[3]
|
||||||
|
device_type = topic[4].split("_")[0]
|
||||||
|
channel_name = topic[5]
|
||||||
|
value = payload[0]['value']
|
||||||
|
timestamp = payload[0]['timestamp']
|
||||||
|
set_firebase_channel(device_id, device_type, channel_name, value, timestamp)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
client = paho.Client()
|
||||||
|
client.username_pw_set("admin", "columbus")
|
||||||
|
client.on_connect = on_connect
|
||||||
|
client.on_message = on_message
|
||||||
|
|
||||||
|
client.connect("mqtt.meshify.com", 1884, 120)
|
||||||
|
|
||||||
|
# Blocking call that processes network traffic, dispatches callbacks and
|
||||||
|
# handles reconnecting.
|
||||||
|
# Other loop*() functions are available that give a threaded interface and a
|
||||||
|
# manual interface.
|
||||||
|
client.loop_forever()
|
||||||
14
requirements.txt
Normal file
14
requirements.txt
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
cachetools==2.1.0
|
||||||
|
certifi==2018.4.16
|
||||||
|
chardet==3.0.4
|
||||||
|
idna==2.6
|
||||||
|
paho-mqtt==1.3.1
|
||||||
|
protobuf==3.5.2.post1
|
||||||
|
pyasn1==0.4.2
|
||||||
|
pyasn1-modules==0.2.1
|
||||||
|
python-firebase==1.2
|
||||||
|
pytz==2018.4
|
||||||
|
requests==2.18.4
|
||||||
|
rsa==3.4.2
|
||||||
|
six==1.11.0
|
||||||
|
urllib3==1.22
|
||||||
Reference in New Issue
Block a user