0 użytkowników i 1 Gość przegląda ten wątek.
--[[ iKettle This virtual device has been created by and is copyright (c) 2014, Fibaro UK Ltd http://www.FibaroUK.co.uk/ You may use this virtual device for personal or commercial purposes however please respect our intellectual property and leave this comment block intact and unmodified.]]-- VERSION 1.1-- change log at bottom of main loop--[[At the top of this page, make sure you change: - The IP address to that of the iKettle - The TCP Port to 2000 This virtual device uses a global variable to pass status changes to other scenes you must create the global variable in panels->variables in order to use this feature. The global variable should be called 'iKettleFB']]-- Only run this block of code once when we first connect to the iKettleif not (started == 'YES') then started = 'YES' actionTable = { a0x100 = '100C selected', a0x95 = '95C selected', a0x80 = '80C selected', a0x65 = '65C selected', a0x11 = 'Warm selected', a0x10 = 'Warm has ended', a0x5 = 'Turned on', a0x0 = 'Turned off', a0x8005 = 'Warm length is 5 minutes', a0x8010 = 'Warm length is 10 minutes', a0x8020 = 'Warm length is 20 minutes', a0x3 = 'Reached temperature', a0x2 = 'Problem (boiled dry?)', a0x1 = 'Kettle was removed (whilst on)' } buffer = '' -- create an empty buffer local thismodule = fibaro:getSelfId() local ip = fibaro:getValue(thismodule, 'IPAddress') local port = fibaro:getValue(thismodule, 'TCPPort') tcpSocket = Net.FTcpSocket(ip, port) tcpSocket:setReadTimeout(1000) bytes, errorCode = tcpSocket:write("get sys status\n") fibaro:debug('Connected to iKettle') fibaro:log('Connected')endfunction bit(bitNum) return 2 ^ (bitNum - 1)endfunction hasbit(data, p) return data % (p + p) >= pendfunction setbit(data, p, setIt) if (setIt) then return hasbit(data, p) and data or data + p else return hasbit(data, p) and data - p or data endendfunction processKey(key) return { on = hasbit(key, bit(1)), warm = hasbit(key, bit(2)), temp65 = hasbit(key, bit(3)), temp80 = hasbit(key, bit(4)), temp95 = hasbit(key, bit(5)), temp100 = hasbit(key, bit(6)) }endfunction setGV(value) fibaro:setGlobal('iKettleFB', value)endfunction processCommand(cmnd) -- a command will always start with 'sys status ' -- so we can remove the first 11 characters. local action = string.sub(cmnd, 12) -- from the 12th char onwards pos = string.find(action, "key=") if (pos) then -- this is a response to a get sys status request state = processKey(string.byte(action, 5, -1)) local res = '' if (state.on) then res = res .. 'On' setGV('0x5') else res = res .. 'Off' setGV('0x0') end if state.warm then res = res .. ' and warming' setGV('0x11') end if state.temp65 then res = res .. ' at 65 degrees' setGV('0x65') end if state.temp80 then res = res .. ' at 80 degrees' setGV('0x80') end if state.temp95 then res = res .. ' at 95 degrees' setGV('0x95') end if state.temp100 then res = res .. ' at 100 degrees' setGV('0x100') end return res .. '.' else --- this is an asyncronus status update setGV(action) message = actionTable['a' .. action] or action return message .. '.' endendfunction parseIt(buf) -- find the first occurance of 'set state ' pos = string.find(buf, "sys status ") if (pos) then buf = string.sub(buf, pos) -- trim any junk from the beginning endPos = string.find(buf, '\r') while (endPos) do -- we have a potential command command = string.sub(buf, 1, endPos-1) buf = string.sub(buf, endPos + 1) local feedback = processCommand(command) fibaro:call(fibaro:getSelfId(), "setProperty", "ui.labStatus.value", feedback) fibaro:debug(feedback) fibaro:log(feedback) endPos = string.find(buf, '\r') end end return bufendstate = tcpSocket:read()if (tostring(state) ~= '') then buffer = (buffer .. state) -- add anything we read from the socket to the buffer buffer = parseIt(buffer) -- process any useful stuff from the buffer and keep the leftoversend--[[Change log1.1- Added buttons for warm on/off, 5 mins, 10 mins and 20 mins.- Added support for HCL (no feedback)1.0- Initial release Buttons: [On,Off,100,95,80,65], Status - all feedback]]