Is er iemand thuis voor iPhone bezitters

Gestart door Dickey, 27 maart 2021, 13:57:05

Vorige topic - Volgende topic

Dickey

Bij het zoeken naar bruikbare scripts kwam ik deze tegen, die aan de hand van de locatie van een iPhone kan bepalen of er iemand thuis is of niet. Voorwaarde is wel dat alle huisgenoten over een iPhone beschikken.

Op regel 5 kun je de interval invullen. Let wel op: 5 minuten is redelijk, lager gaat ten koste van je iPhone batterij. -:)
Vul de Apple account gegevens in op regel 13. Device naam moet exact overeenkomen met de naam van je toestel.

Op regel 17 de long. en latitude opgeven. Klik even op google maps om je thuis locatie te vinden.
De radius heb ik op 0,5 staan (500 meter), dit voldoet prima.

Voeg een dummy device toe voor elke telefoon die je wilt volgen.
Optioneel kun je een extra device toevoegen, die checkt of alle telefoons weg zijn waarna vervolgens de door jou gewenste actie kan worden uitgevoerd.


-- 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 = 5
local m = os.date('%M')
if (m % interval == 0) then

  json = require('json')

  -- Array of users to be checked
  users = {
            Name1 = {username = "Name1@gmail.com" ; password = "*********" ; devicename = "iPhone Name1"};
            Name2 = {username = "Name2@gmail.com" ; password = "*********" ; devicename = "iPhone Name2"};
          }
  -- The latitude and longitude of your house (use Google Maps or similar to find this)
  homelongitude = *.*****
  homelatitude = *.*****
  -- 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 = "p43-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['Positie ' .. 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
  [/font][/size]