-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make code more modular and work for multiple devices
- Loading branch information
Showing
4 changed files
with
35 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
class PagesController < ApplicationController | ||
def home | ||
@devices = Device.all | ||
@led_status = WebConnectedLed.status | ||
@devices = Device.all_connected | ||
end | ||
|
||
def toggle_led | ||
WebConnectedLed.toggle | ||
WebConnectedLed.new(params['device_name'])&.toggle | ||
redirect_to root_path | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,22 @@ | ||
class Device | ||
class << self | ||
def all | ||
def all_connected | ||
Particle.devices | ||
.select { |d| d.connected? } | ||
.each { |d| d | ||
.attributes | ||
.merge!(extra_attributes(d.name)) | ||
} | ||
end | ||
|
||
def first | ||
all.first | ||
end | ||
|
||
def extra_attributes(name) | ||
{ | ||
:led_status => WebConnectedLed.new(name)&.status | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,25 @@ | ||
class WebConnectedLed | ||
class << self | ||
def toggle | ||
if status == 0 | ||
switch('on') | ||
else | ||
switch('off') | ||
end | ||
end | ||
def initialize(name) | ||
@name = name | ||
end | ||
|
||
def device | ||
Particle.device(name) | ||
def toggle | ||
if status == 0 | ||
switch('on') | ||
else | ||
switch('off') | ||
end | ||
end | ||
|
||
def name | ||
'photon-0002' | ||
end | ||
def device | ||
Particle.device(@name) | ||
end | ||
|
||
def status | ||
device.variable('led') | ||
end | ||
def status | ||
device.variable('led') | ||
end | ||
|
||
def switch(state) | ||
device.function('led', state) | ||
end | ||
def switch(state) | ||
device.function('led', state) | ||
end | ||
end |