myteletask 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,45 @@
1
+ module TeletaskApi
2
+ module ConstantName
3
+ def name( val )
4
+ constants.find{ |name| const_get(name)==val }
5
+ end
6
+ end
7
+ module Command
8
+ extend ConstantName
9
+ SET = 7
10
+ GET = 6
11
+ GROUPSET = 9
12
+ LOG = 3
13
+ EVENTREPORT = 0x10
14
+ WRITEDISPLAY = 4
15
+ KEEPALIVE = 0x11
16
+ end
17
+
18
+ module Function
19
+ extend ConstantName
20
+ RELAY = 1
21
+ DIMMER = 2
22
+ MOTOR = 6
23
+ LOCALMOOD = 9
24
+ GENERALMOOD = 10
25
+ FLAG = 15
26
+ SENSOR = 20
27
+ AUDIO = 31
28
+ PROCESS = 3
29
+ REGIME = 14
30
+ SERVICE = 53
31
+ MESSAGE = 54
32
+ CONDITION = 60
33
+ end
34
+
35
+ module Setting
36
+ extend ConstantName
37
+ ON = 255
38
+ TOGGLE = 103
39
+ OFF = 0
40
+
41
+ MOTORUP = 1
42
+ MOTORDOWN = 2
43
+ MOTORSTOP = 3
44
+ end
45
+ end
@@ -0,0 +1,27 @@
1
+ module TeletaskApi
2
+ class Converter
3
+ def self.short_to_temperature short
4
+ (short.pack("cc").unpack("s>").first / 10.0 - 273).round(1)
5
+ end
6
+
7
+ def self.temperature_to_short temperature_to_short
8
+ raise NotImplementedError
9
+ end
10
+
11
+ def self.byte_to_humidity byte
12
+ raise NotImplementedError
13
+ end
14
+
15
+ def self.humidity_to_byte humidity
16
+ raise NotImplementedError
17
+ end
18
+
19
+ def self.byte_to_lux byte
20
+ raise NotImplementedError
21
+ end
22
+
23
+ def self.lux_to_byte lux
24
+ raise NotImplementedError
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,39 @@
1
+ module TeletaskApi
2
+ class Request
3
+ START = 2
4
+ def initialize command, function = 0, number = 0, setting = nil
5
+ @command = command
6
+ @parameters = Array.new
7
+
8
+ case command
9
+ when Command::KEEPALIVE
10
+ when Command::LOG
11
+ @parameters = Array.new
12
+ @parameters[0] = function
13
+ @parameters[1] = number
14
+ else
15
+ @parameters[0] = 1 #central number
16
+ @parameters[1] = function
17
+ @parameters[2] = 0 #byte1
18
+ @parameters[3] = number #byte2
19
+ @parameters[4] = setting if setting != nil
20
+ end
21
+ end
22
+
23
+ def to_s
24
+ #"s,8,7,1,1,0,21,103,143,"
25
+ request = [START, length, @command] + @parameters + [checksum]
26
+ request.pack("C*")
27
+ end
28
+
29
+ private
30
+ def checksum
31
+ parametersum = @parameters.empty? ? 0 : @parameters.inject{|sum,x| sum + x }
32
+ return (START + length + @command + parametersum) % 256
33
+ end
34
+
35
+ def length
36
+ return @parameters.length + 3
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,59 @@
1
+ require_relative './converter'
2
+
3
+ module TeletaskApi
4
+
5
+ class Response
6
+
7
+ attr_reader :central, :function, :number, :parameters
8
+
9
+ def initialize central, function, number, parameters
10
+ @central, @function, @number, @parameters = central, function, number, parameters
11
+ end
12
+
13
+ def self.parse data
14
+ begin
15
+ data = data.unpack("C*")
16
+ #unless data.first == 10
17
+ startindex = data.index(2)
18
+ raise "Start byte not found in Response: #{data.inspect}" unless startindex
19
+ length = data[startindex+1]
20
+ calculatedChecksum = data[startindex..startindex+length-1].inject{|sum,x| sum + x } % 256
21
+ checksum = data[startindex+length]
22
+ raise "Checksum mismatch. Expecting #{checksum}, but calculated #{calculatedChecksum}" unless checksum == calculatedChecksum
23
+ raise "Not an response." unless data[startindex+2] == Command::EVENTREPORT
24
+ central = data[startindex+3]
25
+ function = data[startindex+4]
26
+ number = data[startindex+5..startindex+6].pack("c*").unpack("n").first
27
+ case function
28
+ when Function::SENSOR
29
+ parameters =data[startindex+8..startindex+9]
30
+ else
31
+ parameters =data[startindex+8]
32
+ end
33
+ return Response.new central, function, number, parameters
34
+ #end
35
+ rescue Exception => ex
36
+ puts ex
37
+ nil
38
+ end
39
+ return nil
40
+ end
41
+
42
+ def to_hash
43
+ hash = Hash.new
44
+ hash[:function] = function
45
+ hash[:function_name] = Function.name function
46
+ hash[:number] = number
47
+ hash[:parameters] = parameters
48
+ case function
49
+ when Function::SENSOR
50
+ hash[:temperature] = Converter.short_to_temperature parameters
51
+ when Function::RELAY
52
+ hash[:state] = parameters
53
+ hash[:state_name] = Setting.name parameters
54
+ end
55
+ hash
56
+ end
57
+ end
58
+
59
+ end
@@ -0,0 +1,88 @@
1
+ require 'socket'
2
+ require 'observer'
3
+ require 'timeout'
4
+
5
+ require_relative './constants'
6
+ require_relative './request'
7
+ require_relative './response'
8
+
9
+
10
+ module TeletaskApi
11
+ class Teletask
12
+ include Observable
13
+
14
+ @host
15
+ @port
16
+ @socket
17
+ @server
18
+
19
+ def initialize(host, port = 55957)
20
+ @host = host
21
+ @port = port
22
+ #add_observer(self)
23
+ end
24
+
25
+ def finalize
26
+ close
27
+ end
28
+
29
+ def connect
30
+ puts "Connecting to Teletask on #{@host} at port #{@port}..."
31
+ @socket = TCPSocket.open @host, @port
32
+ puts "Connected"
33
+ @t = Thread.new{
34
+ while (data = @socket.recv(1024))
35
+ response = Response.parse data
36
+ if response
37
+ changed
38
+ notify_observers(self, response)
39
+ end
40
+ end
41
+ }
42
+ end
43
+
44
+ def close()
45
+ puts "closing connection"
46
+ @socket.close
47
+ end
48
+
49
+ def keep_alive
50
+ f = Request.new Command::KEEPALIVE
51
+ @socket.write f.to_s
52
+ end
53
+
54
+ def set function, number, setting
55
+ f = Request.new Command::SET, function, number, setting
56
+ @socket.write f.to_s
57
+ end
58
+
59
+ def get function, number
60
+ f = Request.new Command::GET, function, number
61
+ @socket.write f.to_s
62
+ end
63
+
64
+ def group_get
65
+ raise NotImplementedError.new
66
+ end
67
+
68
+ def log function, setting
69
+ r = Request.new Command::LOG, function, setting
70
+ @socket.write r.to_s
71
+ end
72
+
73
+ def report_event
74
+ raise NotImplementedError.new
75
+ end
76
+
77
+ def message
78
+ raise NotImplementedError.new
79
+ end
80
+ end
81
+
82
+ class TeleTaskEvent
83
+ def update object, response
84
+ puts "Update: #{response.to_hash.inspect}"
85
+ end
86
+ end
87
+
88
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: myteletask
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sille Van Landschoot
9
+ - Timothy De Mey
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2015-06-01 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description: Teletask gem enables the communication with a Teletask Domitics central
16
+ using the DoIP protocol
17
+ email:
18
+ - info@sillevl.be
19
+ - timothy.de.mey@gmail.com
20
+ executables: []
21
+ extensions: []
22
+ extra_rdoc_files: []
23
+ files:
24
+ - lib/teletask.rb
25
+ - lib/constants.rb
26
+ - lib/request.rb
27
+ - lib/response.rb
28
+ - lib/converter.rb
29
+ homepage: http://rubygems.org/gems/MyTeletask
30
+ licenses:
31
+ - MIT
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 1.8.23
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Teletask DoIP
54
+ test_files: []