parent
f33d101ca1
commit
92a38374cb
6 changed files with 101 additions and 0 deletions
@ -0,0 +1,56 @@ |
||||
import { RequestHandler } from 'express'; |
||||
|
||||
import buildGetRequestHandler from '../buildGetRequestHandler'; |
||||
import { buildQueryResultReducer } from '../../buildQueryResultModifier'; |
||||
import { stdout } from '../../shell'; |
||||
|
||||
export const getFence: RequestHandler = buildGetRequestHandler( |
||||
(request, buildQueryOptions) => { |
||||
const query = ` |
||||
SELECT |
||||
fence_uuid, |
||||
fence_name, |
||||
fence_agent, |
||||
fence_arguments |
||||
FROM fences |
||||
ORDER BY fence_name ASC;`;
|
||||
const afterQueryReturn: QueryResultModifierFunction | undefined = |
||||
buildQueryResultReducer<{ [fenceUUID: string]: FenceOverview }>( |
||||
(previous, [fenceUUID, fenceName, fenceAgent, fenceArgumentString]) => { |
||||
const fenceParameters = fenceArgumentString |
||||
.split(/\s+/) |
||||
.reduce<FenceParameters>((previous, parameterPair) => { |
||||
const [parameterId, parameterValue] = parameterPair.split(/=/); |
||||
|
||||
previous[parameterId] = parameterValue.replace(/['"]/g, ''); |
||||
|
||||
return previous; |
||||
}, {}); |
||||
|
||||
stdout( |
||||
`${fenceAgent}: ${fenceName} (${fenceUUID})\n${JSON.stringify( |
||||
fenceParameters, |
||||
null, |
||||
2, |
||||
)}`,
|
||||
); |
||||
|
||||
previous[fenceUUID] = { |
||||
fenceAgent, |
||||
fenceParameters, |
||||
fenceName, |
||||
fenceUUID, |
||||
}; |
||||
|
||||
return previous; |
||||
}, |
||||
{}, |
||||
); |
||||
|
||||
if (buildQueryOptions) { |
||||
buildQueryOptions.afterQueryReturn = afterQueryReturn; |
||||
} |
||||
|
||||
return query; |
||||
}, |
||||
); |
@ -0,0 +1,22 @@ |
||||
import { RequestHandler } from 'express'; |
||||
import { getAnvilData } from '../../accessModule'; |
||||
import { stderr } from '../../shell'; |
||||
|
||||
export const getFenceTemplate: RequestHandler = (request, response) => { |
||||
let rawFenceData; |
||||
|
||||
try { |
||||
({ fence_data: rawFenceData } = getAnvilData( |
||||
{ fence_data: true }, |
||||
{ predata: [['Striker->get_fence_data']] }, |
||||
)); |
||||
} catch (subError) { |
||||
stderr(`Failed to get fence device template; CAUSE: ${subError}`); |
||||
|
||||
response.status(500).send(); |
||||
|
||||
return; |
||||
} |
||||
|
||||
response.status(200).send(rawFenceData); |
||||
}; |
@ -0,0 +1,2 @@ |
||||
export * from './getFence'; |
||||
export * from './getFenceTemplate'; |
@ -0,0 +1,9 @@ |
||||
import express from 'express'; |
||||
|
||||
import { getFence, getFenceTemplate } from '../lib/request_handlers/fence'; |
||||
|
||||
const router = express.Router(); |
||||
|
||||
router.get('/', getFence).get('/template', getFenceTemplate); |
||||
|
||||
export default router; |
@ -0,0 +1,10 @@ |
||||
type FenceParameters = { |
||||
[parameterId: string]: string; |
||||
}; |
||||
|
||||
type FenceOverview = { |
||||
fenceAgent: string; |
||||
fenceParameters: FenceParameters; |
||||
fenceName: string; |
||||
fenceUUID: string; |
||||
}; |
Loading…
Reference in new issue