首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >西弗利:如何用python激活设备?

西弗利:如何用python激活设备?
EN

Stack Overflow用户
提问于 2013-06-22 11:08:05
回答 2查看 956关注 0票数 1

自从COSM变成了Xively之后,一个很好的设备api就被添加了(或者一直存在--不确定)。流量是

  1. 创建带有序列号的产品批次
  2. 使用某些产品批处理标识符激活设备(?)
  3. 使用获取的feed/api密钥开始使用设备

我不知道如何通过python来完成这个任务--有什么指针吗?

EN

Stack Overflow用户

发布于 2013-08-28 09:30:52

这是我写的另一个有用的课程:

代码语言:javascript
运行
复制
## Logging for debugging purposes
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


import os
from os import environ 
from hashlib import sha1 
from binascii import a2b_hex 
import hmac
import sys, subprocess
import ConfigParser
import xively


PROVISIONING = 'PROVISIONING'

PROVISIONING_PRODUCT_SECRET = 'PRODUCT_SECRET'

PROVISIONING_FEED_ID = 'FEED_ID'
PROVISIONING_API_KEY = 'API_KEY'


class XivelyManager:

    def __init__(self, settings="xively.conf"):
        # main
        self.settings=settings
        self.config = ConfigParser.RawConfigParser()
        self.config.read(settings)

        try:
            # see if we already have an api key and feed id

            self.api_key = self.get_setting(PROVISIONING, PROVISIONING_API_KEY)
            self.secret = self.get_setting(PROVISIONING, PROVISIONING_PRODUCT_SECRET)
            # continue working with your activated product here

        except:
            logger.exception( "API KEY and SECRET NOT FOUND" )

    def activate_sensor(self,serial):

            try:    
                creds = self.xively_activate_product(str(serial))

                # check if there were errors
                try:
                    creds["errors"]
                except:
                    pass
                else:
                    logger.exception("Product activation failed (" + creds["title"] +": "+ creds["errors"] + ").")
                    return False

                feed_id = creds['feed_id']
                api_key = creds['apikey']

                if not self.config.has_section(PROVISIONING):
                    self.config.add_section(PROVISIONING)

                if not self.config.has_section(str(serial)):
                    self.config.add_section(str(serial))

                self.config.set(PROVISIONING, PROVISIONING_API_KEY, api_key)
                self.config.set(str(serial), PROVISIONING_FEED_ID , feed_id)

                # Writing our configuration file to 'xively.cfg'
                with open(self.settings, 'wb') as configfile:
                    self.config.write(configfile)
                return True

            except Exception as e:
                logger.exception("Product activation failed (" + str(e) +").")
                return False

    def get_setting(self, section, key):
        try:

            value = self.config.get(section, key)
        except:
            logger.exception( key + " not found in config file. Using environment variable " + key + " instead.")
##            try:
##                value = environ[key]
##            except:
##                logger.exception( key + " not found in environment.")
##            finally:
##                pass
        finally:

            # value defined?
            if not value:
                raise
            return value

    def get_feed(self,serial):
        try:
            if self.config.has_section(str(serial)):
                feed_id = self.get_setting(str(serial), PROVISIONING_FEED_ID)
            else:
                feed_id=False
        except Exception, e:
            feed_id=False
        finally:
            return feed_id

    def xively_activate_product(self, serial):
        activation = hmac.new(a2b_hex(self.secret), serial, sha1).hexdigest()
        creds = xively.Client(key=None).get('/v2/devices/'+activation+'/activate').json()
        return creds 

if __name__ == "__main__":
    print "Testing Xively Manager "
    settings = os.path.join(os.path.dirname(sys.argv[0]), "config", "xively.conf")
    print settings

    testxive=XivelyManager(settings)
    #print testxive.activate_sensor(10)

    print testxive.get_feed(10)

当您的internet网关连接到其他几个设备时,这是很有用的。您的配置文件将被更新如下:

代码语言:javascript
运行
复制
[PROVISIONING]
product_secret = xxxxxxxxxxxxxxxxxxxxxxxxxxxx
api_key = xxxxxxxxxxxxxxxxxxxxxxxx

[productserial1]
feed_id = xxxxxxxx

[productserial2]
feed_id = xxxxxxxx
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17250056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档