michaelkrog / blaubergventojs

MIT License
2 stars 2 forks source link

Better documentation #3

Closed andypmuc closed 1 month ago

andypmuc commented 6 months ago

Sorry, I am not a profession programmer and have some trouble.

I installed it first without the -g option and it did work, then with the -g option

C:\Users\Administrator\Desktop>npm list
C:\Users\Administrator
`-- (empty)

C:\Users\Administrator\Desktop>npm list -g
C:\Users\Administrator\AppData\Roaming\npm
`-- blaubergventojs@1.0.0-alpha.3

C:\Users\Administrator\Desktop>node example.mjs
node:internal/modules/esm/resolve:853
  throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base), null);
        ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'blaubergventojs' imported from C:\Users\Administrator\Desktop\example.mjs
    at packageResolve (node:internal/modules/esm/resolve:853:9)
    at moduleResolve (node:internal/modules/esm/resolve:910:20)
    at defaultResolve (node:internal/modules/esm/resolve:1130:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:396:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:365:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:85:39)
    at link (node:internal/modules/esm/module_job:84:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Node.js v20.11.0

Content of

import 'blaubergventojs';

console.log('Hello World!');

Also Tried it with

import { BlaubergVentoClient, FunctionType, Parameter, DataEntry } from 'blaugbergventojs'

and also with the path before blauberg vento.

When I use .js file I end up with

C:\Users\Administrator\Desktop>node example.js
(node:4576) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
C:\Users\Administrator\Desktop\example.js:2
import 'blaubergventojs';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:77:18)
    at wrapSafe (node:internal/modules/cjs/loader:1288:20)
    at Module._compile (node:internal/modules/cjs/loader:1340:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at node:internal/main/run_main_module:28:49

Node.js v20.11.0

Could you improve the docs. I also like to integrated it in Openhab, which is also Java. I would assume the error I get there ist the same.

michaelkrog commented 6 months ago

Hi @andypmuc.

The docs you request are out of scope for this project. It s basic NPM packaging that is troubling you here. You cannot use this on its own. You can install it and use it in your own Javascript application.

You need to install the package into an existing npm based project. The following should get you going, but please take a look at npm docs for more details about managing a Javascript project.

(The following is an example on a Linux/Unix machine, but should be translatable to Windows)

mkdir my-project
cd my-project
npm init my-project
npm install blaubergventojs

You will now have a package.json in your folder with a reference to blaubergventojs. Afterwards create a javascript file called fx. example.mjs and add the highlevel example from the README in this project.

import { BlaubergVentoResource, FunctionType, Parameter, DataEntry } from 'blaugbergventojs'

const resource = new BlaubergVentoResource();

// Find all devices on the local network
const devices = await resource.findAll();

// Change a device and save it
let device = devices.contents[0];
device.speed = 1;
device = await resource.save(device);

Run it: node example.mjs. Because the folder has a package.json, node will use the dependencies from there and import should work.

This should give you the general idea.

andypmuc commented 6 months ago

OK, thanks anyway. I run it on Windows, and maybe that is the reason, why it is a little bit different.

I had to use

npm init

Then import did work, I used VS Code and it helped me there a little bit

import pkg from 'blaubergventojs';

const resource = new pkg.BlaubergVentoResource();

// Find all devices on the local network
const devices = await resource.findAll();

// Change a device and save it
let device = devices.content[0];

console.log (device.on);
console.log (device.mode);
console.log (device.manualSpeed);

It told me, that this is a common JS module, which cannot be imported like it is in your documentation.

Then contents is without s -> let device = devices.content[0];

At least I discovered that error in your documentation ;-)

Now it is working and maybe I propose some changes that I can use weekly schedule as high level command. Tried it yesterday as low level and it did work. I also need to find out, how I integrate it in OpenHAB.

andypmuc commented 6 months ago

Maybe you put it something like this as well. I struggled a little bit with the data entries.

const pkt3 = new pkg.Packet(addresses[0].id, '1111', pkg.FunctionType.WRITEREAD, [pkg.DataEntry.of(pkg.Parameter.WEEKLY_SCHEDULE, 0)])
const rsp3 = await client.send(pkt3, addresses[0].ip);
console.log (rsp3.packet.dataEntries[0].value[0]);