Skip to content

tago-io/custom-widget

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

87 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

TagoIO Custom Widget SDK

Create amazing custom widgets and run them inside TagoIO Admin with this comprehensive toolkit. The Custom Widget SDK provides a powerful JavaScript interface for building interactive IoT dashboards and data visualization components.

πŸš€ Quick Start

CDN Installation (Recommended for beginners)

The simplest way to get started is to include the SDK directly in your HTML:

<!DOCTYPE html>
<html>
<head>
    <title>My Custom Widget</title>
    <script src="https://admin.tago.io/dist/custom-widget.min.js"></script>
    <link rel="stylesheet" href="https://admin.tago.io/dist/custom-widget.min.css"> <!-- OPTIONAL -->
</head>
<body>
    <script>
        // Your widget code here
        window.TagoIO.onStart(function(widget) {
            console.log('Widget started!', widget);
        });
        
        window.TagoIO.ready();
    </script>
</body>
</html>

NPM Installation (For development environments)

For projects using webpack or other build tools:

npm install @tago-io/custom-widget --save

Then import in your entry component:

import "@tago-io/custom-widget";
import "@tago-io/custom-widget/dist/custom-widget.css"; // OPTIONAL

πŸ“š SDK Documentation

Core API Reference

The TagoIO Custom Widget SDK provides a comprehensive API through the global window.TagoIO object:

Initialization Functions

  • TagoIO.ready(options) - [You send] Signal to TagoIO that your widget has finished loading and is ready to receive data and events. This must be called to activate your widget.
  • TagoIO.onStart(callback) - [You receive] Asynchronous event triggered when TagoIO starts your widget and provides configuration data, variables, and settings.
  • TagoIO.onError(callback) - [You receive] Asynchronous event triggered when API errors or widget issues occur during runtime.

Data Operations

Note: These data operations are heavily dependent on the Custom Widget configuration at TagoIO. A widget can only manipulate data that is within the Custom Widget settings and permissions.

  • TagoIO.sendData(data, callback) - [You send] Send data to TagoIO devices asynchronously
  • TagoIO.editData(data, callback) - [You send] Edit existing device data asynchronously
  • TagoIO.deleteData(data, callback) - [You send] Delete device data asynchronously
  • TagoIO.editResourceData(data, callback) - [You send] Edit platform resources asynchronously

Real-time Data

  • TagoIO.onRealtime(callback) - [You receive] Asynchronous event triggered when new data is received through dashboard real-time events from connected devices. This callback is invoked automatically whenever real-time data updates occur based on the dashboard's real-time configuration.
  • TagoIO.onSyncUserInformation(callback) - [You receive] Asynchronous event triggered when user context changes or authentication updates occur. Provides user context, authentication tokens, and session information based on dashboard real-time events.
  • TagoIO.onSyncBlueprintDevices(callback) - [You receive] Asynchronous event triggered when blueprint device configurations change or device selections are updated. Provides blueprint device configurations and selected devices through dashboard real-time events.

Utility Functions

  • TagoIO.openLink(url) - Navigate to other dashboards or external links
  • TagoIO.closeModal() - Close widget modal (for header button widgets)

Configuration

  • TagoIO.autoFill - Boolean flag to enable/disable automatic device/bucket ID filling

Complete API Documentation

For detailed API documentation, type definitions, and advanced usage patterns, visit:

πŸ“– Complete SDK Documentation

The repository includes:

  • Full TypeScript type definitions
  • Detailed function parameters and return types
  • Advanced usage examples
  • Integration guides for different frameworks

🎯 Examples & Learning Resources

Practical Examples

This repository includes a comprehensive /examples folder with 7 different HTML examples:

Example Purpose Best For
basic-widget.html Minimal setup and widget lifecycle Beginners learning the basics
read-data.html Displaying real-time device data Data visualization widgets
read-resource.html Accessing platform resources User context and blueprint devices
read-entity.html Complex structured data handling Advanced data relationships
send-data.html Sending data back to devices Interactive input widgets
time-interval.html Time-based data analysis Historical data and time controls
custom-units.html Unit conversions and formatting Multi-unit sensor displays

πŸ“ View All Examples

External Project Examples

Complete project implementations:

  • Boilerplate Project: Basic boilerplate project using Preact, showing fundamental SDK usage patterns.

  • SendData Widget: Simple example demonstrating how to send data from your Custom Widget to TagoIO.

  • Wizard Widget: Advanced demo showing a multi-step wizard built using TagoIO's Custom Widget SDK.

  • ECharts Custom Gauge Widget: Example of a custom gauge widget using the TagoIO Custom Widget SDK.

πŸ› οΈ Development Guide

Building Your First Widget

  1. Start with a basic structure:
document.addEventListener('DOMContentLoaded', function() {
    // Initialize your widget when DOM is ready
    window.TagoIO.onStart(function(widget) {
        // Widget configuration and variables available here
        console.log('Widget config:', widget);
    });
    
    // Handle errors gracefully
    window.TagoIO.onError(function(error) {
        console.error('Widget error:', error);
    });
    
    // Signal that your widget is ready
    window.TagoIO.ready({
        header: {
            color: '#2196F3' // Optional header customization
        }
    });
});
  1. Handle real-time data:
window.TagoIO.onRealtime(function(realtimeData) {
    realtimeData.forEach(function(dataGroup) {
        if (dataGroup.result) {
            dataGroup.result.forEach(function(dataPoint) {
                // Process each data point
                console.log('Variable:', dataPoint.variable);
                console.log('Value:', dataPoint.value);
                console.log('Time:', dataPoint.time);
            });
        }
    });
});
  1. Send data back to devices:
async function sendSensorData() {
    try {
        const result = await window.TagoIO.sendData({
            variable: 'temperature',
            value: 25.5,
            unit: 'Β°C',
            time: new Date().toISOString()
        });
        console.log('Data sent successfully:', result);
    } catch (error) {
        console.error('Failed to send data:', error);
    }
}

Development Best Practices

Error Handling

Always implement comprehensive error handling:

window.TagoIO.onError(function(error) {
    // Log for debugging
    console.error('Widget error:', error);
    
    // Show user-friendly message
    showUserMessage('Something went wrong: ' + error.message);
});

Performance Optimization

  • Use window.TagoIO.autoFill = true to automatically handle device/bucket IDs
  • Implement data throttling for high-frequency updates
  • Clean up timers and event listeners when the widget is destroyed
  • Limit stored data to prevent memory issues

TypeScript Support

The SDK includes comprehensive TypeScript definitions. For TypeScript projects:

import "@tago-io/custom-widget";

// Types are automatically available
window.TagoIO.onStart((widget: TWidget) => {
    // Full type safety and IntelliSense support
    widget.display.variables.forEach((variable: TWidgetVariable) => {
        console.log(`Variable: ${variable.variable}, Device: ${variable.origin.id}`);
    });
});

πŸ”§ Advanced Features

Working with Blueprint Devices

window.TagoIO.onSyncBlueprintDevices(function(blueprintData) {
    // Access blueprint device configurations
    console.log('Blueprint settings:', blueprintData.settings);
    
    // Get currently selected devices
    console.log('Selected devices:', blueprintData.selected);
});

User Context and Authentication

window.TagoIO.onSyncUserInformation(function(userInfo) {
    // Access user language, token, and run URL
    console.log('User language:', userInfo.language);
    console.log('Has token:', !!userInfo.token);
    console.log('Run URL:', userInfo.runURL);
});

Navigation and Modals

// Navigate to another dashboard
window.TagoIO.openLink('https://admin.tago.io/dashboards/info/dashboard-id');

// Close modal (for header button widgets)
window.TagoIO.closeModal();

πŸ“¦ Build Process

Development Scripts

# Install dependencies
npm install

# Build the library
npm run build

# Run tests
npm test

# Type checking
npm run check:types

# Generate coverage report
npm run coverage

🀝 Contributing

We welcome contributions! Please:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Make your changes and add tests
  4. Ensure all tests pass: npm test
  5. Commit your changes: git commit -m 'Add amazing feature'
  6. Push to the branch: git push origin feature/amazing-feature
  7. Open a Pull Request

πŸ“ž Support & Community

Getting Help

πŸ“„ License

This project is licensed under the Apache-2.0 License - see the LICENSE.md file for details.


Made by Tago LLC

For more information about TagoIO's IoT platform and services, visit tago.io.

About

TagoIO Toolkit to build your own widgets

Resources

License

Stars

Watchers

Forks

Contributors