Page 12 of 14

Re: Find My iPhone implementation in LUA script

Posted: Tuesday 20 February 2018 16:55
by dens1975
Thanks landaisbenj but do you have an example how to add a condition?

I now have:

Code: Select all

command = "curl -s https://maps.googleapis.com/maps/api/geocode/json?latlng=" .. latitude .. "," .. longitude .. "&sensor=false"

Re: Find My iPhone implementation in LUA script

Posted: Tuesday 20 February 2018 18:04
by numanx
I've added print('The iPhone check script is running') right after the if statement.
Right now the interval set to 25 and here is the log output:

2018-02-20 18:00:15.317 dzVents: The iPhone check script is running
2018-02-20 18:00:22.652 dzVents: The iPhone check script is running
2018-02-20 18:00:29.354 dzVents: The iPhone check script is running
2018-02-20 18:00:32.940 dzVents: The iPhone check script is running
2018-02-20 18:00:36.976 dzVents: The iPhone check script is running
2018-02-20 18:00:44.629 dzVents: The iPhone check script is running
2018-02-20 18:00:52.070 dzVents: The iPhone check script is running
2018-02-20 18:00:55.461 dzVents: The iPhone check script is running
2018-02-20 18:00:59.058 dzVents: The iPhone check script is running
2018-02-20 18:25:00.169 dzVents: The iPhone check script is running
2018-02-20 18:25:04.156 dzVents: The iPhone check script is running
2018-02-20 18:25:07.449 dzVents: The iPhone check script is running
2018-02-20 18:25:14.613 dzVents: The iPhone check script is running
2018-02-20 18:25:22.316 dzVents: The iPhone check script is running
2018-02-20 18:25:25.731 dzVents: The iPhone check script is running
2018-02-20 18:25:32.835 dzVents: The iPhone check script is running
2018-02-20 18:25:36.534 dzVents: The iPhone check script is running
2018-02-20 18:25:43.779 dzVents: The iPhone check script is running
2018-02-20 18:25:47.470 dzVents: The iPhone check script is running
2018-02-20 18:25:51.075 dzVents: The iPhone check script is running
2018-02-20 18:25:54.718 dzVents: The iPhone check script is running

Re: Find My iPhone implementation in LUA script

Posted: Tuesday 20 February 2018 18:41
by landaisbenj
dens1975 wrote: Tuesday 20 February 2018 16:55 Thanks landaisbenj but do you have an example how to add a condition?

I now have:

Code: Select all

command = "curl -s https://maps.googleapis.com/maps/api/geocode/json?latlng=" .. latitude .. "," .. longitude .. "&sensor=false"
I have remade this script but I don't have finish. I will post here when I do.
I post gps in uservariable to get last position. On second pass I read this position, compare with present position and if difference is upper than radius, I have movement. I post in position..user just "movement". And if difference is under radius, and if position..user equal movement I do google request and I post it in position..user. It's ok?
numanx wrote: Tuesday 20 February 2018 18:04 I've added print('The iPhone check script is running') right after the if statement.
Right now the interval set to 25 and here is the log output:

2018-02-20 18:00:15.317 dzVents: The iPhone check script is running
2018-02-20 18:00:22.652 dzVents: The iPhone check script is running
2018-02-20 18:00:29.354 dzVents: The iPhone check script is running
2018-02-20 18:00:32.940 dzVents: The iPhone check script is running
2018-02-20 18:00:36.976 dzVents: The iPhone check script is running
2018-02-20 18:00:44.629 dzVents: The iPhone check script is running
2018-02-20 18:00:52.070 dzVents: The iPhone check script is running
2018-02-20 18:00:55.461 dzVents: The iPhone check script is running
2018-02-20 18:00:59.058 dzVents: The iPhone check script is running
2018-02-20 18:25:00.169 dzVents: The iPhone check script is running
2018-02-20 18:25:04.156 dzVents: The iPhone check script is running
2018-02-20 18:25:07.449 dzVents: The iPhone check script is running
2018-02-20 18:25:14.613 dzVents: The iPhone check script is running
2018-02-20 18:25:22.316 dzVents: The iPhone check script is running
2018-02-20 18:25:25.731 dzVents: The iPhone check script is running
2018-02-20 18:25:32.835 dzVents: The iPhone check script is running
2018-02-20 18:25:36.534 dzVents: The iPhone check script is running
2018-02-20 18:25:43.779 dzVents: The iPhone check script is running
2018-02-20 18:25:47.470 dzVents: The iPhone check script is running
2018-02-20 18:25:51.075 dzVents: The iPhone check script is running
2018-02-20 18:25:54.718 dzVents: The iPhone check script is running
Here you have a script for all. Device time and variable. So ever a change appear in that 3 stuff you do the script. Make this script has time.

