查找我的iPhone获取指定ios设备位置,电量,电池状态等信息

文档:
https://en.domoticz.cn/wiki/DzVents:_ne ... _scripting
回复
AlvinWu
帖子: 1
注册时间: 周日 5月 28, 2017 22:44

查找我的iPhone获取指定ios设备位置,电量,电池状态等信息

帖子 AlvinWu »

照着达达群主改的,Domoticz里不显示数据, 帮忙看看
:D :D :D :D
commandArray = {}
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
d_interval = 15
dcharging_interval = 5
dle20_interval = 30
dle10_interval = 60
local m = os.date('%M')

amapkey='9e35a0efc87f11c20f0e27aa8526e274'
baiduAk='ZYXgvWGzqo8plmKZsFhYkasFNblV3sGC'
users = {
wsy = {username = 'xxx' ;
password = 'xxx' ;
devicename = 'iPhone7 plus' ;
homelongitude = 117 ;
homelatitude = 34 ;
cityname = "徐州市" ;
interval = 15 ;
charging_interval = 5 ;
le20_interval = 30 ;
le10_interval = 60};

}
radius = 0.15


function getdevname4idx(deviceIDX)
for i, v in pairs(otherdevices_idx) do
if v == deviceIDX then
return i
end
end
return 0
end


function fmipinfo(credentials)
command = "curl -s -X POST -L -u '" .. credentials.username .. ":" .. credentials.password .. "' -H 'Content-Type: application/json; charset=utf-8' -H 'X-Apple-Find-Api-Ver: 3.0' -H 'X-Apple-Authscheme: UserIdGuest' -H 'X-Apple-Realm-Support: 1.0' -H 'User-agent: FindMyiPhone/500 CFNetwork/758.4.3 Darwin/15.5.0' -H 'X-Client-Name: iPad' -H 'X-Client-UUID: d98c8ae0db3311e687b92890643032df' -H 'Accept-Language: en-us' -H 'Connection: keep-alive' https://fmipmobile.icloud.com/fmipservice/device/" .. credentials.username .."/initClient"
local handle = io.popen(command)
local result = handle:read("*a")
handle:close()
output = json:decode(result)
return output
end


function address(longitude, latitude)
command1 = "curl -s 'http://restapi.amap.com/v3/assistant/co ... nvert?key=" .. amapkey .. "&coordsys=gps&locations=" .. longitude .. "," .. latitude .. "&output=json'"
local handle = io.popen(command1)
local result = handle:read("*a")
handle:close()
output1= json:decode(result)
lonlat=output1.locations
dh=string.find(lonlat,',')
lenlonlat=string.len(lonlat)
lon=string.sub(lonlat,1,(dh-1))
lat=string.sub(lonlat,(dh+1),lenlonlat)
command2 = "curl -s 'restapi.amap.com/v3/geocode/regeo?key=" .. amapkey .. "&location=" .. lon .. "," .. lat .. "&output=json'"
local handle = io.popen(command2)
local result = handle:read("*a")
handle:close()
output2 = json:decode(result)
addr = output2.regeocode.formatted_address
if (type(addr)=="table") then
command3 = "curl -s 'http://api.map.baidu.com/geocoder/v2/?c ... gs84ll&ak=" .. baiduAk .. "&coordsys=gps&location=" .. latitude .. "," .. longitude .. "&output=json'"
local handle = io.popen(command3)
local result = handle:read("*a")
handle:close()
result = string.gsub(result,"renderReverse&&renderReverse%(","")
result = string.gsub(result,"%)","")
output3= json:decode(result)
addr = output3.result.addressComponent.country ..','.. output3.result.addressComponent.province ..','.. output3.result.addressComponent.city ..','.. output3.result.addressComponent.district ..','.. output3.result.addressComponent.street
end
return addr
end


function shortenurl(text)
commandsu = "curl -s 'http://tinyurl.com/api-create.php?url=" .. text .. "'"
local handle = io.popen(commandsu)
local result = handle:read("*a")
handle:close()
short_url = result
return result
end

function updateinfo(user,credentials)
info=fmipinfo(credentials)
for key,value in pairs(info.content) do
if value.name == credentials.devicename then
lon = value.location.longitude
lat = value.location.latitude
bat = value.batteryLevel * 100 / 1
powerstateval = value.batteryStatus
timestamp = math.floor(value.location.timeStamp/1000)
timedelta = os.difftime(os.time(),timestamp)
print (lon)
print (lat)

powerstatus = '未知'
if powerstateval == 'NotCharging' then
powerstatus = '使用中'
elseif (powerstateval == 'Charging') then
powerstatus = '充电中'
elseif (powerstateval == 'Charged') then
powerstatus = '已充满'
end


