Friday 15 May 2015

cosm - Xively: how to activate a device with the python api? -


Since the COSM has been synchronized, a good device API has been added (or always there - not sure). Flow

  1. Creating a product batch with serial numbers
  2. Activate devices using some product batch identifiers (?)
  3. Received devices Start using the device from the Feed / API key

    How do I know if there is any signal through the Python API?

    It should be added to the library, but for now you have to apply the device activation to this code You can use. I have used environment variables to store product secret and device serial , but whatever your suit of use case, it is only difficult to change The thing is that you need to call.

      xively binascii import a2b_hex import OS import environment hashlib import SHA1 and HMAC secret = environment [ 'XIVELY_PRODUCT_SECRET'] = serial environment [ 'XIVELY_DEVICE_SERIAL_NUMBER'] activation = hmac.new (a2b_hex ( Secret), serial, SHA1) .hexdigest () creds = xively.Client (key = none) .get ('/ v2 / devices /' + activation + '/ active'). Jason () xi_feed = xively.XivelyAPIClient (creds ['apikey']). Feeds.get (credit ['feed_id'])   

    You should also be careful to store credentials. A file can only be activated once in the form of a tool if you If you try to run this code repeatedly, you will notice 403 errors, so use the Axml Developer workspace to disable the device under testing (you may need to refresh the page).

    Here's a fully working example using config file or environment variable:

      # / usr / bin / binascii import a2b_hex import HHML imports from hashlib import SHA1, Subprocity Import ConfigParser Import xively Python from OS import environment from CONFIG_FILE! = 'Xively.conf' = 'PROVISIONING_PRODUCT_SECRET =' PRODUCT_SECRET 'PROVISIONING_DEVICE_SERIAL =' DEVICE_SERIAL 'PROVISIONING_FEED_ID =' FEED_ID 'PROVISIONING_API_KEY =' API_KEY 'def get_setting (config, section, key): try: value = config.get (section, key ): Print key + "not found in config file" Instead use the environment variable "+ key +". "Try to: value = except environ [key]: Print Key +" not found in the environment. "Raise # define value? If the price is not: Return Value Increase xively_activate_product (secret serial): Activation = hmac.new ( a2b_hex (Sikrel, Shaa 1) .hexdigest () creds = xively.Client (key = none) .get ( '/ v2 / Devices /' + activation + '/ activate'). Json () return creds # main config = ConfigParser try .RawConfigParser () config.read (CONFIG_FILE): # see that we already have an API key and feed ID feed_id = the config. (provisioning, PROVISIONING_FEED_ID) api_key = config.get (Provisioning, PROVISIONING_API_KEY) printed "provision Product description:" Print "FEED_ID:" + str (FEED_ID) print "API_KEY:" + api_key # except continue to work with the active product here: print "FEED_ID and API_KEY not found . now the product to activate. "# Try an error handling no it _ais_ necessary secret: secret = except get_setting (config, PROVISIONING_PRODUCT_SECRET): print" find "+ PROVISIONING_PRODUCT_SECRET +" failing. "Syskexit Try (1): serial = get_setting (config, p) Ravdhan, PROVISIONING_DEVICE_SERIAL) except: serial = subprocess.check_output ( "hostname" shell = True) if serial No: Print + PROVISIONING_DEVICE_SERIAL + "failed" to the host name for the "withdrawal. Dedication. "Sidaks: (1) Try: creds = xive_activate_product (secret serial) # check whether there were errors except CDS [" errors "]: Pass: print" product activation failed ( "+ creds [" title "] +": "+ Cred [[" errors "] +"). "Sys.exit (1) feed_id = creds [ 'feed_id'] api_key = creds [ 'apikey'] print" product activation is successful. "Print" FEED_ID: "+ str (FEED_ID) print" API_KEY: "+ api_key not config.has_section (provisioning): config.add_section (provisioning) config.set (provisioning, PROVISIONING_FEED_ID, FEED_ID) config.set (provisioning, PROVISIONING_API_ KEY, Api_key) # except as our configuration file Conffail to write on 'example.cfg' (CONFIG_FILE, 'wb'): config.write (configfile) Exception e: print "product activation failed (" + str (E) + "). "Sys.exit (1)    

No comments:

Post a Comment