GithubHelp home page GithubHelp logo

maplesteve / fhem.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from winne27/fhem.js

0.0 1.0 0.0 75 KB

node.js extension for fhem server (smart home)

License: ISC License

Shell 15.67% JavaScript 77.10% HTML 7.23%

fhem.js's Introduction

Purpose

This is a node.js server which works as a websocket gateway to a fhem (home automation) server. Clients can use socket.io in Javascript and Java to establish a websocket connection to fhem.

It is possible to subscribe for updates of fhem resource stati like on/off or temperature. The websocket connection will deliver this values just in time. Furthermore fhem commands could be send other this connection.

Install

Install first node.js on the server on which fhem is installed. Ensure that the version of node.js is greater than 4.x. Check it with

node -v

Ensure that telnet is enabled on the standard fhem server.

Install the package with

npm install -g fhem.js

All needed npm packages like socket.io, socket-auth and forever getting installed automatically by this command.

Now follow the instructions made by the installation output, e.g. run:

sudo /usr/lib/node_modules/fhem.js/bin/postinstall

This will establish the config environment in /etc/fhem.js and /etc/init.d.

Have a look to /etc/fhem.js/params.js. Adjust telnet port of fhem if neccessary. Optionally set a connection password or set SSL for connections can be done there.

Now starting the server with

/usr/bin/fhem.js

or when /usr/bin is in your path simply by

fhem.js

Autostart as service

If this package is installed on a system with /etc/init.d for starting init processes in this directory a file named fhem.js is found after installation. Check this file for some parameters in case you don't like the defaults.

Activate autostart by

sudo update-rc.d fhem.js enable

Not in every Linux distribution this folder exists, but the script can also be found in path-to-nodejs-modules/fhem.js/etc/init.d (Default of path-to-nodejs-modules: /usr/lib/node_modules).

Operation breakdown

The fhem.js server establishes a permanent telnet connection to the standard fhem server and requests information for all changed values (command: inform on). It also forwards commands and responses between clients and the fhem server.

Communication to clients with websockets were realized by the socket.io package.

Customize

Adjust in params.js telnet port of fhem.pl server and port on which this server (fhem.js) is reachable.

The fhem.pl server must be on the same server and the telnet must be configured without local password.

To secure the connection to this node.js server with SSL set

exports.useSSL = true;
exports.sslcert =
{
    key:    '/etc/ssl/private/bundle/ssl.key',
    cert:   '/etc/ssl/private/bundle/allcert.pem'
}

Client

On client side you need socket.io (tested with different browsers and with Android Java using java class com.github.nkzawa.socketio.client for realizing a websocket connection.).

Establish connection to node.js server by:

 socket = IO.socket(url, options);        
 socket.connect();

On client you can emit the following async requests (fast response with minimized data):

  • 'getValueOnce' : requests a value from fhem once
  • 'getValueOnChange' : subscribes delivery of a single updated value by a websocket connection
  • 'getAllValuesOnChange' : subscribes delivery of all updated values by a websocket connection

On client you can emit the following async requests (slower response with much more data):

  • 'getDeviceOnChange' : subscribes delivery of a single updated device in JsonList2 format
  • 'getAllDevicesOnChange' : subscribes delivery of all updated devices in JsonList2 format

Example:

socket.emit('getValueOnChange','fhem-device-name'); 

For catching the response define a listener with label 'value' in the first case and label 'device' in the second case.

Java example:

socket.on("value", new Emitter.Listener()
{
    @Override
    public void call(Object... args)
    {
        Log.i("get value", args[0].toString());
        JSONObject obj = (JSONObject) args[0];
        Iterator<String> iterator = obj.keys();
        String unit = null;
        while (iterator.hasNext())
        {
           unit = (String) iterator.next();
           value = obj.getString(unit);
        }
    }
}

Javascript example:

socket.on('value',function(data)
{
    for (unit in data)
    {
       var value = data[unit];
    }
});

On client you can emit the following sync requests:

  • 'command',cmd : send a fhem command like "list xyz". Response is send back as ack response
  • 'commandNoResp',cmd: send a fhem command like "set xyz off". No response of this command is send back
  • 'getAllSwitches' : returns JSON array with all devices which have state on, off or toggle
  • 'getAllValues' : returns JSON array with all devices and their state
  • 'getAllUnitsOf' : returns JSON array with all devices of type, there type is a argument
  • 'JsonList2',cmd : returns response from JsonList2 as JSON object

Java example for getAllSwitches:

mySocket.socket.emit("getAllSwitches", new Ack()
{
    @Override
    public void call(Object... args)
    {
        JSONArray JSONswitches = (JSONArray) args[0];
        for (int i = 0, size = JSONswitches.length(); i < size; i++)
        {
            String device = JSONswitches.getString(i);
        }
    }
});

Java example for getAllUnitsOf (with "LightScene" as argument type):

mySocket.socket.emit("getAllUnitsOf", "LightScene", new Ack()
{
    @Override
    public void call(Object... args)
    {
        JSONArray JSONlightscenes = (JSONArray) args[0];
        try
        {
            for (int i = 0, size = JSONlightscenes.length(); i < size; i++)
            {
                String unit = JSONlightscenes.getString(i);
            }
        }
    }
});

Javascript example:

socket.emit("getAllUnitsOf", "LightScene", function(data)
{
    for (unit in data)
    {
       var value = data[unit];
    }
});

Example

The folder "test" contains a html/javascript example for an client program.

fhem.js's People

Contributors

winne27 avatar

Watchers

Stephan Esch 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.