This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

ELK M1 Alarm panel
#5
Hi Admin,

Thank you, I am trying to get the connection working with ssl,  but the connection gets closed.  I want to use copas since this gives more flexibility of building a non-blocking solution.  I have the option also to run code in separate threads. 

Code:
if not ready then
 socket = require("socket")
 copas = require("copas")
 require('ssl')
 ready=true
 esys = {}  

 function checksum(str)
   local sum = 0
   for i = 1, #str do
     sum = (sum + string.byte(str,i)) % 256
   end
   x = 256-sum
   return string.format('%02X', 256 - sum)
 end
 
 function validchecksum(str)
   calccs = checksum(str:sub(1, -3))    -- calculated checksum from message data
   recvcs = str:sub(-2, -1)        -- received checksum
   return calccs == recvcs        -- when they the same return true
 end
 
 --helper functions to round and increment values  
 function increment(n)
   n = n + 1
   return n
 end
 
 function parse(data)
   alert('elk parsing: %s', data)
   if(validchecksum(data)==true)then
     local response = string.sub(data,3,-5)
     alert('elk message is: %s',response)
   end
 end
 
 function sendElk(command)
   skt:send(command)
   sleep(1)    
 end
   
 function init()
   alert('running init')

 end
 
 function fromKNX(command)
   local telegram = string.split(command,',')
   alert('from knx %s',command)

 end
 
 local server = socket.udp()
 server:setsockname("127.0.0.1",33456)
 function handler(skts)
   skts = copas.wrap(skts)
   alert("UDP connection handler")
   while true do
     local s, err
     alert("UDP receiving..")
     s, erro = skts:receive(2048)
     if not s then
       alert("UDP Receive error: %s", erro)
       break
     end
     alert("UDP Received data, bytes: %s",s)
     fromKNX(s)
   end
 end
   
 copas.addserver(server, handler, 1)


--[[ co = copas.addthread(function()
     while true do  
       copas.sleep(5) -- wait 5 seconds in case of other events

       copas.sleep(-1) -- put the thread to sleep until wakeup is called
     end
   end)]]--
end

if not skt then
 -- create tcp client
 sslparams = {
 mode = "client",
 protocol = "sslv3",
 key = "crt/ca-key.pem",
 certificate = "crt/client.pem",
 cafile = "crt/ca-cert.pem",
 verify = "peer",
 options = {"all", "sslv3"}
  }
 skt = copas.wrap(socket.tcp(), sslparams)

 skt, err = socket.connect('192.168.0.11', 2601)
 -- when theres no error connect ok, initialize
 if(not err) then
   skt:settimeout(0)
   -- add receive thread

   copas.addthread(function()
       while true do
         local resp,err = copas.receive(skt)
         -- if theres no connection start a new connection
         if not resp then
           alert("[tcp-client] Receive error: %s", err)
           copas.removeserver(skt)
           skt = nil
           break
         end
         if(string.find(resp,'^16XK')==false)then -- don't process timer messages
           local fd,prtd = pcall(parse,resp)
           if(fd==false)then
             alert("Error with parsemsg %s ",prtd)
           end
         end
       end
     end)
   if skt then
     alert('[tcp-client] connection ok')
     init()
     -- error while connecting,
   else
     if warningfailed then alert('[tcp-client] connection failed (conn): %s', err) end
     return
   end
 else
   alert('[tcp-client] error connecting %s',err)
   return
 end
end
copas.loop()

What am I doing wrong?

Thanks,


Roger
Reply


Messages In This Thread
ELK M1 Alarm panel - by rocfusion - 07.08.2017, 17:04
RE: ELK M1 Alarm panel - by admin - 08.08.2017, 08:50
RE: ELK M1 Alarm panel - by rocfusion - 12.08.2017, 23:21
RE: ELK M1 Alarm panel - by admin - 14.08.2017, 06:13
RE: ELK M1 Alarm panel - by rocfusion - 14.08.2017, 17:14
RE: ELK M1 Alarm panel - by admin - 17.08.2017, 06:30
RE: ELK M1 Alarm panel - by rocfusion - 17.08.2017, 07:07
RE: ELK M1 Alarm panel - by rocfusion - 19.08.2017, 23:32
RE: ELK M1 Alarm panel - by rocfusion - 22.08.2017, 06:31
RE: ELK M1 Alarm panel - by admin - 22.08.2017, 06:52

Forum Jump: