译Zettajs官方文档个人学习记录1

49次阅读

共计 10758 个字符,预计需要花费 27 分钟才能阅读完成。

相关链接
刚学习 node.js 相关内容,最近刚好在学习这个物联网相关的框架,发现只有英文文档,自己结合谷歌翻译,做一点简单的翻译,会有很多错漏,也作为自己的学习记录。

Zetta

Open source – Zetta is an open source Node.js based platform for the internet of things. It’s a complete toolkit for generating HTTP APIs for devices.
开源。Zetta 是一个开源的,基于 Node.js 的物联网平台。它是一个为设备生成 http api 的完整数据包。

Elegant – Using JavaScript, Zetta developers express devices as state machines using an elegant and expressive syntax. Zetta is a completely Node.js based framework that is cross platform, and easily deployable to multiple cloud services.
简洁。Zetta 开发者使用 JavaScript,用简洁和形象的语法将设备表示为状态机。Zetta 是一个完全基于 node.js 的框架,跨平台,可以轻松部署到多个云服务。

API driven – Zetta generates a consistent Hypermedia HTTP API for devices modeled in JavaScript. Our HTTP APIs are expressed using the Siren specification.
API 驱动。Zetta 为使用 JavaScript 建模的设备生成一致的 Hypermedia HTTP API。我们的 HTTP API 使用 Siren 规范表示。

Real time – Zetta exposes websocket endpoints to stream real time events out of the Zetta system. This paradigm of merging Hypermedia with websocket streaming is also known as Reactive Hypermedia.
实时。Zetta 开放 websocket 终端,以便将实时事件从 zetta 系统输出,这种将超媒体与 websocket 流合并的模式也被称为响应式超媒体。

Protocol agnostic – Zetta can support almost all device protocols, and mediate them to HTTP.
支持多协议。Zetta 可以支持几乎所有的设备协议,并将它们转换为 HTTP。

Persistent connections – Zetta can create persistent connections between servers to expose APIs in new and unique ways. You can peer a Zetta server in your home with a server in the cloud, and allow for access to devices on the open internet.
持久连接。zetta 可以在服务器之间创建持久连接,以新的和独特的方式开放 api。你可以通过云服务器在家中部署 zetta,并允许在开放互联网上访问设备。

App creation – Zetta allows you to write stateless applications that live in the server itself. You can query for devices, and wire up interactions between them in these applications. The apps run consistently whether or not your hub has internet connectivity.
应用程序创建——Zetta 允许你编写驻留在服务器本身的无状态应用程序。你可以查询设备,并在这些应用程序中实现它们之间的交互联动。无论你的 hub 是否有互联网连接,这些应用程序都会一直运行。

Query and notification – Zetta has a robust query system that allows you to not only search for devices, but also subscribe to websockets to be notified when new devices come online that fulfill the query. The queries have a SQL-like syntax that makes it easy to wrangle devices in the API, and Zetta itself.
查询和通知。Zetta 有强大的查询系统,你不仅可以搜索设备,而且可以订阅 websocket 推送,当满足查询条件的新设备上线时,你可以收到推送。这些查询语句具有类似 sql 的语法,所以很方便将设备和 zetta 本身打包成 api。

Consistent interfaces – Built from the ground up, Zetta solves specific problems around generating consistent interfaces for devices.
统一接口。从构建框架一开始,zetta 就是为解决设备接口统一这个问题而生。

模拟设备

说明

在这节教程中,我们将通过添加一个模拟设备驱动在 zetta hub 上来构建一个快速开始项目。拟驱动程序模拟一个简单的状态机,用于打开和关闭模拟 LED,如下面的状态机符号所示:
根据图表,当 led 是 off 时它只能转变为 on 的状态,反之亦然。
为了简单表示,我们给你写了示例设备驱动,并且提供下载。在这个教程中,我们将安装示例驱动,然后把它加到 zetta 的服务器代码里面去。

前提条件

本教程假设你已完成快速入门教程,并且“Hello Zetta”项目可以正常工作。

安装模拟设备驱动

1 If the Zetta server is still running, hit Ctrl-c to stop it.

2 In the hello-zetta directory, install the mock driver using NPM:
npm install zetta-led-mock-driver –save

