Class: ParticlePi::Setup
- Inherits:
-
Object
- Object
- ParticlePi::Setup
- Defined in:
- lib/particlepi/setup.rb
Instance Attribute Summary collapse
-
#device_id ⇒ Object
readonly
Returns the value of attribute device_id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#prompt ⇒ Object
readonly
Returns the value of attribute prompt.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #auth_client ⇒ Object
- #claim_device(tries = 5) ⇒ Object
- #device_id_path ⇒ Object
- #error(message) ⇒ Object
- #generate_device_key ⇒ Object
- #info(message) ⇒ Object
-
#initialize(options) ⇒ Setup
constructor
A new instance of Setup.
- #key_path ⇒ Object
- #load_settings ⇒ Object
- #perform_login ⇒ Object
- #prompt_credentials ⇒ Object
- #prompt_device_id ⇒ Object
- #prompt_device_name ⇒ Object
- #public_key_path ⇒ Object
- #publish_device_key ⇒ Object
- #reenter_credentials ⇒ Object
- #rename_device ⇒ Object
- #restart_agent ⇒ Object
- #run! ⇒ Object
- #save_credentials ⇒ Object
- #save_device_id ⇒ Object
- #save_device_name ⇒ Object
- #title(message) ⇒ Object
Constructor Details
Instance Attribute Details
#device_id ⇒ Object (readonly)
Returns the value of attribute device_id.
19 20 21 |
# File 'lib/particlepi/setup.rb', line 19 def device_id @device_id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
20 21 22 |
# File 'lib/particlepi/setup.rb', line 20 def name @name end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
17 18 19 |
# File 'lib/particlepi/setup.rb', line 17 def password @password end |
#prompt ⇒ Object (readonly)
Returns the value of attribute prompt.
21 22 23 |
# File 'lib/particlepi/setup.rb', line 21 def prompt @prompt end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
22 23 24 |
# File 'lib/particlepi/setup.rb', line 22 def settings @settings end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
18 19 20 |
# File 'lib/particlepi/setup.rb', line 18 def token @token end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
17 18 19 |
# File 'lib/particlepi/setup.rb', line 17 def username @username end |
Instance Method Details
#auth_client ⇒ Object
116 117 118 |
# File 'lib/particlepi/setup.rb', line 116 def auth_client Particle.access_token = settings.values["token"] end |
#claim_device(tries = 5) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/particlepi/setup.rb', line 172 def claim_device(tries = 5) Particle.device(device_id).claim rescue Particle::Error => e tries -= 1 unless tries.zero? sleep 1 retry end raise ClaimError end |
#device_id_path ⇒ Object
140 141 142 |
# File 'lib/particlepi/setup.rb', line 140 def device_id_path File.join(ParticlePi.project_root, "settings/device_id.txt") end |
#error(message) ⇒ Object
196 197 198 |
# File 'lib/particlepi/setup.rb', line 196 def error() prompt.say Paint[, :red] end |
#generate_device_key ⇒ Object
156 157 158 159 |
# File 'lib/particlepi/setup.rb', line 156 def generate_device_key system "openssl genrsa 1024 | openssl rsa -outform DER -out #{key_path}" system "openssl rsa -inform DER -in #{key_path} -pubout -outform PEM -out #{public_key_path}" end |
#info(message) ⇒ Object
192 193 194 |
# File 'lib/particlepi/setup.rb', line 192 def info() prompt.say end |
#key_path ⇒ Object
148 149 150 |
# File 'lib/particlepi/setup.rb', line 148 def key_path File.join(ParticlePi.project_root, "settings/device_key.der") end |
#load_settings ⇒ Object
71 72 73 |
# File 'lib/particlepi/setup.rb', line 71 def load_settings settings.load end |
#perform_login ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/particlepi/setup.rb', line 101 def perform_login Spinner.start "Logging in" do particle_token = Particle.login(username, password, expires_in: 0) @token = particle_token.id end rescue Particle::BadRequest => e raise LoginFailedError.new end |
#prompt_credentials ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/particlepi/setup.rb', line 89 def prompt_credentials @username = prompt.ask("Email address: ") do |q| q.default = username q.responses[:ask_on_error] = :question q.validate =/@/ end @password = prompt.ask("Password: ") do |q| q.default = password q.echo = false end end |
#prompt_device_id ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/particlepi/setup.rb', line 120 def prompt_device_id prompt.say "For the alpha phase, you should have received a device ID for the Raspberry Pi" @device_id = prompt.ask "Device ID: " do |q| q.validate = /^[0-9a-z]{24}$/ q.default = IO.read(device_id_path).chomp if File::exist?(device_id_path) end end |
#prompt_device_name ⇒ Object
128 129 130 131 132 133 |
# File 'lib/particlepi/setup.rb', line 128 def prompt_device_name prompt.say "How do you want your device to be labeled in the Particle tools?" @name = prompt.ask "Name: " do |q| q.default = settings.values["name"] || "pi" end end |
#public_key_path ⇒ Object
152 153 154 |
# File 'lib/particlepi/setup.rb', line 152 def public_key_path File.join(ParticlePi.project_root, "settings/device_key.pub.pem") end |
#publish_device_key ⇒ Object
161 162 163 164 165 166 |
# File 'lib/particlepi/setup.rb', line 161 def publish_device_key public_key = IO.read(public_key_path) Particle.device(device_id).update_public_key(public_key) rescue Particle::ForbiddenError raise KeyUpdateError.new end |
#reenter_credentials ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/particlepi/setup.rb', line 75 def reenter_credentials username = settings.values["username"] if username prompt.say "\nYou are already logged in as #{Paint[username, :yellow, :bright]}." prompt.agree "Do you want to log in as a different user? " do |q| q.default = 'yes' end else info "Log in with your Particle account" info "Don't have an account yet? Create one at https://login.particle.io" true end end |
#rename_device ⇒ Object
184 185 186 |
# File 'lib/particlepi/setup.rb', line 184 def rename_device Particle.device(device_id).rename(name) end |
#restart_agent ⇒ Object
168 169 170 |
# File 'lib/particlepi/setup.rb', line 168 def restart_agent system "sudo service particlepi restart" end |
#run! ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/particlepi/setup.rb', line 33 def run! load_settings title "Let's connect your Raspberry Pi to the Particle Cloud!" if reenter_credentials prompt_credentials perform_login save_credentials else auth_client end prompt_device_id save_device_id prompt_device_name save_device_name generate_device_key Spinner.start "Claiming the device to your Particle account" do publish_device_key restart_agent claim_device rename_device end info "Done! Go to #{Paint['https://build.particle.io', :blue, :bright]} to flash code to your Raspberry Pi" rescue LoginFailedError error "Wrong username or password" rescue KeyUpdateError error "Could not update keys for this device. Are you sure this device is not owned by another account?" rescue ClaimError error "Could not claim the device to your account. ==> The fix would be to ensure tinker is running." rescue Faraday::ClientError error "Network error. Check your internet connection and try again" end |
#save_credentials ⇒ Object
110 111 112 113 114 |
# File 'lib/particlepi/setup.rb', line 110 def save_credentials settings.values["username"] = username settings.values["token"] = token settings.save end |
#save_device_id ⇒ Object
144 145 146 |
# File 'lib/particlepi/setup.rb', line 144 def save_device_id IO.write(device_id_path, device_id + "\n") end |
#save_device_name ⇒ Object
135 136 137 138 |
# File 'lib/particlepi/setup.rb', line 135 def save_device_name settings.values["name"] = name settings.save end |
#title(message) ⇒ Object
188 189 190 |
# File 'lib/particlepi/setup.rb', line 188 def title() prompt.say Paint[, :cyan, :bright] end |