MiSchroe / klf-200-api

This module provides a wrapper to the REST API of a KLF-200 interface. Supporting the new API with firmware version 0.2.0.0.71 is in progress.
MIT License
38 stars 2 forks source link

TypeError during loginAsync #34

Closed jslettengren closed 2 years ago

jslettengren commented 3 years ago

I'm trying to run this from within a docker container, and I get an error which I don't fully understand. It is during the call to loginAsync.

await conn.loginAsync('mypassword'); The error message is as follows:

/app/node_modules/klf-200-api/dist/connection.js:277
                                (_b = this.sckt) === null || _b === void 0 ? void 0 : _b.off("error", loginErrorHandler);
                                                                                         ^

TypeError: _b.off is not a function
    at TLSSocket.sckt.tls_1.connect (/app/node_modules/klf-200-api/dist/connection.js:277:90)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:106:13)
    at TLSSocket.emit (events.js:208:7)
    at TLSSocket.<anonymous> (_tls_wrap.js:1126:14)
    at emitNone (events.js:106:13)
    at TLSSocket.emit (events.js:208:7)
    at TLSSocket._finishInit (_tls_wrap.js:639:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:469:38)

Am I missing some dependencies? Or something else?

I did have this all working in a non-dockerized environment. I hope to get it running also from within a docker container.

jslettengren commented 3 years ago

Complete script if that is of help:

#!/usr/bin/env node

import { Connection, Products, Product, Gateway } from "klf-200-api";
import { readFileSync } from "fs";

async function enable_HSM() {
  const myFingerprint = "AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD";
  const myCA = readFileSync("/app/velux-cert.pem");
  /*
    Create connection using IP
  */
  const conn = new Connection('192.168.1.200', myCA, myFingerprint);

  /*
    Login with WLAN password
  */
  await conn.loginAsync('mypassword');

  try {
    const gw = new Gateway(conn);
    await gw.enableHouseStatusMonitorAsync();
  } finally {
    await conn.logoutAsync();
  }
}

enable_HSM();
MiSchroe commented 3 years ago

Can you provide the docker file? I would assume that there is no difference between running in a docker container or running on the physical machine.

And can you also give it a try to await the call to enable_HSM();. Maybe that is a race condition that leads to the situation that the connection object is already destroyed when the callback is called.

jslettengren commented 3 years ago

So I'm doing this from within a running docker image from demidyuk/domoticz (https://github.com/stas-demydiuk/domoticz-docker/blob/master/Dockerfile.x86-64).

I run the docker image using this docker-compose.yml:

version: '3'
services:
  domoticz:
    container_name: domoticz
    image: demydiuk/domoticz:latest
    restart: unless-stopped
    volumes:
      - /some/path/data:/config
      - /some/path/keys:/config/keys
      - /etc/localtime:/etc/localtime:ro
    privileged: true
    labels:
      - traefik.http.routers.home.rule=Host(`host.name`)
      - traefik.http.routers.home.tls=true
      - traefik.http.routers.home.tls.certresolver=lets-encrypt
      - traefik.port=8080
    environment:
      - PUID=1000
      - PGID=1000
    tty: true
    devices:
      - `/dev/ttyACM0:/dev/ttyACM0`
      - /dev/ttyUSB0:/dev/ttyUSB0
    networks:
      - web

networks:
  web:
    external: true

After bashing into the docker container (docker exec -it <container name> bash), I have installed nodejs, tsc, klf-200-api, etc... My goal is to write a docker file of my own that setups the whole environment, but I'm not quite there yet.

I did try your suggestion to await the call to enable_HSM(), but that gives me another error: enable_house_status_monitor.ts:44:1 - error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.

I did try to set some compiler options according to the error message, but that just gave me other error messages. As you can see, I'm not skilled enough to figure this out on my own. Any more ideas?

MiSchroe commented 2 years ago

I can't reproduce that error.