Re: Find My iPhone implementation in LUA script

Posted: Tuesday 20 February 2018 18:42
by landaisbenj
landaisbenj wrote: Tuesday 20 February 2018 18:41
dens1975 wrote: Tuesday 20 February 2018 16:55 Thanks landaisbenj but do you have an example how to add a condition?

I now have:

Code: Select all

command = "curl -s https://maps.googleapis.com/maps/api/geocode/json?latlng=" .. latitude .. "," .. longitude .. "&sensor=false"
I have remade this script but I don't have finish. I will post here when I do.
I post gps in uservariable to get last position. On second pass I read this position, compare with present position and if difference is upper than radius, I have movement. I post in position..user just "movement". And if difference is under radius, and if position..user equal movement I do google request and I post it in position..user. It's ok?
numanx wrote: Tuesday 20 February 2018 18:04 I've added print('The iPhone check script is running') right after the if statement.
Right now the interval set to 25 and here is the log output:

2018-02-20 18:00:15.317 dzVents: The iPhone check script is running
2018-02-20 18:00:22.652 dzVents: The iPhone check script is running
2018-02-20 18:00:29.354 dzVents: The iPhone check script is running
2018-02-20 18:00:32.940 dzVents: The iPhone check script is running
2018-02-20 18:00:36.976 dzVents: The iPhone check script is running
2018-02-20 18:00:44.629 dzVents: The iPhone check script is running
2018-02-20 18:00:52.070 dzVents: The iPhone check script is running
2018-02-20 18:00:55.461 dzVents: The iPhone check script is running
2018-02-20 18:00:59.058 dzVents: The iPhone check script is running
2018-02-20 18:25:00.169 dzVents: The iPhone check script is running
2018-02-20 18:25:04.156 dzVents: The iPhone check script is running
2018-02-20 18:25:07.449 dzVents: The iPhone check script is running
2018-02-20 18:25:14.613 dzVents: The iPhone check script is running
2018-02-20 18:25:22.316 dzVents: The iPhone check script is running
2018-02-20 18:25:25.731 dzVents: The iPhone check script is running
2018-02-20 18:25:32.835 dzVents: The iPhone check script is running
2018-02-20 18:25:36.534 dzVents: The iPhone check script is running
2018-02-20 18:25:43.779 dzVents: The iPhone check script is running
2018-02-20 18:25:47.470 dzVents: The iPhone check script is running
2018-02-20 18:25:51.075 dzVents: The iPhone check script is running
2018-02-20 18:25:54.718 dzVents: The iPhone check script is running
Here you have a script for all. Device time and variable. So ever a change appear in that 3 stuff you do the script. Make this script as time.

Re: Find My iPhone implementation in LUA script

Posted: Tuesday 20 February 2018 18:44
by landaisbenj
dens1975 wrote: Tuesday 20 February 2018 16:55 Thanks landaisbenj but do you have an example how to add a condition?

I now have:

Code: Select all

command = "curl -s https://maps.googleapis.com/maps/api/geocode/json?latlng=" .. latitude .. "," .. longitude .. "&sensor=false"
Make this:

Code: Select all

command = "curl -s https://maps.googleapis.com/maps/api/geocode/json?latlng=" .. latitude .. "," .. longitude .. "&sensor=false&key=googlekey"
I have remade this script but I don't have finish. I will post here when I do.
I post gps in uservariable to get last position. On second pass I read this position, compare with present position and if difference is upper than radius, I have movement. I post in position..user just "movement". And if difference is under radius, and if position..user equal movement I do google request and I post it in position..user. It's ok?
numanx wrote: Tuesday 20 February 2018 18:04 I've added print('The iPhone check script is running') right after the if statement.
Right now the interval set to 25 and here is the log output:
Here you have a script for all. Device time and variable. So ever a change appear in that 3 stuff you do the script. Make this script as time.

Re: Find My iPhone implementation in LUA script

Posted: Tuesday 20 February 2018 20:14
by dens1975
landaisbenj wrote: Tuesday 20 February 2018 18:44
dens1975 wrote: Tuesday 20 February 2018 16:55 Thanks landaisbenj but do you have an example how to add a condition?

I now have:

Code: Select all

command = "curl -s https://maps.googleapis.com/maps/api/geocode/json?latlng=" .. latitude .. "," .. longitude .. "&sensor=false"
Make this:

Code: Select all

