def getClient() -> SmartObjectsClient: from configparser import ConfigParser myconfig = ConfigParser() myconfig.read("ittests/creds.ini") key = myconfig.get("Credentials", "key") secret = myconfig.get("Credentials", "secret") return SmartObjectsClient(key, secret, Environments.Sandbox)
def get_mnubo_client(): """ A method to return the mnubo client and initialize it if not initialized :return: A SmartObjectsClient """ global mnubo_client global config if not isinstance(mnubo_client, SmartObjectsClient): mnubo_client = SmartObjectsClient( client_id=config['client_id'], client_secret=config['client_secret'], environment=config['environment']) return mnubo_client
def main(): # initialize the client with your client_id and client_secret # (available in the "API Credential" section of your dashboard) client = SmartObjectsClient(CLIENT_ID, CLIENT_SECRET, Environments.Production) # use the OwnersService to find if an owner already exists or to create it if not client.owners.owner_exists("*****@*****.**"): owner = { "username": "******", "x_password": "******", "zip_code": "H3K 1G6" } client.owners.create(owner) # use the ObjectsService to find if an object already exists or to create it if not client.objects.object_exists("cat_detector_kitchen"): smartobject = { "x_device_id": "cat_detector_kitchen", "x_object_type": "cat_detector", "model": "pro" } client.objects.create(smartobject) # link an object to an owner with "claim" client.owners.claim("*****@*****.**", "cat_detector_kitchen") # use the EventsService to publish new events # events can be sent by batches of up to 1000 events events = [ {"x_event_type": "cat_detected", "type": "soft", "speed": i} for i in range(10) ] event_results = client.events.send_from_device("cat_detector_kitchen", events) # make sure every event was accepted failed = filter(lambda r: r.result != "success", event_results) for f in failed: print("- {id}: {reason}".format(id=f.id, reason=f.message)) # it is recommended to validate your complex queries with this special endpoints to ease development # and reduce errors query = { "from": "event", "where": { "and": [ {"speed": {"eq": 0}}, {"x_timestamp": {"dateBetween": "2016-08-22", "and": "2016-08-27"} } ] }, "select": [ {"value": "version"}, {"value": "time_created"} ] } validation_result = client.search.validate_query(query) if not validation_result.is_valid: print(validation_result.validation_errors) else: # search for events with the search API # note: events submitted above might not be immediately available for querying as they must be fully # processed by the SmartObjects platform resultset = client.search.search(query) print("We got {} results:".format(len(resultset))) for row in resultset: print("version: {}, created: {}".format(row.get("version"), row.get("time_created", ResultSet.ToDatetime))) # explore properties of the datamodel with search.get_datasets # https://smartobjects.mnubo.com/apps/doc/api_search.html#get-api-v3-search-datasets datasets = client.search.get_datasets() for owner_field in datasets["event"].fields: if not owner_field.key.startswith("x_"): print("{0} ({1}): {2}".format(owner_field.key, owner_field.high_level_type, owner_field.description))
def getClient(): Config = ConfigParser.ConfigParser() Config.read("ittests/creds.ini") key = Config.get("Credentials", "key") secret = Config.get("Credentials", "secret") return SmartObjectsClient(key, secret, Environments.Sandbox)
def main(): # initialize the client with your client_id and client_secret client = SmartObjectsClient(CLIENT_ID, CLIENT_SECRET, Environments.Sandbox) # create an object type client.model.sandbox_ops.object_types_ops.createOne({ 'key': 'cat_detector', 'description': '', 'objectAttributesKeys': [] }) # create an object attribute bound to the object type above client.model.sandbox_ops.object_attributes_ops.createOne({ 'key': 'model', 'displayName': 'Model', 'description': 'model of the cat detector', 'type': { 'containerType': 'none', 'highLevelType': 'TEXT' }, 'objectTypeKeys': ['cat_detector'] }) # create an event type client.model.sandbox_ops.event_types_ops.createOne({ 'key': 'cat_detected', 'origin': 'unscheduled', 'description': 'events triggered when a cat is detected', 'timeseriesKeys': [] }) # create a few timeseries and bind them to the event type above client.model.sandbox_ops.timeseries_ops.createOne({ 'key': 'speed', 'displayName': 'Speed (m/s)', 'description': 'Speed of the cat', 'type': { 'highLevelType': 'DOUBLE' }, 'eventTypeKeys': ['cat_detected'] }) client.model.sandbox_ops.timeseries_ops.createOne({ 'key': 'version', 'displayName': 'Version', 'description': 'Version of the detector', 'type': { 'highLevelType': 'TEXT' }, 'eventTypeKeys': ['cat_detected'] }) # create an owner attribute owner = OwnerAttribute({ 'key': 'zip_code', 'displayName': 'Zip Code', 'description': 'Zip code of the owner of the detector', 'type': { 'containerType': 'none', 'highLevelType': 'TEXT' } }) client.model.sandbox_ops.owner_attributes_ops.createOne(owner) # deploy the entities in production client.model.sandbox_ops.timeseries_ops.deploy('speed') client.model.sandbox_ops.timeseries_ops.deploy('version') client.model.sandbox_ops.object_attributes_ops.deploy('model') client.model.sandbox_ops.owner_attributes_ops.deploy(owner.key) model = client.model.export() print("Number of event types - {}".format(len(model.event_types))) print("Number of timeseries - {}".format(len(model.timeseries))) print("Number of object types - {}".format(len(model.object_types))) print("Number of object attributes - {}".format(len(model.object_attributes)))