mirror of
https://github.com/espruino/Espruino.git
synced 2025-12-08 19:06:15 +00:00
7 lines
3.2 KiB
JavaScript
7 lines
3.2 KiB
JavaScript
function m(){}function l(b,c){return new Promise(function(d,a){var e="";f.cmd(b+"\r\n",c||1E3,function p(h){if(void 0===h||"ERROR"==h)a(b+": "+(h||"TIMEOUT"));else if("OK"==h)d(e);else return e+=(e?"\n":"")+h,p})})}var f,g=[],k=" ".split(" "),n=!1,u={create:function(b,c){var d=0;if(void 0===b)return m("Server not implemented"),-1;for(;void 0!==g[d];)d++;if(6<=d)throw Error("No free sockets.");g[d]="Wait";k[d]="";n=!0;f.cmd(`AT+QIOPEN=1,${d},"TCP",${JSON.stringify(b)},${c},0,1\r\n`,
|
|
1E4,function(a){"OK"!=a&&(g[d]=void 0,n=!1)});return d},close:function(b){void 0!==g[b]&&(k[b]="",f.cmd(`AT+QICLOSE=${b}\r\n`,1E3,function(){g[b]=void 0}))},accept:function(){return-1},recv:function(b,c){if(k[b]){if(k[b].length>c){var d=k[b].substr(0,c);k[b]=k[b].substr(c)}else d=k[b],k[b]="","DataClose"==g[b]&&(m("Got DataClose - forcing close"),g[b]="closing");return d}return"closing"!=g[b]&&g[b]?"":-1},send:function(b,c){if(n||f.isBusy()||"Wait"==g[b])return 0;if("closing"==g[b]||!g[b])return-1;
|
|
n=!0;f.register("> ",function(d){m("AT+QISEND got prompt");f.unregister("> ");f.write(c);return d.substr(2)});f.cmd(`AT+QISEND=${b},${c.length}\r\n`,1E4,function e(a){m("AT+QISEND response "+JSON.stringify(a));if(""==a)return e;f.unregister("> ");n=!1;"SEND OK"!=a&&(g[b]=null)});return c.length}},t={debug:function(b){m=b||void 0===b?function(c){print("[BG96]",c)}:function(){};return{socks:g,sockData:k,busy:n}},getVersion:function(b){l("AT+GMR").then(function(c){b(null,c)}).catch(function(c){b(c)})},
|
|
getIP:function(b){var c;f.cmd("AT+QISTATE=0,1\r\n",1E3,function(d){"OK"==d?b(null,c):d.startsWith("+QISTATE")?c=d.split(",")[2]:b(null,d)})}};exports.connect=function(b,c,d){c=c||{};b.removeAllListeners();t.at=f=require("AT").connect(b);c.debug&&(f.debug(),t.debug(1));require("NetworkJS").create(u);f.register('+QIURC: "recv"',function(a){var e=a.indexOf("\r\n");if(0>e)return a;var q=a.substr(0,e).split(","),h=0|q[1],p=0|q[2];a=a.substr(e+2);k[h]+=a;m(q);f.getData(p-a.length,function(r){k[h]+=r});
|
|
return""});f.registerLine('+QIURC: "closed"',function(a){a=0|a.substr(17);g[a]=k[a]?"DataClose":"closing";n=!1});f.registerLine("+QIOPEN: ",function(a){a=a.substr(9).split(",");0!=a[1]?console.log("QIOPEN ERROR "+a[1]):g[0|a[0]]=!0;n=!1});f.registerLine("+CME ERROR",function(a){console.log(a)});l("ATE0",4E3).then(function(){return l("AT+CEREG=2")}).then(function(){return new Promise(function(a,e){var q=60,h="",p=setInterval(function(){f.cmd(c.lte?"AT+CEREG?\r":"AT+CREG?\r",500,function(r){h=r&&r.split(",")[1];
|
|
if(1==h||5==h)clearInterval(p),a()});0>=q--&&(clearInterval(p),e("Timeout while registering. CREG = "+h))},1E3)})}).then(function(){return l("AT+CGATT=1",1E4)}).then(function(){return l("AT+CGREG?")}).then(function(a){var e=a&&a.split(",")[1];if(2==e)throw Error("GPRS still connecting, "+a);if(1!=e&&5!=e)throw Error("GPRS not registered, "+a);return l("AT+COPS?")}).then(function(a){var e=a.split(",")[2];if(!e)throw Error("No Operator, "+a);m("Operator "+e);return l(`AT+QICSGP=1,1,${JSON.stringify(c.apn||
|
|
"")},${JSON.stringify(c.username||"")},${JSON.stringify(c.password||"")}`,6E4)}).then(function(){return l("AT+QIACT?")}).then(function(a){m("QIACT state "+a);if(1!=(a&&a.split(",")[1]))return l("AT+QIACT=1")}).then(function(){d&&d(null)}).catch(function(a){d&&d(a||"Unknown error")});return t} |