command = "curl -s https://maps.googleapis.com/maps/api/geocode/json?latlng=" .. latitude .. "," .. longitude .. "&sensor=false&key=googlekey"
I have remade this script but I don't have finish. I will post here when I do.
I post gps in uservariable to get last position. On second pass I read this position, compare with present position and if difference is upper than radius, I have movement. I post in position..user just "movement". And if difference is under radius, and if position..user equal movement I do google request and I post it in position..user. It's ok?
Thanks, I Added the API key but that doesn't make a difference. :(

Re: Find My iPhone implementation in LUA script

Posted: Tuesday 20 February 2018 22:32
by landaisbenj
What is your url now ?

Re: Find My iPhone implementation in LUA script

Posted: Wednesday 21 February 2018 9:08
by dens1975
landaisbenj wrote: Tuesday 20 February 2018 22:32 What is your url now ?
I now have:
xxxxxx are where my API key is.

Code: Select all

   command = "curl -s https://maps.googleapis.com/maps/api/geocode/json?latlng=" .. latitude .. "," .. longitude .. "&sensor=false&key=xxxxxx"

Re: Find My iPhone implementation in LUA script

Posted: Wednesday 21 February 2018 10:39
by landaisbenj
Ok.

I realized i'had the same problem.

In google api it says:

Free until 2 500 request by day and under 50 by seconds.... I don't think we are above.

Maybe it's curl parameter.

Re: Find My iPhone implementation in LUA script

Posted: Wednesday 21 February 2018 12:18
by dens1975
landaisbenj wrote: Wednesday 21 February 2018 10:39 Ok.

I realized i'had the same problem.

In google api it says:

Free until 2 500 request by day and under 50 by seconds.... I don't think we are above.

Maybe it's curl parameter.
I don't see any traffic on my api if I look at my Google dashboard, so I think it's not using it.

Re: Find My iPhone implementation in LUA script

Posted: Wednesday 21 February 2018 12:37
by landaisbenj
:shock: yes me too :| I Check more if I can this weekend.

I have been finish my script. I post it in same time. With it, i don't have problem with curl error.

Re: Find My iPhone implementation in LUA script

Posted: Thursday 22 February 2018 13:42
by landaisbenj
landaisbenj wrote: Wednesday 21 February 2018 12:37 :shock: yes me too :| I Check more if I can this weekend.

I have been finish my script. I post it in same time. With it, i don't have problem with curl error.
My bad it's filter problem

Re: Find My iPhone implementation in LUA script

Posted: Sunday 18 March 2018 18:47
by GuidoP18
The scrip is running inconsistent. Sometimes i get the distance reported to the log and changed to the dummy devices. And sometimes i get this code

Code: Select all

 2018-03-18 18:40:01.629 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_checkphones.lua: /home/pi/domoticz/scripts/lua/script_time_checkphones.lua:29: attempt to index field '?' (a nil value) 
In the script i can't see anything strange in line 29.

Re: Find My iPhone implementation in LUA script

Posted: Wednesday 21 March 2018 20:26
by uronito
For work correctly with api key this is the command:

Code: Select all

command = "curl -s --insecure -H 'Content-Type: application/json' -H 'https://maps.googleapis.com/maps/api/geocode/json?key={API_KEY}&latlng=" .. latitude .. "," .. longitude .."'"
With this, uses the apikey and works alwways. Is Consistent

Regards!!!!!!!

Re: Find My iPhone implementation in LUA script

Posted: Thursday 22 March 2018 11:21
by landaisbenj
It's like to be good for me! It's cool.

But you make a mistake with -H before url. It's:

Code: Select all

"curl -s --insecure -H 'Content-Type: application/json'  'https://maps.googleapis.com/maps/api/geocode/json?key={API_KEY}&latlng=" .. latitude .. "," .. longitude .."'"
But i still not have my dev board update with quotas

Re: Find My iPhone implementation in LUA script

Posted: Thursday 22 March 2018 15:30
by dens1975
Thanks! This was exactly what I needed. Now it's working!

I only get the following error::

Code: Select all

Error: EventSystem: Warning!, lua script /usr/local/domoticz/var/scripts/lua/script_time_checkphones.lua has been running for more than 10 seconds
Any idea what to do about this error. It looks like a warning but I would like to get rid of it.

Re: Find My iPhone implementation in LUA script

Posted: Friday 03 August 2018 16:34
by Irmin
Hello,

I need some help. I'v been using this script for the past months without any issue. 2 Weeks ago the script stopped working and I have no idea why.

This is the script:

Code: Select all

