Skip to content

Instantly share code, notes, and snippets.

@svoboda-jan
Created October 2, 2024 10:13
Show Gist options
  • Save svoboda-jan/8d97bfb4dd2e05ab2cb92503ffdd9033 to your computer and use it in GitHub Desktop.
Save svoboda-jan/8d97bfb4dd2e05ab2cb92503ffdd9033 to your computer and use it in GitHub Desktop.
require "socket"
class Pt32
class Status
attr_reader :temperature, :program, :boiler_on
def initialize(temperature, program, boiler_on)
@temperature, @program, @boiler_on = temperature, program, boiler_on
end
end
class NetworkInfo
attr_reader :temperature, :program, :boiler_on
def initialize(ip, netmask, gateway)
@ip, @netmask, @gateway = ip, netmask, gateway
end
end
attr_reader :socket
def initialize(host, port = 4000)
@socket = TCPSocket.new host, port
end
def status
arr_request = [ 0x7f, 0xff, 0x0, 0x08, 0x80, 0x02, 0x0, 0x0 ]
socket.write arr_request.pack("C*")
response = socket.recv(128)
temperature = "#{Integer(response.bytes[8])}.#{response.bytes[10]}".to_f
program = response.bytes[11]
boiler_on = response.bytes[14] == 64
Status.new temperature, program, boiler_on
end
def set_temp(temp, prg = 1, run = 0x01)
arr_request = [ 0x0, 0x02, 0x0, 0x03, prg, run, (temp * 2) ]
socket.write arr_request.pack("C*")
response = socket.recv(128)
end
def get_program(prg)
arr = [ 0x80, 0x0d, 0x0, 0x1, prg ]
socket.write arr.pack("C*")
response = socket.recv(128)
program = {}
day = 0
weekday = 1
(5..87).step(2).each do |i|
hour = response.bytes[i]
temp = response.bytes[i + 1]
program[weekday] ||= []
program[weekday] << {
"hour" => (hour / 8),
"temp" => (temp / 2)
}
day += 1
if day == 6
day = 0
weekday += 1
end
weekday = 0 if weekday == 7
end
program
end
# TODO
# def set_program(prg, arr_data)
#
# end
def turn_off
set_temp 0x00, 0x01, 0x00
end
def set_manual_mode(temp = 19)
set_temp temp, 0x01, 0x00
end
# run time in minutes
def run_time
arr_request = [ 0x0, 0x22, 0x0, 0x1, 0x0 ]
socket.write arr_request.pack("C*")
response = socket.recv(128)
(response.bytes[5] * 60) + response.bytes[6]
end
def network_info
arr_request = [ 0x37, 0x16, 0x0, 0x0 ]
socket.write arr_request.pack("C*")
ip, netmask, gw = socket.recv(128)[4..-1].split(",")
NetworkInfo.new ip, netmask, gw
end
# TODO send close request
def close
socket.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment