tamashii-common 0.1.6 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +120 -15
- data/lib/tamashii/common.rb +0 -2
- data/lib/tamashii/common/version.rb +1 -1
- data/lib/tamashii/resolver.rb +2 -1
- metadata +2 -4
- data/lib/tamashii/config.rb +0 -55
- data/lib/tamashii/hook.rb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fcc88eaa8ec67a5c9098396905962e21ceca0ba6
|
4
|
+
data.tar.gz: 25321f5d4b2c993f421caf122b4dcafe1e9a2122
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68ef0d4e242e1597d4cf9804fc1fd56507843720f8bd6dc12b4b1c37801b8b57a7850ec939eeccebe204edc65ee55fc37a92a6976983087cc4700ae18930235f
|
7
|
+
data.tar.gz: 9420294cb59cf5b8c8ae90641456d899bd6eef0ea29617c525b96c8d67cf682d2c185388abdc5983cbc42f08e33b1c2dfadea1062b5b143dda03e8f98badd192
|
data/README.md
CHANGED
@@ -1,38 +1,143 @@
|
|
1
|
-
|
1
|
+
Tamashii Common
|
2
|
+
===
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
4
|
+
Tamashii Common is a collection of some commonly used features in Tamashii, including recorded, setting, packet encapsulation, etc.
|
6
5
|
|
7
6
|
## Installation
|
8
7
|
|
9
|
-
Add
|
8
|
+
Add the following code to your `Gemfile`:
|
10
9
|
|
11
10
|
```ruby
|
12
11
|
gem 'tamashii-common'
|
13
12
|
```
|
14
13
|
|
15
14
|
And then execute:
|
15
|
+
```ruby
|
16
|
+
$ bundle install
|
17
|
+
```
|
16
18
|
|
17
|
-
|
19
|
+
Or install it yourself with:
|
20
|
+
```ruby
|
21
|
+
$ gem install tamashii-common
|
22
|
+
```
|
18
23
|
|
19
|
-
|
24
|
+
## Usage
|
20
25
|
|
21
|
-
|
26
|
+
### Tamashii::Resolver
|
22
27
|
|
23
|
-
|
28
|
+
Used to handle with different types of packets in Tamashii.
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
Tamashii::Resolver.handle Tamashii::AUTH_TOKEN, Tamashii::Manager::Authorization
|
32
|
+
```
|
33
|
+
|
34
|
+
#### Default processing
|
35
|
+
|
36
|
+
In the absence of a specified processing mode, we can specify the way the default processing.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
Tamashii::Resolver.default_handler Tamashii::Manager::Handler::Broadcaster
|
40
|
+
```
|
41
|
+
|
42
|
+
#### Hook
|
43
|
+
|
44
|
+
This is a mechanism similar to Rack Middleware, which can intercept or process packets before the Handler executes.
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
Tamashii::Resolver.hook TamashiiRailsHook
|
48
|
+
```
|
49
|
+
|
50
|
+
When there are lots of items need to be setting, we can write the following way instead.
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
Tamashii::Resolver.config do
|
54
|
+
handler Tamashii::Manager::Authorization
|
55
|
+
# ...
|
56
|
+
|
57
|
+
hook MyRailsHook
|
58
|
+
# ...
|
59
|
+
end
|
60
|
+
```
|
24
61
|
|
25
|
-
|
62
|
+
### Tamashii::Packet
|
63
|
+
|
64
|
+
IoT devices need some information about the data when transferring, so define a simple format to encapsulate the data in Tamashii.
|
65
|
+
|
66
|
+
|Field |Size |Description
|
67
|
+
|--- |--- |-
|
68
|
+
|type | 1byte |control code(Octal)
|
69
|
+
|tag | 2bytes |tag, default 0 is broadcast(will be removed)
|
70
|
+
|size | 2bytes |data size
|
71
|
+
|body | ~ |data content
|
72
|
+
|
73
|
+
We can use `Tamashii::Packet` to encapsulate different types of information, e.g., String, JSON,and Binary.
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
packet = Tamashii::Packet.new(Tamashii::Type::AUTH_TOKEN, 0, '1234')
|
77
|
+
```
|
78
|
+
|
79
|
+
When we transfer data, we will translate the data into a Binary format before the transmission. If you want to convert, then we can use `dump` and `load` these two methods.
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
buffer = packet.dump
|
83
|
+
recv_packet = Tamashii::Packet.load(buffer)
|
84
|
+
```
|
85
|
+
|
86
|
+
To get the original content of the data, you can use `type` and` body` to determine what information you want to access.
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
if packet.type == Tamashii::Type::AUTH_TOKEN
|
90
|
+
puts packet.body # AUTH_TOKEN is a string
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
### Tamashii::Type
|
95
|
+
|
96
|
+
There are a wide variety of types data exchange in Tamashii. In order to access these packet easily, we define a series of settings to assist in accessing these types.
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
# System Action
|
100
|
+
Tamashii::Type::POWEROFF
|
101
|
+
Tamashii::Type::REBOOT
|
102
|
+
# ...
|
103
|
+
```
|
104
|
+
|
105
|
+
|Type id|Name|Description
|
106
|
+
|------|---------|-
|
107
|
+
|000 |POWEROFF|Shut down
|
108
|
+
|001 |REBOOT|Restart
|
109
|
+
|002 |RESTART|Restart service
|
110
|
+
|003 |UPDATE|Update the software
|
111
|
+
|010 |AUTH_TOKEN|Token certification
|
112
|
+
|017 |AUTH_RESPONSE|Certification results
|
113
|
+
|030 |RFID_NUMBER|Authentication card number
|
114
|
+
|031 |RFID_DATA|Card information
|
115
|
+
|036 |RFID_RESPONSE_JSON|Card reader results
|
116
|
+
|037 |RFID_RESPONSE_STRING|Card reader results
|
117
|
+
|040 |BUZZER_SOUND|Make buzzer sound
|
118
|
+
|050 |LCD_MESSAGE|Display LCD text
|
119
|
+
|051 |LCD_SET_IDLE_TEXT|Set the display text when standby
|
26
120
|
|
27
121
|
## Development
|
28
122
|
|
29
|
-
|
123
|
+
To get the source code
|
124
|
+
|
125
|
+
$ git clone git@github.com:tamashii-io/tamashii-common.git
|
126
|
+
|
127
|
+
Initialize the development environment
|
128
|
+
|
129
|
+
$ ./bin/setup
|
130
|
+
|
131
|
+
Run the spec
|
132
|
+
|
133
|
+
$ rspec
|
30
134
|
|
31
|
-
|
135
|
+
Installation the version of development on localhost
|
32
136
|
|
33
|
-
|
137
|
+
$ bundle exec rake install
|
138
|
+
## Contribution
|
34
139
|
|
35
|
-
|
140
|
+
Please report to us on [Github](https://github.com/tamashii-io/tamashii-common) if there is any bug or suggested modified.
|
36
141
|
|
37
|
-
|
142
|
+
The project was developed by [5xruby Inc.](https://5xruby.tw/)
|
38
143
|
|
data/lib/tamashii/common.rb
CHANGED
@@ -3,10 +3,8 @@ require "tamashii/packet"
|
|
3
3
|
require "tamashii/type"
|
4
4
|
require "tamashii/agent_hint"
|
5
5
|
require "tamashii/handler"
|
6
|
-
require "tamashii/hook"
|
7
6
|
require "tamashii/resolver"
|
8
7
|
require "tamashii/logger"
|
9
|
-
require "tamashii/config"
|
10
8
|
require "tamashii/environment"
|
11
9
|
|
12
10
|
module Tamashii
|
data/lib/tamashii/resolver.rb
CHANGED
@@ -7,7 +7,7 @@ module Tamashii
|
|
7
7
|
def method_missing(name, *args, &block)
|
8
8
|
self.instance.send(name, *args, &block)
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
def instance
|
12
12
|
@instance ||= self.new
|
13
13
|
end
|
@@ -17,6 +17,7 @@ module Tamashii
|
|
17
17
|
@handlers ||= {}
|
18
18
|
end
|
19
19
|
|
20
|
+
# TODO: Rename hook to middleware
|
20
21
|
def hooks
|
21
22
|
@hooks ||= []
|
22
23
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tamashii-common
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- 蒼時弦也
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: exe
|
12
12
|
cert_chain: []
|
13
|
-
date: 2017-
|
13
|
+
date: 2017-11-20 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
@@ -118,10 +118,8 @@ files:
|
|
118
118
|
- lib/tamashii/agent_hint.rb
|
119
119
|
- lib/tamashii/common.rb
|
120
120
|
- lib/tamashii/common/version.rb
|
121
|
-
- lib/tamashii/config.rb
|
122
121
|
- lib/tamashii/environment.rb
|
123
122
|
- lib/tamashii/handler.rb
|
124
|
-
- lib/tamashii/hook.rb
|
125
123
|
- lib/tamashii/logger.rb
|
126
124
|
- lib/tamashii/packet.rb
|
127
125
|
- lib/tamashii/resolver.rb
|
data/lib/tamashii/config.rb
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
require "tamashii/environment"
|
2
|
-
|
3
|
-
module Tamashii
|
4
|
-
class Config < Hash
|
5
|
-
SHARED_CONFIG = %i(auth_type token log_file log_level token env).freeze
|
6
|
-
|
7
|
-
class << self
|
8
|
-
attr_accessor :default_config
|
9
|
-
|
10
|
-
def method_missing(name, *args, &block)
|
11
|
-
(@instance ||= self.new).send(name, *args, &block)
|
12
|
-
end
|
13
|
-
|
14
|
-
def register(name, default = nil)
|
15
|
-
self.default_config ||= {}
|
16
|
-
self.default_config[name.to_sym] = default
|
17
|
-
end
|
18
|
-
|
19
|
-
def [](name)
|
20
|
-
send(name.to_sym)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def initialize
|
25
|
-
@accept_config = Set.new(SHARED_CONFIG)
|
26
|
-
(self.class.default_config || {}).each { |name, value| register(name, value) }
|
27
|
-
end
|
28
|
-
|
29
|
-
def method_missing(name, *args, &block)
|
30
|
-
return unless accept?(name) || accept?(name[0..-2])
|
31
|
-
return register(name[0..-2], args.first) if name[-1..-1] == "="
|
32
|
-
return register(name, args.first) if args.size > 0
|
33
|
-
return register(name, yield(self)) if block_given?
|
34
|
-
self[name]
|
35
|
-
end
|
36
|
-
|
37
|
-
def register(key, default = nil)
|
38
|
-
@accept_config.add(key.to_sym)
|
39
|
-
self[key.to_sym] = default if default
|
40
|
-
end
|
41
|
-
|
42
|
-
def accept?(key)
|
43
|
-
@accept_config.include?(key.to_sym)
|
44
|
-
end
|
45
|
-
|
46
|
-
def env(env = nil)
|
47
|
-
return Environment.new(self[:env]) if env.nil?
|
48
|
-
self[:env] = env.to_s
|
49
|
-
end
|
50
|
-
|
51
|
-
def env=(env)
|
52
|
-
self[:env] = env.to_s
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|