teletask 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8350efc1b6d4c835ab2cc2320539fc1ba33d2a10
4
- data.tar.gz: 514ce5aaa2c07422c8b9277e11b9e8120d50c161
3
+ metadata.gz: 5bf110c899a56469721c423745f3ef5c733db49a
4
+ data.tar.gz: 0eb0b4e397f907457ae01f881f94f4f4da9d5c93
5
5
  SHA512:
6
- metadata.gz: 194cd77565b42009579133d36038c74abb3737999c36985800eaf0fb5b4854f2bb7a1265e36d5a668c677d16287167cbc58d7384c16817452da6d6673b9b63e9
7
- data.tar.gz: e2f009831ea9c6b9c0298abe94be035de9be905bde7d97df471879e0b6d0cb1aac2b66973273e28b80dbfccdcda4e3455cdd14f35cd3e693705e4ce65bffe34b
6
+ metadata.gz: 32ab75974fd417efb56248547e3e3ee2635ec7b00dfbe83e22d035a5d3f46e4b24b6ee7f318ac4a5437e295ed39f3f76e5a9f7469cfd61f8028c2d228fa9dd9e
7
+ data.tar.gz: 615974b959e61ab11055b0f8c854e574553e0999575614eb75c409d69d37254795c21c04a8766bbd4bd5bdc64ad492b00c501708a1ccaaf1f039c159418122d8
data/lib/constants.rb CHANGED
@@ -1,5 +1,11 @@
1
- class Teletask
1
+ module TeletaskApi
2
+ module ConstantName
3
+ def name( val )
4
+ constants.find{ |name| const_get(name)==val }
5
+ end
6
+ end
2
7
  module Command
8
+ extend ConstantName
3
9
  SET = 7
4
10
  GET = 6
5
11
  GROUPSET = 9
@@ -10,6 +16,7 @@ class Teletask
10
16
  end
11
17
 
12
18
  module Function
19
+ extend ConstantName
13
20
  RELAY = 1
14
21
  DIMMER = 2
15
22
  MOTOR = 6
@@ -26,6 +33,7 @@ class Teletask
26
33
  end
27
34
 
28
35
  module Setting
36
+ extend ConstantName
29
37
  ON = 255
30
38
  TOGGLE = 103
31
39
  OFF = 0
data/lib/request.rb CHANGED
@@ -1,37 +1,39 @@
1
- class Request
2
- START = 2
3
- def initialize command, function = 0, number = 0, setting = nil
4
- @command = command
5
- @parameters = Array.new
6
-
7
- case command
8
- when Teletask::Command::KEEPALIVE
9
- when Teletask::Command::LOG
1
+ module TeletaskApi
2
+ class Request
3
+ START = 2
4
+ def initialize command, function = 0, number = 0, setting = nil
5
+ @command = command
10
6
  @parameters = Array.new
11
- @parameters[0] = function
12
- @parameters[1] = number
13
- else
14
- @parameters[0] = 1 #central number
15
- @parameters[1] = function
16
- @parameters[2] = 0 #byte1
17
- @parameters[3] = number #byte2
18
- @parameters[4] = setting if setting != nil
7
+
8
+ case command
9
+ when Teletask::Command::KEEPALIVE
10
+ when Teletask::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
19
21
  end
20
- end
21
22
 
22
- def to_s
23
- #"s,8,7,1,1,0,21,103,143,"
24
- request = [START, length, @command] + @parameters + [checksum]
25
- request.pack("C*")
26
- end
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
27
28
 
28
- private
29
- def checksum
30
- parametersum = @parameters.empty? ? 0 : @parameters.inject{|sum,x| sum + x }
31
- return (START + length + @command + parametersum) % 256
32
- end
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
33
34
 
34
- def length
35
- return @parameters.length + 3
35
+ def length
36
+ return @parameters.length + 3
37
+ end
36
38
  end
37
39
  end
data/lib/response.rb CHANGED
@@ -1,33 +1,59 @@
1
- class Response
1
+ require_relative './converter'
2
2
 
3
- attr_reader :central, :function, :number, :parameters
3
+ module TeletaskApi
4
4
 
5
- def initialize central, function, number, parameters
6
- @central, @function, @number, @parameters = central, function, number, parameters
7
- end
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
8
12
 
9
- def self.parse data
10
- begin
11
- data = data.unpack("C*")
12
- unless data.first == 10
13
- startindex = data.index(2)
14
- raise "Start byte not found in Response: #{data.inspect}" unless startindex
15
- length = data[startindex+1]
16
- calculatedChecksum = data[startindex..startindex+length-1].inject{|sum,x| sum + x } % 256
17
- checksum = data[startindex+length]
18
- raise "Checksum mismatch. Expecting #{checksum}, but calculated #{calculatedChecksum}" unless checksum == calculatedChecksum
19
- raise "Not an response." unless data[startindex+2] == Teletask::Command::EVENTREPORT
20
- central = data[startindex+3]
21
- function = data[startindex+4]
22
- number = data[startindex+5..startindex+6].pack("c*").unpack("n").first
23
- parameters =data[startindex+8]
24
- return Response.new central, function, number, parameters
25
- else
26
- return nil
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] == Teletask::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 Teletask::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
27
38
  end