Note: By convention, Zetta device driver names follow the pattern zetta-[device name]-[platform name]-driver. In this example, we’re controlling an LED device on a “mock” platform. For example, a driver that controls an LED from an Intel Edison Board might be called zetta-led-edison-driver.
Note: 为了方便,zetta 设备驱动名称会遵从这个模式:zetta-[设备名称]-[平台名称]-driver。在这个例子中,我们控制一个 led 设备在一个模拟的平台上。
例如,一个控制英特尔板子上 led 的驱动,可能会被叫做 zetta-led-edison-driver。

3 Open index.js in a text editor.

4 Add this require statement to line 2:
var LED = require(‘zetta-led-mock-driver’);

5 Add this use statement to line 6:
.use(LED)

6 Be sure the code looks like this, then save the file:

  • var zetta = require(‘zetta’);var LED = require(‘zetta-led-mock-driver’);

zetta()
.name(‘Hello Zetta’)
.use(LED)
.listen(1337, function(){

 console.log('Zetta is running at http://127.0.0.1:1337');

});

测试服务器

现在我们已经配置好了服务器的模拟 led 驱动。在我们找到驱动前,测试一下。

1 Start the server:
node index.js

2 Notice the terminal output has some new information that we didn’t see before — the LED device we added to the hub was discovered by something called a “scout”. We’ll discuss scouts in detail in another topic.
注意到终端输出出现了一些之前没有见过的信息 – 我们加到 zetta hub 的 led 设备被叫做“scout”的东西发现了。我们将在另一个主题中讨论 scout 的细节。
Oct-23-2015 10:02:24 [scout] Device (led) f826f6e0-ebb8-430a-9e1e-6efddebc42fc was discovered
Oct-23-2015 10:02:24 [server] Server (Hello Zetta) Hello Zetta listening on http://127.0.0.1:1337
Zetta is running at http://127.0.0.1:1337
Device discovery is an important Zetta concept. When Zetta discovers a device, the device information is automatically added to the API. In this case, you could write a very simple app that turns the LED on and off using this API. Let’s see how that works.
设备发现是一个重要的 zetta 概念。当 zetta 发现设备时,设备信息会自动添加到 API 中。在下面例子中,您可以编写一个非常简单的应用程序,使用此 API 打开和关闭 LED。让我们看看它是如何工作的。

调用 API

1 Using cURL or in a REST app like Postman or Advanced REST client, call the root URL:
curl http://127.0.0.1:1337
使用 curl 或者是在像 postman 这样的 rest 应用里,调用根地址:
http://127.0.0.1:1337

2 Then, in the response, locate the /servers/Hello%20Zetta URL and call it:
curl http://localhost:1337/servers/Hello%20Zetta
然后,在响应里,定位 /servers/Hello%20Zetta 地址,调用它:
http://localhost:1337/servers/Hello%20Zetta

3 Notice that this time the entities element lists a device. This is the LED device driver we added to the server. The driver defines the current and possible state properties for the device. In the case of the LED, those properties are on and off. The current state (reflected in the properties attribute of the JSON response) is off.
注意到这一次实体列表里列出了一个设备。这是我们加到服务器上的 led 设备驱动。这个驱动定义了设备的当前还有可能的状态属性。在 led 这种设备里,他们的特性是开和关。当前状态(反映在 json 响应中的实例属性)是关。
{
“class”: [

"server"

],
“properties”: {

"name": "Hello Zetta"

},
“entities”: [

{
  "class": [
    "device",
    "led"
  ],
  "rel": ["http://rels.zettajs.io/device"],
  "properties": {
    "id": "e6f5b480-e96e-4fdc-8718-91aeb0234c99",
    "type": "led",
    "state": "off"
  },
  "links": [
    {
      "rel": [
        "self",
        "edit"
      ],
      "href": "http://localhost:1337/servers/Hello%20Zetta/devices/e6f5b480-e96e-4fdc-8718-91aeb0234c99"
    },
    {
      "rel": [
        "http://rels.zettajs.io/type",
        "describedby"
      ],
      "href": "http://localhost:1337/servers/Hello%20Zetta/meta/led"
    },
    {
      "title": "Hello Zetta",
      "rel": [
        "up",
        "http://rels.zettajs.io/server"
      ],
      "href": "http://localhost:1337/servers/Hello%20Zetta"
    }
  ]
}

],
“actions”: [

{
  "name": "query-devices",
  "method": "GET",
  "href": "http://localhost:1337/servers/Hello%20Zetta",
  "type": "application/x-www-form-urlencoded",
  "fields": [
    {
      "name": "ql",
      "type": "text"
    }
  ]
}

],
“links”: [

{
  "rel": ["self"],
  "href": "http://localhost:1337/servers/Hello%20Zetta"
},
{
  "rel": ["http://rels.zettajs.io/metadata"],
  "href": "http://localhost:1337/servers/Hello%20Zetta/meta"
},
{
  "rel": ["monitor"],
  "href": "ws://localhost:1337/servers/Hello%20Zetta/events?topic=logs"
}

]
}

