-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #354 from dfpc-coe/channel-status
Channel Status
- Loading branch information
Showing
10 changed files
with
1,583 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import TAKAPI from '../tak-api.js'; | ||
import { Type, Static } from '@sinclair/typebox'; | ||
import { Group } from './groups.js'; | ||
import { TAKList } from './types.js'; | ||
|
||
export const Subscription = Type.Object({ | ||
dn: Type.Union([Type.String(), Type.Null()]), | ||
callsign: Type.String(), | ||
clientUid: Type.String(), | ||
lastReportMilliseconds: Type.Integer(), | ||
takClient: Type.String(), | ||
takVersion: Type.String(), | ||
username: Type.String(), | ||
groups: Type.Array(Group), | ||
role: Type.String(), | ||
team: Type.String(), | ||
ipAddress: Type.String(), | ||
port: Type.String(), | ||
pendingWrites: Type.Integer(), | ||
numProcessed: Type.Integer(), | ||
protocol: Type.String(), | ||
xpath: Type.Union([Type.String(), Type.Null()]), | ||
subscriptionUid: Type.String(), | ||
appFramerate: Type.String(), | ||
battery: Type.String(), | ||
batteryStatus: Type.String(), | ||
batteryTemp: Type.String(), | ||
deviceDataRx: Type.String(), | ||
deviceDataTx: Type.String(), | ||
heapCurrentSize: Type.String(), | ||
heapFreeSize: Type.String(), | ||
heapMaxSize: Type.String(), | ||
deviceIPAddress: Type.String(), | ||
storageAvailable: Type.String(), | ||
storageTotal: Type.String(), | ||
incognito: Type.Boolean(), | ||
handlerType: Type.String(), | ||
lastReportDiffMilliseconds: Type.Integer() | ||
}); | ||
|
||
export const ListSubscriptionInput = Type.Object({ | ||
sortBy: Type.String({ | ||
default: 'CALLSIGN', | ||
enum: ['CALLSIGN', 'UID'] | ||
}), | ||
direction: Type.String({ | ||
default: 'ASCENDING', | ||
enum: ['ASCENDING', 'DESCENDING'] | ||
}), | ||
page: Type.Integer({ | ||
default: -1 | ||
}), | ||
limit: Type.Integer({ | ||
default: -1 | ||
}) | ||
}) | ||
|
||
export default class { | ||
api: TAKAPI; | ||
|
||
constructor(api: TAKAPI) { | ||
this.api = api; | ||
} | ||
|
||
async list( | ||
query: Static<typeof ListSubscriptionInput> | ||
): Promise<TAKList<Static<typeof Subscription>>> { | ||
const url = new URL(`/Marti/api/subscriptions/all`, this.api.url); | ||
|
||
let q: keyof Static<typeof ListSubscriptionInput>; | ||
for (q in query) { | ||
if (query[q] !== undefined) { | ||
url.searchParams.append(q, String(query[q])); | ||
} | ||
} | ||
|
||
return await this.api.fetch(url, { | ||
method: 'GET' | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { Type } from '@sinclair/typebox' | ||
import Schema from '@openaddresses/batch-schema'; | ||
import Err from '@openaddresses/batch-error'; | ||
import Auth from '../lib/auth.js'; | ||
import Config from '../lib/config.js'; | ||
import { Subscription, ListSubscriptionInput } from '../lib/api/subscriptions.js' | ||
import TAKAPI, { | ||
APIAuthCertificate | ||
} from '../lib/tak-api.js'; | ||
|
||
export default async function router(schema: Schema, config: Config) { | ||
await schema.get('/marti/subscription', { | ||
name: 'List Subscriptions', | ||
group: 'MartiSubscription', | ||
description: 'Helper API to list subscriptions that the client can see', | ||
query: ListSubscriptionInput, | ||
res: Type.Object({ | ||
version: Type.String(), | ||
type: Type.String(), | ||
data: Type.Array(Subscription), | ||
messages: Type.Optional(Type.Array(Type.String())), | ||
nodeId: Type.Optional(Type.String()) | ||
}) | ||
}, async (req, res) => { | ||
try { | ||
await Auth.is_auth(config, req); | ||
|
||
const user = await Auth.as_user(config, req); | ||
const profile = await config.models.Profile.from(user.email); | ||
const api = await TAKAPI.init(new URL(String(config.server.api)), new APIAuthCertificate(profile.auth.cert, profile.auth.key)); | ||
|
||
const groups: Set<string> = new Set(); | ||
(await api.Group.list()).data.forEach((group) => { | ||
groups.add(group.name) | ||
}); | ||
|
||
const subs = await api.Subscription.list(req.query); | ||
|
||
subs.data.forEach((sub) => { | ||
return sub.groups.filter((group) => { | ||
return groups.has(group.name); | ||
}) | ||
}); | ||
|
||
return res.json(subs); | ||
} catch (err) { | ||
return Err.respond(err, res); | ||
} | ||
}); | ||
|
||
await schema.get('/marti/subscription/:clientuid', { | ||
name: 'Get Subscription', | ||
group: 'MartiSubscription', | ||
description: 'Helper API to get a subscription', | ||
params: Type.Object({ | ||
clientuid: Type.String() | ||
}), | ||
res: Subscription | ||
}, async (req, res) => { | ||
try { | ||
await Auth.is_auth(config, req); | ||
|
||
const user = await Auth.as_user(config, req); | ||
const profile = await config.models.Profile.from(user.email); | ||
const api = await TAKAPI.init(new URL(String(config.server.api)), new APIAuthCertificate(profile.auth.cert, profile.auth.key)); | ||
|
||
const subs = await api.Subscription.list({ | ||
sortBy: 'CALLSIGN', | ||
direction: 'ASCENDING', | ||
page: -1, | ||
limit: -1 | ||
}); | ||
|
||
const groups: Set<string> = new Set(); | ||
(await api.Group.list()).data.forEach((group) => { | ||
groups.add(group.name) | ||
}); | ||
|
||
for (const sub of subs.data) { | ||
if (sub.clientUid === req.params.clientuid) { | ||
sub.groups = sub.groups.filter((group) => { | ||
return groups.has(group.name); | ||
}); | ||
return res.json(sub); | ||
} | ||
} | ||
|
||
throw new Err(404, null, `Subscription for ${req.params.clientuid} not found`); | ||
} catch (err) { | ||
return Err.respond(err, res); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.