0

List devices

by
Published today

List the site's devices (IMSIs) with their status, one page at a time, with the total count.

Script ericsson_ep5g Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 6 hours ago
1
//native
2

3
/**
4
 * List devices
5
 * List the site's devices (IMSIs) one page at a time, with their status (Online, Disconnected, SIM not written, Disabled). Returns the page and the total count of matching IMSIs; fetch the next page with page + 1.
6
 */
7
export async function main(
8
  auth: RT.EricssonEp5g,
9
  page: number | undefined,
10
  perpage: number | undefined,
11
  sorton:
12
    | "DeviceName"
13
    | "SegmentName"
14
    | "Status"
15
    | "Imsi"
16
    | "NumberOfSegments"
17
    | undefined,
18
  order: "ASC" | "DESC" | undefined,
19
  search: string | undefined,
20
  watchdog: "true" | "false" | undefined,
21
  filters:
22
    | (
23
        | "Gateway"
24
        | "Watchdog"
25
        | "Online"
26
        | "Disconnected"
27
        | "Disabled"
28
        | "SIM not written"
29
        | "eSIM not installed"
30
        | "eSIM"
31
        | "VisitingSIM"
32
        | "LocalSIM"
33
        | "IsLockedToDevice"
34
        | "IsReversedLockedToDevice"
35
        | "NonUniqueLockedImei"
36
      )[]
37
    | undefined,
38
  segment_filter: string[] | undefined
39
) {
40
  const base = auth.base_url.replace(/\/+$/, "")
41
  const url = new URL(
42
    `${base}/v2/organization/${encodeURIComponent(auth.organization)}/site/${encodeURIComponent(auth.site)}/nc/imsi`
43
  )
44
  const params: { [key: string]: string | number | undefined } = {
45
    page: page ?? undefined,
46
    perpage: perpage ?? undefined,
47
    sorton: sorton || undefined,
48
    order: order || undefined,
49
    search: search || undefined,
50
    watchdog: watchdog || undefined,
51
    filters: filters?.length ? filters.join(",") : undefined,
52
    segmentFilter: segment_filter?.length
53
      ? segment_filter.join(",")
54
      : undefined,
55
  }
56
  for (const [k, v] of Object.entries(params)) {
57
    if (v != null && v !== "") {
58
      url.searchParams.append(k, String(v))
59
    }
60
  }
61

62
  const response = await fetch(url, {
63
    headers: {
64
      "X-Api-Key": auth.api_key,
65
      ...(auth.user_role ? { "X-User-Role": auth.user_role } : {}),
66
      Accept: "application/json",
67
    },
68
  })
69

70
  if (!response.ok) {
71
    throw new Error(`${response.status} ${await response.text()}`)
72
  }
73

74
  const totalCount = response.headers.get("Total-Count")
75
  return {
76
    items: await response.json(),
77
    total_count: totalCount != null ? Number(totalCount) : null,
78
    page: page ?? null,
79
    perpage: perpage ?? null,
80
  }
81
}
82