· 6 years ago · Jan 13, 2020, 03:58 PM
1import * as aws4 from "aws4";
2import * as AWS from "aws-sdk";
3import * as request from "request";
4
5function invokeUpgrader(function(err) {
6 AWS.config.getCredentials(function(err) {
7 if (err) console.log(err.stack);
8 // credentials not loaded
9 else {
10 console.log("Access key:", AWS.config.credentials.accessKeyId);
11 console.log("Secret access key:", AWS.config.credentials.secretAccessKey);
12 }
13 });
14
15 const post_body = { "user_id": "1871" };
16 const req_path = "/serialnumber/C05BCBA0C0A000001492/4g/activate";
17 var opts = {
18 host: "apigw.prod.veeaplatform.net",
19 path: req_path,
20 uri: "https://"+"apigw.prod.veeaplatform.net"+req_path,
21 json: true,
22 method: "POST",
23 headers: {},
24 body: JSON.stringify(post_body),
25 region: "us-east-1",
26 service: "execute-api"};
27 var signer = new aws4.RequestSigner(opts, AWS.config.credentials);
28 signer.sign();
29 console.log("Activating 4G");
30 request.post(opts, (err: any, httpResponse: any, body: any) => {
31 LOGGER.info(manager.UnitSerial + ": response from node management backend: " + JSON.stringify(body) + ", httpResponse: " + httpResponse + ", err: " + err);
32 if (httpResponse.statusCode == 404) {
33 LOGGER.error(manager.UnitSerial + ": 404 status code from node management gateway: " + body);
34 callback(null, "Unable to locate the ICCID for your device, please contact support.");
35 } else if (httpResponse.statusCode == 200) {
36 LOGGER.info(manager.UnitSerial + ": succeeded in activating 4G");
37 request.post({
38 url,
39 body: toSend,
40 json: true,
41 headers,
42 rejectUnauthorized: false},
43 (_err: any, _httpResponse: any, body: any) => {
44 if (body.response != null && body.meta.status === 200) {
45 callback(body.response, null);
46 } else {
47 LOGGER.error(manager.UnitSerial + ": Error from mesh upgrade API call: " + body);
48 callback(null, "Invalid response received. Please try again or go to veea.com/support for resources.");
49 }});
50 } else {
51 LOGGER.error(manager.UnitSerial + ": Error subscribing to package: " + body);
52 callback(null, "Error attempting to subscribe to your 4G package.");
53 }});
54}
55
56)