This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.ts
261 lines (210 loc) · 5.71 KB
/
device.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import { assert } from "../deps/std_testing_asserts.ts";
import type * as models from "../models/mod.ts";
import type { BasicObjectInit, Creator } from "./client.ts";
import { suppressAPIError } from "./api_error.ts";
import { customInspect } from "./custom_inspect.ts";
const enrollment = {
"ac2": Object.freeze({ type: "ac2", pending: false } as const),
"dep": Object.freeze({ type: "dep", pending: false } as const),
"ac2Pending": Object.freeze({ type: "ac2", pending: true } as const),
"depPending": Object.freeze({ type: "dep", pending: true } as const),
"manual": Object.freeze({ type: "manual", pending: false } as const),
} as const;
// Very dumb regex that matches the Jamf School region coordinate string format
const coords = /^([+-]?\d{1,3}(?:\.\d{1,15})?),([+-]?\d{1,3}(?:\.\d{1,15})?)$/;
// /devices and /devices/:udid both return subtly different data, but /devices
// is the more sane of the two routes.
export type DeviceData = models.APIData["getDevices"][number];
export class Device implements models.Device {
#api: models.API;
#client: Creator;
#data: DeviceData;
constructor(init: BasicObjectInit<DeviceData>) {
this.#api = init.api;
this.#client = init.client;
this.#data = init.data;
}
toString() {
return this.#data.name;
}
toJSON() {
return this.#data;
}
[Symbol.for("Deno.customInspect")]() {
return customInspect(this);
}
get type() {
return "Device" as const;
}
get udid() {
return this.#data.UDID;
}
get serialNumber() {
return this.#data.serialNumber;
}
get name() {
return this.#data.name;
}
get isManaged() {
return this.#data.isManaged;
}
get isSupervised() {
return this.#data.isSupervised;
}
get deviceClass() {
return this.#data.class;
}
get assetTag() {
return this.#data.assetTag;
}
get os() {
return `${this.osPrefix} ${this.osVersion}`;
}
get osPrefix() {
return this.#data.os.prefix;
}
get osVersion() {
return this.#data.os.version;
}
get modelName() {
return this.#data.model.name;
}
get modelIdentifier() {
return this.#data.model.identifier;
}
get modelType() {
return this.#data.model.type;
}
get batteryPercentage() {
return this.#data.batteryLevel;
}
get batteryCapacity() {
return this.#data.totalCapacity;
}
get storageTotal() {
return this.#data.totalCapacity;
}
get storageRemaining() {
return this.#data.availableCapacity;
}
get enrollment() {
return enrollment[this.#data.enrollType];
}
get locationId() {
return this.#data.locationId;
}
get ownerId() {
return this.#data.owner.id;
}
get ownerName() {
return this.#data.owner.name;
}
getRegion() {
if (this.#data.region.string === "") {
return null;
}
const match = this.#data.region.coordinates?.match(coords);
assert(match, "Unexpected coordinate format");
return {
name: this.#data.region.string,
latitude: parseFloat(match[1]),
longitude: parseFloat(match[2]),
};
}
async update() {
const devices = await this.#api.getDevices({
serialNumber: this.#data.serialNumber,
});
if (devices.length !== 1) {
throw new Error(`Expected 1 device, got ${devices.length}`);
}
this.#data = devices[0];
}
async getOwner() {
if (this.#data.owner.id === 0) {
return null;
}
let userData;
try {
userData = await this.#api.getUser(this.#data.owner.id);
} catch (e: unknown) {
return suppressAPIError(null, e);
}
return this.#client.createUser(userData);
}
async setOwner(user: { id: number }) {
assert(
user.id !== 0,
"Using ID 0 would remove the owner. If this is intentional, use `Device.removeOwner()`",
);
if (this.#data.owner.id !== user.id) {
await this.#api.setDeviceOwner(this.#data.UDID, user.id);
}
}
async removeOwner() {
if (this.#data.owner.id !== 0) {
await this.#api.setDeviceOwner(this.#data.UDID, 0);
}
}
async getGroups(): Promise<models.DeviceGroup[]> {
let allGroups;
try {
allGroups = await this.#api.getDeviceGroups();
} catch (e: unknown) {
return suppressAPIError([], e);
}
const groups = new Set(this.#data.groups);
const myGroups = allGroups.filter((group) => groups.has(group.name));
return myGroups.map((group) => this.#client.createDeviceGroup(group));
}
async restart() {
await this.#api.restartDevice(this.#data.UDID);
}
async wipe() {
await this.#api.wipeDevice(this.#data.UDID);
}
async getApps() {
let apps, myAppData;
try {
[apps, myAppData] = await Promise.all([
this.#api.getApps(),
this.#api.getDevice(this.#data.UDID, { includeApps: true }),
]);
} catch (e: unknown) {
return suppressAPIError([], e);
}
// It's possible that this could be omitted, which is an error condition.
// That shouldn't fail silently, since that's (in this particular case) a
// validation failure.
if (!("apps" in myAppData)) {
throw new Error("Missing 'apps' property in returned apps");
}
const myAppIdentifiers = new Set(myAppData.apps!.map((app) => app.identifier));
const myApps = apps.filter((app) => myAppIdentifiers.has(app.bundleId));
return myApps.map((app) => this.#client.createApp(app));
}
async getLocation() {
let locationData;
try {
locationData = await this.#api.getLocation(this.#data.locationId);
} catch (e: unknown) {
return suppressAPIError(null, e);
}
return this.#client.createLocation(locationData);
}
async setAssetTag(text: string) {
if (this.#data.assetTag !== text) {
await this.#api.updateDevice(this.#data.UDID, { assetTag: text });
}
}
async setNotes(text: string) {
if (this.#data.notes !== text) {
await this.#api.updateDevice(this.#data.UDID, { notes: text });
}
}
async setLocation(location: { id: number }) {
if (this.#data.locationId !== location.id) {
await this.#api.moveDevice(this.#data.UDID, location.id);
}
}
}