-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.lua
More file actions
executable file
·159 lines (130 loc) · 5.1 KB
/
driver.lua
File metadata and controls
executable file
·159 lines (130 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
--[[=============================================================================
Main script file for driver
Copyright 2015 Control4 Corporation. All Rights Reserved.
===============================================================================]]
require ('drivers-common-public.global.url')
require ('drivers-common-public.global.timer')
require ('drivers-common-public.global.lib')
JSON = require ('drivers-common-public.module.json')
require "common.c4_driver_declarations"
require "common.c4_common"
require "common.c4_init"
require "common.c4_property"
require "common.c4_command"
require "common.c4_notify"
require "common.c4_utils"
require "lib.c4_timer"
require "actions"
require "device_specific_commands"
require "device_messages"
require "proxy_init"
require "proxy_commands"
require "connections"
require "helper"
require "blind.blind_proxy_class"
require "blind.blind_proxy_commands"
require "blind.blind_proxy_notifies"
require "blind.blind_proxy_constants"
-- This macro is utilized to identify the version string of the driver template version used.
if (TEMPLATE_VERSION ~= nil) then
TEMPLATE_VERSION.driver = "2015.11.30"
end
--[[=============================================================================
Constants
===============================================================================]]
DOORSTATION_PROXY_BINDINGID = 5001
--[[=============================================================================
Initialization Code
===============================================================================]]
LEVEL_UNKNOWN = -9999
LEVEL_VARIOUS = 9999
POLLTIMER=nil
function ON_DRIVER_EARLY_INIT.main()
gVariousMode = nil -- set to unset to indicate we need to calcualte if we're in variousMode
gCanStop = nil
end
function ON_DRIVER_INIT.main()
end
function ON_DRIVER_LATEINIT.main()
gBlindProxy:dev_Stopped(LEVEL_UNKNOWN) -- Needed to initalize the proxy that we're in an unknonw state
DRIVER_NAME = C4:GetDriverConfigInfo("name")
print("my driver name is " .. DRIVER_NAME)
SetLogName(DRIVER_NAME)
for property, _ in pairs (Properties) do
OnPropertyChanged (property)
end
-- Update Position based on the lastest information in HomeAssistant
SetTimer(1337, 30000, FetchPosition, true)
end
function FetchPosition()
local url = "http://" .. HASS_HOST .. ":" .. HASS_PORT .. "/api/states/" .. ENTITY
local headers = {
['Authorization'] = 'Bearer ' .. TOKEN,
['Content-Type'] = 'application/json'
}
print ('Sending GET to ' .. url)
urlGet (url, headers, ParsePosition, contextInfo, DEFAULT_URL_OPTIONS)
end
function ParsePosition (strError, responseCode, tHeaders, data, context, url)
print("Current Position: " .. data["attributes"]["current_position"])
DEV_MSG.Stopped(data["attributes"]["current_position"])
end
function OpenConnection()
--C4:CreateNetworkConnection(1337, HASS_HOST)
FetchPosition()
end
function OnPropertyChanged (strProperty)
local value = Properties [strProperty]
if (value == nil) then
value = ''
end
if (strProperty == "HomeAssistant Host") then
HASS_HOST = value
elseif (strProperty == "HomeAssistant Port") then
HASS_PORT = value
elseif (strProperty == "Cover Entity") then
ENTITY = value
elseif (strProperty == "Bearer Token") then
TOKEN = value
end
print("HASS_HOST:" .. HASS_HOST)
print("HASS_PORT:" .. HASS_PORT)
if (HASS_HOST ~= nil) then
print("OPENING CONNECTION")
OpenConnection()
end
end
--[[=============================================================================
Driver Code
===============================================================================]]
function PackAndQueueCommand(...)
local command_name = select(1, ...) or ""
local command = select(2, ...) or ""
local command_delay = select(3, ...) or tonumber(Properties["Command Delay Milliseconds"])
local delay_units = select(4, ...) or "MILLISECONDS"
LogTrace("PackAndQueueCommand(), command_name = " .. command_name .. ", command delay set to " .. command_delay .. " " .. delay_units)
if (command == "") then
LogWarn("PackAndQueueCommand(), command_name = " .. command_name .. ", command string is empty - exiting PackAndQueueCommand()")
return
end
-- TODO: pack command with any any required starting or ending characters
local cmd, stx, etx
print("PackandQueueCommand Method: " .. gControlMethod)
if (gControlMethod == "Network") then
-- TODO: define any required starting or ending characters.
stx = ""
etx = "\r"
cmd = stx .. command .. etx
elseif (gControlMethod == "Serial") then
-- TODO: define any required starting or ending characters.
stx = ""
etx = "\r"
cmd = stx .. command .. etx
elseif (gControlMethod == "IR") then
cmd = command
else
LogWarn("PackAndQueueCommand(): gControlMethod is not valid, ".. gControlMethod)
return
end
gCon:QueueCommand(cmd, command_delay, delay_units, command_name)
end