Gizra / negawatt-server

negawatt data server
http://negawatt-dev.gizra.com
5 stars 1 forks source link

Read occupancy data from file #793

Open shahardo opened 9 years ago

shahardo commented 9 years ago

Fields: 18/08/2015 - When the report was created 17/08/2015 - Date of data. We should refer only to the first line (the day before the report was generated), the rest of the file is predicted data for future dates. 2- Weekday? 1 4 401 85 - Occupancy in percent 85% . . . 411 129 - Occupancy - rooms . . . 417 452 - Total guests

shahardo commented 9 years ago

Use a vbscript to send the data, and at cscript.exe to schedule running the script. See: http://stackoverflow.com/questions/4277292/how-to-post-a-http-request-from-command-line

shahardo commented 9 years ago

Use VBS script send_occupancy.vbs:

WScript.Echo "running...."
WScript.Echo "logging in...."

' Set your settings
strServer = "192.168.1.31"
Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP")

' Fetch access-token for astral
strURL = "negawatt-server2/www/api/login-token"

strFullURL = "http://" & strServer & "/" & strURL
objXMLHTTP.open "GET", strFullURL, false
objXMLHTTP.setRequestHeader "Authorization", "Basic YXN0cmFsOmFzdDQzMjE="
objXMLHTTP.send()

WScript.Echo "status = ", objXMLHTTP.Status
WScript.Echo "response = ", objXMLHTTP.responseText

' Look for access token
iTokenPos = InStr(objXMLHTTP.responseText, "access_token") + 15
iTokenEnd = InStr(iTokenPos, objXMLHTTP.responseText, """")
iTokenLen = iTokenEnd - iTokenPos

strToken = Mid(objXMLHTTP.responseText, iTokenPos, iTokenLen)
WScript.Echo "token = ", strToken
WScript.Echo 

' Read data file

strFName = "marina.txt"
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile(strFName,1)
strLine = objFileToRead.ReadLine()
objFileToRead.Close
Set objFileToRead = Nothing

' Read date
iDatePos = InStr(strLine, ",") + 1
iDateEnd = InStr(iDatePos, strLine, ",")
iDateLen = iDateEnd - iDatePos
strDate = Mid(strLine, iDatePos, iDateLen)

' Read occupancy pcnt (field 401)
iOccupPos = InStr(strLine, "401,") + 4
iOccupEnd = InStr(iOccupPos, strLine, ",")
iOccupLen = iOccupEnd - iOccupPos
strOccup = Mid(strLine, iOccupPos, iOccupLen)

WScript.Echo "date = ", strDate
WScript.Echo "occup = ", strOccup

' Send POST request to add new occupancy data

Set objXMLHTTP = Nothing
Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP")

WScript.Echo "sending POST request..."

strURL = "negawatt-server2/www/api/v1.0/occupancy_raw"
strPostData = "meter=2374&frequency=3&datetime=" & strDate & "&occupancy_rel=" & strOccup

strFullURL = "http://" & strServer & "/" & strURL
objXMLHTTP.open "POST", strFullURL, false
objXMLHTTP.setRequestHeader "access-token", strToken
objXMLHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objXMLHTTP.send(strPostData)

WScript.Echo "status = ", objXMLHTTP.Status
WScript.Echo "response = ", objXMLHTTP.responseText
WScript.Echo

Set objXMLHTTP = Nothing

Run it using: cscript send_occupancy.vbs