more G-Labs products

Author Topic: Quering a sql database from within a widget using a RESTful server  (Read 1092 times)

December 29, 2015, 01:41:38 PM
Read 1092 times

louis

  • **
  • Information
  • Jr. Member
  • Posts: 26
I have, i thought, a simple problem.

I want to change the energymonitor widget.

I currently have a sql database to which i log every 10 seconds the status of my energy meter (the physical device from the energy company).

I would like to change the enerymonitor widget to take data from this database instead of homegenies internal data.

As far as i understand all i need to do is create a RESTful server which queries this database, and then use this RESTful server to give info the the $.ajax stuff in the energymonitor widget.

i'm currently at the point where the RESTful api is working. But for some reason the widget gets an error.

i'm currently only sending the same info as i get when i query /api/HomeAutomation.HomeGenie/Config/Modules.StatisticsGet/HomeAutomation.EnergyMonitor/1/EnergyMonitor.KwCounter

I checked this with Postman.

When i run this ajax request on my own api i get an error in homegenie and no data.

For the record ..... i'm in no way realy familiar with java script ... ajax and more related stuff.

So i might do something realy stupid ;)

The ajax query currently looks like:
    $.ajax({
      url: '192.168.2.6:3000/api/p1logging',
      type: 'GET',
      dataType: 'json',
      success: function (counterData) {


i added en error: function to get some more info, but al it says is 'error' in the return string .....

Anyone out there who has some advice on how to tackle this ?

Thanks in advance.

Louis

December 29, 2015, 08:37:02 PM
Reply #1

louis

  • **
  • Information
  • Jr. Member
  • Posts: 26
Oke,

I Figured it out ;)

i had to define an api call in HomeGenie due to the fact that ajax is not allowed to leave it's own domain.

I now have defined a HomeGenie api which does the call to the webservice.

Now i get my info  8)

Cheers

Louis
« Last Edit: December 29, 2015, 08:39:19 PM by louis »

January 29, 2016, 04:13:45 PM
Reply #2

HGexperimenter

  • **
  • Information
  • Jr. Member
  • Posts: 42
I am interested - same type of project.  Do you have a code snippet to share?

January 30, 2016, 01:15:10 AM
Reply #3

louis

  • **
  • Information
  • Jr. Member
  • Posts: 26
Hi HGexperimenter,

Sure :)

I'll attach 2 examples with 2 widgets, make your pick.

Btw all code is mainly copied from Gene's program's. But i guess everyone does it :)

If you're interested in REST server let me know.

Louis