28
- rescue Exception => ex
29
- puts ex
30
- nil
39
+ return nil
40
+ end
41
+
42
+ def to_hash
43
+ hash = Hash.new
44
+ hash[:function] = function
45
+ hash[:function_name] = Teletask::Function.name function
46
+ hash[:number] = number
47
+ hash[:parameters] = parameters
48
+ case function
49
+ when Teletask::Function::SENSOR
50
+ hash[:temperature] = TeletaskApi::Converter.short_to_temperature parameters
51
+ when Teletask::Function::RELAY
52
+ hash[:state] = parameters
53
+ hash[:state_name] = Teletask::Setting.name parameters
54
+ end
55
+ hash
31
56
  end
32
57
  end
58
+
33
59
  end
data/lib/teletask.rb CHANGED
@@ -6,79 +6,87 @@ require_relative './constants'
6
6
  require_relative './request'
7
7
  require_relative './response'
8
8
 
9
- class Teletask
10
- include Observable
11
-
12
- @host
13
- @port
14
- @socket
15
- @server
16
-
17
- def initialize(host, port = 55957)
18
- @host = host
19
- @port = port
20
- #add_observer(self)
9
+ include TeletaskApi
10
+
11
+ module TeletaskApi
12
+
13
+ class Teletask
14
+ include Observable
15
+
16
+ @host
17
+ @port
18
+ @socket
19
+ @server
20
+
21
+ def initialize(host, port = 55957)
22
+ @host = host
23
+ @port = port
24
+ #add_observer(self)
25
+ end
26
+
27
+ def finalize
28
+ close
29
+ end
30
+
31
+ def connect
32
+ puts "Connecting to Teletask on #{@host} at port #{@port}..."
33
+ @socket = TCPSocket.open @host, @port
34
+ puts "Connected"
35
+ @t = Thread.new{
36
+ while (data = @socket.recv(1024))
37
+ response = Response.parse data
38
+ if response
39
+ changed
40
+ notify_observers(self, response)
41
+ end
42
+ end
43
+ }
44
+ end
45
+
46
+ def close()
47
+ puts "closing connection"
48
+ @socket.close
49
+ end
50
+
51
+ def keep_alive
52
+ f = Request.new Command::KEEPALIVE
53
+ @socket.write f.to_s
54
+ end
55
+
56
+ def set function, number, setting
57
+ f = Request.new Command::SET, function, number, setting
58
+ @socket.write f.to_s
59
+ end
60
+
61
+ def get function, number
62
+ f = Request.new Command::GET, function, number
63
+ @socket.write f.to_s
64
+ end
65
+
66
+ def group_get
67
+ raise NotImplementedError.new
68
+ end
69
+
70
+ def log function, setting
71
+ r = Request.new Command::LOG, function, setting
72
+ @socket.write r.to_s
73
+ end
74
+
75
+ def report_event
76
+ raise NotImplementedError.new
77
+ end
78
+
79
+ def message
80
+ raise NotImplementedError.new
81
+ end
21
82
  end
22
83
 
23
- def finalize
24
- close
84
+ class TeleTaskEvent
85
+ def update object, response
86
+ puts "Update: #{response.to_hash.inspect}"
87
+ end
25
88
  end
26
89
 
27
- def connect
28
- puts "connecting..."
29
- @socket = TCPSocket.open @host, @port
30
-
31
- @t = Thread.new{
32
- while (data = @socket.recv(1024))
33
- response = Response.parse data
34
- if response
35
- changed
36
- notify_observers(self, response)
37
- end
38
- end
39
- }
40
- end
41
-
42
- def close()
43
- puts "closing connection"
44
- @socket.close
45
- end
46
-
47
- def keep_alive
48
- f = Request.new Command::KEEPALIVE
49
- @socket.write f.to_s
50
- end
51
-
52
- def set function, number, setting
53
- f = Request.new Command::SET, function, number, setting
54
- @socket.write f.to_s
55
- end
56
-
57
- def get function, number
58
- f = Request.new Command::GET, function, number
59
- @socket.write f.to_s
60
- end
61
-
62
- def group_get
63
- raise NotImplementedError.new
64
- end
65
-
66
- def log function, setting
67
- r = Request.new Command::LOG, function, setting
68
- @socket.write r.to_s
69
- end
70
-
71
- def report_event
72
- raise NotImplementedError.new
73
- end
74
-
75
- def message
76
- raise NotImplementedError.new
77
- end
78
90
  end
79
91
 
80
- class TeleTaskEvent
81
- def update object, response
82
- puts "Update: #{response.inspect}"
83
- end
84
- end
92
+ Teletask = TeletaskApi::Teletask
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: teletask
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sille Van Landschoot
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-06 00:00:00.000000000 Z
11
+ date: 2014-10-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Teletask gem enables the communication with a Teletask Domitics central
14
14
  using the DoIP protocol
@@ -41,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
41
  version: '0'
42
42
  requirements: []
43
43
  rubyforge_project:
44
- rubygems_version: 2.4.1
44
+ rubygems_version: 2.4.2
45
45
  signing_key:
46
46
  specification_version: 4
47
47
  summary: Teletask DoIP