///
import http from "http";
import { Device } from "./device";
export interface Context {
req: http.IncomingMessage;
res: http.ServerResponse;
body: {
[key: string]: any;
};
}
export declare class ContextError extends Error {
code: number;
constructor(message: string, code?: number);
}
/**
* Validate that the POST body is valid JSON
*/
export declare function requireBody(req: http.IncomingMessage): Promise;
/**
* Convience method that makes a query on a device for the passed in command.
*
* @param device An instance of a Device that will receive and send responses.
* @param command The command to be issued the request. (e.g. "MVL", "AMT", ...)
* @param timeout An optional integer that specifies a timeout (in ms) to wait
* for a response before timing out. (default is 1000).
*/
export declare function sendQuery(device: Device, command: string, timeout?: number): Promise;
/**
* Calls the sendCommand method on a device with the passed in parameters.
*
* @param device An instance of a Device that will receive and send responses.
* @param command The command to be issued the request. (e.g. "MVL", "AMT", ...)
* @param parameter An optional value that specifies a value/action to be
* applied to a command. (e.g. "01", "UP", ...)
* @param timeout An optional integer that specifies a timeout (in ms) to wait
* for a response before timing out. (default is 1000).
*/
export declare function sendCommand(device: Device, command: string, parameter?: string, timeout?: number): Promise;
/**
* Parses incoming HTTP requests and passes the request parameters to an
* appropriate function.
*
* @param ctx Contains the req, res, and optional body.
* @param device An instance of a Device that will receive and send responses.
* @returns A promise that is resolved with a device response. It will reject
* with an error if the request times out or the device responds with an
* error.
*/
export declare function handleCommand(ctx: Context, device: Device): Promise;
/**
* Generates an HTTP handler that has access to a Device instance. It does some
* minimal validation on a request before passing a Context and the Device
* instance to the passed in callback.
*
* @param device An instance of a Device that will receive and send responses
* @param callback A function that accepts a Context and Device instance and
* should return a promise that resolves to a value that will be sent back
* to the client.
* @returns An HTTP handler
*/
export declare function createDeviceHandler(device: Device, callback?: typeof handleCommand): (req: http.IncomingMessage, res: http.ServerResponse) => Promise;
//# sourceMappingURL=api.d.ts.map