Query the device capabilities

查询设备功能

Now, let’s follow the URL to the device itself. It looks something like this:
现在,我们按照设备本身的 URL。它看起来像这样:
http://127.0.0.1:1337/servers/Hello%20Zetta/devices/a3bbdd2d-67fe-4fad-97b2-3851f18aae7e
Here we can discover what actions the actual device is capable of performing. This device has a “transition” action that lets you change its state. Because the current state, as we saw previously, is off, the available action is turn-on.
这里我们可以发现实际设备能够执行的操作。这个设备有一个可以让你改变它状态的“转换”动作。显而易见的,当前状态是关闭,所以有效的动作是打开。
{
“class”: [

"device",
"led"

],
“properties”: {

"id": "e6f5b480-e96e-4fdc-8718-91aeb0234c99",
"type": "led",
"state": "off"

},
“actions”: [

{
  "class": ["transition"],
  "name": "turn-on",
  "method": "POST",
  "href": "http://localhost:1337/servers/Hello%20Zetta/devices/e6f5b480-e96e-4fdc-8718-91aeb0234c99",
  "fields": [
    {
      "name": "action",
      "type": "hidden",
      "value": "turn-on"
    }
  ]
}

],
“links”: [

{
  "rel": [
    "self",
    "edit"
  ],
  "href": "http://localhost:1337/servers/Hello%20Zetta/devices/e6f5b480-e96e-4fdc-8718-91aeb0234c99"
},
{
  "title": "Hello Zetta",
  "rel": [
    "up",
    "http://rels.zettajs.io/server"
  ],
  "href": "http://localhost:1337/servers/Hello%20Zetta"
},
{
  "rel": [
    "http://rels.zettajs.io/type",
    "describedby"
  ],
  "href": "http://localhost:1337/servers/Hello%20Zetta/meta/led"
},
{
  "title": "state",
  "rel": [
    "monitor",
    "http://rels.zettajs.io/object-stream"
  ],
  "href": "ws://localhost:1337/servers/Hello%20Zetta/events?topic=led%2Fe6f5b480-e96e-4fdc-8718-91aeb0234c99%2Fstate"
},
{
  "title": "logs",
  "rel": [
    "monitor",
    "http://rels.zettajs.io/object-stream"
  ],
  "href": "ws://localhost:1337/servers/Hello%20Zetta/events?topic=led%2Fe6f5b480-e96e-4fdc-8718-91aeb0234c99%2Flogs"
}

]
}

改变设备状态

You might have noticed the transition action includes an href link. The link is an HTTP POST command that you can use to change the LED’s state. To “turn the light on”, you simply form the correct POST request.
你也许注意到了转换动作包括了一个 href 链接。这个链接是一个 http post 请求,可以用于改变 led 的状态。要打开灯,你只需简单地写出正确地 post 请求。
“actions”: [

    {
        "class": ["transition"],
        "name": "turn-on",
        "method": "POST",
        "href": "http://localhost:1337/servers/Hello%20Zetta/devices/e511deee-a266-4431-8a42-eeef7f2412f9",
        "fields": [
            {
                "name": "action",
                "type": "hidden",
                "value": "turn-on"
            }
        ]
    }
]

