Zetta Quick Start是 Zetta 的典型“Hello World”项目。了解如何安装 Zetta、创建新的 Zetta 项目、启动 Zetta 服务器以及调用 Zetta API。只需几分钟!
您可以通过从 GitHub 克隆或使用 NPM 来安装 Zetta:
从 GitHub 克隆
git clone git@github.com:zettajs/zetta.git && cd zetta
npm install
NPM
npm install zetta
在Zetta Doc wiki上,您将找到概念概述、入门教程、API 参考和其他主题,以帮助您构建 Zetta 项目。
Ctrl-c
以停止它。hello-zetta
目录中,使用 NPM 安装模拟驱动程序:npm install zetta-led-mock-driver --save
注意:按照惯例,Zetta 设备驱动程序名称遵循模式 zetta-[device name]-[platform name]-driver
。在此示例中,我们在“模拟”平台上控制 LED 设备。例如,从 Intel Edison 开发板控制 LED 的驱动程序可能称为 zetta-led-edison-driver
.index.js
在文本编辑器中打开。var LED = require('zetta-led-mock-driver');
use
语句添加到第 6 行:.use(LED)
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');
});
node index.js
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
curl http://127.0.0.1:1337
/servers/Hello%20Zetta
URL 并调用它:curl http://localhost:1337/servers/Hello%20Zetta
properties
JSON 响应的属性中)是off
。{
"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"
}
]
}
http://127.0.0.1:1337/servers/Hello%20Zetta/devices/a3bbdd2d-67fe-4fad-97b2-3851f18aae7e
off
可用的操作是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"
}
]
}
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"
}
]
}
]
curl -i -X POST -H http://127.0.0.1:1337/servers/Hello%20Zetta/devices/e6f5b480-e96e-4fdc-8718-91aeb0234c99 -d 'action=turn-on'
提示:要从 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
在响应中,您可以看到状态已从 更改off
为on
。此外,可用的操作更改为turn-off
。要再次关闭灯,请按照相同的模式发布到转换 URL。
{
"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"
}
]
}
Zetta Doc Wiki是您可以找到 Zetta 的所有文档的地方,包括概述、教程和参考内容。
您还可以访问 Zetta 站点http://zettajs.org/项目配方、活动信息、社区链接等。