forked from Moddable-OpenSource/moddable
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'public' into ag-linux-cli
- Loading branch information
Showing
11 changed files
with
279 additions
and
50 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
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,42 @@ | ||
/* | ||
* Copyright (c) 2016-2020 Moddable Tech, Inc. | ||
* | ||
* This file is part of the Moddable SDK. | ||
* | ||
* This work is licensed under the | ||
* Creative Commons Attribution 4.0 International License. | ||
* To view a copy of this license, visit | ||
* <http://creativecommons.org/licenses/by/4.0>. | ||
* or send a letter to Creative Commons, PO Box 1866, | ||
* Mountain View, CA 94042, USA. | ||
* | ||
*/ | ||
|
||
import {Request} from "http" | ||
|
||
const kBodyPart = "Fragment...\n"; | ||
const kRepeat = 128; | ||
|
||
let request = new Request({ | ||
host: "httpbin.org", | ||
path: "/post", | ||
method: "POST", | ||
body: true, // callback is provided by Request.requestFragment callbacks | ||
headers: ["Content-Length", kBodyPart.length * kRepeat, "Content-Type", "text/plain"], | ||
response: String | ||
}); | ||
|
||
request.bytesToSend = kBodyPart.length * kRepeat; | ||
|
||
request.callback = function(message, value) { | ||
if (Request.requestFragment === message) { | ||
if (0 === this.bytesToSend) | ||
return; // no more body | ||
this.bytesToSend -= kBodyPart.length; | ||
return kBodyPart; // fragment of body | ||
} | ||
else if (Request.responseComplete === message) { | ||
value = JSON.parse(value); | ||
trace(`Posted body: ${value.data}\n`); | ||
} | ||
} |
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,15 @@ | ||
{ | ||
"include": [ | ||
"$(MODDABLE)/examples/manifest_base.json", | ||
"$(MODDABLE)/examples/manifest_net.json", | ||
], | ||
"modules": { | ||
"*": [ | ||
"./main", | ||
"$(MODULES)/network/http/*" | ||
] | ||
}, | ||
"preload": [ | ||
"http" | ||
] | ||
} |
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,32 @@ | ||
/* | ||
* Copyright (c) 2016-2020 Moddable Tech, Inc. | ||
* | ||
* This file is part of the Moddable SDK. | ||
* | ||
* This work is licensed under the | ||
* Creative Commons Attribution 4.0 International License. | ||
* To view a copy of this license, visit | ||
* <http://creativecommons.org/licenses/by/4.0>. | ||
* or send a letter to Creative Commons, PO Box 1866, | ||
* Mountain View, CA 94042, USA. | ||
* | ||
*/ | ||
|
||
import Scanner from "wifiscanner"; | ||
import WiFi from "wifi"; | ||
|
||
WiFi.mode = 1; | ||
|
||
trace("Scan start\n"); | ||
const scanner = new Scanner({ | ||
onFound(ap) { | ||
trace(`+ ${ap.ssid}\n`); | ||
}, | ||
onLost(ssid) { | ||
trace(`- ${ssid}\n`); | ||
}, | ||
max: 64, | ||
scanOptions: { | ||
hidden: true, | ||
}, | ||
}); |
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,35 @@ | ||
{ | ||
"include": "$(MODDABLE)/examples/manifest_base.json", | ||
"modules": { | ||
"*": [ | ||
"./main", | ||
"./wifiscanner", | ||
"$(MODULES)/network/wifi/*" | ||
] | ||
}, | ||
"preload": [ | ||
"wifiscanner", | ||
"wifi" | ||
], | ||
"platforms": { | ||
"esp": { | ||
"modules": { | ||
"*": "$(MODULES)/network/wifi/esp/*" | ||
} | ||
}, | ||
"esp32": { | ||
"modules": { | ||
"*": "$(MODULES)/network/wifi/esp32/*" | ||
} | ||
}, | ||
"qca4020": { | ||
"modules": { | ||
"*": "$(MODULES)/network/wifi/qca4020/*" | ||
} | ||
}, | ||
"...": { | ||
"error": "WiFi module unsupported" | ||
} | ||
} | ||
} | ||
|
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,97 @@ | ||
/* | ||
* Copyright (c) 2016-2020 Moddable Tech, Inc. | ||
* | ||
* This file is part of the Moddable SDK. | ||
* | ||
* This work is licensed under the | ||
* Creative Commons Attribution 4.0 International License. | ||
* To view a copy of this license, visit | ||
* <http://creativecommons.org/licenses/by/4.0>. | ||
* or send a letter to Creative Commons, PO Box 1866, | ||
* Mountain View, CA 94042, USA. | ||
* | ||
*/ | ||
|
||
import WiFi from "wifi"; | ||
import Time from "time"; | ||
import Timer from "timer"; | ||
|
||
class Scanner { | ||
#items = []; | ||
#interval; | ||
#max; | ||
#scanOptions; | ||
#onLost; | ||
#onFound; | ||
#timer; | ||
|
||
constructor(options) { | ||
this.#onFound = options.onFound; | ||
this.#onLost = options.onLost; | ||
this.#max = options.max || 32; | ||
this.#interval = options.interval || 1000; | ||
this.#scanOptions = options.scanOptions || {}; | ||
|
||
this.#scan(); | ||
} | ||
#scan() { | ||
WiFi.scan(this.#scanOptions, item => { | ||
if (!this.#items) | ||
return; | ||
|
||
if (item) { | ||
const i = this.#items.find(i => i.ssid === item.ssid); | ||
if (i) | ||
i.ticks = Time.ticks; | ||
else { | ||
this.#items.push({ssid: item.ssid, ticks: Time.ticks}); | ||
if (this.#onFound) | ||
this.#onFound(item); | ||
} | ||
|
||
if (this.#items.length > this.#max) | ||
this.#purge(); | ||
} | ||
else { | ||
this.#purge(); | ||
this.#timer = Timer.set(() => { | ||
this.#timer = undefined; | ||
this.#scan(); | ||
}, this.#interval); | ||
} | ||
}); | ||
} | ||
#purge() { | ||
const items = this.#items; | ||
let expire = Time.ticks - 60 * 1000; // haven't been seen in 60 seconds? gone. | ||
if (expire < 0) expire = 0; | ||
for (let i = 0; i < items.length; i++) { | ||
if (items[i].ticks > expire) | ||
continue; | ||
|
||
if (this.#onLost) | ||
this.#onLost(items[i].ssid); | ||
|
||
items.splice(i, 1); | ||
i -= 1; | ||
} | ||
|
||
if (items.length > this.#max) { | ||
items.sort((a, b) => b.ticks - a.ticks); // most recently seen first | ||
if (this.#onLost) { | ||
for (let i = this.#max; i < items.length; i++) | ||
this.#onLost(items[i].ssid) | ||
} | ||
items.length = this.#max; | ||
} | ||
} | ||
close() { | ||
if (this.#timer) | ||
Timer.clear(this.#timer); | ||
this.#timer = undefined; | ||
this.#items = undefined; | ||
} | ||
} | ||
Object.freeze(Scanner.prototype); | ||
|
||
export default Scanner; |
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
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
Oops, something went wrong.