Module: AgriController::MA
- Defined in:
- lib/agri-controller/device/ma.rb
Class Method Summary collapse
-
.check_sum(str) ⇒ Object
check_sum(“u0002P02EB3D”) # =>“3D” check_sum(“u0002P02EB”) # =>“3D”.
-
.check_sum?(str, sum = ) ⇒ Boolean
check_sum?(“u0002P02EB3D”) # =>true check_sum?(“u0002P02EB”,“3D”) # =>true check_sum?(“u0002P02EB”,“0F”) # =>false check_sum?(“bad”) # =>nil.
-
.parse(str) ⇒ Object
parce(“x02P02EB3Drx02B120C60rx02n4E530Frn”) # =>“Celsius Degree”=>15.600000000000023.
-
.sample ⇒ Object
“u0002P02EB3Dru0002B120C60ru0002n4E530Frn”.
-
.samples ⇒ Object
5 samples in Array.
Class Method Details
.check_sum(str) ⇒ Object
check_sum(“u0002P02EB3D”) # =>“3D” check_sum(“u0002P02EB”) # =>“3D”
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/agri-controller/device/ma.rb', line 21 def check_sum(str) x=str.chomp #sum=x[-2..-1] #p str[1] int1=str[1].ord int2=str[2..3].to_i(16) int3=str[4..5].to_i(16) check_sum=(int1+int2+int3).to_s(16) if check_sum.size<=1 check_sum="0"+check_sum elsif check_sum.size>2 check_sum=check_sum.slice(-2,2) end #p check_sum.upcase return check_sum.upcase end |
.check_sum?(str, sum = ) ⇒ Boolean
check_sum?(“u0002P02EB3D”) # =>true check_sum?(“u0002P02EB”,“3D”) # =>true check_sum?(“u0002P02EB”,“0F”) # =>false check_sum?(“bad”) # =>nil
43 44 45 46 47 48 49 50 |
# File 'lib/agri-controller/device/ma.rb', line 43 def check_sum?(str,sum=str[-2..-1]) begin res =check_sum(str) return res== sum rescue return nil end end |
.parse(str) ⇒ Object
parce(“x02P02EB3Drx02B120C60rx02n4E530Frn”) # =>“Celsius Degree”=>15.600000000000023
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 |
# File 'lib/agri-controller/device/ma.rb', line 53 def parse(str) hash={} begin list=str.chomp.split("\r") list.each do |x| if check_sum?(x) if x[1]=="P" a=x.slice(2,4).to_i(16) hash["ppm"]=a elsif x[1]=="B" a=x.slice(2,4).to_i(16) hash["Celsius Degree"]=a/16.0-273.15 elsif x[1]=="A" a=x.slice(2,4).to_i(16)/100.0 hash["humidity"]=a end else return nil end end rescue return nil end return hash end |
.sample ⇒ Object
“u0002P02EB3Dru0002B120C60ru0002n4E530Frn”
5 6 7 |
# File 'lib/agri-controller/device/ma.rb', line 5 def sample samples[0] end |
.samples ⇒ Object
5 samples in Array
10 11 12 13 14 15 16 17 |
# File 'lib/agri-controller/device/ma.rb', line 10 def samples ["\u0002P02EB3D\r\u0002B120C60\r\u0002n4E530F\r\n", "\u0002n4E5915\r\u0002P02E83A\r\u0002B120B5F\r\n", "\u0002n4E4C08\r\u0002P02F042\r\u0002B12075B\r", "nonono!\rbadchar!", nil] #ppms=[747,744,752] end |