从嵌入式系统启动Rest Streaming

时间:2014-07-25 18:35:53

标签: rest events streaming nest-api

我使用相当有限的嵌入式系统,因此我无法使用任何库,而且我自己构建了HTTP请求。我可以通过民意调查处理统计数据,但我试图启用Rest Streaming

Nest网站会将您引导至Firebase网站,该网站会将您引导至W3C网站,我所能完成的所有操作都包括标题:接受:请求中的text / event-stream' 。

如果我发送请求(重定向后):

GET /?auth=[auth code] HTTP/1.1
Host: firebase-apiserver02-tah01-iad01.dapi.production.nest.com:9553

我得到了JSON的完整结构。如果我发送:

GET /?auth=[auth code] HTTP/1.1
Host: firebase-apiserver02-tah01-iad01.dapi.production.nest.com:9553
Accept: text/event-stream
Connection: keep-alive

我得到200 OK回复,但没有别的。连接没有关闭,但没有任何后续。我在这里走在正确的轨道上吗?看起来这应该是一个更大的交易,它必须是因为我什么都没有,也没有任何线索下一步去哪里。

是否有人使用Rest Streaming with Nest(没有库)?

2 个答案:

答案 0 :(得分:1)

套接字保持活动状态,并通过它发送任何更新。它返回200 OK,包含整个数据,然后在发生更新时继续向您发送更新。这是curl的一个例子。需要注意的一点是,如果您更新恒温器的状态(通过网络或物理),您将获得更新。

< HTTP/1.1 200 OK
< Content-Type: text/event-stream; charset=UTF-8
< Access-Control-Allow-Origin: *
< Cache-Control: private, no-cache, max-age=0
<
event: put
data: {"path":"/","data":{"devices":{...}}

event: keep-alive
data: null

...

希望有所帮助

答案 1 :(得分:1)

audiotech, 看起来nest-firebase正在使用WebSockets将更新发送回客户端。 我在C#WindowsForms工作,但我发现对你有用。

我可以使用这个来获取结构信息:

    HttpWebRequest myStructHttpWebRequest=(HttpWebRequest)WebRequest.Create("https://developer-api.nest.com/structures/?auth=" + AccessToken);
myStructHttpWebRequest.Method = "GET";
myStructHttpWebRequest.MaximumAutomaticRedirections=3;
myStructHttpWebRequest.AllowAutoRedirect=true;
myStructHttpWebRequest.Accept = "text/event-stream; application/json";
myStructHttpWebRequest.Credentials = CredentialCache.DefaultCredentials;
myStructHttpWebRequest.KeepAlive = false;
using(HttpWebResponse myHttpWebResponse = (HttpWebResponse) myStructHttpWebRequest.GetResponseAsync())
{
    if (null != myHttpWebResponse)
    {
        // Store the response.
        Stream sData = myHttpWebResponse.GetResponseStream(); 
        StreamReader readStream = new StreamReader (sData, Encoding.UTF8);
        string data = readStream.ReadToEnd();
        readStream.Close();
        Debug.WriteLine("Response Structure stream received: " + data);
        success = deserializeStructure(data);
    }
}

我将上面的代码更改为:myStructHttpWebRequest.AllowAutoRedirect = false; 并获得重定向地址路径:

if (HttpStatusCode.TemporaryRedirect == myHttpWebResponse.StatusCode)
{
    string[] redirect = myHttpWebResponse.Headers.GetValues("Location");
    path = redirect[0];
}

我使用此函数启动与此新路径的Web套接字连接,并从firebase服务器接收此消息: &#34; {\&#34;吨\&#34;:\&#34; C ^ \&#34; \&#34; d \&#34;:{\&#34;吨\& #34;:\&#34; H \&#34; \&#34; d \&#34;:{\&#34; TS \&#34;:1406745104997,\&#34; v \ &#34;:\&#34; 5 \&#34; \&#34; H \&#34;:\&#34; firebase-apiserver01-tah01-i​​ad01.dapi.production.nest.com: 9553 \&#34; \&#34; S \&#34;:\&#34;会话819323581 \&#34;}}}&#34;

这是我的WebSocket代码:

        private async void startNestWebSocketConnection(string path)
    {
        try
        {
            listener = new ClientWebSocket();
            if (path.StartsWith("https:"))
            {
                path = "wss:" + path.Substring(6) + ":" + COMMUNICATION_PORT.ToString();
            }
            await listener.ConnectAsync(new Uri(path),CancellationToken.None);
            var buffer = new byte[1024 * 4];
            Debug.WriteLine("ClientWebSocket connected " + listener.State.ToString());
            while (true)
            {
                ArraySegment<byte> segment = new ArraySegment<byte>(buffer);

                WebSocketReceiveResult result = await listener.ReceiveAsync(segment, CancellationToken.None);

                if (result.MessageType == WebSocketMessageType.Close)
                {
                    Debug.WriteLine("ClientWebSocket CLOSING");
                    await listener.CloseAsync(WebSocketCloseStatus.InvalidMessageType, "Closing ClientWebSocket", CancellationToken.None);
                    return;
                }

                int count = result.Count;
                while (!result.EndOfMessage)
                {
                    if (count >= buffer.Length)
                    {
                        await listener.CloseAsync(WebSocketCloseStatus.InvalidPayloadData, "That's too long", CancellationToken.None);
                        return;
                    }

                    segment = new ArraySegment<byte>(buffer, count, buffer.Length - count);
                    result = await listener.ReceiveAsync(segment, CancellationToken.None);
                    count += result.Count;
                    Debug.WriteLine("ClientWebSocket getting " + count.ToString());
                }

                string message = Encoding.UTF8.GetString(buffer, 0, count);
                Debug.WriteLine(">" + message);
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine("startNestWebSocketConnection path=" + path + " Exception=" + ex.ToString());
        }
    }

我不知道答复意味着什么,但它显示了时间戳和会话ID!当我做更多测试时,我会让你知道更多。