lightwavez 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: da6552ea173370cbbd96bebb304ad2f6431b53e9
4
+ data.tar.gz: 986c18230dc462567fa5194141240049da7acb43
5
+ SHA512:
6
+ metadata.gz: aed46c72751f7d8996999677352786e8077397f4ed8c99f9480f77d06d78d6283144d13a412bf6b09f46b1dc94219a86388af87ce740c827a057cf1e89318b94
7
+ data.tar.gz: d19df62409641986d48907cf1c88fe21c30f203fd8efd1233ecbbd902e26a94e56236b653fbd4811c1c15a2405dfaace1610ccc88840bd424eeeee3e866cb8dc
data/.gitignore ADDED
@@ -0,0 +1,36 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ ## Specific to RubyMotion:
14
+ .dat*
15
+ .repl_history
16
+ build/
17
+
18
+ ## Documentation cache and generated files:
19
+ /.yardoc/
20
+ /_yardoc/
21
+ /doc/
22
+ /rdoc/
23
+
24
+ ## Environment normalization:
25
+ /.bundle/
26
+ /vendor/bundle
27
+ /lib/bundler/man/
28
+
29
+ # for a library or gem, you might want to ignore these files since the code is
30
+ # intended to run in multiple environments; otherwise, check them in:
31
+ # Gemfile.lock
32
+ # .ruby-version
33
+ # .ruby-gemset
34
+
35
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
36
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1 @@
1
+ source 'https://rubygems.org'
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Khash Sajadi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/lib/client.rb ADDED
@@ -0,0 +1,81 @@
1
+ module Lightwavez
2
+
3
+ require 'socket'
4
+
5
+ class Client
6
+
7
+ COMMANDS = { :on => 'F1', :off => 'F0', :all_off => 'Fa', :last_dim => 'Fo', :dim => 'Fd', :mood => 'Fm'}
8
+
9
+ def initialize(address = nil, port = 9760)
10
+ @address = address || discover
11
+ @port = port
12
+ end
13
+
14
+ # generates a command for a room and a device. Only supports on/off/dimm commands for now
15
+ # accepted operations are: :on, :off, :all_off, :last_dim, :dim (needs param to be 1-32), :mood (needs mood id)
16
+ # device is not needed for :all_off and :mood
17
+ # rooms, devices and moods (params for :mood) and be either a number or name if mapping is loaded
18
+ def get_command(room, device, operation, params = nil)
19
+ raise 'invalid command' unless COMMANDS.has_key? operation
20
+ raise 'missing dim level (1-32)' if params.nil? && operation == :dim
21
+ raise 'missing mood id' if params.nil? && operation == :mood
22
+
23
+ cmd = ""
24
+ if operation != :all_off && operation != :mood
25
+ cmd = "R#{room}D#{device}#{COMMANDS[operation]}"
26
+ if operation == :dim
27
+ raise 'invalid dim level (1-32)' if params < 1 || params > 32
28
+ cmd = cmd + "P#{params}"
29
+ end
30
+ else
31
+ cmd = "R#{room}#{COMMANDS[operation]}"
32
+ if operation == :mood
33
+ cmd = cmd + "P#{params}"
34
+ end
35
+ end
36
+
37
+ return cmd
38
+ end
39
+
40
+ # consructs and sends a command over to the link without waiting for the answer
41
+ def send(command)
42
+ sequence = Random.rand(100)
43
+ send_raw("#{sequence},!#{command}\r")
44
+ end
45
+
46
+ # this will register the first time and get the IP address for the link afte that
47
+ def discover
48
+ sock = UDPSocket.new
49
+ sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
50
+
51
+ result = ""
52
+ server_thread = Thread.start do
53
+ server = UDPSocket.open
54
+ server.bind('0.0.0.0', 9761)
55
+ result = server.recvfrom(64)
56
+ server.close
57
+ end
58
+
59
+ sock.send("100,!F*p\r", 0, '255.255.255.255', 9760)
60
+ server_thread.join
61
+ sock.close
62
+
63
+ if !result.is_a? Array || result.length < 1 || result[1].length < 2
64
+ raise 'invalid response'
65
+ end
66
+
67
+ return result[1][2]
68
+ end
69
+
70
+ private
71
+
72
+ # sends a command to the link without waiting for answer
73
+ def send_raw(command)
74
+ sock = UDPSocket.new
75
+ sock.send(command, 0, @address, @port)
76
+ sock.close
77
+ end
78
+
79
+ end
80
+
81
+ end
data/lib/controller.rb ADDED
@@ -0,0 +1,83 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), "version")
2
+ require File.join(File.expand_path(File.dirname(__FILE__)), "client")
3
+ require 'yaml'
4
+
5
+ module Lightwavez
6
+
7
+ # you will need to create one of these:
8
+ # c = Lightwavez::Controller.new
9
+ class Controller
10
+
11
+ # if addres is missing, it will use discover to find it
12
+ # if mapping_file is missing, it will load 'mapping.yml' from the running folder
13
+ def initialize(address = nil, port = 9760, mapping_file = "")
14
+ @mapping = {}
15
+
16
+ mapping_file = File.join(Dir.pwd, 'mapping.yml') if mapping_file == ""
17
+ if File.exists? mapping_file
18
+ @mapping = YAML.load_file(mapping_file)
19
+ end
20
+
21
+ @client = Lightwavez::Client.new(address, port)
22
+ end
23
+
24
+ # registers the client with the link. The first time this runs the lights on the link
25
+ # will flash and the button needs to be pressed to authorise the client
26
+ # calling this again after initial registration will return the IP address of the link
27
+ def register
28
+ @client.discover
29
+ end
30
+
31
+ # sends a command to the link.
32
+ # room is the name of the room based on mapping
33
+ # device is the name of the device based on mapping. pass '' if the command doesn't need it (all_off and mood)
34
+ # operation can be :on, :off, :all_off, :dim, :last_dim, :mood
35
+ # params is used for dim (1-32) and mood (mood name based on mapping)
36
+ def send(room, device, operation, params = {})
37
+ room_id = get_room(room)['id']
38
+ device_id = get_device(room, device)
39
+
40
+ puts "Room: #{room_id}, Device: #{device_id}"
41
+
42
+ pa = nil
43
+ if operation == :mood
44
+ pa = get_mood(room, params)
45
+
46
+ puts "Mood: #{params}"
47
+ else
48
+ pa = params
49
+ end
50
+
51
+ cmd = @client.get_command(room_id, device_id, operation, pa)
52
+
53
+ puts "Sending command #{cmd}"
54
+
55
+ @client.send(cmd)
56
+ end
57
+
58
+ private
59
+
60
+ def get_room(room)
61
+ raise 'invalid mapping file. no "rooms" node found' unless @mapping.has_key? 'rooms'
62
+ if @mapping['rooms'].has_key? room
63
+ return @mapping['rooms'][room]
64
+ else
65
+ raise "invalid room name #{room}"
66
+ end
67
+ end
68
+
69
+ def get_device(room, device)
70
+ m_room = get_room(room)
71
+ raise "invalid mapping format for room #{room}" unless m_room.has_key? 'devices'
72
+ return m_room['devices'][device]
73
+ end
74
+
75
+ def get_mood(room, mood)
76
+ m_room = get_room(room)
77
+ raise "invalid mapping format for room #{room}" unless m_room.has_key? 'moods'
78
+ return m_room['moods'][mood]
79
+ end
80
+
81
+ end
82
+
83
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Lightwavez
2
+ VERSION = Gem::Version.new("0.0.1")
3
+ end
@@ -0,0 +1,19 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require './lib/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lightwavez"
8
+ spec.version = Lightwavez::VERSION
9
+ spec.authors = ["Khash Sajadi"]
10
+ spec.email = ["khash@sajadi.co.uk"]
11
+ spec.summary = "A simple Ruby gem to control LightwaveRF link"
12
+ spec.description = "This is a quick and simple ruby client gem for LightwaveRF link. No liability is accepted etc etc."
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+ end
data/mapping.yml ADDED
@@ -0,0 +1,13 @@
1
+ # Modify as needed. This is a sample
2
+ rooms:
3
+ main:
4
+ id: 1
5
+ devices:
6
+ living_room: 1
7
+ counter: 2
8
+ kitchen: 3
9
+ hallway: 4
10
+ moods:
11
+ kitchen_work: 1
12
+ tv: 2
13
+ cooking: 3
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lightwavez
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Khash Sajadi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This is a quick and simple ruby client gem for LightwaveRF link. No liability
14
+ is accepted etc etc.
15
+ email:
16
+ - khash@sajadi.co.uk
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - Gemfile
23
+ - LICENSE
24
+ - lib/client.rb
25
+ - lib/controller.rb
26
+ - lib/version.rb
27
+ - lightwavez.gemspec
28
+ - mapping.yml
29
+ homepage:
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 2.2.2
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: A simple Ruby gem to control LightwaveRF link
53
+ test_files: []