Tamashii Manager is a package for managing IoT devices that can handle communication between IoT devices in a way similar to Rack.
Installation
Add the following code to your Gemfile
:
gem 'tamashii-manager'
And then execute:
$ bundle install
Or install it yourself with:
$ gem install tamashii-manager
Usage
Tamashii Manager can be started directly through tamashii-manager
.
$ tamashii-manager
Because the connection of IoT devices may need verification, we implement a simple Token authentication function, which can achieve through the configuration file.
# config.rb
Tamashii::Manager.config do |config|
config.env = :test
config.auth_type = :token
config.token = 'abc123'
config.port = ENV['PORT'] || 3000
end
Then start with tamashii-manager
:
$ tamashii-manager -C config.rb
Rack
To integrate with the project that use Rack through config.ru
.
# config.ru
require 'tamashii/manager'
require './config.rb'
run Tamashii::Manager.server
Then start Tamashii Manager through the web server.
$ puma
Use Rack :: URLMap
to consolidate your project when collocating with Sinatra and other frameworks.
# config.ru
Rack::URLMap.new(
'/' => App,
'/tamashii' => Tamashii::Manager.server
)
Rails
To integrate with the Rails project, you can use the mount
function to plug Tamashii Manager onto Rails.
# config/routes.rb
Rails.application.routes.draw do
mount Tamashii::Manager.server => '/tamashii'
end
In Rails, we will want to intercept information in the Tamashii Manager, processed in advance and then send to each IoT device, so it will use Tamashii Resolver function.
# config/initializer/tamashii.rb
Tamashii::Manager.config do |config|
config.env = Rails.env
config.log_file = Rails.root.join('log', 'tamashii.log')
config.auth_type = :token
config.token = 'example'
end
Tamashii::Resolver.config do
hook RailsHookForTamashii
end
Use the call
method in Resolver function to handle incoming packets.
# app/tamashii/rails_hook_for_tamashii.rb
class RailsHookForTamashii
def initialize(*args)
super
@client = @env[:client]
end
def call(packet)
# Handle packets here
return false if packet.nil? # The processing failed and let the other Handler go on
true # Finished processing
end
end
In this way, you can use the Hook to handle the packets sent to Tamashii Manager.
send_to method
Tamashii Manager will require a serial number
as a machine ID when authenticating, and so we can use the send_to
function to send packets to a specify machine.
Tamashii::Manager::Client.send_to('example', '...')
Development
To get the source code
$ git clone git@github.com:tamashii-io/tamashii-manager.git
Initialize the development environment
$ ./bin/setup
Run the spec
$ rspec
Installation the version of development on localhost
$ bundle exec rake install
Contribution
Please report to us on Github if there is any bug or suggested modified.
The project was developed by 5xruby Inc.