The POST request to turn on the light looks something like this (you’ll need to use the link provided in your own response, which has the correct device ID):
这个开灯的 post 请求看起来像这个(你需要使用你自己的响应中提供的链接,它有正确的设备 id)
curl -i -X POST -H http://127.0.0.1:1337/servers/Hello%20Zetta/devices/e6f5b480-e96e-4fdc-8718-91aeb0234c99 -d ‘action=turn-on’
Tip: To produce nicely formatted JSON from cURL, you can pipe the cURL response to this Python command to pretty-format the response: python -m json.tool. For example:
注意:为了从 curl 中生成格式良好的 json,你可以将 curl 响应传送到这个 python 命令,来对响应进行格式化:python -m json.tool。例如:
curl -i -X POST http://127.0.0.1:1337/servers/Hello%20Zetta/devices/e6f5b480-e96e-4fdc-8718-91aeb0234c99 -d‘action=turn-on | python -m json.tool
In the response, you can see that the state has been changed from off to on. Furthermore, the available action is changed to turn-off. To turn the light off again, follow the same pattern by POSTing to the transition URL.
在响应中,您可以看到状态已从更改 off 为 on。此外,可用操作更改为 turn-off。要再次关闭灯光,按照相同的方式使用 post 请求。
{
“class”: [

"device",
"led"

],
“properties”: {

"id": "e6f5b480-e96e-4fdc-8718-91aeb0234c99",
"type": "led",
"state": "on"

},
“actions”: [

{
  "class": ["transition"],
  "name": "turn-off",
  "method": "POST",
  "href": "http://localhost:1337/servers/Hello%20Zetta/devices/e6f5b480-e96e-4fdc-8718-91aeb0234c99",
  "fields": [
    {
      "name": "action",
      "type": "hidden",
      "value": "turn-off"
    }
  ]
}

],
“links”: [

{
  "rel": [
    "self",
    "edit"
  ],
  "href": "http://localhost:1337/servers/Hello%20Zetta/devices/e6f5b480-e96e-4fdc-8718-91aeb0234c99"
},
{
  "title": "Hello Zetta",
  "rel": [
    "up",
    "http://rels.zettajs.io/server"
  ],
  "href": "http://localhost:1337/servers/Hello%20Zetta"
},
{
  "rel": [
    "http://rels.zettajs.io/type",
    "describedby"
  ],
  "href": "http://localhost:1337/servers/Hello%20Zetta/meta/led"
},
{
  "title": "state",
  "rel": [
    "monitor",
    "http://rels.zettajs.io/object-stream"
  ],
  "href": "ws://localhost:1337/servers/Hello%20Zetta/events?topic=led%2Fe6f5b480-e96e-4fdc-8718-91aeb0234c99%2Fstate"
},
{
  "title": "logs",
  "rel": [
    "monitor",
    "http://rels.zettajs.io/object-stream"
  ],
  "href": "ws://localhost:1337/servers/Hello%20Zetta/events?topic=led%2Fe6f5b480-e96e-4fdc-8718-91aeb0234c99%2Flogs"
}

]
}

总结

In this topic, we added a sample “mock” device driver to a server, and then we traced the links provided in the server’s JSON response to transition the state of the device.
在本主题中,我们向服务器添加了一个示例“模拟”设备驱动程序,然后我们利用服务器的 Jason 响应中的链接,以此来转换设备的状态。
The cool thing about Zetta APIs is that each response contains everything an app developer needs to navigate the API, discover its actions, and execute them.
Zetta 很酷的地方在于,每一个响应都包含开发人员所需要的 API,找到它的动作,并且执行它们。

Another thing to remember is that this pattern is central to Zetta development. You write the device driver for whatever device you want to access over HTTP, and Zetta provides a fully functional REST API for that device!
要记住的另一件事是,这种模式对于 Zetta 的开发至关重要。你可以为通过 HTTP 访问的任何设备编写设备驱动程序,Zetta 为该设备提供了功能齐全的 REST API!
In the next part of the tutorial, we’ll take a close look at the LED driver code.
在本教程的下一部分中,我们将仔细研究 LED 驱动器代码。

个人测试结果(用 postman)

正文完
 0