Hi,
I created a crude script that pols and accepts web hooks from a LOQED doorlock.
My other script sends actions to the LOQED doorlock
Action Script
Create a virtual device and run an event with this script every time the device value changes
Fill in the Loqed IP, Key number, Secret and RefID in this script
Webhook script
It uses port 9090 but you can adjust it in the script.
By creating 4 virtual devices you can see the status of the lock and stop the loop in the script.
Don't forget to make a script running virtual device so you can stop the script from running
I created a crude script that pols and accepts web hooks from a LOQED doorlock.
My other script sends actions to the LOQED doorlock
Action Script
Create a virtual device and run an event with this script every time the device value changes
Fill in the Loqed IP, Key number, Secret and RefID in this script
Code:
Function Loqed(Action As String) As String Dim Host As String = LOQED IP ADDRESS Dim URL1 As String = "http://" & Host & "/state?command=" Dim Key As String = KEY Dim Secret As String = SECRET Dim URL2 As String = URL1 & Action & "&local_key_id=" & Key & "&secret=" & Secret Dim client As New System.Net.WebClient Dim Resp As String Try Resp = client.DownloadString(URL2) Loqed = Resp Catch ex As Exception hs.WriteLog("Error", "In Script Hardware_LOQED_Actions.vb with Exception: ") hs.WriteLog("Error", ex.Message) End Try End Function Sub Main(ByVal Parm As String) Dim Success As String = "Message signed and sent to the lock" Dim ActionRef As Integer = REFID OF VIRTUAL DEVICE Dim Action As Integer = hs.DeviceValue(ActionRef) Dim Response As String Select Case Action Case 1 Response = Loqed("OPEN") Case 2 Response = Loqed("DAY_LOCK") Case 3 Response = Loqed("NIGHT_LOCK") End Select If Not Response.Contains(Success) Then hs.WriteLog("Error", "In Script Hardware_LOQED_Actions.vb with Exception: ") hs.WriteLog("Error", ex.Message) End If End Sub
It uses port 9090 but you can adjust it in the script.
By creating 4 virtual devices you can see the status of the lock and stop the loop in the script.
Don't forget to make a script running virtual device so you can stop the script from running
Code:
Imports System.Timers Imports Newtonsoft.Json Imports System.Text Imports System.Net Imports System.IO ' RUN SCRIPT AS ADMIN IN VS Dim IP_HS As String = IP HS Dim IP_LOQ As String = IP LOQED Dim ScriptRef As Integer = SCRIPT REF Dim StatusRef As Integer = STATUS REF Dim UserRef As Integer = USER REF Dim BatteryRef As Integer = BATTERY Dim Ticks As Integer Private WithEvents Timer1 As New Timer() With {.Interval = 3000} Sub Timer1_Tick(sender As Object, args As EventArgs) Handles Timer1.Elapsed Dim Scriptrunning As Integer = hs.DeviceValue(ScriptRef) Try If Scriptrunning = 0 Then Close() End If If Ticks = 28800 Then Ticks = 0 StatusRequest() Else Ticks += 1 End If Catch ex As Exception hs.WriteLog("Error", "In Sub Timer1_Tick with Exception:") hs.WriteLog("Error", ex.Message) End Try End Sub Function Webhook() As String Try While True Dim Listener As New HttpListener Listener.Prefixes.Add("http://" & IP_HS & ":9090/") Listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous Listener.Start() Dim Context As HttpListenerContext = Listener.GetContext() Context.Response.StatusCode = 200 Context.Response.StatusDescription = "OK" Dim response As HttpListenerResponse = Context.Response Dim Request = Context.Request Dim Stream As New MemoryStream() Request.InputStream.CopyTo(Stream) Dim JSON As String = Encoding.UTF8.GetString(Stream.ToArray()) Stream.Close() Listener.Stop() If JSON <> "close" Then Deserialize(JSON) Else Listener.Close() hs.WriteLog("LOQED", "Closed TCP listener") Exit While End If End While Catch ex As Exception hs.WriteLog("Error", "In Function Webhook with Exception: ") hs.WriteLog("Error", ex.Message) End Try End Function Function Close() As String Dim client As New System.Net.WebClient Dim URL As String = "http://" & IP_HS & ":9090" Try Timer1.Stop() hs.SetDeviceValueByRef(ScriptRef, 0, True) client.UploadString(URL, "close") Catch ex As Exception hs.WriteLog("Error", "In Function Close with Exception: ") hs.WriteLog("Error", ex.Message) End Try End Function Function StatusRequest() As String Dim client As New System.Net.WebClient Dim URL As String = "http://" & IP_LOQ & "/status" Try Dim JSON As String = client.DownloadString(URL) Deserialize(JSON) Catch ex As Exception hs.WriteLog("Error", "In Function StatusRequest with Exception: ") hs.WriteLog("Error", ex.Message) End Try End Function Function Deserialize(JSON As String) As String Dim LOQEDObj As loqed = JsonConvert.DeserializeObject(Of loqed)(JSON) 'hs.WriteLog("LOQED", "Webhook: " & JSON) If LOQEDObj.key_local_id.HasValue And LOQEDObj.key_local_id <> 255 Then hs.SetDeviceValueByRef(UserRef, LOQEDObj.key_local_id, True) End If Dim State As Integer If LOQEDObj.go_to_state <> Nothing Then Select Case LOQEDObj.go_to_state Case "OPEN" State = 1 Case "DAY_LOCK" State = 2 Case "NIGHT_LOCK" State = 3 End Select hs.SetDeviceValueByRef(StatusRef, State, True) End If If LOQEDObj.requested_state_numeric.HasValue Then hs.SetDeviceValueByRef(StatusRef, LOQEDObj.requested_state_numeric, True) End If If LOQEDObj.bolt_state_numeric.HasValue Then hs.SetDeviceValueByRef(StatusRef, LOQEDObj.bolt_state_numeric, True) End If If LOQEDObj.battery_percentage.HasValue Then hs.SetDeviceValueByRef(BatteryRef, LOQEDObj.battery_percentage, True) End If End Function Public Sub Main(ByVal Parm As String) hs.SetDeviceValueByRef(ScriptRef, 1, True) Try ' Get current status directly and deserialize 'hs.WriteLog("LOQED", "Sending HTTP GET") StatusRequest() ' Start Timer to monitor button value 'hs.WriteLog("LOQED", "Starting Timer") Timer1.Start() ' Start Webhook function that loops 'hs.WriteLog("LOQED", "Starting Webhook loop") Webhook() Catch ex As Exception hs.WriteLog("Error", "In Sub Main with Exception: ") hs.WriteLog("Error", ex.Message) End Try End Sub Public Class loqed Public battery_percentage As Double? Public bolt_state_numeric As Double? Public key_local_id As Double? Public go_to_state As String Public requested_state_numeric As Double? End Class
Comment