ADDING SOCKET CODE FOR
TODAY AND FORCE TOUCH WIDGET
On NAC/SHAC :
-- Copyright (c) 2018 Crowhurst Technical Consulting
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- Notwithstanding the foregoing, you may not use, copy, modify, merge, publish,
-- distribute, sublicense, create a derivative work, and/or sell copies of the
-- Software in any work that is designed, intended, or marketed for pedagogical or
-- instructional purposes related to programming, coding, application development,
-- or information technology. Permission for such use, copying, modification,
-- merger, publication, distribution, sublicensing, creation of derivative works,
-- or sale is expressly withheld.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
-- FUNCTIONS
--
-- SET LIGHT sets the group to a level with a ramp rate
--
-- FORMAT = set light,GROUP,LEVEL,RAMP RATE eg set light,1,255,0
--
-- TOGGLE LIGHT toggles the group to between a level with a ramp rate and 0
--
-- FORMAT = toggle light,GROUP,LEVEL,RAMP RATE eg set light,1,255,0
--
-- SET TRIGGER sets the trigger group to a level
--
-- FORMAT = set trigger,GROUP,LEVEL eg set light,1,255
--
-- TOGGLE TRIGGER toggles the trigger group to between a level and 0
--
-- FORMAT = toggle light,GROUP,LEVEL eg set light,1,255
--
-- GET LIGHT gets the group level
--
-- FORMAT = get light,GROUP eg get light,1
--
-- GET TRIGGER gets the trigger group level
--
-- FORMAT = get trigger,GROUP eg get light,1
--
-- GET LABEL gets the label of group
--
-- FORMAT = get label,CBUS NETWORK,CBUS APPLICATION,GROUP eg get label,0,56,1
switch = function (line)
lchoice = string.lower(line:match("([^,]+)"))
choice = lchoice and tonumber(lchoice) or lchoice
case =
{
['set light'] = function()
cbusfunction, cbusg, cbusl, cbusr = line:match("([^,]+),([^,]+),([^,]+),([^,]+)")
if cbusg == nil then cbusg = "0" end
local cbusgroup = 0 + cbusg
if cbusgroup > 255 then cbusgroup = 0 end
if cbusl == nil then cbusl = "0" end
local cbuslevel = 0 + cbusl
if cbuslevel > 255 then cbuslevel = 255 end
if cbusr == nil then cbusr = "0" end
local cbusramp = 0 + cbusr
log(line)
SetLightingLevel(cbusgroup,cbuslevel,cbusramp)
client:send(cbuslevel)
end,
['toggle light'] = function()
cbusfunction, cbusg, cbusl, cbusr = line:match("([^,]+),([^,]+),([^,]+),([^,]+)")
if cbusg == nil then cbusg = "0" end
local cbusgroup = 0 + cbusg
if cbusgroup > 255 then cbusgroup = 0 end
if cbusl == nil then cbusl = "0" end
local cbuslevel = 0 + cbusl
if cbuslevel > 255 then cbuslevel = 255 end
if cbusr == nil then cbusr = "0" end
local cbusramp = 0 + cbusr
log(line)
CurrentCBusLevel = GetLightingLevel(cbusgroup)
if CurrentCBusLevel == -1 then CurrentCBusLevel = 0 end
if CurrentCBusLevel == 0 then
SetLightingLevel(cbusgroup,cbuslevel,cbusramp)
client:send(cbuslevel)
else
SetLightingLevel(cbusgroup,0,cbusramp)
client:send(0)
end
end,
['set trigger'] = function()
cbusfunction, cbusg, cbusl = line:match("([^,]+),([^,]+),([^,]+)")
if cbusg == nil then cbusg = "0" end
local cbusgroup = 0 + cbusg
if cbusgroup > 255 then cbusgroup = 0 end
if cbusl == nil then cbusl = "0" end
local cbuslevel = 0 + cbusl
if cbuslevel > 255 then cbuslevel = 255 end
log(line)
SetTriggerLevel(cbusgroup,cbuslevel)
client:send(cbuslevel)
end,
['toggle trigger'] = function()
cbusfunction, cbusg, cbusl = line:match("([^,]+),([^,]+),([^,]+)")
if cbusg == nil then cbusg = "0" end
local cbusgroup = 0 + cbusg
if cbusgroup > 255 then cbusgroup = 0 end
if cbusl == nil then cbusl = "0" end
local cbuslevel = 0 + cbusl
if cbuslevel > 255 then cbuslevel = 255 end
log(line)
CurrentCBusLevel = GetTriggerLevel(cbusg)
if CurrentCBusLevel == -1 then CurrentCBusLevel = 0 end
if CurrentCBusLevel == 0 then
SetTriggerLevel(cbusgroup,cbuslevel)
client:send(cbuslevel)
else
SetTriggerLevel(cbusgroup,0)
client:send(0)
end
end,
['get light'] = function()
cbusfunction, cbusg = line:match("([^,]+),([^,]+)")
if cbusg == nil then cbusg = "0" end
local cbusgroup = 0 + cbusg
if cbusgroup > 255 then cbusgroup = 0 end
local cbuslevel = GetLightingLevel(cbusgroup)
if cbuslevel == nil then cbuslevel = 0 end
if cbuslevel == -1 then cbuslevel = 0 end
client:send(cbuslevel)
log(line)
end,
['get trigger'] = function()
cbusfunction, cbusg = line:match("([^,]+),([^,]+)")
if cbusg == nil then cbusg = "0" end
local cbusgroup = 0 + cbusg
if cbusgroup > 255 then cbusgroup = 0 end
local cbuslevel = GetTriggerLevel(cbusgroup)
if cbuslevel == nil then cbuslevel = 0 end
if cbuslevel == -1 then cbuslevel = 0 end
client:send(cbuslevel)
log(line)
end,
['get label'] = function()
cbusfunction, cbusn, cbusa, cbusg = line:match("([^,]+),([^,]+),([^,]+),([^,]+)")
if cbusn == nil then cbusn = "0" end
local cbusnetwork = 0 + cbusn
if cbusnetwork > 255 then cbusnetwork = 0 end
if cbusa == nil then cbusa = "56" end
local cbusapplication = 0 + cbusa
if cbusapplication > 255 then cbusapplication = 255 end
if cbusg == nil then cbusg = "0" end
local cbusgroup = 0 + cbusg
if cbusgroup > 255 then cbusgroup = 0 end
local grptag = GetCBusGroupTag(cbusnetwork, cbusapplication, cbusgroup)
if grptag == nil then grptag = "N/A" end
client:send(grptag)
log(line)
end,
default = function()
end,
}
if case[choice] then
case[choice]()
else
case["default"]()
end
end
local socket = require("socket")
local server = assert(socket.bind("*", 51515))
local ip, port = server:getsockname()
while 1 do
-- wait for a connection from any client
client = server:accept()
-- make sure we don't block waiting for this client's line
client:settimeout(1)
-- receive the line
local line, err = client:receive()
if line ~= nil then
switch(line)
end
client:close()
end