Giter Site home page Giter Site logo

csowada / openhab-typescript-rules-example Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 9 KB

This project is just a Prof of Concept to use Type Script with openHAB. I use Babel 7 to convert TypeScritp to valid Nashorn JavaScript.

TypeScript 93.39% JavaScript 6.61%
openhab typescript babel jsr223

openhab-typescript-rules-example's Introduction

This project is just a Prof of Concept to use Type Script with openHAB. I use Babel 7 to convert TypeScritp to valid Nashorn JavaScript.

Please check my automation bundle for openHAB

https://github.com/csowada/org.eclipse.smarthome.automation.module.script.jsext https://github.com/csowada/openhab-typescript-definition

Install

Download or clone this repository and run npm install to install all required modules.

Example

import { JSRule } from "../lib/rules/rules";
import { ChangedEventTrigger } from "../lib/rules/triggers";
import { Helpers } from "../lib/utils/helpers"

export function loadRules() {

    var vols = [1, 2];

    vols.forEach(function(volNumber) {

        JSRule({
            name: "NAS VOL" + volNumber + " Update",
            triggers: [
                ChangedEventTrigger("NAS_Vol" + volNumber + "_Used")
            ],
            execute: function (module: Module, input: TriggerValues) {

                Helpers.getItemNumber("NAS_Vol"+volNumber+"_Size", (size: number) => {
                    Helpers.getItemNumber("NAS_Vol"+volNumber+"_Used", (used: number) => {
                        Helpers.getItemNumber("NAS_Vol"+volNumber+"_Unit", (blockSize: number) => {

                            events.postUpdate(ir.getItem("NAS_Vol"+volNumber+"_Used_GB"), (used * blockSize) / 1073741824)
                            events.postUpdate(ir.getItem("NAS_Vol"+volNumber+"_Available_GB"), ((size - used) * blockSize) / 1073741824)
                            events.postUpdate(ir.getItem("NAS_Vol"+volNumber+"_Used_Percent"), used / size * 100)
                        })
                    })
                })
            }
        });
    });
}

Compile

Run npm run build:js to create a transpiled JavaScript version. Se package.json for more details.

openhab-typescript-rules-example's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.