if (timedelta >=3600) then
fixedtime = '一个小时前'
elseif (timedelta>=60) then
minutes = math.floor(timedelta/60)
seconds = timedelta % 60
fixedtime = minutes .. '分' .. seconds .. '秒前'
elseif (timedelta<60) then
seconds = timedelta %60
fixedtime = seconds .. '秒前'
end

distance = math.sqrt(((lon - credentials.homelongitude) * 111.320 * math.cos(math.rad(lat)))^2 + ((lat - credentials.homelatitude) * 110.547)^2)
distance = math.floor((distance*1000+0.5)/1000)
position = address(lon,lat)
position=string.gsub(position,credentials.cityname,"")
if (string.find(position, "区")~=nil) then
long_url = 'http://uri.amap.com/marker?position='..lon..','..lat
short_url=shortenurl(long_url)
position_text = '<a style="color:black" target="blank" href="'..short_url..'">'..position..'('..fixedtime..')</a><br>电池状态:' .. powerstatus.. '</br>'
long_pic='http://restapi.amap.com/v3/staticmap?ma ... 30&zoom=14'
short_pic = shortenurl(long_pic)
location_pic = '<iframe width="300" height="130" frameborder="0" style="border:0" src="'..short_pic..'" allowfullscreen></iframe>'
print (location_pic)
table.insert(commandArray,{['UpdateDevice'] = otherdevices_idx[user .. '位于'] .. '|0|' .. location_pic})
else
long_url = 'http://api.map.baidu.com/marker?title=' ... ..','..lon
short_url=shortenurl(long_url)

position_text = '<a style="color:black" target="blank" href="'..short_url..'">'..position..'('..fixedtime..')</a><br>电池状态:' .. powerstatus.. '</br>'
long_pic='http://api.map.baidu.com/staticimage/v2 ... height=130'
short_pic = shortenurl(long_pic)
location_pic = '<iframe width="300" height="130" frameborder="0" style="border:0" src="'..short_pic..'" allowfullscreen></iframe>'
print (location_pic)
table.insert(commandArray,{['UpdateDevice'] = otherdevices_idx[user .. '位于'] .. '|0|' .. location_pic})
end
table.insert(commandArray,{['UpdateDevice'] = otherdevices_idx[user .. '的位置'] .. '|0|' .. position_text})
table.insert(commandArray,{['UpdateDevice'] = otherdevices_idx[user .. '的iPhone离家'] .. '|0|' .. distance})
if distance < radius then
if otherdevices[user .. '的iPhone'] == 'Off' then
commandArray[user .. '的iPhone'] = 'On'
table.insert(commandArray, {['SendNotification'] = '位置更新#' .. user .. '到家了'})
end
else
if otherdevices[user .. '的iPhone'] == 'On' then
commandArray[user .. '的iPhone'] = 'Off'
table.insert(commandArray, {['SendNotification'] = '位置更新#' .. user .. '离家了'})
end
end
if (powerstatus~='未知') then
table.insert(commandArray,{['UpdateDevice'] = otherdevices_idx[user .. '的iPhone电量'] .. '|0|' .. bat})
end

if (credentials.interval==nil) then
interval = d_interval
charging_interval = dcharging_interval
le20_interval = dle20_interval
le10_interval = dle10_interval
else
interval = credentials.interval
charging_interval = credentials.charging_interval
le20_interval = credentials.le20_interval
le10_interval = credentials.le10_interval
end

commandArray['Variable:'..user..'_interval']=tostring(interval)
if (powerstateval == 'Charging' or powerstateval == 'Charged') then
commandArray['Variable:'..user..'_interval']=tostring(charging_interval)
else
if (bat==0) then
commandArray['Variable:'..user..'_interval']='1'
elseif (bat<=10) then
commandArray['Variable:'..user..'_interval']=tostring(le10_interval)
elseif (bat<=20) then
commandArray['Variable:'..user..'_interval']=tostring(le20_interval)
end
end
if (uservariables[user..'_set_interval'] ~= 0) then
commandArray['Variable:'..user..'_interval'] = tostring(uservariables[user..'_set_interval'])
end

print(position_text)
print('查找我的iPhone: ' .. user .. '在' .. position .. '于' .. fixedtime .. ',电池状态:' .. bat .. '%,' .. powerstatus)
end
end
end


for user,credentials in pairs(users) do
while true do
if (m == 0) then m=60 end
interval = uservariables[user..'_interval']

if ( interval > 5) then
if ((m+1) % interval ==0) then
info=fmipinfo(credentials)
print ('刷新查找我的iPhone服务')
end
end
if (m % interval ~=0) then break end
updateinfo(user,credentials)
break
end
end

return commandArray
blindlight
帖子: 98
注册时间: 周四 3月 30, 2017 00:03

Re: 查找我的iPhone获取指定ios设备位置,电量,电池状态等信息

帖子 blindlight »

代码: 全选

commandArray = {}
return commandArray
用这种,该缩进缩进 否则什么都看不清楚
回复