Ejemplo n.º 1
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
Ejemplo n.º 2
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
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()
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)
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
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
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")
Ejemplo n.º 9
0
        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()
Ejemplo n.º 10
0
def run_async_server():
    """
    The main loop instantiates one or more PyModbus servers mapped to ClearBlade Modbus proxies based on
    IP address and port defined in a ClearBlade platform Collection
    """
    log = None
    virtual_ifs = []
    err_msg = None
    defer_reactor = False

    try:
        parser = get_parser()
        user_options = parser.parse_args()
        local_ip_address = user_options.ip_address
        local_tcp_port = user_options.tcp_port
        net_if = user_options.net_if
        if user_options.log_level == 'DEBUG':
            _debug = True
        else:
            _debug = False
        HEARTBEAT = user_options.heartbeat

        log = headless.get_wrapping_logger(name=ADAPTER_DEVICE_ID, debug=_debug)
        server_log = headless.get_wrapping_logger(name="pymodbus.server", debug=_debug)

        log.info("Initializing ClearBlade System connection")
        cb_system = System(systemKey=user_options.systemKey, systemSecret=user_options.systemSecret, url=user_options.url)
        cb_auth = cb_system.Device(name=user_options.deviceName, key=user_options.deviceKey)
        cb_slave_config = user_options.slaves_collection
        cb_data = user_options.data_collection

        ip_proxies = []
        proxy_ports = []
        ip_address = None
        collection = cb_system.Collection(cb_auth, collectionName=cb_slave_config)
        query = Query()
        query.notEqualTo(COL_PROXY_IP_ADDRESS, '')
        rows = collection.getItems(query)
        for row in rows:
            # TODO: allow for possibility of multiple IPs with same port or same IP with multiple ports
            ip_address = str(row[COL_PROXY_IP_ADDRESS])
            tcp_port = int(row[COL_PROXY_IP_PORT])
            if ip_address not in ip_proxies:
                log.info("Found slave at {} on ClearBlade adapter config".format(ip_address))
                ip_proxies.append(ip_address)
                proxy_ports.append(tcp_port)
            else:
                log.warning("Duplicate proxy IP address {} found in configuration - ignoring".format(ip_address))

        log.debug("Processing {} slaves".format(len(ip_proxies)))
        for i in range(0, len(ip_proxies)):
            log.debug("Getting server context for {}".format(ip_proxies[i]))
            context = ClearBladeModbusProxyServerContext(cb_system=cb_system, cb_auth=cb_auth,
                                                         cb_slaves_config=cb_slave_config, cb_data=cb_data,
                                                         ip_address=ip_proxies[i], log=log)
            # Create IP aliases
            local_ip_address = ip_proxies[i]
            ip_mask = '255.255.255.0'
            local_tcp_port = proxy_ports[i]
            if sys.platform.startswith('win'):
                log.info("I'm on Windows!")
                local_ip_address = 'localhost'
            elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
                virtual_if = '{nif}:{alias}'.format(nif=net_if, alias=i)
                virtual_ifs.append(virtual_if)
                linux_command = "ifconfig {vif} {ip}".format(vif=virtual_if, ip=local_ip_address)
                if ip_mask is not None:
                    linux_command += " netmask {mask}".format(mask=ip_mask)
                log.info("Creating virtual IP address / alias via $ {}".format(linux_command))
                subprocess.call(linux_command, shell=True)

            # Create Server Identification
            identity = ModbusDeviceIdentification()
            identity.VendorName = 'PyModbus'
            identity.VendorUrl = 'http://github.com/bashwork/pymodbus/'
            identity.ProductName = 'Inmarsat/ClearBlade Modbus Server Adapter'
            identity.ModelName = ip_proxies[i]
            identity.MajorMinorRevision = '1.0'

            # Setup Modbus TCP Server
            log.info("Starting Modbus TCP server on {}:{}".format(local_ip_address, local_tcp_port))
            modbus_server_args = {
                'context': context,
                'identity': identity,
                'address': (local_ip_address, local_tcp_port),
                # 'console': _debug,
                'defer_reactor_run': True,
            }
            if modbus_server_args['defer_reactor_run']:
                defer_reactor = True
            reactor.callInThread(StartTcpServer, **modbus_server_args)

            if local_ip_address == 'localhost':
                log.info("Windows retricted environment prevents IP alias - running localhost for {}"
                         .format(ip_proxies[i]))
                break

        reactor.callInThread(_heartbeat, log, time.time(), HEARTBEAT)
        if defer_reactor:
            reactor.suggestThreadPoolSize(len(ip_proxies))
            reactor.run()

    except KeyboardInterrupt:
        err_msg = "modbus_server_adapter.py halted by Keyboard Interrupt"
        if log is not None:
            log.info(err_msg)
        else:
            print(err_msg)
        sys.exit("modbus_server_adapter.py halted by Keyboard Interrupt")

    except Exception as e:
        err_msg = "EXCEPTION: {}".format(e)
        if log is not None:
            log.info(err_msg)
        else:
            print(err_msg)
        sys.exit("modbus_server_adapter.py halted by exception {}".format(e))

    finally:
        if defer_reactor and reactor.running:
            reactor.stop()
        for vif in virtual_ifs:
            debug_msg = "Taking down virtual interface {}".format(vif)
            if log is not None:
                log.debug(debug_msg)
            else:
                print(debug_msg)
            linux_command = "ifconfig {} down".format(vif)
            subprocess.call(linux_command, shell=True)
        print("Exiting...")
Ejemplo n.º 11
0
 def __init__(self, key, secret):
     self.system = System(key, secret)
Ejemplo n.º 12
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):
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)
from clearblade.ClearBladeCore import System, Query, Developer, Users
import time
import json
import ast
import sys

published = 0
rc = 1

print(sys.argv)
url = "https://staging.clearblade.com"

SystemKey = "a6c2e6d10bc2f183fca3c7d3d0fe01"
SystemSecret = "A6C2E6D10BCADB819FABF8FCD94E"

mySystem = System(SystemKey, SystemSecret, url, safe=False, sslVerify=True)

user = mySystem.User("*****@*****.**", "password")
print("Welcome User!")

mqtt = mySystem.Messaging(user)


def on_connect(client, userdata, flags, rc):
    client.subscribe("config")
    client.subscribe("send")
    client.subscribe("send/results")


def on_message(client, userdata, message):
    if (message.topic == "config"):
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)
    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
    #########################
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
Ejemplo n.º 18
0
                        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:
Ejemplo n.º 19
0
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
Ejemplo n.º 20
0
    def _system_from_cfg(self):
        if 'system' not in self.config:
            raise KeyError('System key not found in clearblade.ini.')

        return System(self.config['system']['key'],
                      self.config['system']['secret'])