Code: Select all
-- Script to check the location of multiple iPhones every X minutes,
-- test if they are "home" and represent this using virtual switches
commandArray = {}
-- polling interval in minutes (1-59), setting this too low may drain the phones' batteries
interval = 10
local m = os.date('%M')
if (m % interval == 0) then
json = require('json')
-- Array of users to be checked
users = {
Peter = {username = "xxxxxxxxx" ; password = "xxxxxxx" ; devicename = "xxxxxxxx"};
-- Person2 = {username = "[email protected]" ; password = "password2" ; devicename = "iPhone Person2"};
-- Person3 = {username = "[email protected]" ; password = "password3" ; devicename = "iPhone Person3"}
}
-- The latitude and longitude of your house (use Google Maps or similar to find this)
homelongitude = 0.000000
homelatitude = 00.00000
-- Radius (in km) which will be used to determine if a device is at home
radius = 0.5
for user,credentials in pairs(users) do
stage1command = "curl -sk -X POST -D - -o /dev/null -L -u '" .. credentials.username .. ":" .. credentials.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/" .. credentials.username .."/initClient"
local handle = io.popen(stage1command)
local result = handle:read("*a")
handle:close()
-- stage2server = string.match(result, "X%-Apple%-MMe%-Host%:%s(.*%.icloud%.com)")
stage2server = "fmipmobile.icloud.com"
stage2command = "curl -sk -X POST -L -u '" .. credentials.username .. ":" .. credentials.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://" .. stage2server .. "/fmipservice/device/" .. credentials.username .."/initClient"
local handle = io.popen(stage2command)
local result = handle:read("*a")
handle:close()
output = json:decode(result)
for key,value in pairs(output.content) do
if value.name == credentials.devicename then
lon = value.location.longitude
lat = value.location.latitude
bat = value.batteryLevel * 100 / 1
powerstateval = value.batteryStatus
table.insert(commandArray,{['UpdateDevice'] = otherdevices_idx['Iphone battery ' .. user] .. '|0|' .. bat})
distance = math.sqrt(((lon - homelongitude) * 111.320 * math.cos(math.rad(lat)))^2 + ((lat - homelatitude) * 110.547)^2) -- approximation
table.insert(commandArray,{['UpdateDevice'] = otherdevices_idx['Position ' .. user] .. '|0|' .. math.floor(distance*100+0.5)/100})
-- print('iPhone ' .. user .. ': ' .. math.floor(distance*100+0.5)/100 .. ' km from home')
if distance < radius then
if otherdevices['Iphone ' .. user] == 'Off' then
commandArray['Iphone ' .. user] = 'On'
-- table.insert(commandArray, {['SendNotification'] = 'Presence update#' .. user .. ' komt thuis'})
end
else
if otherdevices['Iphone ' .. user] == 'On' then
commandArray['Iphone ' .. user] = 'Off'
-- table.insert(commandArray, {['SendNotification'] = 'Presence update#' .. user .. ' gaat van huis'})
end
end
end
end
end
end
return commandArray