Skip to content

Commit

Permalink
messing with wifi functions
Browse files Browse the repository at this point in the history
  • Loading branch information
konsumer committed Jul 30, 2021
1 parent 42ff0bb commit 9e4277c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
48 changes: 41 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,51 @@ export default class TPLSmartDevice {

// Scans the wifi networks in range of the device
async listwifi () {
const msg = {
const r1 = await this.send({
netif: {
get_scaninfo: {
refresh: 1
}
}
})

if (r1?.netif?.get_scaninfo?.ap_list) {
return r1.netif.get_scaninfo.ap_list
} else {
// on fail, try older message-format
const r2 = await this.send({
'smartlife.iot.common.softaponboarding': {
get_scaninfo: {
refresh: 1
}
}
})
if (r2 && r2['smartlife.iot.common.softaponboarding']?.get_scaninfo?.ap_list) {
return r2['smartlife.iot.common.softaponboarding'].get_scaninfo.ap_list
}
}
const r = await this.send(msg)
return r?.netif.get_scaninfo.ap_list || r?.smartlife.iot.common.softaponboarding
}

// Connects the device to the access point in the parameters
async connectwifi (ssid, password, keyType, cypherType) {
const msg = {
async connectwifi (ssid, password, keyType = 1, cypherType = 0) {
const r1 = await this.send({
netif: {
set_stainfo: {
cypher_type: cypherType,
key_type: keyType,
password,
ssid
}
}
})

if (r1?.netif?.set_stainfo?.err_code === 0) {
return true
}

// on fail, try older message-format

const r2 = await this.send({
'smartlife.iot.common.softaponboarding': {
set_stainfo: {
cypher_type: cypherType,
Expand All @@ -87,9 +118,12 @@ export default class TPLSmartDevice {
ssid
}
}
})
if (r2['smartlife.iot.common.softaponboarding'] && r2['smartlife.iot.common.softaponboarding'].err_msg) {
throw new Error(r2['smartlife.iot.common.softaponboarding'].err_msg)
} else {
return true
}
const r = await this.send(msg)
return r['smartlife.iot.common.softaponboarding'].set_stainfo
}

// Get info about the TPLSmartDevice
Expand Down
5 changes: 3 additions & 2 deletions src/tplight.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,9 @@ module.exports = yargs
if (!chosen) {
handleError(`${argv.SSID} not found.`)
}
console.log(argv)
const status = await bulb.connectwifi(argv.SSID, argv.SECRET, chosen.key_type)
// sometimes data is missing cypher_type, so I guess based on key_type
const status = await bulb.connectwifi(argv.SSID, argv.SECRET, chosen.key_type, chosen.key_type === 3 ? 2 : 0)

if (status) {
console.log(`OK, joined ${argv.SSID}.`)
} else {
Expand Down

0 comments on commit 9e4277c

Please sign in to comment.