Object building from JSON List

Hello,

I have to say, I’m a bit new on C++. My goal is to have a multi switch build which includes a OLED (SPI) display. The photon should read out the devices for a specific room from FHEM (Home Automation system) and display (and later toggle) the status of the devices. FHEM provides a simple JSON protocol which includes all devices in a list. This list I would like to turn into a list of (device)-objects to iterate through on the photon and then display the status scrollable on the display.
Now running the display and also present some values there I made already. But the challenge for me is to build the list of objects right out of the JSON list. The list contains currently also additional values which I will strip down later in the FHEM. My interest is currently only the NAME,Value and from the Attributes section the group.

As the PHOTON switch should be a generic build (later for multiple rooms) I would like to keep the code “stupid” and the objects should be get their definition out of the JSON.

I know, that there are already libraries out there, reading a json string, but I’m mainly failing in building the object structure out of it.

Can someone help my to get at least a point, where I can go by myself.

Thanks a lot.

Jens

This is the provided list from the Server:

        {
  "Arg":"TYPE=EIB:FILTER=room=Wohnzimmer",
  "Results": [
  {
    "Name":"EIB_000c",
    "Internals": {
      "DEF": "000c",
      "GROUP": "000c",
      "LASTGROUP": "000c",
      "LASTInputDev": "tul",
      "MSGCNT": "158",
      "NAME": "EIB_000c",
      "NR": "31",
      "RAWSTATE": "off",
      "STATE": "off",
      "TYPE": "EIB",
      "tul_MSGCNT": "158",
      "tul_RAWMSG": "Bf10dw000c00",
      "tul_TIME": "2015-08-20 23:27:13"
    },
    "Readings": {
      "state": { "Value":"off", "Time":"2015-08-20 23:27:13" }
    },
    "Attributes": {
      "IODev": "tul",
      "alias": "Szene Essen",
      "fm_type": "lamp",
      "group": "Licht,Szenen,Wohnzimmer",
      "room": "Wohnzimmer,Favourites"
    }
  },
  {
    "Name":"EIB_000d",
    "Internals": {
      "DEF": "000d",
      "GROUP": "000d",
      "NAME": "EIB_000d",
      "NR": "33",
      "STATE": "off",
      "TYPE": "EIB"
    },
    "Readings": {
      "state": { "Value":"off", "Time":"2015-04-25 23:30:11" }
    },
    "Attributes": {
      "IODev": "tul",
      "alias": "Szene Fernsehen",
      "fm_type": "lamp",
      "group": "Szenen,Licht,Wohnzimmer",
      "room": "Wohnzimmer,Favourites"
    }
  },
  {
    "Name":"Esstisch",
    "Internals": {
      "DEF": "0004",
      "GROUP": "0004",
      "NAME": "Esstisch",
      "NR": "42",
      "STATE": "off",
      "TYPE": "EIB"
    },
    "Readings": {
      "state": { "Value":"off", "Time":"2015-04-25 21:50:15" }
    },
    "Attributes": {
      "IODev": "tul",
      "alias": "Esstisch",
      "fm_type": "lamp",
      "fp_Home": "8,323,1,,",
      "group": "Wohnzimmer",
      "room": "Wohnzimmer,Favourites"
    }
  },
  {
    "Name":"Pfette",
    "Internals": {
      "DEF": "0003",
      "GROUP": "0003",
      "LASTGROUP": "0003",
      "LASTInputDev": "tul",
      "MSGCNT": "7",
      "NAME": "Pfette",
      "NR": "28",
      "RAWSTATE": "on",
      "STATE": "on",
      "TYPE": "EIB",
      "tul_MSGCNT": "7",
      "tul_RAWMSG": "B11fbw000301",
      "tul_TIME": "2015-08-20 22:59:33"
    },
    "Readings": {
      "state": { "Value":"on", "Time":"2015-08-20 22:59:33" }
    },
    "Attributes": {
      "IODev": "tul",
      "alias": "Pfette",
      "fm_type": "lamp",
      "fp_Home": "224,240,1,,",
      "group": "Wohnzimmer,Licht",
      "room": "Wohnzimmer"
    }
  },
  {
    "Name":"Sky",
    "Internals": {
      "DEF": "000b",
      "GROUP": "000b",
      "LASTGROUP": "000b",
      "LASTInputDev": "tul",
      "MSGCNT": "14",
      "NAME": "Sky",
      "NR": "37",
      "RAWSTATE": "off",
      "STATE": "off",
      "TYPE": "EIB",
      "tul_MSGCNT": "14",
      "tul_RAWMSG": "B11fbw000b00",
      "tul_TIME": "2015-08-21 12:15:59"
    },
    "Readings": {
      "state": { "Value":"off", "Time":"2015-08-21 12:15:59" }
    },
    "Attributes": {
      "IODev": "tul",
      "alias": "Sky",
      "fm_type": "lamp",
      "fp_Home": "286,390,1,,",
      "group": "Licht,Wohnzimmer",
      "room": "Wohnzimmer"
    }
  },
  {
    "Name":"Stehlampe",
    "Internals": {
      "DEF": "0002",
      "GROUP": "0002",
      "LASTGROUP": "0002",
      "LASTInputDev": "tul",
      "MSGCNT": "164",
      "NAME": "Stehlampe",
      "NR": "29",
      "RAWSTATE": "on",
      "STATE": "on",
      "TYPE": "EIB",
      "tul_MSGCNT": "164",
      "tul_RAWMSG": "Bf10dw000201",
      "tul_TIME": "2015-08-20 12:21:27"
    },
    "Readings": {
      "state": { "Value":"on", "Time":"2015-08-20 12:21:27" }
    },
    "Attributes": {
      "IODev": "tul",
      "alias": "Stehlampe",
      "fm_type": "lamp",
      "fp_Home": "113,630,1,,",
      "group": "Licht,Wohnzimmer",
      "room": "Wohnzimmer"
    }
  }
  ],
  "totalResultsReturned":6
}

@jcbmania

If your FHEM controller is exposed to the internet, you can create a WebHook that can query your controller at regular intervals. Your JSON return can then be filtered with a ResponseTemplate to give you just the data you need.

Check out this tutorial and search the forum for more examples.

example Response Template on your JSON:

{{#Results}}{{Name}}~{{#Internals}}{{STATE}}~{{/Internals}}{{/Results}}

returns:

EIB_000c~off~EIB_000d~off~Esstisch~off~Pfette~on~Sky~off~Stehlampe~on~

Hello @BulldogLowell,

Sorry for the late reply. Was not able to go forward with the project.

Unfortunately I don’t want to expose my server to the internet. So my plan is to use the SparkJson but my C++ isn’t so good.
Currently I have reduced my json string, to make it simpler:

{
  "Arg":"room=Wohnzimmer STATE",
  "Results": [
  {
    "Name":"WZ.Essen",
    "Internals": {      "STATE": "off"    }
  },
  {
    "Name":"WZ.Esstisch",
    "Internals": {      "STATE": "off"    }
  },
  {
    "Name":"WZ.Fernsehen",
    "Internals": {      "STATE": "off"    }
  },
  {
    "Name":"WZ.Media",
    "Internals": {      "STATE": "off"    }
  },
  {
    "Name":"WZ.Pfette",
    "Internals": {      "STATE": "off"    }
  },
  {
    "Name":"WZ.Sky",
    "Internals": {      "STATE": "off"    }
  },
  {
    "Name":"WZ.Stehlampe",
    "Internals": {      "STATE": "on"    }
  }  ],
  "totalResultsReturned":7
}