Example #1
0
    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
Example #2
0
 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()
Example #4
0
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)
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
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)

    saved_model_pb = df['saved_model_pb'][0]
    saved_model_pb = saved_model_pb.encode('ascii')
    saved_model_pb_decode = base64.b64decode(saved_model_pb)
    # print(type(saved_model_pb_decode))
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
mqtt.connect()
Example #8
0
'''
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()
Example #9
0
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])


# Connect callback to client
mqtt.on_connect = on_connect

# Connect and spin for 30 seconds before disconnecting
mqtt.connect()
Example #10
0
    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()
            os._exit(0)
            raise
Example #11
0
        "secsleft": batt[1],
        "power_plugged": power
    }
    js = json.dumps(dic)
    return js


SystemKey = "dce7d4cb0b80c1eec1bbbaa9a262"
SystemSecret = "DCE7D4CB0BC2A092D3E1FFC3D831"

mySystem = System(SystemKey, SystemSecret)
email = "*****@*****.**"
password = "******"

jiang = mySystem.User(email, password)
mqtt = mySystem.Messaging(jiang)


def on_connect(client, userdata, flags, rc):
    # When we connect to the broker, start publishing our data to the keelhauled channel
    for i in range(10):
        payload = get_resource()
        print payload
        client.publish("jiang/info", payload)
        time.sleep(3)


#Connect callback to client
mqtt.on_connect = on_connect

# Connect and spin for 30 seconds before disconnecting
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.
            "process_queue": psutil.getloadavg()[0],
        }  # length of process queue.

        mqtt.publish("data_collection", json.dumps(payload))
        time.sleep(3)
    except:
Example #13
0
SystemKey = "d4a68fb60becb78bcdb4ecdffd2c"
SystemSecret = "D4A68FB60BD2DF8998A5EBC0E5EA01"

mySystem = System(SystemKey, SystemSecret, safe=False)

# Auth as anon
# anon = mySystem.AnonUser()

# Use the anon user to register Martin
# martin = mySystem.registerUser("*****@*****.**", "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)
imgcv = cv2.imread("./sample_img/sample_dog.jpg")

# result = tfnet.return_predict(imgcv)


# Set up callback functions
def on_connect(client, userdata, flags, rc):
    # When we connect to the broker, subscribe to the southernplayalisticadillacmuzik channel
    client.subscribe("config")
    client.subscribe("predict")
    client.subscribe("predict/results")
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
Example #15
0
        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

newUser = mySystem.User(email,
                        password)  #access the platform and generate a token
mqtt = mySystem.Messaging(
    newUser)  #use the token to access messaging on your account

mqtt.connect()  #connect to messaging client
counter = 0
time.sleep(1)
print("\nPlease press 'S' to start scanning your CPU average.")
while True:
    try:
        if keyboard.is_pressed('s'):
            print("Scanning is about to start!")
            time.sleep(1)
            break
    except:
        break

CPU_values = []
Example #16
0
# my system credentials
SystemKey = "e0b3f8e40ba4879dc5c7e2e7ce1b"
SystemSecret = "E0B3F8E40BA4ADDCAAE4AFA7FB67"

# instance of my system
mySystem = System(SystemKey, SystemSecret)

# Log in as Sulochan
user = "******"
userPass = "******"

# user object of current session
thisUserObject = mySystem.User(user, userPass)

# MQTT object
mqtt = mySystem.Messaging(thisUserObject)

#what to do after connection
def on_connect(client, userdata, flags, rc):
    #listen/subscribe to analytics topic
    client.subscribe("analytics")

#what to do after receiving message
def on_message(client, userdata, message):
    #display analytics received from the clearblade IoT code service
    print "Message received from SERVER: \n";
    print "*****************************\n";
    print message.payload
    print "*****************************\n\n";

mqtt.on_connect = on_connect
Example #17
0
##        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):
    zmq_message = "{}{}{}".format(message.topic, zmq_topic_delimiter,
                                  message.payload)
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)
Example #19
0
    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...
mqtt.connect()

# coverting the data to string format
## change: no need to convert data to string
str_data = str(data)

# sending the data
mqtt.publish("preetlaptop", str_data)

## Change: logging out the user
preet.logout()

## Change: disconnecting the messaging service
        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
        except KeyboardInterrupt:
            SCOPE_VARS['EXIT_APP'] = True
            CB_MQTT.disconnect()
            sys.exit(0)
        except Exception as e:
            logging.info("EXCEPTION:: %s", str(e))
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)

collectionobj = mySystem.Collection(
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


def on_connect(client, userdata, flags, rc):
    # When we connect to the broker, start publishing our data to the keelhauled topic
    print("Return Code: ", rc)
    if rc == 0:
        print('Successfully connected')
    #print('waiting ... %s' % datetime.datetime.now())


# Connect callback to client
mqtt.on_connect = on_connect
Example #23
0
        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()
    swap = psutil.swap_memory()
    mem_total = f"{get_size(svmem.total)}"
    mem_available = f"{get_size(svmem.available)}"
    mem_used = f"{get_size(svmem.used)}"
    mem_per = f"{svmem.percent}%"
    swap_total = f"{get_size(swap.total)}"