• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

suculent / thinx-device-api / #252646970

27 Oct 2017 03:17PM UTC coverage: 12.466% (+1.3%) from 11.197%
#252646970

push

suculent
added support for displaying/exporting extended SigFox attributes

37 of 1808 branches covered (2.05%)

Branch coverage included in aggregate %.

735 of 4385 relevant lines covered (16.76%)

0.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

12.64
/lib/thinx/deployment.js
1
/*
2
 * This THiNX-RTM API module is responsible for managing deployment data.
3
 */
4

5
var Deployment = (function() {
1✔
6

7
        var fs = require("fs-extra");
1✔
8
        var util = require("util");
1✔
9
        var semver = require("semver");
1✔
10
        var mkdirp = require("mkdirp");
1✔
11
        var typeOf = require("typeof");
1✔
12
        var finder = require("fs-finder");
1✔
13

14
        var builder = require("./builder");
1✔
15

16
        var app_config = require("../../conf/config.json");
1✔
17
        if (typeof(process.env.CIRCLE_USERNAME) !== "undefined") {
1!
18
                console.log("ยป Configuring for Circle CI...");
×
19
                app_config = require("../../conf/config-test.json");
×
20
        }
21

22
        var _private = {
1✔
23

24
                getFiles: function(dir, files_) {
25
                        files_ = files_ || [];
×
26
                        if (!fs.existsSync(dir)) {
×
27
                                return null;
×
28
                        }
29
                        return finder.from(dir).findFiles();
×
30
                },
31

32
                pathForOwner: function(owner) {
33
                        var user_path = __dirname + "/../.." + app_config.deploy_root + "/" +
×
34
                                owner;
35
                        return user_path;
×
36
                }
37

38
        };
39

40
        // public
41
        var _public = {
1✔
42

43
                initWithDevice: function(device) {
44
                        this.device = device;
×
45
                        this.owner = device.owner;
×
46
                        this.udid = device.udid;
×
47
                        this.version = device.version;
×
48
                        _public.initWithOwner(this.owner, null, function(success, response) {
×
49
                                console.log("initWithDevice -- success:" + success + " response" +
×
50
                                        response);
51
                        });
52
                },
53

54
                initWithOwner: function(owner) {
55

56
                        this.owner = owner;
×
57
                        var user_path = _private.pathForOwner(owner);
×
58
                        if (typeof(user_path) === "undefined" || user_path === null) return;
×
59

60
                        fs.lstat(user_path, function(err, stats) {
×
61
                                if (err) {
×
62
                                        if (err.errno == -2) {
×
63
                                                mkdirp(user_path, function(err) {
×
64
                                                        if (err) console.log(err);
×
65
                                                        else console.log(user_path + " created.");
×
66
                                                });
67
                                        } else {
68
                                                return console.log(err);
×
69
                                        }
70
                                }
71
                                if (!fs.existsSync(user_path)) {
×
72
                                        mkdirp(user_path, function(err) {
×
73
                                                if (err) console.log(err);
×
74
                                                else console.log(user_path + " created.");
×
75
                                        });
76
                                }
77
                        });
78
                },
79

80
                pathForDevice: function(owner, udid) {
81
                        this.owner = owner;
×
82
                        this.udid = udid;
×
83
                        var user_path = _private.pathForOwner(owner);
×
84
                        var device_path = user_path + "/" + udid;
×
85
                        return device_path;
×
86
                },
87

88
                latestFirmwarePath: function(owner, udid) {
89

90
                        if (typeOf(owner) == "Object") {
×
91
                                console.log("Suspicious owner: " + JSON.stringify(owner));
×
92
                        }
93

94
                        var dpath = _public.pathForDevice(owner, udid);
×
95
                        var files = _private.getFiles(dpath);
×
96
                        var latest_date = 0;
×
97
                        var latest_firmware = null;
×
98
                        for (var index in files) {
×
99
                                var filename = files[index];
×
100
                                var valid = false;
×
101
                                var supported_extensions = builder.supportedExtensions();
×
102
                                for (var sex_index in supported_extensions) {
×
103
                                        var extension = "." + supported_extensions[sex_index];
×
104
                                        if (filename.indexOf(extension) !== -1) {
×
105
                                                valid = true;
×
106
                                        }
107
                                }
108
                                if (valid) {
×
109
                                        var stats = fs.statSync(files[index]);
×
110
                                        var mtime = new Date(util.inspect(stats.mtime));
×
111
                                        if (mtime > latest_date) {
×
112
                                                latest_date = mtime;
×
113
                                                latest_firmware = filename;
×
114
                                        }
115
                                } else {
116
                                        return null;
×
117
                                }
118
                        }
119
                        return latest_firmware;
×
120
                },
121

122
                latestFirmwareVersion: function(owner, udid) {
123
                        var envelope = _public.latestFirmwareEnvelope(owner, udid);
×
124
                        if (envelope !== null) {
×
125
                                return envelope.version;
×
126
                        } else {
127
                                return "0.0.0"; // no firmware
×
128
                        }
129
                },
130

131
                latestFirmwareEnvelope: function(owner, udid) {
132

133
                        var path = _public.latestFirmwarePath(owner, udid);
×
134

135
                        if (path !== null) {
×
136
                                var envpath = path.replace(".bin", ".json");
×
137
                                var supported_extensions = builder.supportedExtensions();
×
138
                                for (var xindex in supported_extensions) {
×
139
                                        var extension = "." + supported_extensions[xindex];
×
140
                                        envpath = envpath.replace(extension, ".json");
×
141
                                }
142

143
                                console.log("Final path: " + envpath);
×
144
                                envpath = envpath.replace(".jsonon", ".json"); // why does this happen?
×
145
                                envpath = envpath.replace(".jsononpp", ".json"); // why does this happen?
×
146

147
                                if (fs.existsSync(envpath) && fs.statSync(envpath).size > 2) {
×
148
                                        var envelope = require(envpath);
×
149
                                        console.log("[LFP] envelope: " + JSON.stringify(envelope) +
×
150
                                                " at path " +
151
                                                envpath);
152
                                        return envelope;
×
153
                                } else {
154
                                        return false;
×
155
                                }
156
                        } else {
157
                                return false;
×
158
                        }
159
                },
160

161
                hasUpdateAvailable: function(device) {
162
                        this.device = device;
×
163
                        this.owner = device.owner;
×
164
                        this.udid = device.udid;
×
165

166
                        var deviceVersion = device.version;
×
167
                        if (typeof(deviceVersion) === "undefined" || deviceVersion === null) {
×
168
                                deviceVersion = "0.0.1";
×
169
                        }
170
                        //console.log("[hasUpdateAvailable] Device version: " + deviceVersion);
171
                        var deployedVersion = _public.latestFirmwareVersion(this.owner, this.udid);
×
172

173
                        if (typeof(deployedVersion) === "undefined") {
×
174
                                deployedVersion = "0.0.0";
×
175
                                return false;
×
176
                        }
177

178
                        //console.log("[hasUpdateAvailable] Deployed version: " + deployedVersion);
179

180
                        if (!semver.valid(deviceVersion)) {
×
181
                                var device_version = [0, 0, 0];
×
182
                                var dev_version_array = deviceVersion.split(".");
×
183
                                for (var index1 in dev_version_array) {
×
184
                                        device_version[index1] = dev_version_array[index1];
×
185
                                }
186
                                deviceVersion = device_version.join(".");
×
187
                                console.log(
×
188
                                        "[hasUpdateAvailable] Device version had invalid semantic versioning: " +
189
                                        deviceVersion);
190
                        }
191

192
                        if (!semver.valid(deployedVersion)) {
×
193
                                console.log(
×
194
                                        "[hasUpdateAvailable] Deployed version has invalid semantic versioning: " +
195
                                        deployedVersion);
196
                                var deployment_version = [0, 0, 0];
×
197
                                var dep_version_array = deployedVersion.split(".");
×
198
                                for (var index2 in dep_version_array) {
×
199
                                        deployment_version[index2] = dep_version_array[index2];
×
200
                                }
201
                                deployedVersion = deployment_version.join(".");
×
202
                                console.log(
×
203
                                        "[hasUpdateAvailable] Deployed version fixed to: " +
204
                                        deployedVersion);
205
                        }
206

207
                        if (semver.lt(deviceVersion, deployedVersion)) {
×
208
                                console.log("Deployed version is newer than device version.");
×
209
                                return true;
×
210
                        } else {
211
                                console.log("Device version is newer than deployed version.");
×
212
                                return false;
×
213
                        }
214
                }
215
        };
216

217
        return _public;
1✔
218

219
})();
220

221
exports.init = Deployment.init;
1✔
222
exports.initWithDevice = Deployment.initWithDevice;
1✔
223
exports.initWithOwner = Deployment.initWithOwner;
1✔
224
exports.pathForDevice = Deployment.pathForDevice;
1✔
225
exports.latestFirmwarePath = Deployment.latestFirmwarePath;
1✔
226
exports.hasUpdateAvailable = Deployment.hasUpdateAvailable;
1✔
227
exports.latestFirmwareVersion = Deployment.latestFirmwareVersion;
1✔
228
exports.latestFirmwareEnvelope = Deployment.latestFirmwareEnvelope;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc