simbiotes 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +194 -0
  4. data/Rakefile +33 -0
  5. data/lib/generators/simbiotes/create_model/USAGE +8 -0
  6. data/lib/generators/simbiotes/create_model/create_model_generator.rb +50 -0
  7. data/lib/generators/simbiotes/create_model/templates/simbiotes_logs_model.rb.erb +7 -0
  8. data/lib/generators/simbiotes/create_model/templates/simbiotes_model.rb.erb +27 -0
  9. data/lib/generators/simbiotes/create_model/templates/simbiotes_module.rb.erb +10 -0
  10. data/lib/generators/simbiotes/create_table/USAGE +9 -0
  11. data/lib/generators/simbiotes/create_table/create_table_generator.rb +52 -0
  12. data/lib/generators/simbiotes/create_table/templates/create_logs_table.rb.erb +14 -0
  13. data/lib/generators/simbiotes/create_table/templates/create_table.rb.erb +16 -0
  14. data/lib/generators/simbiotes/initializer/USAGE +8 -0
  15. data/lib/generators/simbiotes/initializer/initializer_generator.rb +18 -0
  16. data/lib/generators/simbiotes/initializer/templates/simbiotes.rb.erb +7 -0
  17. data/lib/generators/simbiotes/initializer/templates/simbiotes_targets.rb.erb +3 -0
  18. data/lib/generators/simbiotes/install/USAGE +11 -0
  19. data/lib/generators/simbiotes/install/install_generator.rb +11 -0
  20. data/lib/generators/simbiotes/migration/USAGE +10 -0
  21. data/lib/generators/simbiotes/migration/migration_generator.rb +54 -0
  22. data/lib/generators/simbiotes/migration/templates/create_logs_table.rb.erb +14 -0
  23. data/lib/generators/simbiotes/migration/templates/migrate_table.rb.erb +12 -0
  24. data/lib/generators/simbiotes/model/USAGE +11 -0
  25. data/lib/generators/simbiotes/model/model_generator.rb +58 -0
  26. data/lib/generators/simbiotes/script/USAGE +8 -0
  27. data/lib/generators/simbiotes/script/script_generator.rb +38 -0
  28. data/lib/generators/simbiotes/script/templates/comm_server.rake +19 -0
  29. data/lib/generators/simbiotes/script/templates/publish_script.rake +6 -0
  30. data/lib/generators/simbiotes/script/templates/script.rb.erb +12 -0
  31. data/lib/generators/simbiotes/script/templates/server.rb +8 -0
  32. data/lib/generators/simbiotes/script/templates/server_control.rb +7 -0
  33. data/lib/simbiotes/client.rb +20 -0
  34. data/lib/simbiotes/configuration.rb +18 -0
  35. data/lib/simbiotes/extract.rb +75 -0
  36. data/lib/simbiotes/insert.rb +89 -0
  37. data/lib/simbiotes/parse.rb +20 -0
  38. data/lib/simbiotes/portal.rb +127 -0
  39. data/lib/simbiotes/railtie.rb +14 -0
  40. data/lib/simbiotes/server.rb +66 -0
  41. data/lib/simbiotes/version.rb +3 -0
  42. data/lib/simbiotes.rb +52 -0
  43. data/lib/tasks/simbiotes_tasks.rake +4 -0
  44. metadata +155 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: defab4e9159fb7dbe9068fcdc94d83d348983bbe
