Class: ParticlePi::Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/particlepi/setup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Setup

Returns a new instance of Setup.



23
24
25
26
27
28
29
30
31
# File 'lib/particlepi/setup.rb', line 23

def initialize(options)
  @user = options[:user]
  @password = options[:password]
  @prompt = HighLine.new
  @token = nil
  @device_id = nil
  @name = nil
  @settings = Settings.new
end

Instance Attribute Details

#device_idObject (readonly)

Returns the value of attribute device_id.



19
20
21
# File 'lib/particlepi/setup.rb', line 19

def device_id
  @device_id
end

#nameObject (readonly)

Returns the value of attribute name.



20
21
22
# File 'lib/particlepi/setup.rb', line 20

def name
  @name
end

#passwordObject (readonly)

Returns the value of attribute password.



17
18
19
# File 'lib/particlepi/setup.rb', line 17

def password
  @password
end

#promptObject (readonly)

Returns the value of attribute prompt.



21
22
23
# File 'lib/particlepi/setup.rb', line 21

def prompt
  @prompt
end

#settingsObject (readonly)

Returns the value of attribute settings.



22
23
24
# File 'lib/particlepi/setup.rb', line 22

def settings
  @settings
end

#tokenObject (readonly)

Returns the value of attribute token.



18
19
20
# File 'lib/particlepi/setup.rb', line 18

def token
  @token
end

#usernameObject (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_clientObject



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_pathObject



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(message)
  prompt.say Paint[message, :red]
end

#generate_device_keyObject



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(message)
  prompt.say message
end

#key_pathObject



148
149
150
# File 'lib/particlepi/setup.rb', line 148

def key_path
  File.join(ParticlePi.project_root, "settings/device_key.der")
end

#load_settingsObject



71
72
73
# File 'lib/particlepi/setup.rb', line 71

def load_settings
  settings.load
end

#perform_loginObject



101
102
103
104
105
106
107
108
# File 'lib/particlepi/setup.rb', line 101

def 
  Spinner.start "Logging in" do
    particle_token = Particle.(username, password, expires_in: 0)
    @token = particle_token.id
  end
rescue Particle::BadRequest => e
  raise LoginFailedError.new
end

#prompt_credentialsObject



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_idObject



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_nameObject



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_pathObject



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_keyObject



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_credentialsObject



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_deviceObject



184
185
186
# File 'lib/particlepi/setup.rb', line 184

def rename_device
  Particle.device(device_id).rename(name)
end

#restart_agentObject



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
    
    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_credentialsObject



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_idObject



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_nameObject



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(message)
  prompt.say Paint[message, :cyan, :bright]
end