mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-09 17:33:55 +00:00
feat: send http
This commit is contained in:
committed by
Carlos Valente
parent
8b84963e17
commit
ab4dd300ca
@@ -1,5 +1,14 @@
|
||||
import type { AutomationFilter, AutomationOutput, FilterRule, RuntimeStore, TimerLifeCycle } from 'ontime-types';
|
||||
import {
|
||||
isHTTPOutput,
|
||||
isOSCOutput,
|
||||
type AutomationFilter,
|
||||
type AutomationOutput,
|
||||
type FilterRule,
|
||||
type RuntimeStore,
|
||||
type TimerLifeCycle,
|
||||
} from 'ontime-types';
|
||||
|
||||
import { getState, type RuntimeState } from '../../stores/runtimeState.js';
|
||||
import { emitOSC } from './clients/osc.client.js';
|
||||
import { emitHTTP } from './clients/http.client.js';
|
||||
import { getAutomations, getBlueprints } from './automation.dao.js';
|
||||
@@ -7,7 +16,7 @@ import { getAutomations, getBlueprints } from './automation.dao.js';
|
||||
/**
|
||||
* Exposes a method for triggering actions based on a TimerLifeCycle event
|
||||
*/
|
||||
export function triggerAction(event: TimerLifeCycle, state: Partial<RuntimeStore>) {
|
||||
export function triggerAutomations(event: TimerLifeCycle, state: RuntimeState) {
|
||||
const automations = getAutomations();
|
||||
const triggerAutomations = automations.filter((automation) => automation.trigger === event);
|
||||
if (triggerAutomations.length === 0) {
|
||||
@@ -31,11 +40,8 @@ export function triggerAction(event: TimerLifeCycle, state: Partial<RuntimeStore
|
||||
});
|
||||
}
|
||||
|
||||
export function testOutput(payload: AutomationOutput, state: Partial<RuntimeStore>) {
|
||||
const success = send([payload], state);
|
||||
if (!success) {
|
||||
throw new Error('Failed to send output');
|
||||
}
|
||||
export function testOutput(payload: AutomationOutput) {
|
||||
send([payload]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,6 +66,7 @@ export function testConditions(
|
||||
const { field, operator, value } = filter;
|
||||
const fieldValue = state[field];
|
||||
|
||||
// TODO: if value is empty string, the user could be meaning to check if the value does not exist
|
||||
switch (operator) {
|
||||
case 'equals':
|
||||
return fieldValue === value;
|
||||
@@ -83,17 +90,14 @@ export function testConditions(
|
||||
* Handles preparing and sending of the data
|
||||
* Returns a boolean indicating whether a message was sent
|
||||
*/
|
||||
function send(output: AutomationOutput[], _state: Partial<RuntimeStore>): boolean {
|
||||
function send(output: AutomationOutput[], state?: RuntimeState) {
|
||||
const stateSnapshot = state ?? getState();
|
||||
output.forEach((payload) => {
|
||||
if (payload.type === 'osc') {
|
||||
if (isOSCOutput(payload)) {
|
||||
emitOSC();
|
||||
return true;
|
||||
}
|
||||
if (payload.type === 'http') {
|
||||
emitHTTP();
|
||||
return true;
|
||||
if (isHTTPOutput(payload)) {
|
||||
emitHTTP(payload, stateSnapshot);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
import { AutomationBlueprint, AutomationFilter, AutomationOutput, timerLifecycleValues } from 'ontime-types';
|
||||
import {
|
||||
AutomationBlueprint,
|
||||
AutomationFilter,
|
||||
AutomationOutput,
|
||||
HTTPOutput,
|
||||
OSCOutput,
|
||||
timerLifecycleValues,
|
||||
} from 'ontime-types';
|
||||
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { body, param, validationResult } from 'express-validator';
|
||||
@@ -130,26 +137,9 @@ function validateOutput(output: Array<unknown>): output is AutomationOutput[] {
|
||||
assert.isString(type);
|
||||
|
||||
if (type === 'osc') {
|
||||
assert.hasKeys(payload, ['targetIP', 'targetPort', 'address', 'args']);
|
||||
const { targetIP, targetPort, address, args } = payload;
|
||||
assert.isString(targetIP);
|
||||
assert.isNumber(targetPort);
|
||||
assert.isString(address);
|
||||
if (typeof args !== 'string' && typeof args !== 'number') {
|
||||
throw new Error('Invalid automation');
|
||||
}
|
||||
validateOSCOutput(payload);
|
||||
} else if (type === 'http') {
|
||||
assert.hasKeys(payload, ['targetIP', 'address']);
|
||||
const { targetIP, address } = payload;
|
||||
assert.isString(targetIP);
|
||||
assert.isString(address);
|
||||
} else if (type === 'companion') {
|
||||
assert.hasKeys(payload, ['targetIP', 'address', 'page', 'bank']);
|
||||
const { targetIP, address, page, bank } = payload;
|
||||
assert.isString(targetIP);
|
||||
assert.isString(address);
|
||||
assert.isNumber(page);
|
||||
assert.isNumber(bank);
|
||||
validateHttpOutput(payload);
|
||||
} else {
|
||||
throw new Error('Invalid automation');
|
||||
}
|
||||
@@ -157,6 +147,25 @@ function validateOutput(output: Array<unknown>): output is AutomationOutput[] {
|
||||
return true;
|
||||
}
|
||||
|
||||
function validateOSCOutput(payload: object): payload is OSCOutput {
|
||||
assert.hasKeys(payload, ['targetIP', 'targetPort', 'address', 'args']);
|
||||
const { targetIP, targetPort, address, args } = payload;
|
||||
assert.isString(targetIP);
|
||||
assert.isNumber(targetPort);
|
||||
assert.isString(address);
|
||||
if (typeof args !== 'string' && typeof args !== 'number') {
|
||||
throw new Error('Invalid automation');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function validateHttpOutput(payload: object): payload is HTTPOutput {
|
||||
assert.hasKeys(payload, ['url']);
|
||||
const { url } = payload;
|
||||
assert.isString(url);
|
||||
return true;
|
||||
}
|
||||
|
||||
export const validateTestPayload = [
|
||||
body('type').exists().isIn(['osc', 'http']),
|
||||
|
||||
@@ -164,10 +173,10 @@ export const validateTestPayload = [
|
||||
body('targetIP').if(body('type').equals('osc')).isIP(),
|
||||
body('targetPort').if(body('type').equals('osc')).isPort(),
|
||||
body('address').if(body('type').equals('osc')).isString().trim(),
|
||||
body('message').if(body('type').equals('osc')).isString().trim(),
|
||||
body('args').if(body('type').equals('osc')).isString().trim(),
|
||||
|
||||
// validation for HTTP message
|
||||
body('url').if(body('type').equals('http')).isString().trim(),
|
||||
body('url').if(body('type').equals('http')).isURL({ require_tld: false }).trim(),
|
||||
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
const errors = validationResult(req);
|
||||
|
||||
@@ -1,18 +1,44 @@
|
||||
import { HTTPOutput, LogOrigin } from 'ontime-types';
|
||||
|
||||
import { logger } from '../../../classes/Logger.js';
|
||||
import type { RuntimeState } from '../../../stores/runtimeState.js';
|
||||
|
||||
import { parseTemplateNested } from '../automation.utils.js';
|
||||
|
||||
/**
|
||||
* Expose possibility to send a message using HTTP protocol
|
||||
*/
|
||||
export function emitHTTP() {
|
||||
console.log('HTTP emit not implemented');
|
||||
const payload = preparePayload();
|
||||
emit(payload);
|
||||
export function emitHTTP(output: HTTPOutput, state: RuntimeState) {
|
||||
const url = preparePayload(output, state);
|
||||
emit(url);
|
||||
}
|
||||
|
||||
/** Parses the state and prepares payload to be emitted */
|
||||
function preparePayload() {
|
||||
return;
|
||||
function preparePayload(output: HTTPOutput, state: RuntimeState): string {
|
||||
const parsedUrl = parseTemplateNested(output.url, state);
|
||||
return parsedUrl;
|
||||
}
|
||||
|
||||
/** Emits message over transport */
|
||||
function emit(_payload) {
|
||||
return;
|
||||
async function emit(url: string) {
|
||||
logger.info(LogOrigin.Rx, `Sending HTTP: ${url}`);
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
if (response.status >= 500 && response.status < 600) {
|
||||
logger.warning(LogOrigin.Tx, `HTTP Integration: Server refused message ${response.status}`);
|
||||
} else if (response.status >= 400) {
|
||||
logger.warning(LogOrigin.Tx, `HTTP Integration: Failed sending message ${response.status}`);
|
||||
} else {
|
||||
logger.warning(LogOrigin.Tx, `HTTP Integration: Failed sending message ${response.status}`);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
if (!(error instanceof Error)) {
|
||||
logger.warning(LogOrigin.Tx, `HTTP Integration: Failed sending message ${error}`);
|
||||
return;
|
||||
}
|
||||
|
||||
logger.warning(LogOrigin.Tx, `HTTP Integration: ${error.name} ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user