simont77 / fakegato-history

Module to emulate Elgato Eve history
MIT License
167 stars 15 forks source link

Do you have any tips for including in a typescript based package? #92

Closed fjs21 closed 3 years ago

fjs21 commented 3 years ago

Hi, I am writing a plugin using the homebridge-plugin-template which utilizes typescript. I would like to use fakegato-history but have been struggling with the correct syntax to 'import' rather than 'require' the FakeGatoHistoryService and add this to my accessory. Any help would be appreciated.

simont77 commented 3 years ago

Sorry, I don't have any experience with typescript, maybe someone else.

faune commented 3 years ago

Fakegate in a typescript project is included like this:

import fakegato from 'fakegato-history';
(...)
class MyClass:
  historyService: fakegato.FakeGatoHistoryService;
  constructor(...) {

  this.log = this.platform.log;
  const FakeGatoHistoryService = fakegato(this.platform.api);
  this.historyService = new FakeGatoHistoryService('weather', this.accessory);
}

I am attempting to include this in https://github.com/faune/homebridge-grohe-sense/ and the graphs are showing up at least. Still attempting to add data, but I am assuming if it shows up in the Eve app it is also configured correctly - time will tell :)

Edit: Not able to get data to show up. Have added a couple of thousand entries, but no data is shown in the graph. Just displays "No data available". I can see data being added when addData() is called, but not sure why it doesn't display in the Eve app. Is it meant to show up right away, or do I have to wait some amount of time?

fjs21 commented 3 years ago

I'm getting an error on this line of code: const FakeGatoHistoryService = fakegato(this.platform.api);

Error: UnhandledPromiseRejectionWarning: TypeError: fakegato_history_1.fakegato is not a function

Any ideas?

faune commented 3 years ago

There seems to be an issue with how fakegate-history has been imported in the namespace of the module?

Mine is installed under: <plugin_dir>/node_modules/fakegato-history/fakegato-history.js

I am also struggling with instantiating historyService properly, so I had to manually set some properties:

const FakeGatoHistoryService = fakegato(this.platform.api);
    this.historyService = new FakeGatoHistoryService('weather', this.accessory, {minutes:1});
    this.historyService.accessoryName = <accessoryName>;
    this.historyService.log = this.platform.log;

Right now I get a new entry every 1 minutes, but I also have 1000+ historical entries that I tried adding with

this.historyService.addEntry({time: <unix timestamp>, temperature: <value>, humidity: <value>})

But looking at the code I am not sure its possible to add historical data this way to fakegato. I am beginning to fear that I have to write them to a file first, and instantiate the fakegato service with a fs (filesystem) backend for persistent data ... and that's a bummer :(

fjs21 commented 3 years ago

Yeah my bad. I was not using import incorrectly with { } brackets around fakegato.

I have the accessory working with Eve now. I can see the graph and the temp is recorded every 10 mins which is fine. However, my accessory will need to be rewritten to properly support it. Currently, I only update the characteristics on an as needed basis and so the temp doesn't update unless someone is viewing the app.

Here's the code:

const FakeGatoHistoryService = fakegato(this.platform.api);
this.historyService = new FakeGatoHistoryService('weather', this.accessory, {  
  storage: "fs",  
  minutes: 10  
});  
this.historyService.name =  this.HeaterCooler.getCharacteristic(this.platform.Characteristic.CurrentTemperature);  \
this.historyService.log = this.platform.log; `  

To add info.

this.historyService.addEntry({   
      time: Date.now(),   
      temp: currentValue   
    })
NorthernMan54 commented 3 years ago

@fjs21 With the latest release I had included some minor tweaks around typescript support, in particular in regards to logging. Please see this in the README

https://github.com/simont77/fakegato-history#advanced-options

fjs21 commented 3 years ago

Thanks. I will take a look. From my perspective this issue can be closed as I was able to incorporate fakegato into the plugin I have been working on (homebridge-kumo).

NorthernMan54 commented 3 years ago

Closing