commandArray = {}
interval = 9
local m = os.date('%M')
if (m % interval == 0) then

  json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()

  -- Array of users to be checked
  users = {
            Iphone 1 = {username = "Email" ; password = "password" ; devicename = "Iphone 1"};
            Iphone 2 = {username = "Email" ; password = "password" ; devicename = "Iphone 2"};
          }
  -- The latitude and longitude of your house (use Google Maps or similar to find this)
  homelongitude = X.XXXXXX
  homelatitude = XX.XXXXXX
  -- Radius (in km) which will be used to determine if a device is at home
  radius = 1.0

  function address(longitude, latitude)
    command = "curl -s --insecure -H 'Content-Type: application/json'  'https://maps.googleapis.com/maps/api/geocode/json?key=APIKEY&latlng=" .. latitude .. "," .. longitude .."'"
	local handle = io.popen(command)
    local result = handle:read("*a")
    handle:close()
    output = json:decode(result)
    return output.results[1].formatted_address
  end
  
  for user,credentials in pairs(users) do
    stage1command = "curl -s -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 = "fmipmobile.icloud.com"
    stage2command = "curl -s -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
		battstat = value.batteryStatus
		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
        position = address(lon,lat)
        position_text = string.gsub(position, ', Netherlands', '') .. ' (' .. (math.floor(distance*10+0.5)/10) .. ' km)'
        --prev_distance_str = string.match(otherdevices['Position ' .. user], '%(.*%)') or '(1000 km)'
        --prev_distance_str = string.match(otherdevices_svalues['Position ' .. user], '%(.*%)') or '(1000 km)'
		prev_distance_str = string.match(otherdevices_idx['Position ' .. user], '%(.*%)') or '(1000 km)'
		print('iPhone ' .. user .. ' has '.. bat .. ' percent battery.')
		print('iPhone ' .. user .. ' is at: ' .. position)
		print('iPhone ' .. user .. ' is ' .. battstat)
		prev_distance = tonumber(string.sub(prev_distance_str, 2,-5))
		if prev_distance == nil then prev_distance = 1000 end
        --update text device, but only if postion has changed more than defined in "radius" to reduce log size
        if math.abs(prev_distance - distance) > radius then  
          table.insert(commandArray,{['UpdateDevice'] = otherdevices_idx['Position ' .. user] .. '|0|' .. position_text})
        end
		if bat < 2  then
		  if otherdevices['iPhone off ' .. user] == 'Off' then
		    commandArray['iPhone off ' .. user] = 'On'
		  end
		else
		  if otherdevices['iPhone off ' .. user] == 'On' then
		    commandArray['iPhone off ' .. user] = 'Off'
		  end
		end
        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 .. ' came home'})
          end
        else
          if otherdevices['iPhone ' .. user] == 'On' then
            commandArray['iPhone ' .. user] = 'Off'
            --table.insert(commandArray, {['SendNotification'] = 'Presence update#' .. user .. ' left home'})
          end
        end
      end
    end
  end

end

return commandArray
This is the error in the log:

Code: Select all

2018-08-03 16:27:00.858 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_checkphones.lua: /home/pi/domoticz/scripts/lua/script_time_checkphones.lua:45: attempt to index global 'output' (a nil value)
Help is welcome! :)

Re: Find My iPhone implementation in LUA script

Posted: Friday 03 August 2018 18:15
by landaisbenj
It's icloud request. You must to view what is the return of:

Code: Select all

    stage1command = "curl -s -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 = "fmipmobile.icloud.com"
    stage2command = "curl -s -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)
You need to try with curl.

Re: Find My iPhone implementation in LUA script

Posted: Friday 03 August 2018 18:52
by Irmin
Thanks for your reply but I dont really know what you mean.
Could you explain what I need to do? :)

Re: Find My iPhone implementation in LUA script

Posted: Friday 03 August 2018 19:00
by landaisbenj
This script make a request to icloud to get your iphone information.

It's like if you connect your browser to an url and get a page on icloud. But all is in text.

This:

Code: Select all

 stage1command = "curl -s -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"
    
is to make the url request.
"curl" is an application (like browser) who can send an url and get the answer.
"credentials.username" and "credentials.password" are your login account so all three together can make a first url.
This:

Code: Select all

    local handle = io.popen(stage1command)
    local result = handle:read("*a")
    handle:close()
In first "local handle = io.popen" send in 'handle' l'input/output of open the once url above (with curl apps)
result read all the return of the site (icloud)
and handle:close make a finnish of io.popen.

And this is make twice with another api url of icloud.

All together return the result in output. So you must to try with app curl the same url in another script or in browser.

I can make a script but not now.