package.path = '/home/pi/domoticz/scripts/lua/?.lua;' .. package.path
It can be run either from Domoticz Lua, or from a stand-alone Lua test script.
It should be able to support multiple lamps, but that's untested.
Code: Select all
local Yee = {};
package.loadlib("core.so", "*")
local Socket = require "socket"
local YeeName = 'Study Yee'
local YeeNames = {}
YeeNames[YeeName] = '172.16.176.75'
local YeeIP = YeeNames[YeeName]
local YeePort = 55443
local YeeState
function TCPopenYee()
local tcp = Socket.connect(YeeIP, YeePort)
if not tcp then
print("Socket connect failed for Yee")
return
else
-- print("Yee OK")
end
tcp:settimeout(2)
return tcp
end
function SendToYee(method, params)
if not params then params = "" end
sendStr = string.format('{"id":1,"method":"%s","params":[%s]}\r\n', method, params)
-- sendStr = string.format('{"id":1,"method":"get_prop","params":["power", "bright", "rgb"]} \r\n')
tcp = TCPopenYee()
i, status = tcp:send(sendStr);
if not i then
print("Yee send failed - "..status)
end
s, status, partial = tcp:receive()
if (status) then
print("Yee receive - "..status)
end
local line = (s or partial)
-- print(line)
tcp:close()
end
function GetState()
sendStr = string.format('{"id":1,"method":"get_prop","params":["power","bright","rgb","flowing"]} \r\n')
tcp = TCPopenYee()
i, status = tcp:send(sendStr);
if not i then
print("Yee send failed - "..status)
return
end
s, status, partial = tcp:receive()
if (status) then
print("Yee receive - "..status)
return
end
local result = (s or partial)
tcp:close()
state, bright, rgb, flowing = string.match(result, '%["(%a+)","(%d+)","(%d+)","(%d)"')
return state, flowing
end
function SetPower(param1, speed)
if not YeeState then YeeState = GetState() end
if (YeeState ~= param1) then SetSpeed("set_power", param1, speed) end
end
function SetSpeed(method, param1, speed)
if speed then
effect = "smooth"
else
effect = "sudden"
speed = 0
end
params = string.format('%s,"%s",%d', param1, effect, speed)
SendToYee(method, params)
end
function Yee.Bright(bright, speed)
SetSpeed("set_bright", bright, speed)
end
function Yee.Colour(colour, speed)
SetSpeed("set_ct_abx", colour, speed)
end
function Yee.RGB(hexRGB, speed)
rgb = tostring(tonumber(hexRGB, 16))
SetSpeed("set_rgb", rgb, speed)
end
function GetAction()
if not YeeState then YeeState, flowing = GetState() end
if (flowing == "1") then
print("Yee is flowing")
action = -1
elseif (YeeState == "on") then
-- print("Yee is ON "..YeeState)
action = 0
else -- Turn lamp on, action 2 will turn it off afterwards
-- print("Yee is OFF "..YeeState)
SetSpeed("set_power",'"on"', 500)
action = 2
end
return action
end
function Yee.OnFor(secs, colour, bright)
hex = string.match(colour, "%x%x%x%x%x%x")
if hex then
col = tostring(tonumber(hex, 16))
mode = 1
else
col = tostring(colour)
mode = 2
end
action = GetAction()
if (action > 0) then
params = string.format('2, %d, "250,%d,%s,%d, %d,7,50,50"', action, mode, col, bright, secs*1000)
SendToYee("start_cf", params)
print("Yee on for "..secs)
end
end
function Yee.Pulse(colour, reps, speed, bmax, bmin)
if colour then col = tonumber(colour, 16) else col = tonumber("aaaaaa", 16) end
if reps then reps = reps*2 else reps = 6 end
if not speed then speed = 500 end
if not bmax then bmax = 100 end
if not bmin then bmin = 5 end
action = GetAction()
if (action > 0) then
params = string.format('%d,%d,"%d,1,%d,%d,%d,1,%d,%d"', reps, action, speed, col, bmax, speed, col, bmin)
SendToYee("start_cf", params)
print("Yee pulse "..reps)
end
end
function Yee.On(speed)
SetPower('"on"', speed)
end
function Yee.Off(speed)
SetPower('"off"', speed)
end
function Yee.Toggle()
SendToYee("toggle")
end
function Yee.Default()
SendToYee("set_default")
end
function Yee.Stop()
SendToYee("stop_cf")
end
function Yee.Set(name)
YeeIP = YeeNames[name]
YeeName = name
YeeState = GetState()
end
return Yee;