Benutzer:StefanStrobel/Module HTTPMOD

Aus FHEMWiki
< Benutzer:StefanStrobel
Version vom 12. Januar 2014, 13:20 Uhr von StefanStrobel (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „== Introduction == This module provides a generic way to retrieve information from devices with an HTTP Interface and store them in Readings. It queries a gi…“)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)

Introduction

This module provides a generic way to retrieve information from devices with an HTTP Interface and store them in Readings. It queries a given URL with Headers and data defined by attributes. From the HTTP Response it extracts Readings named in attributes using Regexes also defined by attributes.

Prerequisits

This Module uses the non blocking HTTP function HttpUtils_NonblockingGet provided by FHEM's HttpUtils in a new Version published in December 2013. If not already installed in your environment, please update FHEM or install it manually using appropriate commands from your environment.

Define

define <name> HTTPMOD <URL> <Interval>

The module connects to the given URL every Interval seconds, sends optional headers and data and then parses the response

Example:

define PM HTTPMOD http://MyPoolManager/cgi-bin/webgui.fcgi 60

Configuration of HTTP Devices

Specify optional headers as attr requestHeader1 to attr requestHeaderX, optional POST data as attr requestData and then pairs of attr readingNameX and attr readingRegexX to define which readings you want to extract from the HTTP response and how to extract them.

Example for a PoolManager 5:

The PoolManager Web GUI can be queried with HTTP POST Requests like this one:

POST /cgi-bin/webgui.fcgi HTTP/1.1
Host: 192.168.70.90
Accept: */*
Content-Type: application/json;charset=UTF-8
Content-Length: 60

{"get" :["34.4001.value" ,"34.4008.value" ,"34.4033.value"]}

The resulting HTTP Response would look like this:

HTTP/1.1 200 OK
Content-type: application/json; charset=UTF-8
Expires: 0
Cache-Control: no-cache
Date: Sun, 12 Jan 2014 12:23:11 GMT
Server: lighttpd/1.4.26
Content-Length: 179

{
	"data":	{
		"34.4001.value":	"7.00",
		"34.4008.value":	"0.52",
		"34.4033.value":	"24.8"
	},
	"status":	{
		"code":	0
	},
	"event":	{
		"type":	1,
		"data":	"48.30000.0"
	}
}

To configure HTTPMOD for a PoolManager one would first define a PoolManager Device with e.g. the name PM, the URL and an Interval of 60 seconds.

Then the Data to be sent in the request needs to be defined because in this example the Device expects a POST Request so the query is not contained in the URL but in the Request Data.

Also as seen above the device expects special HTTP Headers in the request so these headers also need to be defined as attr PM requestHeader1 and attr PM requestHeader2

Then the names of the Readings to be extracted would be set with attributes

Then for each Reading value to be extracted a regular expression needs to be set that will match the value in question within ().

Example:

define PM HTTPMOD http://MyPoolManager/cgi-bin/webgui.fcgi 60
attr PM requestData {"get" :["34.4001.value" ,"34.4008.value" ,"34.4033.value", "14.16601.value", "14.16602.value"]}

attr PM requestHeader1 Content-Type: application/json
attr PM requestHeader2 Accept: */*

attr PM readingsName1 PH
attr PM readingsName2 CL
attr PM readingsName3 TEMP

attr PM readingsRegex1 34.4001.value":[ \t]+"([\d\.]+)"
attr PM readingsRegex2 34.4008.value":[ \t]+"([\d\.]+)"
attr PM readingsRegex3 34.4033.value":[ \t]+"([\d\.]+)"

attr PM stateFormat {sprintf("%.1f Grad, PH %.1f, %.1f mg/l Chlor", ReadingsVal($name,"TEMP",0), ReadingsVal($name,"PH",0), ReadingsVal($name,"CL",0))}

Set-Commands

none

Get-Commands

none

Attributes

  • do_not_notify
  • readingFnAttributes
  • requestHeader.*
    • Define an additional HTTP Header to set in the HTTP request
  • requestData
    • POST Data to be sent in the request. If not defined, it will be a GET request as defined in HttpUtils used by this module
  • readingsName.*
    • the name of a reading to extract with the corresponding readingRegex
  • readingsRegex.*
    • defines the regex to be used for extracting the reading. The value to extract should be in a sub expression e.g. ([\d\.]+) in the above example

notes

If you don't know which URLs, headers or POST data your web GUI uses, you might try a local proxy like BurpSuite >BurpSuite to track requests and responses