function
sys.wait(15000)
local taskname="userTask"
log.info(taskname,"start")
local nid=1
local uid=1
local netsta =0
local needup =1
local needupall =1
local count =0
local count2 =0
local count3 =0
local shake=0
local thisshake=0
local lastshake=0
local acc=1
local run=0
local adc =0
local e ={}
local lbslng =0
local lbslat =0
local gpslng =0
local gpslat =0
local gpsisfix =0
GpsInit()
GpsExecAgnss()
e.adc=0
PronetStopProRecCh(1)
UartStopProRecCh(1) --如果串口数据还需要透传,需要删除
local EARTH_RADIUS = 6378.137
local function getDistance(lat1,lng1,lat2,lng2)
local radLat1 = lat1 * math.pi / 180.0
local radLat2 = lat2 * math.pi / 180.0
local a = radLat1 - radLat2
local b = lng1 * math.pi / 180.0 - lng2 * math.pi / 180.0
local s = 2 * math.asin(math.sqrt(math.pow(math.sin(a/2),2) + math.cos(radLat1)*math.cos(radLat2)*math.pow(math.sin(b/2),2)))
s = s * EARTH_RADIUS
return s*1000
end
local function calculateBearing(lat1, lng1, lat2, lng2)
local deltaLng = math.rad(lng2 - lng1)
lat1 = math.rad(lat1)
lat2 = math.rad(lat2)
local y = math.sin(deltaLng) * math.cos(lat2)
local x = math.cos(lat1) * math.sin(lat2) - math.sin(lat1) * math.cos(lat2) * math.cos(deltaLng)
local bearing = math.deg(math.atan2(y, x))
return (bearing + 360) % 360
end
while true do
--解析网络数据
netr = PronetGetRecChAndDel(nid)
if netr then
log.info(taskname,"netr data",netr)
local j =json.decode(netr)
if j and type(j)=="table" and j.cmd then
if j.cmd =="on1" then
PerSetDo(1,1)
elseif j.cmd =="off1" then
PerSetDo(1,0)
elseif j.cmd =="on2" then
PerSetDo(2,1)
elseif j.cmd =="off2" then
PerSetDo(2,0)
elseif j.cmd =="onoff2" then
PerSetDo(2,1)
sys.wait(1000)
PerSetDo(2,0)
elseif j.cmd =="dw" then --立即定位
needupall =1
end
local jb={}
jb.cmd =j.cmd
jb.res =1
jb.In1=PerGetDoSta(1)
jb.In2=PerGetDoSta(2)
jb.csq=mobile.csq()
local bck = json.encode(jb)
if j.cmd ~="dw" and bck and 1==PronetGetNetSta(nid) then
PronetSetSendCh(nid,bck)
end
local jb={}
end
end
local d ={}
if needupall ==1 then
d.t=os.date("%Y-%m-%d %H:%M:%S")
d.csq=mobile.csq()
d.In1=PerGetDoSta(1)
d.In2=PerGetDoSta(2)
end
adc =tonumber(PerGetAdcGatherValByAdcId(1))
if math.abs(e.adc-adc) >1000 or needupall ==1 then
d.adc = adc
e.adc = d.adc
needup =1
end
acc=PerGetDiById(1)
if needupall ==1 or e.acc~=acc then
d.acc=acc
e.acc=acc
needup =1
end
thisshake = PerGetGpsZdSta()
if thisshake == lastshake then
count3 = count3+1
else
count3 = 0
end
lastshake = thisshake
if thisshake == 1 and count3>2 then
shake = 1
end
if thisshake == 0 and count3>30 then
shake = 0
end
if needupall ==1 or e.s ~= shake then
d.s = shake
e.s = shake
needup =1
end
if libgnss.isFix() then
gpsisfix = 1
else
gpsisfix = 0
end
if needupall ==1 or e.f~=gpsisfix then
d.f=gpsisfix
e.f=d.f
needup =1
end
if libgnss.isFix() then
local tg =libgnss.getRmc(2)
local m = getDistance(tg.lat,tg.lng,gpslat,gpslng)
d.m = m
d.gps={}
d.gps.lat,d.gps.lng=tg.lat,tg.lng
d.gps.speed=tg.speed * 1.85
d.gps.course = calculateBearing(tg.lat,tg.lng,gpslat,gpslng)
if needup ==1 then
gpslat,gpslng = d.gps.lat,d.gps.lng
end
end
lbslng,lbslat = GetLbs()
if needupall ==1 or e.lbslng ~= lbslng then
d.lbs={}
d.lbs.lng,d.lbs.lat=lbslng,lbslat
e.lbslng,e.lbslat=lbslng,lbslat
end
local updata = json.encode(d)
local netsta = PronetGetNetSta(nid)
log.info(taskname,"updata",updata,"netsta",netsta)
if needup ==1 and updata and next(d)~=nil and netsta ==1 then
needup =0
needupall =0
count =0
PronetSetSendCh(nid,updata)
end
count = count+1
count2 = count2+1
if (acc ==0 or e.s ==1) and count > 15 then
needup =1
end
if e.s ==1 and libgnss.isFix() == false and count2 > 300 then
GpsSetPower(0)
sys.wait(1000)
GpsSetPower(1)
sys.wait(1000)
GpsExecAgnss()
count2 =0
end
if count > 600 then
needup =1
needupall =1
end
sys.wait(1000)
end
end