4
+ data.tar.gz: bfc041d76870d672a67997de703ac7a86462365b
5
+ SHA512:
6
+ metadata.gz: 9b6076785ffd4471c6d7e30b0dee448297e818911a645269e3d7b4bb816450f914954813b7c7b1769947e954b4face9a5c7d3582736a00d032a0cfe593c73c5c
7
+ data.tar.gz: 14e72ba3bf7d6210bdee462038c8543a61a798587ef6acebb340c7c50e91b4215571bf3443edde7c41597b7fc2c6288fdf7dadb03ac31d21cd374b45f30a4146
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 MicroArx Corporation
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,194 @@
1
+ # Simbiotes
2
+ The easy way to make your Rails application interact with IoT devices.
3
+
4
+ ## Installation
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem "simbiotes", :git => "https://github.com/MicroArx/simbiotes-gem.git"
9
+ ```
10
+
11
+ Or install it yourself as:
12
+ ```bash
13
+ $ gem install simbiotes #Not working yet
14
+ ```
15
+
16
+ And then execute:
17
+ ```bash
18
+ $ bundle
19
+ $ rails generate simbiotes:install
20
+ ```
21
+ On the Simbiotes portal located at www.simbiotes.com, you can create a Hive for your project. Hives have Workers (each type of IoT device in your project will have a corresponding worker in the Hive). Workers have Drivers and, optionally, Scripts. Drivers and Scripts have interfaces. An example worker might be a Thermostat. The Thermostat has a Control Script with two interfaces: temperature and target temperature. If you set the target temperature to 72 and the temperature is 65, the Thermostat will turn the heat on. We'll use this example again and flesh it out below.
22
+
23
+ Once you have created a Hive, it will give you a public and a private API key. Best practice is to set environment variables in your development, test and production environments to equal these API keys. Then, set config.public_key and config.private_key in config/initializers/simbiotes.rb to those environment variables. You can enter the API keys directly into the configuration file, but you should only do this if you never plan to put your Rails application and Hive into production, as it is a significant security risk.
24
+
25
+
26
+ ```ruby
27
+ Simbiotes.configure do |config|
28
+ config.public_key = ENV['SIMBIOTES_PUBLIC_API_KEY']
29
+ config.private_key = ENV['SIMBIOTES_PRIVATE_API_KEY']
30
+ config.local_port = 8001
31
+ config.server_port = 8000
32
+ config.local_logging = true
33
+ end
34
+ ```
35
+
36
+ ## Usage
37
+ After you have installed the API keys, log in to the Simbiotes portal (www.simbiotes.com) to see that your Hive has Workers and that those Workers have Drivers and, if applicable, Scripts defined. Each Worker, Driver or Script will have one or more Interfaces. For example, if you are making a connected Thermostat, your Hive might have a Worker named 'Thermostat' and it might have the following Drivers/Scripts: Thermometer, Furnace and Control.
38
+
39
+ Each of the Drivers/Scripts has Interfaces (all of this would be viewable on the www.simbiotes.com portal). The Control script might have two interfaces: Temperature and Target Temperature.
40
+
41
+ We want to be able to interact with these interfaces easily through our Rails application for all of the Thermostats that we have out there in the field.
42
+
43
+ ### Models and Tables
44
+
45
+ To do this, we will create a model in our Rails application with attributes that correspond to the interfaces we want to interact with. We will then create records in our Rails database that correspond to our devices. Once we've done that, we can interact with a database record using ActiveRecord as usual, but the attribute values from our database records will be automatically synced to the corresponding device.
46
+
47
+ Because our model is going to sync with our IoT devices, we need to use the Simbiotes model generator that comes with the gem to create the model rather than the model generator that comes by default with Rails. To use it, we pass two arguments, the name of the Worker and the name of the Driver or Script that we want to interact with. In this case, our worker is Thermostat and we want to interact with Control, so we create the model as follows:
48
+
49
+ ```bash
50
+ $ rails generate simbiotes:model Thermostat Control
51
+ ```
52
+
53
+ This generator actually creates a model and a module. The module is in app/models/thermostat.rb and the model in app/models/thermostat/control.rb. If you create models that correspond to one or more of your Thermostat's other Drivers/Scripts, they will be placed in the app/models/thermostat directory as well.
54
+
55
+ Now you should migrate your database in order to create the database tables that correspond to your model.
56
+
57
+ ```bash
58
+ $ rake db:migrate
59
+ ```
60
+
61
+ The migration creates three tables: thermostat_controls, thermostat_control_temperature_logs and thermostat_control_target_temperature_logs. The thermostat_controls table holds the data for temperature and target temperature for all of the devices within your Hive.
62
+
63
+ ### Instances and Devices
64
+
65
+ Now that you have your models and database tables created, you can use them to communicate with the devices that are out in the field.
66
+
67
+ The first thing to do is to create database records that correspond to actual devices. On the Simbiotes Portal (www.simbiotes.com) you will see a list of Worker Instances (each Instance represents an individual device of that particular Worker type) on each Worker page. Each Worker Instance has an id, you'll see them as a long string of characters on the page, one for each Worker Instance.
68
+
69
+ Now, run:
70
+
71
+ ```bash
72
+ $ rails console
73
+ ```
74
+
75
+ This will open up an irb console with your Rails environment (including the Simbiotes gem) loaded.
76
+
77
+ In your console run:
78
+
79
+ ```irb
80
+ > Thermostat.sync_device_instances
81
+ ```
82
+
83
+ That will create a database record for each device you currently have.
84
+
85
+ Next, exit your rails console by typing:
86
+
87
+ ```irb
88
+ > exit
89
+ ```
90
+
91
+ The next thing we need to do is establish a communications channel we can use to communicate between our Rails application and the devices in the field. To do this, we use a local server that handles sending communications to the remote devices and handles communications that we will receive from them.
92
+
93
+ To start it up type:
94
+
95
+ ```bash
96
+ $ rake comms:start
97
+ ```
98
+
99
+ This starts our communications server. If you want to stop the communications server for any reason simply type:
100
+
101
+ ```bash
102
+ $ rake comms:stop
103
+ ```
104
+
105
+ Note that you can only communication with devices if the local communications server is running.
106
+
107
+ ### Creating and uploading scripts.
108
+
109
+ As mentioned, Workers can have both Drivers and Scripts. These are quite similar, but have a very important difference. Drivers are reasonably direct representations of physical interfaces that the device has. In the case of the Thermostat, it has two Drivers, a Thermometer Driver and a Furnace Driver.
110
+
111
+ The Thermometer Driver has one read-only Interface: Temperature. This Interface corresponds with the actual physical temperature sensor on the device that allows us to know what the ambient temperature is at the device.
112
+
113
+ The Furnace Driver has one read/write Interface: Heat. This Interface turns the furnace on or off, and corresponds to the physical interface that the Thermostat has with the physical furnace.
114
+
115
+ In addition to these low level drivers, what we want is something that lets us interact with them and run some logic on board the device. This is where the Script comes in. Like Drivers, Scripts have Interfaces that we can interact with via simple Rails ActiveRecord database operations. The difference is that Scripts calculate the values that their Interfaces hold based on logic and inputs from Drivers or other Scripts on the Device.
116
+
117
+ In the Thermostat case, what we want is to be able to set a target temperature (as mentioned above). If the ambient temperature is below the target temperature, we want to turn on the heat. If the ambient temperature is above the target temperature, we want to turn the heat off.
118
+
119
+ We could do this using logic in our Rails application, instantiate the Thermometer and Furnace Drivers, and set the heat on or off as required. But it would be better to run this logic on the device, and have our Rails application send target temperatures to the device.
120
+
121
+ When you created the Thermostat::Control model, you also created a stub Script file that can contain the logic that will run on the device. this file is at lib/scripts/simbiotes/thermostat/control.rb
122
+
123
+ We edit the stub script file to look like the following:
124
+
125
+ ```ruby
126
+ #######
127
+ # This script will be deployed to your device.
128
+ # The $global variables below represent the values your script will receive/needs to provide to/from its various interfaces and inputs.
129
+ # Your script should change the value of these variables to what they should be, and they will then get passed back.
130
+ # You can also add additional global variables. If you do, these will be passed back to the script along with the
131
+ # interface and input variables the next time the script is invoked. These are useful for keeping state across script
132
+ # invocations.
133
+
134
+ $Temperature
135
+ $Target_Temperature
136
+ $Thermometer_Temperature
137
+ $Furnace_Heat
138
+
139
+ if $Thermometer_Temperature < $Target_Temperature
140
+ $Furnace_Heat = "on"
141
+ else
142
+ $Furnace_Heat = "off"
143
+ end
144
+
145
+ $Temperature = $Thermometer_Temperature
146
+ ```
147
+
148
+ The $Temperature and $Target_Temperature variables hold the values for the Script's interfaces. The script also has two inputs (defined on the Simbiotes portal: www.simbiotes.com). These are the Thermometer Driver's temperature interface and the Furnace Driver's Heat interface. The $Thermometer_Temperature and $Furnace_Heat variables hold the values of these interfaces, respectively.
149
+
150
+ The script has the logic to turn the heat on or off depending on the temperature's relationship to the target temperature. The last line of the script sets the $Temperature variable equal to the $Thermometer_Temperature so that we can access the correct temperature using the temperature interface of the Control script, and not need to access the interface of the Thermometer driver in Rails at all.
151
+
152
+ Once we have completed our edits to the script, we publish it to the Hive by running the following command:
153
+
154
+ ```bash
155
+ $ rake script:publish[Thermostat:Control]
156
+ ```
157
+
158
+ ### Communicating with Devices
159
+
160
+ Now that the preliminaries are out of the way, let's communicate with devices.
161
+
162
+ Open up your rails console again by typing:
163
+
164
+ ```bash
165
+ $ rails c
166
+ ```
167
+
168
+ In your console, type the following to get a device instance and assign it to the variable d:
169
+
170
+ ```irb
171
+ > d = Thermostat::Control.first
172
+ ```
173
+
174
+ The first thing to do is to synchronize the database's data with the data from the device. (This sends a get request for each Interface in the current Driver/Script) To do this type in your console:
175
+
176
+ ```irb
177
+ > d.sync
178
+ ```
179
+
180
+ Now that you are synced with the device, you can set the device's Interfaces easily right from Rails. Let's say you want to set the temperature of your thermostat to 68 degrees. Here's how you do it:
181
+
182
+ ```irb
183
+ > d.target_temperature = 68
184
+ > d.save
185
+ ```
186
+
187
+ All of the communications to the device are taken care of for you. If the value of these Interfaces changes (if the ambient temperature rises or falls for example) then your database will be kept up to date via the communications server without needing to sync the device instance going forward.
188
+
189
+
190
+ ## Contributing
191
+ Contribution directions go here.
192
+
193
+ ## License
194
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Simbiotes'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+
33
+ task default: :test
@@ -0,0 +1,8 @@
1
+ Description:
2
+ This generator creates model files with the right code for Simbiotes interactions.
3
+
4
+ Example:
5
+ rails generate simbiotes:model Thing
6
+
7
+ This will create:
8
+ app/models/thing.rb
@@ -0,0 +1,50 @@
1
+ module Simbiotes
2
+ class CreateModelGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('../templates', __FILE__)
4
+ argument :parent, :type => :string
5
+ argument :name, :type => :string
6
+ argument :portal, :type => :string, :required => false
7
+
8
+ def copy_model_file
9
+ if portal == "true"
10
+ @c = Simbiotes.get_attributes(parent, name)
11
+ end
12
+ template "simbiotes_module.rb.erb", "app/models/#{module_file_name}.rb"
13
+ template "simbiotes_model.rb.erb", "app/models/#{module_file_name}/#{file_name}.rb"
14
+ unless Simbiotes.configuration.local_logging == false
15
+ if portal == "true"
16
+ @c[:attributes].each do |k,v|
17
+ @k = k
18
+ @v = v
19
+ template "simbiotes_logs_model.rb.erb", "app/models/#{module_file_name}/#{file_name}_#{@k.underscore.downcase.gsub(" ","_")}_log.rb"
20
+ end
21
+ else
22
+ @k = name
23
+ template "simbiotes_logs_model.rb.erb", "app/models/#{module_file_name}/#{file_name}_#{@k.underscore.downcase.gsub(" ","_")}_log.rb"
24
+ end
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def file_name
31
+ name.underscore
32
+ end
33
+
34
+ def module_file_name
35
+ parent.underscore
36
+ end
37
+
38
+ def class_name
39
+ name.classify
40
+ end
41
+
42
+ def module_name
43
+ parent.classify
44
+ end
45
+
46
+ def table_prefix
47
+ parent.underscore + "_"
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,7 @@
1
+ module <%= module_name %>
2
+ class <%= class_name + @k.gsub(" ","_").classify%>Log < ApplicationRecord
3
+
4
+ belongs_to :<%=class_name.downcase %>
5
+
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ module <%= module_name %>
2
+ class <%= class_name %> < ApplicationRecord
3
+ <% unless @c == nil %># The methods that correspond to device interfaces are <% @c[:attributes].each do |k,v| %><%=k.downcase.gsub(" ", "_")%> <% end %><% end %>
4
+ <%unless @c == nil %><% unless Simbiotes.configuration.local_logging == false %><% @c[:attributes].each do |k,v| %>
5
+ has_many :<%= class_name.underscore.downcase.gsub(" ", "_") + "_#{k.underscore.downcase.gsub(" ", "_")}_logs"%>, dependent: :destroy
6
+ <% end %><% end %><% end %>
7
+ attr_accessor :skip_extract
8
+ after_commit :extract, unless: :skip_extract
9
+ <% unless @c == nil %><% @c[:attributes].each do |k,v| %><% unless v[:values] == "" %>validates :<%= k.downcase.gsub(" ", "_").underscore %>, inclusion: { in: %w(<%= v[:values].join(" ") %>,
10
+ message: "%{value} is not a valid <%= k %>" }<% end %><% unless v[:range] == "" %>
11
+ validates :<%= k.gsub(" ", "_").underscore %>, inclusion: { in: <%= v[:range] %>,
12
+ message: "%{value} is not within the range <%= v[:range] %>" }<% end %><% if v[:type] == "enum" %>
13
+ enum <%= k.gsub(" ", "_").underscore %>: <%= v[:values] %><% end %><% end %><% end %>
14
+
15
+
16
+ def sync
17
+ Simbiotes.sync(self)
18
+ end
19
+
20
+ private
21
+
22
+ def extract
23
+ Simbiotes::Extract.send(self)
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,10 @@
1
+ module <%= module_name %>
2
+ def self.table_name_prefix
3
+ '<%= table_prefix %>'
4
+ end
5
+
6
+ def self.sync_device_instances
7
+ Simbiotes.sync_device_instances(self.name)
8
+ end
9
+
10
+ end
@@ -0,0 +1,9 @@
1
+ Description:
2
+ Creates a database table with attributes that Simbiotes requires.
3
+
4
+ Example:
5
+ rails generate simbiotes:create_table Table attribute:type attribute:type ... attribute:type
6
+
7
+ This will create:
8
+ db/migrate/{datetime_stamp}_create_table.rb
9
+ db/migrate/{datetime_stamp}_create_table_logs.rb
@@ -0,0 +1,52 @@
1
+ module Simbiotes
2
+ class CreateTableGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('../templates', __FILE__)
4
+ argument :parent, :type => :string
5
+ argument :name, :type => :string
6
+ argument :attributes, :type => :hash, :required => false
7
+
8
+ def copy_create_table_files
9
+ template "create_table.rb.erb", "db/migrate/#{date_string}_create_#{plural_name}.rb"
10
+ unless Simbiotes.configuration.local_logging == false
11
+ sleep 1
12
+ attributes.each do |k,v|
13
+ @k = k
14
+ @v = v
15
+ template "create_logs_table.rb.erb", "db/migrate/#{date_string}_create_#{table_name}_#{@k}_logs.rb"
16
+ sleep 1
17
+ end
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def date_string
24
+ date_string = DateTime.now.strftime("%Y%m%d%H%M%S")
25
+ end
26
+
27
+ def plural_name
28
+ parent.underscore + "_" + name.underscore.pluralize
29
+ end
30
+
31
+ def table_name
32
+ parent.underscore + "_" + name.underscore
33
+ end
34
+
35
+ def class_name
36
+ name.classify
37
+ end
38
+
39
+ def plural_class_name
40
+ parent.classify + name.classify.pluralize
41
+ end
42
+
43
+ def logs_class_name(k)
44
+ parent.classify + name.classify + k.classify + "Logs"
45
+ end
46
+
47
+ def logs_table_name(k)
48
+ parent.underscore + "_" + name.underscore + "_" + k.underscore + "_" + "logs"
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,14 @@
1
+ class Create<%= logs_class_name(@k) %> < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :<%= logs_table_name(@k) %> do |t|
4
+ t.<%= @v %> :<%= @k %>
5
+ t.boolean :<%= @k %>_ack
6
+ t.boolean :<%= @k %>_complete
7
+ t.string :<%= @k %>_timestamp
8
+ t.string :<%= @k %>_status
9
+ t.string :<%= @k %>_action
10
+ t.integer :<%= name.downcase %>_id
11
+ t.timestamps
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ class Create<%= plural_class_name %> < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :<%= plural_name %> do |t|
4
+ <% attributes.each do |k,v| %>
5
+ t.<%= v %> :<%= k %>
6
+ t.boolean :<%= k %>_ack
7
+ t.boolean :<%= k %>_complete
8
+ t.string :<%= k %>_timestamp
9
+ t.string :<%= k %>_status
10
+ t.string :<%= k %>_action
11
+ <% end %>
12
+ t.string :simbiotes_instance
13
+ t.timestamps
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ This generator creates an simbiotes.rb initializer file at config/initializers
3
+
4
+ Example:
5
+ rails generate simbiotes_initializer
6
+
7
+ This will create:
8
+ config/initializers/simbiotes.rb
@@ -0,0 +1,18 @@
1
+ module Simbiotes
2
+ class InitializerGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('../templates', __FILE__)
4
+ argument :portal, :type => :string, :required => false
5
+
6
+ def copy_initializer_file
7
+ if portal == "true"
8
+ @c = Simbiotes::Portal.parse_all_interfaces
9
+ template "simbiotes_targets.rb.erb", "config/initializers/simbiotes_targets.rb"
10
+ else
11
+ template "simbiotes.rb.erb", "config/initializers/simbiotes.rb"
12
+ end
13
+ end
14
+
15
+
16
+
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ Simbiotes.configure do |config|
2
+ config.public_key = nil #set this to an Environment variable
3
+ config.private_key = nil #set this to an Environment variable
4
+ config.local_port = 8001
5
+ config.server_port = 8000
6
+ config.local_logging = true
7
+ end
@@ -0,0 +1,3 @@
1
+ Simbiotes.configure do |config|
2
+ config.targets = <%= @c %>
3
+ end
@@ -0,0 +1,11 @@
1
+ Description:
2
+ Install the initial files for the Simbiotes gem.
3
+
4
+ Example:
5
+ rails g simbiotes:install
6
+
7
+ This will create:
8
+ config/initializers/simbiotes.rb
9
+ lib/scripts/server.rb
10
+ lib/scripts/server_control.rb
11
+ lib/tasks/comm_server.rake
@@ -0,0 +1,11 @@
1
+ module Simbiotes
2
+ class InstallGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ def install
6
+ generate "simbiotes:initializer"
7
+ generate "simbiotes:script"
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ Description:
2
+ Creates database migrations for Simbiotes tables
3
+
4
+ Example:
5
+ rails generate simbiotes:migration AddAttributeToTableName attribute:type attribute:type ... attribute:type
6
+
7
+ This will create:
8
+ db/migrate/{datetime_string}_migration_name.rb
9
+
10
+ The AddAttributeToTableName argument is flexible, but expects an existing Simbiotes table name after the "To" portion of the name.
@@ -0,0 +1,54 @@
1
+ module Simbiotes
2
+ class MigrationGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('../templates', __FILE__)
4
+ argument :name, :type => :string
5
+ argument :attributes, :type => :hash, :required => true
6
+
7
+ def copy_create_table_files
8
+ template "migrate_table.rb.erb", "db/migrate/#{date_string}_#{underscore_name}.rb"
9
+ sleep 1
10
+ attributes.each do |k,v|
11
+ @k = k
12
+ @v = v
13
+ template "create_logs_table.rb.erb", "db/migrate/#{date_string}_#{underscore_name}_logs.rb"
14
+ sleep 1
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def date_string
21
+ date_string = DateTime.now.strftime("%Y%m%d%H%M%S")
22
+ end
23
+
24
+ def table_name
25
+ array = name.underscore.split('_')
26
+ i = 0
27
+ new_array = Array.new
28
+ marker = false
29
+ while i < array.length
30
+ if marker == true
31
+ new_array << array[i]
32
+ end
33
+ if array[i] == "to"
34
+ marker = true
35
+ end
36
+ i += 1
37
+ end
38
+ new_array.join('_')
39
+ end
40
+
41
+ def underscore_name
42
+ name.underscore
43
+ end
44
+
45
+ def class_name
46
+ name.classify
47
+ end
48
+
49
+ def logs_table_name(k)
50
+ table_name + "_" + k.underscore + "_" + "logs"
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,14 @@
1
+ class <%= class_name %>Logs < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :<%= logs_table_name(@k) %> do |t|
4
+ t.<%= @v %> :<%= @k %>
5
+ t.boolean :<%= @k %>_ack
6
+ t.boolean :<%= @k %>_complete
7
+ t.string :<%= @k %>_timestamp
8
+ t.string :<%= @k %>_status
9
+ t.string :<%= @k %>_action
10
+ t.integer :<%= table_name %>_id
11
+ t.timestamps
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ class <%= class_name %> < ActiveRecord::Migration[5.1]
2
+ def change
3
+ <% attributes.each do |k,v| %>
4
+ add_column :<%= table_name %>, :<%= k %>, :<%= v %>
5
+ add_column :<%= table_name %>, :<%= k %>_ack, :boolean
6
+ add_column :<%= table_name %>, :<%= k %>_complete, :boolean
7
+ add_column :<%= table_name %>, :<%= k %>_timestamp, :string
8
+ add_column :<%= table_name %>, :<%= k %>_status, :string
9
+ add_column :<%= table_name %>, :<%= k %>_action, :string
10
+ <% end %>
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ Description:
2
+ Creates a model and associated database table with the attributes specified. If using data from the portal (recommended) Worker should be the name of a worker defined in your Hive, and Model should be the name of a Driver or Script in that Worker.
3
+
4
+ Example:
5
+ rails generate simbiotes:model Worker Model attribute:type attribute:type ... attribute:type
6
+
7
+ This will create:
8
+ app/models/worker.rb
9
+ app/models/worker/model.rb
10
+ db/migrate/{datetime_stamp}_create_table.rb
11
+ db/migrate/{datetime_stamp}_create_table_attribute_logs.rb