Skip to content

Instantly share code, notes, and snippets.

@izumogeiger
Created June 3, 2017 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save izumogeiger/d983806b80607ae008ee991fe96d5222 to your computer and use it in GitHub Desktop.
Save izumogeiger/d983806b80607ae008ee991fe96d5222 to your computer and use it in GitHub Desktop.
require 'webrick'
require 'webrick/cgi'
require 'pp'
require 'net/http'
require 'fileutils'
require 'time'
class Locked < StandardError
end
def lock(lock_file_path='/tmp/monitor.lock')
File.open(lock_file_path, 'w') do |lock_file|
if lock_file.flock(File::LOCK_EX|File::LOCK_NB)
yield
else
raise Locked
end
end
end
class Monitor
def log2file(name,vals)
now = Time.now
dir = File.join(ENV["HOME"],'monitor_log',name,now.strftime("%Y"),now.strftime("%m"))
file = File.join(dir,now.strftime("%d")+".txt")
FileUtils.mkdir_p(dir)
mode = File.exists?(file) ? "a" : "w"
File.open(file,mode) do |io|
out = "#{now.iso8601}"
vals.each do |v|
out += ",#{v}"
end
io.puts out
end
end
def m2x_gc10(cpm)
uri = URI.parse('http://api-m2x.att.com/v2/devices/[DEVICEID]/streams/gc10/value')
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = false
headers = {
'X-M2X-KEY' => 'xxxxxxxxx',
'Content-Type' => 'application/json'
}
data =<<-"EOS"
{
"timestamp":"#{Time.now.iso8601}",
"value":#{cpm}
}
EOS
res = https.send_request('PUT',uri.request_uri,data,headers)
STDERR.puts "gc10 #{res.code} #{cpm}"
end
def m2x_mix1(dht11h,dht11t,mq135,pm25,pressure)
uri = URI.parse('http://api-m2x.att.com/v2/devices/[DEVICEID]/updates')
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = false
headers = {
'X-M2X-KEY' => 'xxxx',
'Content-Type' => 'application/json'
}
time = Time.now.iso8601
data =<<-"EOS"
{
"values":{
"mq135":[
{
"timestamp":"#{time}",
"value":#{mq135}
}
],
"dht11t":[
{
"timestamp":"#{time}",
"value":#{dht11t}
}
],
"dht11h":[
{
"timestamp":"#{time}",
"value":#{dht11h}
}
],
"pm25":[
{
"timestamp":"#{time}",
"value":#{pm25}
}
],
"pressure":[
{
"timestamp":"#{time}",
"value":#{pressure}
}
],
}
}
EOS
res = https.send_request('POST',uri.request_uri,data,headers)
STDERR.puts "air #{res.code} #{mq135} #{mq2}"
end
def run(opts = {})
opts[:DocumentRoot] ||= './'
opts[:BindAdress] ||= '*'
opts[:Port] ||= 8000
srv = WEBrick::HTTPServer.new(opts)
srv.mount_proc('/mix1') do |req,res|
dht11h = req.query['dht11h']
dht11t = req.query['dht11t']
mq135 = req.query['mq135']
pm25 = req.query['pm25']
pressure = req.query['pressure']
if dht11h and dht11t and mq135 and pm25 and pressure
m2x_mix1(dht11h,dht11t,mq135,pm25,pressure)
log2file('mix1',[dht11h,dht11t,mq135,pm25,pressure])
res.status = 200
else
res.status = 402
end
end
srv.mount_proc('/gc10') do |req,res|
cpm = req.query['cpm']
if cpm
m2x_gc10(cpm)
log2file('gc10',[cpm])
res.status = 200
else
res.status = 402
end
end
Signal.trap(:INT){srv.shutdown}
srv.start
end
end
begin
lock do
mon = Monitor.new
mon.run
end
rescue Locked
exit 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment