simbiotes 0.1.7 → 0.1.8

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: a0b73563d6456052d13077e9ed9127acffed6ed5
4
- data.tar.gz: 78c54d38eebf679a8c4e6fee7e01f9d2092d7712
3
+ metadata.gz: e16db3b4d2fbc5b2c45392f8c784c8947f421648
4
+ data.tar.gz: 420e5d67aef8834e313bafd585bc6f55096dc60c
5
5
  SHA512:
6
- metadata.gz: 9656ff68fd8bb7737b78f54320298c6df229522d646d2d0918dc600f1752d661e3ad72f2d559a0ecd71d3999b174b2d28db260dafd9959badbab365ccc4a5806
7
- data.tar.gz: f53567489f15567f3b267e2618d1e4082f9397a52ea193c912ba7c29f5800415db5587f162209469d77dec073e20ac0ff63a12c4b05c23d4feb218a83d159112
6
+ metadata.gz: de03d2823abfe34895d1ed13b46f4e400688400d2d4574554b67a26f03628035c175aaafa4114f43b36e97f5175849e80663b35d3fdaa7b5e51c55a5eeeae746
7
+ data.tar.gz: 0252fc8f0cc05632037e3d9abdf73b2479b00f81336d2595d1eeb9b4172d24a0463a99c3c1c802af4bb776fabba0b0f08aee80312a2a36d1cb95183982a92465
data/README.md CHANGED
@@ -130,57 +130,6 @@ $ rake test_comms:stop
130
130
 
131
131
  Note that you can only communicate with devices if the local communications server is running.
132
132
 
133
- ### Creating and uploading scripts.
134
-
135
- 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.
136
-
137
- 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.
138
-
139
- 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.
140
-
141
- 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.
142
-
143
- 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.
144
-
145
- 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.
146
-
147
- 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
148
-
149
- We edit the stub script file to look like the following:
150
-
151
- ```ruby
152
- #######
153
- # This script will be deployed to your device.
154
- # The $global variables below represent the values your script will receive/needs to provide to/from its various interfaces and inputs.
155
- # Your script should change the value of these variables to what they should be, and they will then get passed back.
156
- # You can also add additional global variables. If you do, these will be passed back to the script along with the
157
- # interface and input variables the next time the script is invoked. These are useful for keeping state across script
158
- # invocations.
159
-
160
- $Temperature
161
- $Target_Temperature
162
- $Thermometer_Temperature
163
- $Furnace_Heat
164
-
165
- if $Thermometer_Temperature < $Target_Temperature
166
- $Furnace_Heat = "on"
167
- else
168
- $Furnace_Heat = "off"
169
- end
170
-
171
- $Temperature = $Thermometer_Temperature
172
- ```
173
-
174
- 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.
175
-
176
- 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.
177
-
178
- Once we have completed our edits to the script, we publish it to the Hive by running the following command:
179
-
180
- ```bash
181
- $ rake script:publish[Thermostat:Control]
182
- ```
183
-
184
133
  ### Communicating with Devices
185
134
 
186
135
  Now that the preliminaries are out of the way, let's communicate with devices.
@@ -1,6 +1,6 @@
1
1
  module <%= module_name %>
2
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 %>
3
+ <% unless @c == nil %># The method(s) that correspond to device interfaces are: <% @c[:attributes].each do |k,v| %><%=k.downcase.gsub(" ", "_")%> <% end %><% end %>
4
4
  <%unless @c == nil %><% unless Simbiotes.configuration.local_logging == false %><% @c[:attributes].each do |k,v| %>
5
5
  has_many :<%= class_name.underscore.downcase.gsub(" ", "_") + "_#{k.underscore.downcase.gsub(" ", "_")}_logs"%>, dependent: :destroy
6
6
  <% end %><% end %><% end %>
@@ -9,10 +9,17 @@ module Simbiotes
9
9
  template "simbiotes_targets.rb.erb", "config/initializers/simbiotes_targets.rb"
10
10
  else
11
11
  template "simbiotes.rb.erb", "config/initializers/simbiotes.rb"
12
+ template "simbiotes_settings.rb.erb", "db/migrate/#{date_string}_create_simbiotes_settings.rb"
12
13
  end
13
14
  end
15
+
16
+
14
17
 
15
-
18
+ private
19
+
20
+ def date_string
21
+ date_string = DateTime.now.strftime("%Y%m%d%H%M%S")
22
+ end
16
23
 
17
24
  end
18
25
  end
@@ -3,5 +3,6 @@ Simbiotes.configure do |config|
3
3
  config.private_key = nil #set this to an Environment variable
4
4
  config.local_port = 8001
5
5
  config.server_port = 8000
6
+ config.tls = true
6
7
  config.local_logging = true
7
8
  end
@@ -0,0 +1,9 @@
1
+ class CreateSimbiotesSettings < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :simbiotes_settings do |t|
4
+ t.string :key
5
+ t.text :value
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -1,7 +1,7 @@
1
1
  module Simbiotes
2
2
  class Configuration
3
3
 
4
- attr_accessor :public_key, :private_key, :local_logging, :targets, :local_port, :server, :server_port, :portal, :push
4
+ attr_accessor :public_key, :private_key, :local_logging, :targets, :local_port, :server, :server_port, :portal, :push, :tls
5
5
 
6
6
  def initialize
7
7
  @public_key = nil
@@ -13,6 +13,7 @@ module Simbiotes
13
13
  @server_port = 8000
14
14
  @portal = "http://www.simbiotes.com/"
15
15
  @push = false
16
+ @tls = true
16
17
  end
17
18
 
18
19
  end
@@ -130,5 +130,11 @@ module Simbiotes
130
130
  }
131
131
  r = RestClient.post("#{Simbiotes.configuration.portal}api/upload_script", payload)
132
132
  end
133
+
134
+ def self.generate_certificate(csr)
135
+ instance_hash = JSON.parse(HTTParty.post("#{Simbiotes.configuration.portal}api/generate_certificate", :query => {:public_key => Simbiotes.configuration.public_key, :private_key => Simbiotes.configuration.private_key, :csr => csr}).body)
136
+ return instance_hash["certificate"]
137
+ end
138
+
133
139
  end
134
140
  end
@@ -3,9 +3,23 @@
3
3
  # to the remote server. Messages received from the remote server are forwarded to the appropriate logic.
4
4
 
5
5
  require "socket"
6
+ require 'openssl'
6
7
  module Simbiotes
7
8
  class Server
8
- def initialize( server = TCPSocket.open( Simbiotes.configuration.server, Simbiotes.configuration.server_port ) )
9
+ def initialize
10
+ if Simbiotes.configuration.tls == true
11
+ socket = TCPSocket.new(Simbiotes.configuration.server, Simbiotes.configuration.server_port)
12
+ context = OpenSSL::SSL::SSLContext.new
13
+ context.key = Server.key
14
+ context.cert = Server.cert
15
+ context.ca_cert = Server.ca_cert
16
+ context.verify_mode = OpenSSL::SSL::VERIFY_PEER
17
+ server = OpenSSL::SSL::SSLSocket.new socket, context
18
+ server.sync_close = true
19
+ server.connect
20
+ else
21
+ server = TCPSocket.open(Simbiotes.configuration.server, Simbiotes.configuration.server_port)
22
+ end
9
23
  @server = server
10
24
  @localport = Simbiotes.configuration.local_port
11
25
  listen_remote
@@ -61,6 +75,84 @@ module Simbiotes
61
75
  end
62
76
  end
63
77
 
78
+ def self.key
79
+ key = SimbiotesSetting.find_by(key: "key")
80
+ if key == nil
81
+ key = Server.generate_key
82
+ else
83
+ key = key.value
84
+ end
85
+ pass_phrase = Setting.find_by(key: "key_pass_phrase").value
86
+ key = OpenSSL::PKey::RSA.new key, pass_phrase
87
+ return key
88
+ end
89
+
90
+ def self.cert
91
+ cert = Setting.find_by(key: "cert")
92
+ if cert == nil
93
+ key = SimbiotesSetting.find_by(key: "key").value
94
+ key = OpenSSL::PKey::RSA.new key
95
+ public_key = SimbiotesSetting.find_by(key: "public_key").value
96
+ public_key = OpenSSL::PKey::RSA.new public_key
97
+ cert = Server.generate_cert(key, public_key)
98
+ c = SimbiotesSetting.new
99
+ c.key = "cert"
100
+ c.value = cert
101
+ c.save
102
+ else
103
+ cert = cert.value
104
+ end
105
+ cert = OpenSSL::X509::Certificate.new cert
106
+ return cert
107
+ end
108
+
109
+ def self.generate_key
110
+ key = OpenSSL::PKey::RSA.new 2048
111
+ pass_phrase = 'simbiotes'
112
+ cipher = OpenSSL::Cipher.new 'AES-128-CBC'
113
+ s = SimbiotesSetting.new
114
+ s.key = "key_cipher"
115
+ s.value = "OpenSSL::Cipher.new 'AES-128-CBC'"
116
+ s.save
117
+ s = SimbiotesSetting.new
118
+ s.key = "key_pass_phrase"
119
+ s.value = 'simbiotes'
120
+ s.save
121
+ s = SimbiotesSetting.new
122
+ s.key = "public_key"
123
+ s.value = key.public_key.to_pem
124
+ s.save
125
+ s = SimbiotesSetting.new
126
+ s.key = "key"
127
+ s.value = key.export(cipher, pass_phrase)
128
+ s.save
129
+ return s.value
130
+ end
131
+
132
+ def self.generate_cert(key, public_key)
133
+ csr = OpenSSL::X509::Request.new
134
+ csr.version = 0
135
+ csr.subject = Setting.cert_name
136
+ csr.public_key = key.public_key
137
+ csr.sign key, OpenSSL::Digest::SHA1.new
138
+ cert = Simbiotes::Portal.generate_certificate(csr)
139
+ return cert
140
+ end
141
+
142
+ def self.ca_cert
143
+ ca_cert = Setting.find_by(key: "ca_cert")
144
+ if ca_cert == nil
145
+ ca_cert = Simbiotes::Portal.generate_certificate(csr)
146
+ c = SimbiotesSetting.new
147
+ c.key = "ca_cert"
148
+ c.value = ca_cert
149
+ c.save
150
+ else
151
+ ca_cert = ca_cert.value
152
+ end
153
+ ca_cert = OpenSSL::X509::Certificate.new ca_cert
154
+ return ca_cert
155
+ end
156
+
64
157
  end
65
-
66
158
  end
@@ -1,3 +1,3 @@
1
1
  module Simbiotes
2
- VERSION = '0.1.7'
2
+ VERSION = '0.1.8'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simbiotes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - MicroArx Corporation
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-19 00:00:00.000000000 Z
11
+ date: 2017-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -167,6 +167,7 @@ files:
167
167
  - lib/generators/simbiotes/initializer/USAGE
168
168
  - lib/generators/simbiotes/initializer/initializer_generator.rb
169
169
  - lib/generators/simbiotes/initializer/templates/simbiotes.rb.erb
170
+ - lib/generators/simbiotes/initializer/templates/simbiotes_settings.rb.erb
170
171
  - lib/generators/simbiotes/initializer/templates/simbiotes_targets.rb.erb
171
172
  - lib/generators/simbiotes/install/USAGE
172
173
  - lib/generators/simbiotes/install/install_generator.rb
@@ -227,7 +228,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
227
228
  version: '0'
228
229
  requirements: []
229
230
  rubyforge_project:
230
- rubygems_version: 2.6.10
231
+ rubygems_version: 2.2.2
231
232
  signing_key:
232
233
  specification_version: 4
233
234
  summary: The easy way to integrate the IoT in your web app.