-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailJetServer.rb
More file actions
187 lines (157 loc) · 5.29 KB
/
mailJetServer.rb
File metadata and controls
187 lines (157 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
require 'sinatra'
require 'httparty'
#require 'debugger'
require 'multi_json'
require 'serialport'
require "rubygems"
require "sequel"
require 'sqlite3'
#set :port, 3000
#setup serial
baud_rate = 57600
port_file = "/dev/ttyUSB0"
data_bits = 8
stop_bits = 1
parity = SerialPort::NONE
port = SerialPort.new(port_file, baud_rate, data_bits, stop_bits, parity)
# https://github.com/aj0strow/sinatra-json_body_params
module Sinatra
module JsonBodyParams
def self.registered(app)
app.before do
params.merge! json_body_params
end
app.helpers do
def json_body_params
@json_body_params ||= begin
MultiJson.load(request.body.read.to_s, symbolize_keys: true)
rescue MultiJson::LoadError
{}
end
end
end
end
end
end
register Sinatra::JsonBodyParams
$i = ''
#$counter=0
$valueLight
$valueMotion
$valueFinger
$valueAvgLight
#$testingvalues = "i want to kill myself, why isnt this data base working to push to in the loop??"
#database setup
DB = Sequel.sqlite
# create an items table
DB.create_table :items do
primary_key :id
String :name
Float :value
end
# create a dataset from the items table
items = DB[:items]
# populate the table
# items.insert(:name => 'abc', :value => 10000)
# print out the number of records
# puts "Item count: #{items.count}"
# puts "Average time is: #{items.avg(:value)}"
# $valueAvgLight = items.avg(:value)
# puts "valueAvgLight : "
# puts $valueAvgLight
$string
Thread.new do
while true do
$string = ''
while ($i = port.gets) do
#puts "$i : #{$i}" #String
$string<< $i
break
end
puts "$string : #{$string.dump}"
puts "$string.length : #{$string.length}"
key, value = $string.split ': ', 2
puts key
puts value
if (key.eql? "Light")
$valueLight = value
items.insert(:name => 'abc', :value => value)
$valueAvgLight = items.avg(:value)
puts "valueAvgLight : "
puts $valueAvgLight
end
if (key.eql? "Finger")
#put "Finger: "
$valueFinger = value
#put $valueFinger
end
if (key.eql? "Motion")
#put "Motion: "
$valueMotion = value
#put $val
end
# if ($string.eql? "this is a test\n")
# puts "comparison worked"
# $counter +=1
# puts "counter changes"
# puts "$counter : #{$counter}"
# end
# if (key.eql? "Light")
# end
sleep 0.5
end
end
# SERVER
post "/email_processor" do
command = params[:Subject].downcase
if command == "status"
if ($valueMotion.eql? "1\r\n")
$motionBackground = "green"
else
$motionBackground ="red"
end
#change text when lighter color
if ($valueLight.to_i > 170)
$textColor = "black"
else
$textColor = "white"
end
HTTParty.post("http://api.mailjet.com/v3/send/message",
:body => {to: params[:From], from: "tmhall1@pipeline.sbcc.edu",
subject: "WiPi - Current Condition of Nodes",
html: "
<html>
<img src=\"http://i.imgur.com/LeUHoyR.png\"/>
<div style=\"font: 35px; margin: 25px; float: left;width: 200px; line-height: 200px; height: 200px; color: white; background: blue; -moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px; /* future proofing */
-khtml-border-radius: 10px; /* for old Konqueror browsers */\">
Finger Value : #{$valueFinger} <br>
</div>
<div style=\"font: 20px; margin: 25px; float: left;width: 200px; line-height: 200px; height: 200px; color: white; background: #{$motionBackground}; -moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px; /* future proofing */
-khtml-border-radius: 10px; /* for old Konqueror browsers */\">
Motion : #{$valueMotion} <br>
</div>
<div style=\"font: 20px; margin: 25px; float: left;width: 200px; line-height: 200px; height: 200px; color: white; background-color: rgb(#{$valueLight},#{$valueLight},#{$valueLight}); -moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px; /* future proofing */
-khtml-border-radius: 10px; /* for old Konqueror browsers */\">
Light: #{$valueLight} <br>
</div>
<div style=\"font: 20px; margin: 25px; float: left;width: 200px; line-height: 200px; height: 200px; color: white; background-color: rgb(#{$valueLight},#{$valueLight},#{$valueLight}); -moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px; /* future proofing */
-khtml-border-radius: 10px; /* for old Konqueror browsers */\">
Light Average (since node start): #{$valueAvgLight} <br>
</div>
</html>"},
:basic_auth => {username: "67277d9381e70cac1ea98102a9463bf3", password: "d26319db8e691bfc47a7efb88ef7d560"})
puts "sent"
end
if (command == "flash")
port.write "f"
end
end
#advanced {