JanLoebel / eufy-node-client

Experiment to talk to eufy security
43 stars 6 forks source link

Example #10

Closed wormiedk closed 3 years ago

wormiedk commented 3 years ago

This seems great but an example for how to use it from e.g. a shell script would be great ;)

I am mostly interested in

JanLoebel commented 3 years ago

@wormiedk have you any experience in javascript/node.js?

wormiedk commented 3 years ago

a little but normally I get by modifying examples. I found this on another forum but cannot get it to work

What I did: sudo npm install -g typescript git clone https://github.com/JanLoebel/eufy-node-client.git cd eufy-node-client/src nano test.ts

Contents of test.ts:

import { HttpService } from './http/http.service';
import { CommandTypes } from './p2p/command.model';
import { DeviceClientService } from './p2p/device-client.service';
import { LocalLookupService } from './p2p/local-lookup.service';
import { PushMessage } from './push/push.model';
import { PushService } from './push/push.service';

(async () => {
  const httpService = new HttpService(USERNAME, PASSWORD);
  const hubs = await httpService.listHubs();

  if (hubs && hubs.length > 0) {
    const hub = hubs[0];
    const lookupService = new LocalLookupService();
    const address = await lookupService.lookup(HOMEBASE_IP);

    const service = new DeviceClientService(address, hub.p2p_did, hub.member.action_user_id);
    const connected = await service.connect().catch((error) => {
      console.log('error trying to connected:', error);
      return false;
    });

    if (connected) {
      console.log('p2p connected, start listening for push messages');

      const pushService = new PushService();
      const credentials = await pushService.createPushCredentials();

      pushService.listen(credentials, async (msg: PushMessage) => {
        const event = msg.data;
        const payload = event.payload;
        if (payload && payload.event_type === 3103 && payload.channel === 2 && payload.push_count === 1) {
          console.log('doorbell pressed');

          if (!streaming) {
            service.sendCommandWithIntString(CommandTypes.CMD_NAS_TEST, 1, 2);
            streaming = true;

            setTimeout(() => {
              if (streaming) {
                service.sendCommandWithIntString(CommandTypes.CMD_NAS_TEST, 0, 2);
              }
              streaming = false;
            }, 6000);
          }
          await got.get('url_to_a_webhook');
        }
      });

      // Register generated token

      const fcmToken = credentials.fcm.token;

      await httpService.registerPushToken(fcmToken);
      await httpService.pushTokenCheck();
    }
  } else {
    console.warn('no hub found');
  }
})();

However this results in an error when trying to compile using tsc test.ts

test.ts:2:10 - error TS2724: Module '"./p2p/command.model"' has no exported member 'CommandTypes'. Did you mean 'CommandType'?

2 import { CommandTypes } from './p2p/command.model';


  p2p/command.model.ts:1:13
    1 export enum CommandType {
'CommandType' is declared here.

test.ts:35:16 - error TS2304: Cannot find name 'streaming'.

35 if (!streaming) {


test.ts:37:13 - error TS2304: Cannot find name 'streaming'.

37             streaming = true;

test.ts:40:19 - error TS2304: Cannot find name 'streaming'.

40 if (streaming) {


test.ts:43:15 - error TS2304: Cannot find name 'streaming'.

43               streaming = false;

test.ts:46:17 - error TS2304: Cannot find name 'got'.

46 await got.get('url_to_a_webhook');

1h8fulkat commented 3 years ago

Try this:

Now tell me what to do next :-D I really want to be able to control the security mode with home assistant!

JanLoebel commented 3 years ago

After @bropat and I will finish the push-rework, I will restructure the index.ts and write some more documentation how to get started. But this is and will stay a library for other developers not users.

JanLoebel commented 3 years ago

I've created a new repository with a 'Getting started' guide. If you have any problems / questions for that, please create an issue there. https://github.com/JanLoebel/eufy-node-client-examples