From e835c94c19b1ce903c4c82242847e7d867ff696e Mon Sep 17 00:00:00 2001 From: Christian Zufferey Date: Sat, 2 Mar 2019 16:56:45 +0100 Subject: [PATCH] =?UTF-8?q?Commenc=C3=A9=20=C3=A0=20tester=20la=20lecture?= =?UTF-8?q?=20d'un=20gros=20flux=20JSON.=20Le=20probl=C3=A8me=20est=20que?= =?UTF-8?q?=20le=20JSON=20est=20plus=20grand=20que=20la=20taille=20de=20la?= =?UTF-8?q?=20RAM.=20Mais=20les=20tests=20sont=20bons?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JSON/a_sjson-streaming.lua | 80 ++++++++++++++++++++++++++++++++++++++ Meteo/README.md | 25 ++++++++++++ Meteo/web_server.sh | 9 +++++ Wifi/credentials.lua | 9 +++++ 4 files changed, 123 insertions(+) create mode 100644 JSON/a_sjson-streaming.lua create mode 100644 Meteo/README.md create mode 100755 Meteo/web_server.sh create mode 100644 Wifi/credentials.lua diff --git a/JSON/a_sjson-streaming.lua b/JSON/a_sjson-streaming.lua new file mode 100644 index 0000000..3cdeee7 --- /dev/null +++ b/JSON/a_sjson-streaming.lua @@ -0,0 +1,80 @@ +-- Script de test sjson and GitHub API. Permet de ne prendre que ce qui est +-- nécéssaire dans un grand flux JSON +-- affiche de https://api.github.com/repos/nodemcu/nodemcu-firmware/git/trees/master +-- le 4e et le 16e champ +-- Source: https://github.com/nodemcu/nodemcu-firmware/blob/master/lua_examples/sjson-streaming.lua + +print("\n sjson-streaming.lua zf190302.1642 \n") + +local s = tls.createConnection() +s:on("connection", function(sck, c) + sck:send("GET /repos/nodemcu/nodemcu-firmware/git/trees/master HTTP/1.0\r\nUser-agent: nodemcu/0.1\r\nHost: api.github.com\r\nConnection: close\r\nAccept: application/json\r\n\r\n") +end) + +function startswith(String, Start) + return string.sub(String, 1, string.len(Start)) == Start +end + +local seenBlank = false +local partial +local wantval = { tree = 1, path = 1, url = 1 } +-- Make an sjson decoder that only keeps certain fields +local decoder = sjson.decoder({ + metatable = + { + __newindex = function(t, k, v) + if wantval[k] or type(k) == "number" then + rawset(t, k, v) + end + end + } +}) +local function handledata(s) + decoder:write(s) +end + +-- The receive callback is somewhat gnarly as it has to deal with find the end of the header +-- and having the newline sequence split across packets +s:on("receive", function(sck, c) + if partial then + c = partial .. c + partial = nil + end + if seenBlank then + handledata(c) + return + end + while c do + if startswith(c, "\r\n") then + seenBlank = true + c = c:sub(3) + handledata(c) + return + end + local s, e = c:find("\r\n") + if s then + -- Throw away line + c = c:sub(e + 1) + else + partial = c + c = nil + end + end +end) + +local function getresult() + local result = decoder:result() + -- This gets the resulting decoded object with only the required fields +-- le 4e champs +print(result['tree'][4]['path'], "is at", result['tree'][4]['url']) +-- le 16e champs + print(result['tree'][16]['path'], "is at", result['tree'][16]['url']) + print(node.heap()) +end + +s:on("disconnection", getresult) +s:on("reconnection", getresult) + +-- Make it all happen! +s:connect(443, "api.github.com") + diff --git a/Meteo/README.md b/Meteo/README.md new file mode 100644 index 0000000..3d2c5a0 --- /dev/null +++ b/Meteo/README.md @@ -0,0 +1,25 @@ +# Récupération des prévisions météo sur un web service sur Internet + + +## Sources + +Pour Lausanne + +https://www.prevision-meteo.ch/services/json/lausanne + +##Documentation + +https://www.prevision-meteo.ch/services + +https://www.prevision-meteo.ch/uploads/pdf/recuperation-donnees-meteo.pdf + +## Simulateur +./web_server.sh + +Lecture sur le PC du simulateur pour tests: + +http://localhost:8080/meteo.lausanne.190302.1231.json + + + +zf190302.1655 diff --git a/Meteo/web_server.sh b/Meteo/web_server.sh new file mode 100755 index 0000000..b88ef7e --- /dev/null +++ b/Meteo/web_server.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +#Petit serveur WEB hyper simple +#zf190302.1640 + +#pour python 2.x +python -m SimpleHTTPServer 8080 + +#pour python 3.3 +#python3 -m http.server 8080 diff --git a/Wifi/credentials.lua b/Wifi/credentials.lua new file mode 100644 index 0000000..0efde32 --- /dev/null +++ b/Wifi/credentials.lua @@ -0,0 +1,9 @@ +-- Petit script pour configurer les choses secrètes que l'on n'aimerait +-- pas être exportées sur Internet (github) +-- faut donc le mettre ailleurs que dans le dépôt ! + +print("\n credential.lua zf181205.1910 \n") + +cli_ssid="3g-s7" +cli_pwd="12234567" +