从网站Bluemix应用程序的index.html文件中的app.js读取变量

时间:2017-05-15 05:20:59

标签: javascript ibm-cloud iot watson-iot

我正在开发一个Web应用程序来控制智能家居设备。我使用Bluemix物联网平台进行设计,但我遇到了一些我无法解决的问题。

我正在我的(currentRelay1)文件中读取变量app.js,该变量直接从物联网设备中读取。

var Client = require('ibmiotf');

var appClientConfig = {
    "org" : "rmpr4l",
    "id" : "dineriot",
    "domain": "internetofthings.ibmcloud.com",
    "auth-key" : "a-rmps4l-0uegqtl8mec",
    "auth-token" : "V8vB!?R3yES7dc@vtj"
};

var appClient = new Client.IotfApplication(appClientConfig);

appClient.connect();

appClient.on("connect", function () {

    appClient.subscribeToDeviceEvents();

});
appClient.on("deviceEvent", function (deviceType, deviceId, eventType, format, payload) {

    console.log("Device Event from :: "+deviceType+" : "+deviceId+" of event "+eventType+" with payload : "+payload);

    var json = JSON.parse(payload);
    currentRelay1 = json["C1"] * 1000;
    currentRelay2 = json["C2"] * 1000;

});

现在,我想分享这个变量,以便在index.html文件中使用它来绘制图表。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

由于这是您的服务器端代码,因此您需要将此数据发送到您的页面。或者从服务器请求它的页面。不幸的是,这是一个非常大的话题:)

您可以设置一个响应的服务器,如下所示:

server.js //“data”是你的变量

class iCadastralObject(AbstractConcreteBase, Base):

    def __init__(self, cadastral_region, cadastral_district, cadastral_block, cadastral_object):
        self.cadastral_region = cadastral_region
        self.cadastral_district = cadastral_district
        self.cadastral_block = cadastral_block
        self.cadastral_object = cadastral_object

    # this is a combined PK
    cadastral_region = Column(Integer, primary_key=True, index=True)
    cadastral_district = Column(Integer, primary_key=True, index=True)
    cadastral_block = Column(Integer, primary_key=True, index=True)
    cadastral_object = Column(Integer, primary_key=True, index=True)

    @declared_attr
    def encumbrances(self):
        return relationship("iEncumbrance")


class Building(iCadastralObject):
    __tablename__ = 'building'

    __mapper_args__ = {
        'polymorphic_identity': 'building',
        'concrete': True
    }

    @declared_attr
    def encumbrances(self):
        return relationship("iEncumbrance")


class Flat(iCadastralObject):
    __tablename__ = 'flat'

    __mapper_args__ = {
        'polymorphic_identity': 'flat',
        'concrete': True
    }

    @declared_attr
    def encumbrances(self):
        return relationship("iEncumbrance")


class Construction(iCadastralObject):
    __tablename__ = 'construction'

    __mapper_args__ = {
        'polymorphic_identity': 'construction',
        'concrete': True
    }


class iEncumbrance(Base):
    __tablename__ = 'iencumbrance'

    id = Column(Integer, primary_key=True, index=True)

    def __init__(self, cadastral_object):
        self.parent_cadastral_region = cadastral_object.cadastral_region
        self.parent_cadastral_district = cadastral_object.cadastral_district
        self.parent_cadastral_block = cadastral_object.cadastral_block
        self.parent_cadastral_object = cadastral_object.cadastral_object

    # FK fields
    parent_cadastral_region = Column(Integer, nullable=False)
    parent_cadastral_district = Column(Integer, nullable=False)
    parent_cadastral_block = Column(Integer, nullable=False)
    parent_cadastral_object = Column(Integer, nullable=False)

    parent_object = relationship(iCadastralObject)

    __table_args__ = (ForeignKeyConstraint(
        [
            parent_cadastral_region,
            parent_cadastral_district,
            parent_cadastral_block,
            parent_cadastral_object],
        [
            iCadastralObject.cadastral_region,
            iCadastralObject.cadastral_district,
            iCadastralObject.cadastral_block,
            iCadastralObject.cadastral_object]),
                      {}
    )

client.js //包含在您的.html文件中

sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship iCadastralObject.encumbrances - there are no foreign keys linking these tables.  Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.

进一步阅读: https://nodejs.org/api/http.html https://www.w3schools.com/xml/xml_http.asp