def __init__(self, credentials): self.systemKey = credentials['systemKey'] self.systemSecret = credentials['systemSecret'] self.platformURL = credentials['platformURL'] self.gatewayAddress = self.GetMacAddress() #Connect to MQTT cbSystem=System(self.systemKey, self.systemSecret, self.platformURL) #authenticate this adapter with the edge sleep_time = 1 cbAuth=cbSystem.User(credentials['username'], credentials['password']) while not cbAuth.checkAuth(): print("trying to authenticate again by sleeping for %d", sleep_time) time.sleep(sleep_time) sleep_time = sleep_time << 1 cbAuth = cbSystem.User(credentials['username'], credentials['password']) if sleep_time > 60: break self.gatewayName = "thunderboard" self.client = cbSystem.Messaging(cbAuth) self.client.connect() # the on_connect is not working self.client.on_message = self.CommandCallback
def __init__(self, credentials): self.systemKey = credentials['systemKey'] self.systemSecret = credentials['systemSecret'] self.username = credentials['username'] self.password = credentials['password'] self.platformURL = credentials['platformURL'] self.gatewayAddress = self.GetMacAddress() #Connect to MQTT cbSystem = System(self.systemKey, self.systemSecret, self.platformURL) # Device Auth if 'active_key' in credentials: self.gatewayName = credentials["name"] self.active_key = credentials['active_key'] cbAuth = cbSystem.Device(self.gatewayName, credentials['active_key']) else: self.gatewayName = self.gatewayAddress cbAuth = cbSystem.User(credentials['username'], credentials['password']) # right now override the GatewayName so that portal demos work easier self.gatewayName = "thunderboard" self.client = cbSystem.Messaging(cbAuth) self.client.connect() # the on_connect is not working self.client.on_message = self.CommandCallback
def main(): logging.basicConfig(filename='clearblade_challenge.log', encoding='utf-8', level=logging.DEBUG) #send message to ClearBlade platform SystemKey = 'c4dfbc880ce891a09fe8eb92eb9d01' SystemSecret = 'C4DFBC880CC0D8A8B5FCE5B4DB44' admin_email = '*****@*****.**' admin_pw = 'H1r3m3pls' mySystem = System(SystemKey, SystemSecret) admin = mySystem.User(admin_email, admin_pw) mqtt = mySystem.Messaging(admin) sys_overview, bles = create_msgs() mqtt.connect() for ble in bles: print(json.dumps(ble)) # debug print statement mqtt.publish('ble/_platform', json.dumps(ble)) sleep(1) mqtt.publish('sysinfo', json.dumps(sys_overview)) mqtt.disconnect()
def initialize_clearblade(self): cbLogs.info("AdapterLibrary - initialize_clearblade - initializing with ClearBlade") self._cb_system = System(self._args[self.SYSTEM_KEY_ARG_KEY], self._args[self.SYSTEM_SECRET_ARG_KEY], self._args[self.PLATFORM_URL_ARG_KEY]) if self.SERVICE_ACCOUNT_ARG_KEY in self._args: self.__auth_with_service_account() else: self.__auth_with_device() return self.__fetch_adapter_config()
class ClearBladeIot(IotProvider): """ Child class containing implementations of IotProvider specific to CleaBlade. """ def __init__(self, iot_provider_cfg): # 1. Load path to ClearBlade-specific module from config and add to path. # 2. Import ClearBlade-specific module. # 3. Load system_key and system_secret from config. # 4. Create "System" object. # 5. Call parent class' __init__ sys.path.append(iot_provider_cfg["module_dir"]) from clearblade.ClearBladeCore import System as ClearBladeSystem self.ClearBladeSystem = ClearBladeSystem( iot_provider_cfg["system_key"], iot_provider_cfg["system_secret"]) super().__init__(iot_provider_cfg) def on_connect(self, client, userdata, flags, rc): # Event handler for connection event. Subscribe to topic(s) here. client.subscribe(self.subscribe_topic) print(f"subscribed to {self.subscribe_topic}") def onmsg(self, client, userdata, msg): # Wraps core event handler for incoming messages msg_payload = msg.payload super().onmsg(msg_payload) def connect(self): # A connection to iot is established at the beginning and if publish fails. # 1. Create AnonUser. # 2. Create Messaging object with AnonUser as param. # 3. Pass on_connect function. # 4. Pass onmsg function. # 5. Call Messaging object's "connect" method. self.clearblade_iot_user = self.ClearBladeSystem.AnonUser() self.clearblade_iot_comm = self.ClearBladeSystem.Messaging( self.clearblade_iot_user, client_id="xqtive") self.clearblade_iot_comm.on_connect = self.on_connect self.clearblade_iot_comm.on_message = self.onmsg self.clearblade_iot_comm.connect() def publish(self, publish_topic, msg_str, qos): self.clearblade_iot_comm.publish(publish_topic, msg_str, qos=qos)
def __init__(self, iot_provider_cfg): # 1. Load path to ClearBlade-specific module from config and add to path. # 2. Import ClearBlade-specific module. # 3. Load system_key and system_secret from config. # 4. Create "System" object. # 5. Call parent class' __init__ sys.path.append(iot_provider_cfg["module_dir"]) from clearblade.ClearBladeCore import System as ClearBladeSystem self.ClearBladeSystem = ClearBladeSystem( iot_provider_cfg["system_key"], iot_provider_cfg["system_secret"]) super().__init__(iot_provider_cfg)
from clearblade.ClearBladeCore import System, Query, Developer, registerDev import urllib.request, json, time ## New system with ClearBlade key and secret systemKey = "SYSTEMKEY_HERE" systemSecret = "SYSTEMSECRET_HERE" mySystem = System(systemKey, systemSecret) ## New device with Clearblade active key and name deviceName = "DEVICENAME_HERE" deviceActiveKey = "ACTIVEKEY_HERE" macDevice = mySystem.Device(deviceName, deviceActiveKey) token = macDevice.token ## Using device to access messaging client mqtt = mySystem.Messaging(macDevice, port=1883) subscribeTopic = "rohith-mac/1/requests" def on_connect(client, userdata, flags, rc): global subscribeTopic client.subscribe(subscribeTopic) incomingRequest = None def on_message(client, userdata, msg): global incomingRequest incomingRequest = (msg.payload).decode('utf-8') mqtt.on_connect = on_connect mqtt.on_message = on_message
from clearblade.ClearBladeCore import System import time #System credentials SystemKey = "b4bab3e70baaaa8cae8397bff6be01" SystemSecret = "9CBFB3E70BC8D0D1F0C4EE8DA79501" mySystem = System(SystemKey,SystemSecret) #User credentials email = "*****@*****.**" password = "******" #Authenticating as username username = mySystem.User(email,password) #use username to message mqtt = mySystem.Messaging(username) def on_connect(client, userdata, flags, rc): # When we connect to the broker, subscribe to the analytics topic client.subscribe("analytics") def on_message(client, userdata, message): # When we receive a message, print it out print("Received message '" + message.payload.decode("utf-8") + "' on topic '" + message.topic + "'") # Connect callbacks to client mqtt.on_connect = on_connect mqtt.on_message = on_message mqtt.connect() while(True): time.sleep(1) # wait for messages
import base64 with open("train_params.json", 'r') as fp: train_params = json.load(fp) key = train_params["systemKey"] secret = train_params["systemSecret"] url = train_params["url"] collection = train_params["featureCol"] user = train_params["email"] pswd = train_params["password"] SystemKey = key SystemSecret = secret mySystem = System(SystemKey, SystemSecret, url=url) user = mySystem.User(user, pswd) myCol = mySystem.Collection(user, collectionName=collection) rows = myCol.getItems() featureDataset = collection + ".json" with open(featureDataset, 'w') as fp: json.dump(rows, fp, indent=2) myCol1 = mySystem.Collection(user, collectionName="TrainingFiles") rows = myCol1.getItems() os.system("mkdir myCode")
# In fulfillment to the coding challenge sent by ClearBlade (Yash Jain). # By Sahil Mehta # Email: [email protected] from clearblade.ClearBladeCore import System import random import time import psutil import os # System credentials SystemKey = os.environ.get("SYSTEM_KEY") SystemSecret = os.environ.get("SYSTEM_SECRET") mySystem = System(SystemKey, SystemSecret) # Log in as Sahil sahil = mySystem.User("*****@*****.**", "sahilmehta") # Use Sahil to access a messaging client mqtt = mySystem.Messaging(sahil) # Set up callback function def on_connect(client, userdata, flags, rc): # When we connect to the broker, start publishing our data to the Test Topic channel payload = "CPU Utilization : " + str( psutil.cpu_percent()) + ", Available Virtual Memory: " + str( psutil.virtual_memory()[2])
fmt='%(asctime)s %(levelname)-8s %(message)s', datefmt='%m-%d-%Y %H:%M:%S %p') handler = logging.StreamHandler(stream=sys.stdout) handler.setFormatter(formatter) logger = logging.getLogger(name) logging.basicConfig(level=os.environ.get("LOGLEVEL", LOGLEVEL)) logger.addHandler(handler) return logger #Main Loop if __name__ == '__main__': logger = setup_custom_logger('BLE Adapter') scanner = ScanDelegate() exitapp = False cbSystem = System(SystemKey, SystemSecret, SystemURL) cbAuth = cbSystem.User(cbUser, cbPass) mqtt = cbSystem.Messaging(cbAuth) #mqtt.on_connect = on_connect #mqtt.on_message = on_message mqtt.connect() #Connect to the msg broker while not exitapp: logging.info("Scan Period: %s seconds", scanTimePeriod) devices = scanner.scanProcess() try: processDeviceList(devices) #logging.info('Sleeping for %s', waitTimeBetweenScans) #time.sleep(waitTimeBetweenScans) except KeyboardInterrupt: exitapp = True mqtt.disconnect()
matter if a few packets arre lost. ''' from clearblade.ClearBladeCore import System, Query, Developer import time import json import threading import psutil with open('../config/config.json', 'r') as f: config = json.load(f) print(config) #Configure SystemKey = config["SystemKey"] SystemSecret = config["SystemSecret"] mySystem = System(SystemKey, SystemSecret) email = config["email"] password = config["password"] akshat = mySystem.User(email, password) #Establish MQTT mqtt = mySystem.Messaging(akshat) mqtt.connect() #Send data every 3 second while (True): try: payload = { "ctx_switches": psutil.cpu_stats().ctx_switches, "processor_count": psutil.cpu_count(), # count keeps changing on linux and windows.
data = {"cpu_info": cpu_info, "vmemory_info": memory_usage} # System credentials ## Change: to increase the security we will get the keys from the env variables systemKey = os.environ.get("SystemKey", None) # None check for system key if systemKey == None: print("Please add System Key") sys.exit(1) systemSecret = os.environ.get("SystemSecret", None) if systemSecret == None: print("Please add System Secret") sys.exit(1) mySystem = System(systemKey, systemSecret) ## Change: geeting the password from environment password = os.environ.get("clearblabe_password", None) if password == None: print("Please add PASSWORD") sys.exit(1) print(password) # Log in as Preet preet = mySystem.User("*****@*****.**", password) # Accessing the messaging client mqtt = mySystem.Messaging(preet) # Connecting...
if __name__ == '__main__': CB_CONFIG = parse_args(sys.argv) LOGGER = setup_custom_logger(ADAPTER_NAME) logging.debug(json.dumps(CB_CONFIG)) if not CB_CONFIG['logCB']: logging.debug("Setting cbLogs.DEBUG to False") cbLogs.DEBUG = False if not CB_CONFIG['logMQTT']: logging.debug("Setting cbLogs.MQTT_DEBUG to False") cbLogs.MQTT_DEBUG = False EXITAPP = False CB_SYSTEM = System(CB_CONFIG['systemKey'], CB_CONFIG['systemSecret'], CB_CONFIG['platformURL'] + ":" + CB_CONFIG['httpPort']) logging.info("Authenticating") CBAUTH = CB_SYSTEM.User(CB_CONFIG['mqttUserID'], CB_CONFIG['mqttPassword']) logging.info("Auth Complete") #Connect to the message broker logging.info("Initializing the ClearBlade message broker") CB_MQTT = CB_SYSTEM.Messaging(CBAUTH) CB_MQTT.on_connect = on_connect CB_MQTT.on_message = on_adapterRequest CB_MQTT.on_disconnect = on_disconnect logging.info("Connecting to the ClearBlade message broker") CB_MQTT.connect() while not SCOPE_VARS['EXIT_APP']: try: pass
from clearblade.Messaging import Messaging from paho.mqtt.client import Client, MQTT_ERR_QUEUE_SIZE from clearblade.ClearBladeCore import System, Query, Developer SystemKey = "d4bfc0890cc2f4d0e6fc8189b722" SystemSecret = "D4BFC0890CC49DA6D69EA590D8D701" mySystem = System(SystemKey, SystemSecret) email = "*****@*****.**" password = "******" sidd = mySystem.User(email, password) sidd = mySystem.Service("PCSystem") params = {"Bluetooth": "Jabra"} mqtt = mySystem.Messaging(sidd) def on_connect(client, userdata, flags, rc): # When we connect to the broker, subscribe to the southernplayalisticadillacmuzik topic client.subscribe("PCSystems") mqtt.on_connect = on_connect mqtt.connect() sidd2 = Developer(email, password) sidd.execute(sidd2, params)
''' author @yash clearBlade MQTT Message Communicator March 9th, 2020 ''' # IMPORTS import psutil from clearblade.ClearBladeCore import System, Query, Developer import time # Setup system I created on the web console using credentials SystemKey = "8eedb6e60b8892f4e0e5d6fe8111" SystemSecret = "8EEDB6E60BFCD5B4E8A6F5E7E264" mySystem = System(SystemKey, SystemSecret) # Setup User yash = mySystem.User("*****@*****.**", "D00pamine!!") # Start messaging system mqtt = mySystem.Messaging(yash) # Connect mqtt.connect() # Test message for i in range(20): mqtt.publish("/test", psutil.cpu_percent()) mqtt.disconnect()
url = "http://localhost:9000" collection = "ModelArchitecture" email = sys.argv[4] token = sys.argv[5] print("System Key: ", SystemKey) print("System Secret: ", SystemSecret) print("URL: ", url) print("email: ", email) print("token: ", token) #user = "******" #pswd = "password" mySystem = System(SystemKey, SystemSecret, url, safe=False) user = mySystem.ServiceUser(email, token) print("Hello User") mqtt = mySystem.Messaging(user) def decode_and_save(): myCol = mySystem.Collection(user, collectionName=collection) rows = myCol.getItems() model = rows[-1] df = pd.DataFrame(data=model, index=[0]) df = df.drop(['item_id'], axis=1)
def get_data(): try: with open("train_params.json", 'r') as fp: train_params = json.load(fp) key = train_params["systemKey"] secret = train_params["systemSecret"] url = train_params["url"] collection = train_params["featureCol"] user = train_params["email"] #pswd = train_params["password"] token = train_params["usertoken"] logging.debug("Training Parameters fetched") SystemKey = key SystemSecret = secret mySystem = System(SystemKey, SystemSecret, url=url) #user = mySystem.User(user, pswd) user = mySystem.ServiceUser(user, token) myCol = mySystem.Collection(user, collectionName=collection) rows = myCol.getItems(pagesize=1000) featureDataset = collection + ".json" logging.debug("Feature Dataset fetched for CB Collections") with open(featureDataset, 'w') as fp: json.dump(rows, fp, indent=2) myCol1 = mySystem.Collection(user, collectionName="TrainingFiles") rows = myCol1.getItems() logging.debug("Model files fetched for CB Collections") os.system("mkdir myCode") files = rows[-1] archfile = train_params["archFile"] datafile = train_params["dataFile"] trainfile = train_params["taskFile"] with open("__init__.py", 'w') as init: init.close() with open(archfile, 'w') as af: decoded = base64.b64decode(files["archfile"]) af.write(decoded.decode('ascii')) with open(datafile, 'w') as df: decoded = base64.b64decode(files["datafile"]) df.write(decoded.decode('ascii')) with open(trainfile, 'w') as tf: decoded = base64.b64decode(files["trainfile"]) tf.write(decoded.decode('ascii')) os.system("mv __init__.py myCode/") os.system("mv " + archfile + " myCode/") os.system("mv " + datafile + " myCode/") os.system("mv " + trainfile + " myCode/") except Exception as e: logging.error(e)
import cv2 import PIL import scipy import _pickle as cpickle from struct import * import time url = "http://*****:*****@clearblade.com", "ashokverma") # mySystem. sanket = mySystem.User("*****@*****.**", "password") print("\n HI\n") # Use Sanket to access a messaging client mqtt = mySystem.Messaging(sanket) # result # options = {"model": "cfg/yolo.cfg", "load": "bin/yolov2-tiny.weights", "threshold": 0.1} # tfnet = TFNet(options)
from clearblade.ClearBladeCore import System, Query, Developer import psutil import time import keyboard import matplotlib.pyplot as plt import numpy as np SystemKey = "" #Enter your system key SystemSecret = "" #Enter your system secret url = "https://platform.clearblade.com" mySystem = System(SystemKey, SystemSecret, url, safe=False) answer = input("Do you have an account registered in this system? Y/n\n") answer = answer.lower() while (answer != "y" or answer != "n"): if (answer == "y"): enterEmail = input("enter your email\n") enterPass = input("enter your password\n") break elif (answer == "n"): anon = mySystem.AnonUser() enterEmail = input("enter your email\n") enterPass = input("enter your password\n") newUser = mySystem.registerUser(anon, enterEmail, enterPass) break else: answer = input("wrong answer, input Y/n again.\n") email = enterEmail password = enterPass
def __init__(self, key, secret): self.system = System(key, secret)
################################ ## Adapter time ## ################################ # Set up ZMQ publisher connection pub_context = zmq.Context() zmq_pub_sock = pub_context.socket(zmq.PUB) zmq_pub_sock.bind(zmq_bind_address) # Set up ZMQ subscriber connection sub_context = zmq.Context() zmq_sub_sock = sub_context.socket(zmq.SUB) # Set up MQTT connection zmq_system = System(system_key, system_secret, cb_url) zmq_user = zmq_system.User(email, password) mqtt = zmq_system.Messaging(zmq_user, client_id="zmq_tester") mqtt_connected = False # When we connect, subscribe to all topics in our subscriptions array def on_connect(client, userdata, flags, rc): global mqtt_connected mqtt_connected = True for topic in mqtt_incoming_subscriptions: client.subscribe(topic) # When we receive a message, forward it via ZMQ def on_message(client, userdata, message):
from clearblade.ClearBladeCore import System import time import random # System credentials SystemKey = "9abbd2970baabf8aa6d2a9abcc47" SystemSecret = "9ABBD2970BA6AABFE6E8AEB8B14F" mySystem = System(SystemKey, SystemSecret) # Log in as Riswan Riswan = mySystem.User("*****@*****.**", "Ghana2020") # Use Riswan to access a messaging client mqtt = mySystem.Messaging(Riswan) # Set up callback functions def on_connect(client, userdata, flags, rc): # When we connect to the broker, subscribe to the CPU Usage topic client.subscribe("ComputerCPU-USAGE") def on_message(client, userdata, message): # When we receive a message, print it out and store it in a collection of dictionary print("Received message '" + message.payload + "' on topic '" + message.topic + "'") # Connect callbacks to client mqtt.on_connect = on_connect mqtt.on_message = on_message
#user = test_params["email"] #pswd = test_params["password"] #url = test_params["url"] # SystemKey = "dacea5d20ba89d95a8a49fc0a89601" # SystemSecret = "DACEA5D20BB6A093F38AF1ACD08701" #url = "https://staging.clearblade.com" url = "http://localhost:9000" SystemKey = "a6c2e6d10bc2f183fca3c7d3d0fe01" SystemSecret = "A6C2E6D10BCADB819FABF8FCD94E" collection = "ModelArchitecture" user = "******" pswd = "password" mySystem = System(SystemKey, SystemSecret, url, safe=False) user = mySystem.User(user, pswd) print("Hello User") mqtt = mySystem.Messaging(user) def decode_and_save(): myCol = mySystem.Collection(user, collectionName=collection) rows = myCol.getItems() model = rows[-1] df = pd.DataFrame(data=model, index=[0]) df = df.drop(['item_id'], axis=1)
import psutil, json, time from datetime import datetime from clearblade.ClearBladeCore import System def get_pc_state(): results = {} results["time"] = datetime.utcnow().isoformat() results["cpu_usage"] = psutil.cpu_percent(10) return json.dumps(results) # System credentials SYSTEM_KEY = "<your_system_key>" SYSTEM_SECRET = "<your_system_secrete>" pc_state_system = System(SYSTEM_KEY, SYSTEM_SECRET) adam = pc_state_system.User("<your_email>", "<your_password>") mqtt = pc_state_system.Messaging(adam) while True: pc_state = get_pc_state() mqtt.connect() mqtt.publish("pc_state", pc_state) mqtt.disconnect() time.sleep(900)
e.g: 1253656 => '1.20MB' 1253656678 => '1.17GB' """ factor = 1024 for unit in ["", "K", "M", "G", "T", "P"]: if bytes < factor: return f"{bytes:.2f}{unit}{suffix}" bytes /= factor # System credentials SystemKey = "ce8dd0e50b8acd82fc969396be3f" SystemSecret = "CE8DD0E50BE0B183A7EDC89EA39001" mySystem = System(SystemKey, SystemSecret) # Log in as Sanket boren = mySystem.User("*****@*****.**", "19990219Zbr#") # Use Boren to access a messaging client mqtt = mySystem.Messaging(boren) #Connect mqtt.connect() #When i connect to the broker, start publishing sytem information to the update topic in a static 1s interval while (True): #Grab memory Information svmem = psutil.virtual_memory()
class AdapterLibrary: DEFAULT_LOG_LEVEL = "info" DEFAULT_PLATFORM_URL = "http://localhost:9000" DEFAULT_MESSAGING_URL = "localhost:1883" DEFAULT_ADAPTER_CONFIG_COLLECTION_NAME = "adapter_config" SYSTEM_KEY_ARG_KEY = "CB_SYSTEM_KEY" SYSTEM_SECRET_ARG_KEY = "CB_SYSTEM_SECRET" DEVICE_NAME_ARG_KEY = "device_name" ACTIVE_KEY_ARG_KEY = "active_key" SERVICE_ACCOUNT_ARG_KEY = "CB_SERVICE_ACCOUNT" SERVICE_ACCOUNT_TOKEN_ARG_KEY = "CB_SERVICE_ACCOUNT_TOKEN" PLATFORM_URL_ARG_KEY = "platform_URL" MESSAGING_URL_ARG_KEY = "messaging_URL" ADAPTER_CONFIG_COLLECTION_NAME_ARG_KEY = "adapter_config_collection_name" LOG_LEVEL_ARG_KEY = "log_level" def __init__(self, adapter_name): cbLogs.info("Initializing AdapterLibrary with adapter name: " + adapter_name) self.adapter_name = adapter_name self._args = {} self._cb_system = None self._device_client = None self._sub_topic = None self._cb_message_handler = None def parse_arguments(self): cbLogs.info("AdapterLibrary - parse_arguments - parsing environment variables and flags") self.__parse_env_variables() self.__parse_flags() self._args[self.LOG_LEVEL_ARG_KEY] = string.upper(self._args[self.LOG_LEVEL_ARG_KEY]) logging.basicConfig(level=self._args[self.LOG_LEVEL_ARG_KEY]) if self._args[self.LOG_LEVEL_ARG_KEY] != "DEBUG": cbLogs.DEBUG = False cbLogs.MQTT_DEBUG = False cbLogs.info("AdapterLibrary - parse_arguments - parsed adapter arguments: " + str(self._args)) cbLogs.info("AdapterLibrary - parse_arguments - Verifying required adapter arguments") if self.SYSTEM_KEY_ARG_KEY not in self._args: cbLogs.error("System Key is required, can be supplied with --systemKey flag or " + self.SYSTEM_KEY_ARG_KEY + " environment variable") exit(-1) if self.SYSTEM_SECRET_ARG_KEY not in self._args: cbLogs.error("System Secret is required, can be supplied with --systemSecret flag or " + self.SYSTEM_SECRET_ARG_KEY + " environment variable") exit(-1) if self.ACTIVE_KEY_ARG_KEY not in self._args and self.SERVICE_ACCOUNT_ARG_KEY not in self._args: cbLogs.error("Device Password is required when not using a Service Account, can be supplied with --password flag") exit(-1) if self.SERVICE_ACCOUNT_ARG_KEY in self._args and self.SERVICE_ACCOUNT_TOKEN_ARG_KEY not in self._args: cbLogs.error("Service Account Token is required when a Service Account is specified, this should have automatically been supplied. Check for typos then try again") exit(-1) cbLogs.info("AdapterLibrary - parse_arguments - Adapter arguments parsed and verified!") def initialize_clearblade(self): cbLogs.info("AdapterLibrary - initialize_clearblade - initializing with ClearBlade") self._cb_system = System(self._args[self.SYSTEM_KEY_ARG_KEY], self._args[self.SYSTEM_SECRET_ARG_KEY], self._args[self.PLATFORM_URL_ARG_KEY]) if self.SERVICE_ACCOUNT_ARG_KEY in self._args: self.__auth_with_service_account() else: self.__auth_with_device() return self.__fetch_adapter_config() def connect_MQTT(self, topic="", cb_message_handler=None): cbLogs.info("AdapterLibrary - connect_MQTT - Initializing the ClearBlade MQTT message broker") self._cb_message_handler = cb_message_handler self._cb_mqtt = self._cb_system.Messaging(self._device_client, client_id=self.adapter_name + "-" + str(random.randint(0, 10000))) self._cb_mqtt.on_connect = self.__on_MQTT_connect self._cb_mqtt.on_disconnect = self.__on_MQTT_disconnect if topic != "": self._cb_mqtt.on_subscribe = self.__on_MQTT_subscribe self._cb_mqtt.on_message = self.__on_MQTT_message_received self._sub_topic = topic self._cb_mqtt.connect() def publish(self, topic, message): cbLogs.info("AdapterLibrary - publish - Publishing MQTT message on topic " + topic) self._cb_mqtt.publish(topic, message) def disconnect_MQTT(self): cbLogs.info("AdapterLibrary - disconnect_MQTT - Disconnecting from ClearBlade MQTT message broker") self._cb_mqtt.disconnect() def __auth_with_service_account(self): cbLogs.info("AdapterLibrary - __auth_with_service_account - Authenticating as service account") self._device_client = self._cb_system.Device(self._args[self.SERVICE_ACCOUNT_ARG_KEY], authToken=self._args[self.SERVICE_ACCOUNT_TOKEN_ARG_KEY]) def __auth_with_device(self): cbLogs.info("AdapterLibrary - __auth_with_device - Authenticating as device") self._device_client = self._cb_system.Device(self._args[self.DEVICE_NAME_ARG_KEY], self._args[self.ACTIVE_KEY_ARG_KEY]) def __fetch_adapter_config(self): cbLogs.info("AdapterLibrary - __fetch_adapter_config - Retrieving adapter config") adapter_config = {"topic_root": self.adapter_name, "adapter_settings": ""} collection = self._cb_system.Collection(self._device_client, collectionName=self._args[self.ADAPTER_CONFIG_COLLECTION_NAME_ARG_KEY]) query = Query() query.equalTo("adapter_name", self.adapter_name) rows = collection.getItems(query) if len(rows) == 1: if rows[0]["topic_root"] != "": adapter_config["topic_root"] = str(rows[0]["topic_root"]) if rows[0]["adapter_settings"] != "": raw_json = json.loads(str(rows[0]["adapter_settings"])) adapter_config["adapter_settings"] = self.__byteify(raw_json) else: cbLogs.warn("No adapter config found for adapter name " + self.adapter_name + ". Using defaults") cbLogs.info("AdapterLibrary - __fetch_adapter_config - Using adapter config: " + str(adapter_config)) return adapter_config def __on_MQTT_connect(self, client, userdata, flags, rc): cbLogs.info("AdapterLibrary - __on_MQTT_connect - MQTT successfully connected!") if self._sub_topic != None: self._cb_mqtt.subscribe(self._sub_topic) def __on_MQTT_disconnect(self, client, userdata, rc): cbLogs.info("AdapterLibrary - __on_MQTT_disconnect - MQTT disconnected with rc " + str(rc)) if self._sub_topic != None and rc == 1: cbLogs.warn("AdapterLibrary - __on_MQTT_disconnect - Verify that your service account has permission to subscribe to the topic: " + self._sub_topic) def __on_MQTT_subscribe(self, client, userdata, mid, granted_qos): cbLogs.info("AdapterLibrary - __on_MQTT_subscribe - MQTT successfully subcribed to topic " + self._sub_topic) def __on_MQTT_message_received(self, client, userdata, message): cbLogs.info("AdapterLibrary - __on_MQTT_message_received - MQTT message received on topic " + message.topic) if self._cb_message_handler != None: cbLogs.info("calling message handler") self._cb_message_handler(message) def __parse_env_variables(self): """Parse environment variables""" env = os.environ possible_vars = [self.SYSTEM_KEY_ARG_KEY, self.SYSTEM_SECRET_ARG_KEY, self.SERVICE_ACCOUNT_ARG_KEY, self.SERVICE_ACCOUNT_TOKEN_ARG_KEY] for var in possible_vars: if var in env: cbLogs.info("Setting adapter arguments from environment variable: " + var + ": " + str(env[var])) self._args[var] = env[var] def __parse_flags(self): """Parse the command line arguments""" parser = argparse.ArgumentParser(description='ClearBlade Adapter') parser.add_argument('--systemKey', dest=self.SYSTEM_KEY_ARG_KEY, help='The System Key of the ClearBlade \ Plaform "System" the adapter will connect to.') parser.add_argument('--systemSecret', dest=self.SYSTEM_SECRET_ARG_KEY, help='The System Secret of the \ ClearBlade Plaform "System" the adapter will connect to.') parser.add_argument('--deviceName', dest=self.DEVICE_NAME_ARG_KEY, default=self.adapter_name, help='The name of the device that will be used for device \ authentication against the ClearBlade Platform or Edge, defined \ within the devices table of the ClearBlade platform. The default is ' + self.adapter_name) parser.add_argument('--password', dest=self.ACTIVE_KEY_ARG_KEY, help='The password (active key) of the device that will be used for device \ authentication against the ClearBlade Platform or Edge, defined within \ the devices table of the ClearBlade platform.') parser.add_argument('--platformURL', dest=self.PLATFORM_URL_ARG_KEY, default=self.DEFAULT_PLATFORM_URL, \ help='The HTTP URL of the ClearBlade Platform or Edge the adapter will \ connect to (including port if non-standard). The default is ' + self.DEFAULT_PLATFORM_URL) parser.add_argument('--messagingURL', dest=self.MESSAGING_URL_ARG_KEY, default=self.DEFAULT_MESSAGING_URL, \ help='The MQTT URL of the ClearBlade Platform or Edge the adapter will \ connect to (including port if non-standard). The default is ' + self.DEFAULT_MESSAGING_URL) parser.add_argument('--adapterConfigCollection', dest=self.ADAPTER_CONFIG_COLLECTION_NAME_ARG_KEY, \ default=self.DEFAULT_ADAPTER_CONFIG_COLLECTION_NAME, \ help='The name of the ClearBlade Platform data collection which contains \ runtime configuration settings for the adapter. The default is ' + self.DEFAULT_ADAPTER_CONFIG_COLLECTION_NAME) parser.add_argument('--logLevel', dest=self.LOG_LEVEL_ARG_KEY, default=self.DEFAULT_LOG_LEVEL, choices=['fatal', 'error', \ 'warn', 'info', 'debug'], help='The level of logging that \ should be utilized by the adapter. The default is ' + self.DEFAULT_LOG_LEVEL) args = vars(parser.parse_args(args=sys.argv[1:])) for var in args: if args[var] != "" and args[var] != None: cbLogs.info("Setting adapter arguments from command line argument: " + var + ": " + str(args[var])) self._args[var] = args[var] def __byteify(self, input): cbLogs.info("in byteify") # helper function for python 2.7 to convert unicode to strings in a dict created with json.loads # https://stackoverflow.com/a/13105359 if isinstance(input, dict): return {self.__byteify(key): self.__byteify(value) for key, value in input.iteritems()} elif isinstance(input, list): return [self.__byteify(element) for element in input] elif isinstance(input, unicode): return input.encode('utf-8') else: return input
LOGGER = setup_custom_logger(ADAPTER_NAME) if not CB_CONFIG['logCB']: logging.debug("Setting cbLogs.DEBUG to False") cbLogs.DEBUG = False if not CB_CONFIG['logMQTT']: logging.debug("Setting cbLogs.MQTT_DEBUG to False") cbLogs.MQTT_DEBUG = False logging.info("Intializing ClearBlade device client") logging.debug("System Key = %s", CB_CONFIG['systemKey']) logging.debug("System Secret = %s", CB_CONFIG['systemSecret']) logging.debug("HTTP URL = %s", CB_CONFIG['httpURL'] + ":" + CB_CONFIG['httpPort']) CB_SYSTEM = System(CB_CONFIG['systemKey'], CB_CONFIG['systemSecret'], CB_CONFIG['httpURL'] + \ ":" + CB_CONFIG['httpPort']) logging.info("Authenticating to ClearBlade") logging.debug("Device ID = %s", CB_CONFIG['deviceID']) logging.debug("Device Active Key = %s", CB_CONFIG['activeKey']) CB_AUTH = CB_SYSTEM.Device(CB_CONFIG['deviceID'], CB_CONFIG['activeKey']) #Retrieve the adapter configuration if CB_CONFIG['adapterSettingsCollectionName'] != "": logging.info("Retrieving the adapter configuration settings") get_adapter_config() ######################### #BEGIN MQTT SPECIFIC CODE #########################
class UUIDEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, UUID): # if the obj is uuid, we simply return the value of uuid return obj.hex return json.JSONEncoder.default(self, obj) CB_CONFIG = {} # Parse and Validate all args parse_env_variables(os.environ) parse_args(sys.argv) check_required_config() # System credentials CB_SYSTEM = System(CB_CONFIG['CB_SYSTEM_KEY'], CB_CONFIG['CB_SYSTEM_SECRET'], CB_CONFIG['httpURL'] + ":" + CB_CONFIG["httpPort"] ) uid = None if 'deviceID' in CB_CONFIG: uid = CB_SYSTEM.Device(CB_CONFIG['deviceID'], CB_CONFIG['activeKey']) elif 'CB_SERVICE_ACCOUNT' in CB_CONFIG: uid = CB_SYSTEM.Device(CB_CONFIG['CB_SERVICE_ACCOUNT'], authToken=CB_CONFIG['CB_SERVICE_ACCOUNT_TOKEN']) else: print("Device Name/Active Key or Device Service Account/Token not provided") exit(-1) mqtt = CB_SYSTEM.Messaging(uid, CB_CONFIG["messagingPort"], keepalive=30) # Set up callback function
help='Adapter Device Name') parser.add_argument('-ak', '--adapterkey', action='store', type=str, default="123456789", help='Adapter Device Key') parser.add_argument('-tp', '--topic', action='store', type=str, default="device/ble/", help='Adapter Device Key') parser.add_argument('-sc', '--schematable', action='store', type=str, default="dev_admin_devicetypes", help='Device Schema Collection') parser.add_argument('-dw', '--devicewhitelist', action='store', type=str, default="dev_whitelist", help='Device Whitelist Collection') arg = parser.parse_args(sys.argv[1:]) TOPIC=arg.topic logger = setup_custom_logger('BLE Adapter') scanner=ScanDelegate() exitapp=False cbSystem=System(arg.systemkey, arg.systemsecret, arg.systemurl) # cbAuth=cbSystem.User(CBUSER, CBPASS) cbAuth=cbSystem.Device(arg.adaptername, arg.adapterkey) mqtt=cbSystem.Messaging(cbAuth) mqtt.connect() #Connect to the msg broker while not exitapp: dev={} #List of devices to schema and devices monitor whitelisttable = cbSystem.Collection(cbAuth, collectionName=arg.devicewhitelist) wl_rows = whitelisttable.getItems() schematable = cbSystem.Collection(cbAuth, collectionName=arg.schematable) schema_rows = schematable.getItems() for row in schema_rows: SC[row["item_id"]]=row["schema"] for row in wl_rows: