Created
June 3, 2019 14:28
-
-
Save OliPassey/4ef3f27eecc6aecd8a447c6ce755defa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'Call this script by runiing &hs.runScriptFunc("L2DB-influxdbV4.vb","Main",0,False,False) | |
'VB.Net script to write Homeseer 3 values to InfluxDB | |
'from this post of the Homeseer forums: https://forums.homeseer.com/showthread.php?t=194145 | |
'Use at your own risk. Edited by Marty. | |
'Uncomment (hs.WriteLog) the log statements if you're having problems to try to track down the error. | |
'V4 sends to new Raspberry Pi 2 which runs only influx and grafana, note the IP address and database name | |
Imports System.Core | |
Imports System.Web | |
Imports System.Net | |
Imports System.IO | |
Imports System.Text | |
Sub Main (ByVal Parms As Integer) | |
'========================================================== | |
'Modify these to fit your system | |
Dim INFLUX_DB_SERVER_IP = "xxx.xxx.xxx.xxx" | |
Dim INFLUX_DB_SERVER_PORT = "xxxx" | |
Dim INFLUX_DB_DATABASE_NAME = "HomeSeer" | |
'Comma separated list of device references to log | |
Dim DEVICE_LIST = { | |
1886, | |
2755, | |
2733, | |
2750, | |
2749, | |
2728, | |
2727, | |
2699, | |
2700, | |
2704, | |
2705, | |
2714, | |
2715, | |
2689, | |
2690, | |
2694, | |
2695, | |
3065, | |
2710 | |
} | |
'========================================================== | |
Dim device_name, device_location, device_location2, deviceObj, device_type | |
Dim device_value As Double | |
'hs.WriteLog("L2DB-InfluxDB", "Script running L2DB-influxdbV4") | |
'Build the post data string | |
Dim postdata As String = "" | |
For Each devRef As Integer In DEVICE_LIST | |
deviceObj = hs.GetDeviceByRef(devRef) | |
device_name = deviceObj.name(hs) | |
device_location = deviceObj.location(hs) | |
device_location2 = deviceObj.location2(hs) | |
device_type = deviceObj.Device_Type_String(hs) | |
device_value = hs.DeviceValueEx(devRef) | |
postdata = postdata & devRef | |
If device_name <> "" Then | |
postdata = postdata & ",name=" & device_name | |
End If | |
If device_location <> "" Then | |
postdata = postdata & ",location=" & device_location | |
End If | |
If device_location2 <> "" Then | |
postdata = postdata & ",location2=" & device_location2 | |
End If | |
If device_type <> "" Then 'Types are optional and not all devices have them | |
postdata = postdata & ",type=" & device_type | |
End If | |
postdata = postdata & "~value=" & device_value '~ will be changed to a space later | |
postdata = postdata & Chr(10) 'insert line feed for next set of values | |
Next | |
'Strip invalid characters and change ~ to a space | |
postdata = postdata.Replace(" ", "_") | |
postdata = postdata.Replace("[", "") | |
postdata = postdata.Replace("]", "") | |
postdata = postdata.Replace("~", " ") | |
'hs.WriteLog("L2DB-InfluxDB", "Postdata: " & postdata) | |
'Set up the Webrequest | |
Dim url = "http://" & INFLUX_DB_SERVER_IP & ":" & INFLUX_DB_SERVER_PORT & "/write?db=" & INFLUX_DB_DATABASE_NAME | |
Dim httpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest) | |
httpWebRequest.ContentType = "application/x-www-form-urlencoded" | |
httpWebRequest.Method = "POST" | |
ServicePointManager.DefaultConnectionLimit = 20 | |
Dim encoding As New System.Text.UTF8Encoding | |
'Make the request to the database | |
Try | |
Dim data As Byte() = encoding.GetBytes(postdata) | |
httpWebRequest.ContentLength = data.Length | |
Dim myStream As Stream = httpWebRequest.GetRequestStream() | |
If data.Length > 0 Then | |
myStream.Write(data, 0, data.Length) | |
myStream.Close() | |
End If | |
Catch ex As Exception | |
hs.WriteLog("L2DB-InfluxDB", "Error1: " & ex.ToString()) | |
End Try | |
Try | |
Dim httpResponse = DirectCast(HttpWebRequest.GetResponse(), HttpWebResponse) | |
dim myReader As StreamReader | |
dim myStream As Stream | |
myStream = httpResponse.GetResponseStream() | |
myReader = New StreamReader(myStream) | |
Dim responseText = myReader.ReadToEnd() | |
If responseText <> "" Then | |
hs.WriteLog("L2DB-InfluxDB", "Response: " & responseText) | |
End If | |
Catch ex As Exception | |
hs.WriteLog("L2DB-InfluxDB", "Error2: " & ex.ToString()) | |
hs.WriteLog("L2DB-InfluxDB", "Request was: " & postdata) | |
End Try | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment