UNPKG

halton-iot-mcp

Version:

MCP Server for Halton IoT Data API

125 lines 3.91 kB
/** * Halton IoT API Client * Handles HTTP communication with the Halton IoT Data API */ const API_BASE_URL = "https://api.halton.io/api/v2"; export class HaltonApiClient { apiToken; constructor(apiToken) { this.apiToken = apiToken; } async request(endpoint, options = {}) { const url = `${API_BASE_URL}${endpoint}`; const headers = { "Content-Type": "application/json", Authorization: `Bearer ${this.apiToken}`, ...options.headers, }; const response = await fetch(url, { ...options, headers, }); if (!response.ok) { const errorText = await response.text(); throw new Error(`API request failed: ${response.status} ${response.statusText} - ${errorText}`); } const text = await response.text(); if (!text) { return {}; } try { return JSON.parse(text); } catch { // Some endpoints return plain text return text; } } // ============ Systems (Timeseries) ============ /** * Get basic information about all accessible systems */ async getAllSystems() { return this.request("/data/systems"); } /** * Get basic information about specified systems */ async getSystems(systems) { return this.request("/data/systems", { method: "POST", body: JSON.stringify({ systems }), }); } // ============ System Variables ============ /** * Retrieve a list of variables for specified systems */ async getSystemVariables(systems) { return this.request("/data/systems/variables", { method: "POST", body: JSON.stringify({ systems }), }); } /** * Search for variables with search words */ async searchSystemVariables(request, includeLastEventValues = false) { const queryParams = includeLastEventValues ? "?includeLastEventValues=true" : ""; return this.request(`/data/systems/variables/search${queryParams}`, { method: "POST", body: JSON.stringify(request), }); } // ============ Aggregate Series ============ /** * Get aggregate series data for a single variable */ async getAggregateSeries(variable, request) { return this.request(`/data/systems/variables/${encodeURIComponent(variable)}/aggregate`, { method: "POST", body: JSON.stringify(request), }); } /** * Get aggregate series data for multiple variables */ async getAggregateSeriesMultiple(request) { return this.request("/data/systems/variables/aggregate", { method: "POST", body: JSON.stringify(request), }); } // ============ Last Event Values ============ /** * Get last event data for a single variable */ async getLastEventValue(variable, systems) { return this.request(`/data/systems/variables/${encodeURIComponent(variable)}/lastEventValue`, { method: "POST", body: JSON.stringify({ systems }), }); } /** * Get last event data for multiple variables */ async getLastEventValueMultiple(request) { return this.request("/data/systems/variables/lastEventValue", { method: "POST", body: JSON.stringify(request), }); } // ============ Raw Events ============ /** * Get raw events for a variable */ async getRawEvents(variable, request) { return this.request(`/data/systems/variables/${encodeURIComponent(variable)}/rawEvents`, { method: "POST", body: JSON.stringify(request), }); } } //# sourceMappingURL=api-client.js.map