Commencé à tester la lecture d'un gros flux JSON. Le problème est que le JSON est plus grand que la taille de la RAM. Mais les tests sont bons
This commit is contained in:
80
JSON/a_sjson-streaming.lua
Normal file
80
JSON/a_sjson-streaming.lua
Normal file
@@ -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")
|
||||
|
||||
25
Meteo/README.md
Normal file
25
Meteo/README.md
Normal file
@@ -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
|
||||
9
Meteo/web_server.sh
Executable file
9
Meteo/web_server.sh
Executable file
@@ -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
|
||||
9
Wifi/credentials.lua
Normal file
9
Wifi/credentials.lua
Normal file
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user