Last active
December 15, 2015 15:29
-
-
Save nippe/5282104 to your computer and use it in GitHub Desktop.
Script for logging some 1wire data to Cosm.org
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
require 'cosm-rb' | |
require 'json' | |
require 'ap' | |
def get_path_for_family path, directory_path | |
family = File.open(File.join(path, directory_path, 'family')).gets.strip.to_i | |
if family == 28 | |
File.join(path, directory_path, 'temperature') | |
elsif family == 26 | |
File.join(path, directory_path, 'humidity') | |
else | |
nil | |
end | |
end | |
def get_cosm_dataseries_key entry | |
if entry == '28.1061D6020000' | |
'temp-roof' | |
elsif entry == '28.10521C040000' | |
'temp-attic' | |
elsif entry == '26.0ECE3B010000' | |
'humidity-attic' | |
else | |
nil | |
end | |
end | |
# TODO: Check if args exist | |
path = ARGV[0] | |
Dir.entries(path).each do |entry| | |
if entry.match(/^[0-9]/) | |
unless get_path_for_family(path, entry).nil? | |
ap entry + ' - ' + get_path_for_family(path, entry) | |
value = File.open( get_path_for_family(path, entry) ).gets.strip.to_f | |
series_key = get_cosm_dataseries_key(entry) | |
datapoint = Cosm::Datapoint.new(:at => Time.now , :value => value) | |
ap datapoint | |
Cosm::Client.post('/v2/feeds/100109/datastreams/' + series_key + '/datapoints', | |
:headers => {"X-ApiKey" => '<api-key>'}, | |
:body => {:datapoints => [datapoint]}.to_json) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment