예제 #1
0
import os
import pprint
from mbed_cloud.connect import ConnectAPI

TOKEN = "YOUR_ACCESS_KEY"

# set up the Python SDK
config = {}
config['api_key'] = os.getenv('TOKEN') or TOKEN
config['host'] = 'https://api.us-east-1.mbedcloud.com'
api = ConnectAPI(config)
api.start_notifications()

devices = list(
    api.list_connected_devices(filters={'device_type': 'light-system'}))

print("Found %d lights" % (len(devices)), [c.id for c in devices])

for device in devices:

    def pir_callback(device_id, path, count):
        print("Motion detected at %s, new count is %s" % (device_id, count))

    api.add_resource_subscription_async(device.id, '/3201/0/5700',
                                        pir_callback)
    print("subscribed to resource")

    pink = 0xff69b4
    api.set_resource_value(device.id, '/3311/0/5706', pink)
    print("set color to pink")
# Override params with CLI inputs
if args.api_key_val:
    app.config["API_KEY"] = args.api_key_val

if args.host_val:
    app.config["API_HOST"] = args.host_val

WEBHOOK_URL = "%s/api/webhook" % app.config["API_BASE_URL"]
PRODUCT_ID_PATH = "/10341/0/26341"
PRODUCT_CURR_COUNT_PATH = "/10341/0/26342"

# Instatiate cloud connect
api_config = {"api_key": app.config["API_KEY"], "host": app.config["API_HOST"]}
connectApi = ConnectAPI(api_config)
# This should be required, why is it breaking things
connectApi.start_notifications()

logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

# Instantiate the database client on Table named 'example'
# Set the username and password to root
db = InfluxDBClient("influxdb", app.config["INFLUX_PORT"], 'root', 'root',
                    'example')

id_num_db = {}


def handleSubscribe(device_id, path, current_value):
    """On change in subscribed resource, dump data to InfluxDB."""
    now = datetime.utcnow()
    id_num = id_num_db[device_id]