TypeScript 中的共同警示协议
该库应允许解析和写入 CAP( 共同提醒协议) 消息 .
安装
@dec112/cap-ts在npm注册处公开发布.
npm install @dec112/cap-ts
构建
# install dependencies
npm install
# build sources
npm run build
示例
创建 CAP 提醒
import {
AlertBuilder,
Category,
Certainty,
InfoBuilder,
MsgType,
Scope,
Severity,
Status,
Urgency,
} from '@dec112/cap-ts/builder';
const info = new InfoBuilder(
// event
'Fire in shopping mall',
Urgency.immediate,
Severity.extreme,
Certainty.observed,
[Category.fire],
);
const alert = new AlertBuilder(
// alert ID
'ca15f4ac-7ee9-4391-a9da-a75f1c3374dd',
// sender ID
'http://shopping.com/sensors/fire/386',
new Date().toISOString(),
Status.actual,
MsgType.alert,
Scope.restricted,
);
// add info to alert
alert.info_list = [info];
const xmlString = alert.build().toXML();
使用字符串得出相应的 enum 值
import {
AlertBuilder,
Category,
Certainty,
FromString,
InfoBuilder,
MsgType,
Scope,
Severity,
Status,
Urgency,
} from '@dec112/cap-ts/builder';
// ############################################
// Of course these examples do not make much sense
// as they use string literals and could therefore
// simply replaced by the enums itself
// However this is handy if the enum string
// is available in a variable and the corresponding
// internal enum value needs to be retreived from it
// ############################################
const info = new InfoBuilder(
// event
'Fire in shopping mall',
FromString.getUrgency('immediate') ?? Urgency.unknown,
FromString.getSeverity('extreme') ?? Severity.unknown,
FromString.getCertainty('observed') ?? Certainty.unknown,
[FromString.getCategory('fire') ?? Category.other],
);
const alert = new AlertBuilder(
// alert ID
'ca15f4ac-7ee9-4391-a9da-a75f1c3374dd',
// sender ID
'http://shopping.com/sensors/fire/386',
new Date().toISOString(),
FromString.getStatus('actual') ?? Status.actual,
FromString.getMsgType('alert') ?? MsgType.alert,
FromString.getScope('restricted') ?? Scope.restricted,
);
// add info to alert
alert.info_list = [info];
const xmlString = alert.build().toXML();
分析 CAP 提醒
import { CAP_1_2 } from "@dec112/cap-ts";
try {
const alert = await CAP_1_2.Alert.fromXML('<?xml version="1.0" ... >')
} catch {
// parsing went wrong
}
谢谢!
非常感谢 乔纳森·维尔伯 发起该项目的 菲尔格雷什 他们为这个项目增加了有价值的测试.
这个项目从下列来源获得了资金: 网络电话17。 。 。 .