-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from konnected-io/wifi-disconnect-fix
improve wifi disconnect logic
- Loading branch information
Showing
6 changed files
with
318 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
describe("init", function() | ||
local mock_timer | ||
|
||
before_each(function() | ||
mock(_G.enduser_setup) | ||
mock_timer = mock({ | ||
register = function() end, | ||
alarm = function() end, | ||
unregister = function() end, | ||
start = function() end | ||
}) | ||
end) | ||
|
||
describe("when wifi is not configured yet", function() | ||
before_each(function() | ||
require("spec/nodemcu_stubs") | ||
nodemcu.wifi.sta.config = "" | ||
mock(_G.enduser_setup) | ||
|
||
dofile("src/init.lua") | ||
end) | ||
|
||
after_each(function() | ||
nodemcu.wifi.sta.config = { ssid = 'test' } | ||
end) | ||
|
||
it("starts enduser_setup", function() | ||
assert.spy(_G.enduser_setup.manual).was.called_with(false) | ||
assert.spy(_G.enduser_setup.start).was.called() | ||
end) | ||
end) | ||
|
||
describe("tmr: poll for IP address", function() | ||
|
||
before_each(function() | ||
require("spec/nodemcu_stubs") | ||
stub(_G.tmr, 'create').returns(mock_timer) | ||
dofile("src/init.lua") | ||
|
||
spy.on(_G.gpio, 'write') | ||
spy.on(wifi.eventmon, 'unregister') | ||
end) | ||
|
||
describe("when there's no IP address", function() | ||
before_each(function() | ||
nodemcu.run_tmr(900, tmr.ALARM_AUTO) | ||
end) | ||
|
||
it("does not stop enduser_setup", function() | ||
assert.spy(_G.enduser_setup.stop).was_not.called() | ||
end) | ||
end) | ||
|
||
describe("when there's an IP address", function() | ||
before_each(function() | ||
nodemcu.wifi.sta.ip = '192.168.1.100' | ||
nodemcu.run_tmr(900, tmr.ALARM_AUTO) | ||
end) | ||
|
||
it("stops enduser_setup", function() | ||
assert.spy(_G.enduser_setup.stop).was.called() | ||
end) | ||
|
||
it("turns off the led", function() | ||
assert.spy(_G.gpio.write).was.called_with(4, _G.gpio.HIGH) | ||
end) | ||
|
||
it("unregisters the failsafeTimer and wifiFailTimer", function() | ||
assert.stub(mock_timer.unregister).was.called() | ||
assert.are.equal(#mock_timer.unregister.calls, 2) -- wifiFailTimer and failSafeTimer | ||
end) | ||
|
||
it("unregisters the wifi disconnect eventmon", function() | ||
assert.spy(wifi.eventmon.unregister).was.called_with(wifi.eventmon.STA_DISCONNECTED) | ||
end) | ||
end) | ||
|
||
end) | ||
|
||
describe("when receiving a disconnect signal", function() | ||
before_each(function() | ||
require("spec/nodemcu_stubs") | ||
stub(_G.tmr, 'create').returns(mock_timer) | ||
dofile("src/init.lua") | ||
spy.on(wifi.eventmon, 'unregister') | ||
|
||
nodemcu.wifi.eventmon['STA_DISCONNECTED']({SSID = "test", BSSID = "test", reason = "201"}) | ||
end) | ||
|
||
it("starts the wifiFailTimer", function() | ||
assert.stub(mock_timer.start).was.called() | ||
end) | ||
|
||
describe("after failing the wifiFailTimer", function() | ||
before_each(function() | ||
nodemcu.run_tmr(30000, tmr.ALARM_SINGLE) | ||
end) | ||
|
||
it("starts wifi setup", function() | ||
assert.spy(_G.enduser_setup.manual).was.called_with(false) | ||
assert.spy(_G.enduser_setup.start).was.called() | ||
end) | ||
|
||
it("starts the failsafeTimer", function() | ||
assert.stub(mock_timer.start).was.called() | ||
end) | ||
|
||
it("unregisters the wifi disconnect eventmon", function() | ||
assert.spy(wifi.eventmon.unregister).was.called_with(wifi.eventmon.STA_DISCONNECTED) | ||
end) | ||
|
||
it("unregisters the wifiFailTimer", function() | ||
assert.stub(mock_timer.unregister).was.called() | ||
assert.are.equal(#mock_timer.unregister.calls, 1) | ||
end) | ||
|
||
describe("after failing the failsafeTimer", function() | ||
before_each(function() | ||
spy.on(node, 'restart') | ||
nodemcu.run_tmr(300000, tmr.ALARM_SINGLE) | ||
end) | ||
|
||
it("restarts", function() | ||
assert.spy(node.restart).was.called() | ||
end) | ||
end) | ||
|
||
describe("after connecting", function() | ||
before_each(function() | ||
nodemcu.wifi.sta.ip = '192.168.1.100' | ||
nodemcu.run_tmr(900, tmr.ALARM_AUTO) | ||
end) | ||
|
||
it("it stops the wifiFailTimer and failSafeTimer", function() | ||
assert.stub(mock_timer.unregister).was.called() | ||
assert.are.equal(#mock_timer.unregister.calls, 2) -- wifiFailTimer and failSafeTimer | ||
end) | ||
|
||
it("unregisters the wifi disconnect eventmon", function() | ||
assert.spy(wifi.eventmon.unregister).was.called_with(wifi.eventmon.STA_DISCONNECTED) | ||
end) | ||
end) | ||
end) | ||
|
||
describe("after connecting", function() | ||
before_each(function() | ||
nodemcu.wifi.sta.ip = '192.168.1.100' | ||
nodemcu.run_tmr(900, tmr.ALARM_AUTO) | ||
end) | ||
|
||
it("it stops the wifiFailTimer and failSafeTimer", function() | ||
assert.stub(mock_timer.unregister).was.called() | ||
assert.are.equal(#mock_timer.unregister.calls, 2) -- wifiFailTimer and failSafeTimer | ||
end) | ||
|
||
it("unregisters the wifi disconnect eventmon", function() | ||
assert.spy(wifi.eventmon.unregister).was.called_with(wifi.eventmon.STA_DISCONNECTED) | ||
end) | ||
end) | ||
end) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,116 @@ | ||
nodemcu = { | ||
tmrs = {}, | ||
run_tmr = function(interval, type) | ||
for _,v in pairs(nodemcu.tmrs) do | ||
if v.interval == interval and v.type == type then | ||
print('Running tmr ' .. type .. ', interval:', interval ) | ||
return v.fn({unregister = function() end}) | ||
end | ||
end | ||
end, | ||
wifi = { | ||
eventmon = {}, | ||
sta = { | ||
config = { ssid = 'test' }, | ||
ip = nil | ||
} | ||
} | ||
} | ||
|
||
_G.node = { | ||
heap = function() return(0) end | ||
heap = function() return 0 end, | ||
chipid = function() return 0 end, | ||
restart = function() end | ||
} | ||
|
||
_G.tmr = { | ||
ALARM_SINGLE = 'ALARM_SINGLE', | ||
ALARM_AUTO = 'ALARM_AUTO', | ||
ALARM_SEMI= 'ALARM_SEMI', | ||
create = function() | ||
return { | ||
alarm = function(_, interval, type, fn) | ||
table.insert(nodemcu.tmrs, { | ||
interval = interval, | ||
type = type, | ||
fn = fn | ||
}) | ||
end, | ||
register = function(_, interval, type, fn) | ||
table.insert(nodemcu.tmrs, { | ||
interval = interval, | ||
type = type, | ||
fn = fn | ||
}) | ||
end, | ||
unregister = function() end, | ||
start = function() end | ||
} | ||
end | ||
} | ||
|
||
_G.file = { | ||
list = function() return {} end, | ||
exists = function() end | ||
} | ||
|
||
_G.gpio = { | ||
HIGH = 'HIGH', | ||
LOW = 'LOW', | ||
mode = function() end, | ||
read = function() end, | ||
write = function() end | ||
} | ||
|
||
_G.wifi = { | ||
eventmon = { | ||
STA_DISCONNECTED = 'STA_DISCONNECTED', | ||
register = function(key, fn) | ||
print(key) | ||
nodemcu.wifi.eventmon[key] = fn | ||
end, | ||
unregister = function(key) | ||
nodemcu.wifi.eventmon[key] = nil | ||
end, | ||
reason = { | ||
AUTH_EXPIRE = 2 | ||
} | ||
}, | ||
|
||
sta = { | ||
getip = function() return nodemcu.wifi.sta.ip end, | ||
getconfig = function() return nodemcu.wifi.sta.config end, | ||
getmac = function() return 'aa:bb:cc:dd:ee:ff' end | ||
} | ||
} | ||
|
||
_G.enduser_setup = { | ||
start = function() end, | ||
stop = function() end, | ||
manual = function() end | ||
} | ||
|
||
_G.net = { | ||
multicastJoin = function() end, | ||
createServer = function() | ||
return { | ||
listen = function() end, | ||
on = function() end | ||
} | ||
end | ||
} | ||
|
||
} | ||
-- Print contents of `tbl`, with indentation. | ||
-- `indent` sets the initial level of indentation. | ||
function tprint (tbl, indent) | ||
if not indent then indent = 0 end | ||
for k, v in pairs(tbl) do | ||
formatting = string.rep(" ", indent) .. k .. ": " | ||
if type(v) == "table" then | ||
print(formatting) | ||
tprint(v, indent+1) | ||
else | ||
print(formatting .. tostring(v)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
local me = { | ||
name = "Security", | ||
hwVersion = "2.0.0", | ||
swVersion = "2.0.2" | ||
swVersion = "2.0.3" | ||
} | ||
return me |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.