first commit
This commit is contained in:
135
lua/iphone.luaTMP
Executable file
135
lua/iphone.luaTMP
Executable file
@@ -0,0 +1,135 @@
|
||||
-- ==============================
|
||||
-- Cherche la position iphone sur icloud
|
||||
-- ==============================
|
||||
|
||||
-- Variables
|
||||
|
||||
-- 1) User et mot de passe icloud
|
||||
username = "votre.login@icloud.com"
|
||||
password = "votre mot de passe icloud"
|
||||
|
||||
-- 2) Pour chaque iPhone déclaré : associer le nom du device de présence
|
||||
devices = {
|
||||
["iPhone de Georges-David"] = "Presence GD"
|
||||
-- , ["iPhone de Jean-Michel"] = "Présence JM"
|
||||
-- , ["iPhone de Pierre-André"] = "Présence PA"
|
||||
}
|
||||
|
||||
-- 3) Pour chaque iPhone déclaré : associer adresse IP et le nom du device de présence
|
||||
ips= {
|
||||
["192.168.0.20"] = "moi"
|
||||
-- , ["xxx.xxx.xxx"] = "Présence JM"
|
||||
-- , ["xxx.xxx.xxx"] = "Présence PA"
|
||||
}
|
||||
|
||||
-- 4) Position de la maison, "lat, long" as given by google map
|
||||
coordonneesGoogleDeLaMaison="47.726695, -2.131950"
|
||||
--(43.693982097361, 0.00010012119960745)
|
||||
-- 5) Périmètre de considération de la maison
|
||||
-- NB : 0.001 = 0.111 kilometres soit 111 metres
|
||||
skew = 0.001
|
||||
|
||||
-- ==============================
|
||||
commandArray = {}
|
||||
|
||||
print('Script v01 script_time_localisation.lua')
|
||||
function debug(m)
|
||||
print("........ localisation "..m)
|
||||
end
|
||||
|
||||
function Allumer(item)
|
||||
if otherdevices[item] ~= 'On' then commandArray[item] = 'On' end
|
||||
end
|
||||
|
||||
function Eteindre(item)
|
||||
if otherdevices[item] ~= 'Off' then commandArray[item] = 'Off' end
|
||||
end
|
||||
|
||||
|
||||
-- Time to run?
|
||||
time = os.date("*t")
|
||||
|
||||
-- toutes les minutes, on allume si l'adresse IP répond au ping
|
||||
-- ce mécanisme n'est pas utilisé pour éteindre : contrairement
|
||||
-- à Android, l'IPhone éteint le wifi quand il est en veille.
|
||||
for ip, devicePresence in pairs(ips) do
|
||||
-- local status = os.execute("ping -W 1 -q -c 1 "..ip) !! pas bon, comme as dit papoo
|
||||
local status = os.execute("ping -W 1 -q -c 4 "..ip)
|
||||
if status == true then
|
||||
Allumer(devicePresence)
|
||||
end
|
||||
end
|
||||
|
||||
-- On vérifie toute les 10 minutes
|
||||
if time.min % 10 == 2 then
|
||||
--- Faire un premier appel pour recherche la position
|
||||
urlCloud1 = ("curl -s -X POST -D - -o /dev/null -L -u '" .. username .. ":" .. password .. "' "
|
||||
.. "-H 'Content-Type: application/json; charset=utf-8' "
|
||||
.. "-H 'X-Apple-Find-Api-Ver: 2.0' "
|
||||
.. "-H 'X-Apple-Authscheme: UserIdGuest' "
|
||||
.. "-H 'X-Apple-Realm-Support: 1.0' "
|
||||
.. "-H 'User-agent: Find iPhone/1.3 MeKit (iPad: iPhone OS/4.2.1)' "
|
||||
.. "-H 'X-Client-Name: iPad' "
|
||||
.. "-H 'X-Client-UUID: 0cf3dc501ff812adb0b202baed4f37274b210853' "
|
||||
.. "-H 'Accept-Language: en-us' "
|
||||
.. "-H 'Connection: keep-alive' https://fmipmobile.icloud.com/fmipservice/device/"
|
||||
.. username .."/initClient")
|
||||
-- debug("urlCloud1="..urlCloud1)
|
||||
local handle = io.popen(urlCloud1)
|
||||
local result = handle:read("*all")
|
||||
handle:close()
|
||||
|
||||
-- Récupération de la position dans le resultat de la premiere URL
|
||||
stageServer = string.match(result, "X%-Apple%-MMe%-Host%:%s(.*%.icloud%.com)")
|
||||
|
||||
-- Faire la deuxième url en cherchant le nom du tel / ipad ou autre avec jq
|
||||
urlCloud2 = ("curl -s -X POST -L -u '" .. username .. ":" .. password .. "' "
|
||||
.. "-H 'Content-Type: application/json; charset=utf-8' "
|
||||
.. "-H 'X-Apple-Find-Api-Ver: 2.0' "
|
||||
.. "-H 'X-Apple-Authscheme: UserIdGuest' "
|
||||
.. "-H 'X-Apple-Realm-Support: 1.0' "
|
||||
.. "-H 'User-agent: Find iPhone/1.3 MeKit (iPad: iPhone OS/4.2.1)' "
|
||||
.. "-H 'X-Client-Name: iPad' -H 'X-Client-UUID: 0cf3dc501ff812adb0b202baed4f37274b210853' "
|
||||
.. "-H 'Accept-Language: en-us' "
|
||||
.. "-H 'Connection: keep-alive' "
|
||||
.. "https://" .. stageServer .. "/fmipservice/device/" .. username .."/initClient ")
|
||||
|
||||
-- debug("urlCloud2="..urlCloud2)
|
||||
local handle = io.popen(urlCloud2)
|
||||
local result = handle:read("*all")
|
||||
|
||||
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
|
||||
local jsonPosition = json:decode(result)
|
||||
handle:close()
|
||||
|
||||
latMaison, lonMaison = string.match(coordonneesGoogleDeLaMaison, "(%d+.%d+), (%d+.%d+)")
|
||||
|
||||
for i, iPhone in ipairs(jsonPosition.content) do
|
||||
devicePresence=devices[iPhone.name]
|
||||
debug("iPhone.name="..iPhone.name.."; devicePresence="..devicePresence
|
||||
.."; iPh pos=("..iPhone.location.latitude..", "..iPhone.location.longitude..")")
|
||||
if devicePresence ~= nil then
|
||||
-- iPhone connu, on teste la position
|
||||
distance=math.sqrt((111*(latMaison-iPhone.location.latitude))^2
|
||||
+ (111*(lonMaison-iPhone.location.longitude))^2)
|
||||
debug("Distance="..distance
|
||||
..", TimeStamp="..iPhone.location.timeStamp
|
||||
..", deltaT="..math.floor(os.time()-iPhone.location.timeStamp/1000).."s")
|
||||
|
||||
ici = ((math.abs(latMaison-iPhone.location.latitude) <= skew)
|
||||
and (math.abs(lonMaison-iPhone.location.longitude) <= skew))
|
||||
|
||||
if ici then
|
||||
Allumer(devicePresence)
|
||||
debug(iPhone.name.." est ici!")
|
||||
else
|
||||
Eteindre(devicePresence)
|
||||
debug(iPhone.name.." est absent!")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--Fin du script
|
||||
return commandArray
|
||||
|
||||
Reference in New Issue
Block a user