Module: AgriController

Included in:
TC_Agri
Defined in:
lib/agri-controller/bit.rb,
lib/agri-controller/dt.rb,
lib/agri-controller/bcc.rb,
lib/agri-controller/web.rb,
lib/agri-controller/main.rb,
lib/agri-controller/housa.rb,
lib/agri-controller/loger.rb,
lib/agri-controller/version.rb,
lib/agri-controller/main_new.rb,
lib/agri-controller/device/kr.rb,
lib/agri-controller/device/ma.rb,
lib/agri-controller/device/tr.rb,
lib/agri-controller/main_linux.rb,
lib/agri-controller/setting_io.rb,
lib/agri-controller/wet_sensor.rb,
lib/agri-controller/device/dacs.rb,
lib/agri-controller/gruff/chdata.rb,
lib/agri-controller/n_dan_thermo.rb,
lib/agri-controller/save_threads.rb,
lib/agri-controller/webrick_test.rb,
lib/agri-controller/device/mch383.rb,
lib/agri-controller/device/wr1010.rb,
lib/agri-controller/error_caption.rb,
lib/agri-controller/gruff/join_csv.rb,
lib/agri-controller/ttyUSB_watchdog.rb,
lib/agri-controller/gruff/rename_csv.rb,
lib/agri-controller/value_controller.rb,
lib/agri-controller/gruff/thermo_gruff.rb,
lib/agri-controller/gruff/thermo_gruff2.rb,
lib/agri-controller/multiple_pulse_timer.rb,
lib/agri-controller/gruff/thermo_gruff_loop.rb,
lib/agri-controller/gruff/thermo_data_each_day.rb,
lib/agri-controller/gruff/thermo_gruff_generate.rb

Overview

require “thermo_gruff” require “thermo_gruff_generate”

Defined Under Namespace

Modules: Bcc, Dacs, KR, Loger, MA, MCH383, TR, WR1010, Web Classes: Bit, N_dan_thermo, Value_controller, Wet_sensor

Constant Summary collapse

VERSION =
"0.0.11"
MINOR_VERSION =
"0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ary2dat(res) ⇒ Object

“2012-12-03 07:02:00,9.0,27.9,436.0,9.0,0.0,0.0n”



9
10
11
12
13
14
15
16
17
18
# File 'lib/agri-controller/gruff/chdata.rb', line 9

def ary2dat(res)
  str=""
  res.each do |ary|
    ary.each do |dat|
      str=str+dat.to_s+","
    end
    str=str.chop+"\n"
  end
  str
end

.chdat(str, list) ⇒ Object

csv文字列を第2リストによって整える

("time"であれば、時間としてパースし、Rなどで使いやすい時間文字列に変換)
(同時表示しやすいように、変換。 falseなどは0になる)

chdat(“2012/12/03 07:02:00,9.0,83.7,436,9.0,83.7,436n”,)

[“2012-12-03 07:02:00”, 9.0, 27.9, 436.0, “9.0”, 0.0, 0.0]


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
# File 'lib/agri-controller/gruff/chdata.rb', line 25

def chdat(str,list)
  str

  res=[]
  str.each_line do |line|
    dat=line.chomp.split(",")
    i=0
    kekka=[]
    dat.each do |datum|
      list[i]
      datum
      if (list[i]!=false) and (list[i]!="time")
        list[i].to_f
        datum.to_f
        kekka << list[i].to_f*datum.to_f
      elsif (list[i]=="time")
        kekka << DateTime.parse(datum).strftime("%Y-%m-%d %H:%M:%S")
      else
        kekka << datum
      end
      i+=1
      
    end
    res << kekka
  end
  res
end

.dt(t1 = Time.now, t2 = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/agri-controller/dt.rb', line 4

def dt(t1=Time.now,t2=nil)
  begin
    unless t2
      yield if block_given?
      return Time.now-t1
    else
      return t2-t1
    end
  rescue
    return nil
  end
end

.housa(t, rh) ⇒ Object

housa(Celsius_degree,Rh(%)) # =>g/m^3



33
34
35
36
37
# File 'lib/agri-controller/housa.rb', line 33

def housa(t,rh)
  x=houwa(t)
  x2=x*rh/100.0
  x-x2
end

.housa2(t, rh) ⇒ Object



40
41
42
43
44
# File 'lib/agri-controller/housa.rb', line 40

def housa2(t,rh)
  x=houwa2(t)
  x2=x*rh/100.0
  x-x2 
end

.houwa(t) ⇒ Object

houwa(Celsius_Degree) # =>g/m^3



20
21
22
23
24
# File 'lib/agri-controller/housa.rb', line 20

def houwa(t)
 eT=houwa_pressure(t)
 #et=houwa_pressure2(t)
 aT=(217*eT)/(t+273.15)
end

.houwa2(t) ⇒ Object



26
27
28
29
30
# File 'lib/agri-controller/housa.rb', line 26

def houwa2(t)
 #et=houwa_pressure(t)
 eT=houwa_pressure2(t)
 aT=(217*eT)/(t+273.15)
end

.houwa_pressure(t) ⇒ Object

houwa_pressure(Celsius_Degree)



8
9
10
# File 'lib/agri-controller/housa.rb', line 8

def houwa_pressure(t)
  eT=6.1078*10**(7.5*t/(t+237.3))
end

.houwa_pressure2(t) ⇒ Object

return hPa(Pa * 100)



13
14
15
16
# File 'lib/agri-controller/housa.rb', line 13

def houwa_pressure2(t)
  x=1-(273.15+t)/647.3
  eT2=221200*Math::E**((-7.76451*x+1.45838*(x**1.5)-2.7758*(x**3)-1.23303*(x**6))/(1-x))
end

.join_csv(f1, f2, comment_f1 = 0, comment_f2 = 0) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/agri-controller/gruff/join_csv.rb', line 23

def join_csv(f1,f2,comment_f1=0,comment_f2=0)
  a=CSV.read(f1)
  a=delete_comment(a,comment_f1)
  size=a[0].size
  hash1=to_h(a,0)
  #hash1
  
  b=CSV.read(f2)
  b=delete_comment(b,comment_f1)
  hash2=to_h(b,0)

  new_hash={}
  hash2.each do |i,j|
    if hash1.has_key?(i)
    else
      hash1[i]=Array.new(size-1){"false"}
    end
    new_hash[i]=hash1[i].concat(j)
  end
  ary=new_hash.sort_by{|i,j| i}
  ary.map{|i| i.flatten}
end

.mainObject Also known as: kr_run



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/agri-controller/main.rb', line 12

def main
  loop do
    error_caption("./cgi-bin/log/errors") do
      if RUBY_PLATFORM.include?("mswin")
        main_new
      else#linux
        main_linux
      end
    end
    Thread.list.each{|th| th.kill unless th==Thread.main}
    p "MAIN:  reload_loop"
    p Thread.list
    sleep 2
  end
end

.main_linux(log = "./cgi-bin/log", config = "./cgi-bin/config", docs = "./htdocs/thermo", dacs_port = "/dev/ttyUSB0") ⇒ Object

main_for_proto_house



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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/agri-controller/main_linux.rb', line 17

def main_linux(log="./cgi-bin/log",config="./cgi-bin/config",docs="./htdocs/thermo",dacs_port="/dev/ttyUSB0")
  log_time=Time.now
  Thread.abort_on_exception=true
      #dacs port
        dacs_port="/dev/ttyUSB1"
      
      #WR1010
      #thermo_thread starts
      yaml_file="last_thermo_data"
      wr_port="/dev/ttyUSB0"
      thermo_port=wr_port#8#6
      thermo_N=4
      
      th="thermo_define.yml"
      yaml_db(th,[thermo_port,thermo_N],config+"/"+th)
      
      #thermo_loop_thread(yaml_file,thermo_port,thermo_N,sec=30)
      thermo_loop_thread(yaml_file,thermo_port,thermo_N,sec=30,"./cgi-bin/config")
      
      #thermo_logger_thread starts
      thermo_data_logger_thread(thermo_N,"./cgi-bin/config","./cgi-bin/log")
  
  #reset manual bool
  yaml_db("manual_bool01.txt",nil,config+"/manual_bool01.txt")
  yaml_db("manual_bool02.txt",nil,config+"/manual_bool02.txt")
  sleep 0.1
  
  loop do
    catch :reset_signal do
      sleep 0.2
      ##
      #Initialize basic DATA
      manual_config =Bit.new(0)#15 & 9 => 9 (AND)
      manual_bit=Bit.new(0)#15 | 9 => 15 (OR)
      
      dacs=Serial.new("W0000000\r",dacs_port)
      dacs.time_out=2
      dacs.set
      res=dacs.serial
      unless res
        res=dacs.serial
      end
      res_i=Dacs::toi(res)
      
      wr=Serial.new(WR1010::send_sample,wr_port,9600,5)
      wr.set
      #thermo=toa(wr.serial)
      #THERMO port
        #wr1010 thrmo request
        thermo=yaml_dbr("last_thermo_data",config+"/last_thermo_data")#WR1010::list(thermo_port,thermo_N)      
        
      #BIT SETTING
           
        dacs_bit=Bit.new        
        in_bits=Bit.new(res_i) ##import signal(dacs)      
        change1=false 
        p house1_set=yaml_dbr("house1",config+"/house1")
        house2_set=yaml_dbr("house2",config+"/house2")
         house1=N_dan_thermo.new(house1_set,diff=1,1)
         house2=N_dan_thermo.new(house2_set,diff=1,1)
              
              a=Value_controller.new(steps=4,open_sec=5,#55
              down_sec=3,#35,
                sensitivity=2,set_value=20,dead_time=15)
              p "house1 Starts at #{(a.up_sec*a.steps).to_s} sec later."
              
              b=Value_controller.new(steps=4,open_sec=5,#55
                down_sec=3,#35,
                sensitivity=2,set_value=20,dead_time=15)
              #p "house2 Starts at #{b.reset_time.to_s}(#{(b.up_sec*b.steps).to_s} sec later)."
    
    #wet_sensor SETTING
      line1=Wet_sensor.new
    
    #illigate Time DEFINE[[10,7,6,3],["17:42"],["17:43"],["18:00"]]
      time_array3=yaml_dbr("time_array",config+"/time_array")
      puts "watering set:#{time_array3.inspect}"
      wait_time=yaml_dbr("wait_time",config+"/wait_time")
    
    #start
      start=Time.now
      
      #start time logging
      dat="Start,"+Time.now.iso8601
      Loger::loger(config+"/last_bit.txt",dat+"<br/>","w")#Start,#{Time.now}<br/>
      Loger::loger(log+"/log.txt",dat)
      
      #value_controller step change initialize
      change_step=[false,false]
      
      ##
      #MAIN LOOP (break if in_bit(22)==off)
      run_save_flag=nil
      
      loop do
          t=Time.now
          time=t.iso8601
            if t.min!=run_save_flag
              run_save_flag=t.min
              #save_run_check_time
              begin 
                yaml_db("run_check",t,config+"/run_check")
              #retry if error.
              rescue
                sleep 0.1
                yaml_db("run_check",t,config+"/run_check")
              end
            end
            if yaml_dbr("reload_flag",config+"/reload_flag")==true
              yaml_db("reload_flag",false,config+"/reload_flag")
              p dat="reload signal:"
              Loger::loger(log+"/log.txt",dat)
              
              sleep 0.1
              throw :reset_signal
            end
              
            #thermo DEFINE
              thermo=nil
              thermo=yaml_dbr("last_thermo_data",config+"/last_thermo_data")
              #last_thermo_time
            
            #illigate Timer
            x=multiple_pulse_timer(time_array3,wait_time)
            #p x
            #x.bit =>Integer
            
            #wet sensor work
            
            #illigating check
            dacs_bit.boolbit(x.on?(2),0)#signal switch
            
            dacs_bit.boolbit(x.on?(0),1)#illigate line1
            
            dacs_bit.boolbit(x.on?(2),2)#illigate line2          ##
            dacs_bit.boolbit(x.on?(4),3)#illigate line3
            
            dacs_bit.boolbit(x.on?(6),4)#illigate line4          ##
            
            pomp =  (x.on?(0)) | (x.on?(2)) | x.on?(4) | x.on?(6)#x.on?(0) | x.on?(2)
            dacs_bit.boolbit(pomp,5)#pomp signal
            
            
            #motor bit
            step_str=""
            if thermo.class==Array && thermo.size==thermo_N
              
              #set_temp from N_dan_thermo
              a.set_value=house1.set_now
              b.set_value=house2.set_now
              
              ##
              #Controll
              bit1=a.value_controll(thermo[0][0]) if thermo[0][0] != nil
              bit2=!a.switch
              bit3=b.value_controll(thermo[1][0]) if thermo[1][0] != nil
              bit4=!b.switch
              check_step=[a.now_step,b.now_step]
              
              if check_step != change_step
                string=[a.set_value,b.set_value].inspect#+check_step.inspect
                #p [bit1,bit2,bit3,bit4]
                #p thermo
                
                step_str=",step:"+a.now_step.to_s+","+"step:"+b.now_step.to_s+","+string
                yaml_db("change_step",check_step,config+"/change_step")
                
                change_step=check_step.dup
              end
            else
              #2010.4.28 changed 
              
              ##
              #commonly open if thrmo error.
              #house1
              bit1=true #open signal
              bit2=false#motor_off_trap
              
              ##
              #commonly open if thrmo error.
              #house2
              bit3=true #open signal
              bit4=false#motor_off_trap
              
              #dat=Time.now.to_s+":thermo_error!! open full time."+thermo.inspect
            end
            #house1 motor
            dacs_bit.boolbit(bit1,19)
            dacs_bit.boolbit(bit2,20)
            #house2 motor
            dacs_bit.boolbit(bit3,21)
            dacs_bit.boolbit(bit4,22)
            
            ##
            #RESULT BIT OUTPUT IF CHANGED
            #sum bits and check changes
        #dacs_bit auto
        #p change1
        
        #p config+"/manual_bool01.txt"
        #p yaml_dbr("manual_bool01.txt",config+"/manual_bool01.txt")
        if yaml_dbr("manual_bool01.txt",config+"/manual_bool01.txt")==nil
            
            (0..23).each do |x|
              dacs_bit.bool_bit(manual_bit.on?,x) if manual_config.on?(x)
            end             
            
            if change1 != dacs_bit.bit
              change1=dacs_bit.bit
              #p time
              str=dacs_bit.tos(24,2)
              if thermo!=nil
                p data=str+","+time+","+
              thermo[0][0].to_s+","+thermo[0][1].to_s+","+thermo[1][0].to_s+","+thermo[2][0].to_s
              else
                p data=str+"   "+time
              end
              #logging thread
              #Thread.start(data){|dat|
                
                Loger::loger(config+"/last_bit.txt",data+"<br/>","w")
                Loger::loger(log+"/log.txt",data)
              #}
             #str=bits.tos(24,2)
             #str_size=str.size
            
            ##
            #command output
             hex0=str
             hex=Bit.new(hex0.to_i(2)).tos(6,16)

              command="W0"+hex+"\r"
              dacs.command = (command)

              res=nil
              xx=0
              10.times do
                res=dacs.serial
                break if res
                if xx==9
                  raise "main_linux.rb __Dacs Fatal error__"
                end
                xx+=1
              end
              #p res
            end
          end
        if log_time.hour != t.hour
          log_time=t.dup
          p "watchdog:"+time
        end
        
        sleep 0.8
        #p Time.now-t
        
        #p Thread.list
        #raise
      end #main_loop
    end   #catch reset_signal
  
  end     #reset_loop
  #q.push nil
  #queue.join
end

.main_new(log = "./cgi-bin/log", config = "./cgi-bin/config", docs = "./htdocs/thermo", kr_port = 5) ⇒ Object

main_for_proto_house



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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/agri-controller/main_new.rb', line 17

def main_new(log="./cgi-bin/log",config="./cgi-bin/config",docs="./htdocs/thermo",kr_port=5)
  log_time=Time.now
  Thread.abort_on_exception=true
  
  #KR port
      kr_response=""
  
  #KR Queue
  q=Queue.new
  queue=Thread.start(q,log,config,kr_port) do |que,lo,co,kr|
    while str=que.pop
      #p str
      res=""
      
      #OUTPUT DATA
      res=KR::export(str,kr).to_s
      
      #LOG
      dat=Time.now.to_s+","+str.chomp+","+res.chomp#+"<br/>"
      Loger::loger(lo+"/kr_command_log.txt",dat)
      if res.include?("R") or str.include?("R")
        #p str+","+res
       
        #response signal
        begin
          
          if res.size>8
            #res="%01$RC0100**\r" (**:BCC)
            p input_num=res.slice(7,1).to_i(16)# =>0..15
            
            b=Bit.new(input_num)
           
            #bit save
            (0..1).each do |x|
              if b.on?(x)
                yaml_db("wet_sensor",true,co+"/wet#{x}")
                yaml_db("wet_read",false,co+"/wet#{x}")
              else
                yaml_db("wet_sensor",false,co+"/wet#{x}")
                yaml_db("wet_read",false,co+"/wet#{x}")
              end
            end
          else
            #send data lost
            #retry signal
            (0..1).each do |x|
               yaml_db("wet_sensor",false,co+"/wet#{x}")
               yaml_db("wet_read",false,co+"/wet#{x}")
            end
            
            ##
            Loger::loger(co+"/wet_input.txt",dat,"w")
          end
        rescue
          sleep 0.1
        #do nothing...
        end
      end
      sleep 0.1
      
      if res.include?("!") or      #include char
         res.include?("$")!=true or#not $
         res=="" or
         res=="false" or
         res.size < 8 or
         (res.size > 9 and res.include?("%01$RC")!=true)
      then
         Loger::loger(lo+"/errors.txt","KR:"+dat+"<br/>")
         Loger::loger(lo+"/errors_.txt","KR:"+dat+"<br/>")
      end
    end
  end
      #thermo_thread starts
      yaml_file="last_thermo_data"
      
      thermo_port=8#6
      thermo_N=4
      th="thermo_define.yml"
      yaml_db(th,[thermo_port,thermo_N],config+"/"+th)
      
      #thermo_loop_thread(yaml_file,thermo_port,thermo_N,sec=30)
      thermo_loop_thread(yaml_file,thermo_port,thermo_N,sec=30,"./cgi-bin/config")
      
      #thermo_logger_thread starts
      thermo_data_logger_thread(thermo_N,"./cgi-bin/config","./cgi-bin/log")
  
  #reset manual bool
  yaml_db("manual_bool01.txt",nil,config+"/manual_bool01.txt")
  yaml_db("manual_bool02.txt",nil,config+"/manual_bool02.txt")
  sleep 0.1
  
  loop do
    catch :reset_signal do
      sleep 0.2
      ##
      #Initialize basic DATA
      
      #THERMO port
        #wr1010 thrmo request
        thermo=yaml_dbr("last_thermo_data",config+"/last_thermo_data")#WR1010::list(thermo_port,thermo_N)      
        
        #if thermo.class==Array and thermo.size==thermo_N
        #  yaml_db("last_thermo_data",thermo,config+"/last_thermo_data")
        #else
        #  p thermo
        #end

      #BIT SETTING
           kr01=Bit.new #KR#01
           kr02=Bit.new #KR#02
        in_bits=Bit.new ##import signal(#KR#01)
          
        change1=false #KR#01
        change2=false #KR#02
        p house1_set=yaml_dbr("house1",config+"/house1")
        house2_set=yaml_dbr("house2",config+"/house2")
         house1=N_dan_thermo.new(house1_set,diff=1,1)
         house2=N_dan_thermo.new(house2_set,diff=1,1)
              
              a=Value_controller.new(steps=4,open_sec=5,#55
              down_sec=3,#35,
                sensitivity=2,set_value=20,dead_time=15)
              p "house1 Starts at #{(a.up_sec*a.steps).to_s} sec later."
              
              b=Value_controller.new(steps=4,open_sec=5,#55
                down_sec=3,#35,
                sensitivity=2,set_value=20,dead_time=15)
              #p "house2 Starts at #{b.reset_time.to_s}(#{(b.up_sec*b.steps).to_s} sec later)."
    
    #wet_sensor SETTING
      delay0=yaml_dbr("wet0_drain",config+"/wet0_drain") || 1
      delay1=yaml_dbr("wet1_drain",config+"/wet1_drain") || 1
      wet0=WetSensor.new(delay0,config+"/wet0")
      wet1=WetSensor.new(delay1,config+"/wet1")
      
    
    #illigate Time DEFINE
      time_array3=yaml_dbr("time_array",config+"/time_array")
      puts "watering set:#{time_array3.inspect}"
      wait_time=yaml_dbr("wait_time",config+"/wait_time")
    
    #start
      start=Time.now
      
      #start time logging
      dat="Start,"+Time.now.to_s
      Loger::loger(config+"/last_bit.txt",dat+"<br/>","w")#Start,#{Time.now}<br/>
      Loger::loger(log+"/log.txt",dat)
      
      #value_controller step change initialize
      change_step=[false,false]
      
      ##
      #MAIN LOOP (break if in_bit(22)==off)
      run_save_flag=nil
      
      loop do
          t=Time.now
          time=t.to_s
            if t.min!=run_save_flag
              run_save_flag=t.min
              #save_run_check_time
              begin 
                yaml_db("run_check",t,config+"/run_check")
              #retry if error.
              rescue
                sleep 0.1
                yaml_db("run_check",t,config+"/run_check")
              end
            end
            if yaml_dbr("reload_flag",config+"/reload_flag")==true
              yaml_db("reload_flag",false,config+"/reload_flag")
              p dat="reload signal:"
              Loger::loger(log+"/log.txt",dat)
              
              sleep 0.1
              throw :reset_signal
            end
              
            #thermo DEFINE
              thermo=nil
              thermo=yaml_dbr("last_thermo_data",config+"/last_thermo_data")
              #last_thermo_time
            
            #illigate Timer
            x=multiple_pulse_timer(time_array3,wait_time)
              #x.bit =>Integer
            
            #wet sensor work
            wet0.commander{ 
              #p "time 0"
              q.push("%01#RCCX00000000**\r")#read request when its time
            }
            
            wet1.commander{
              #p "time 1"
              q.push("%01#RCCX00000000**\r")#read request when its time
            }
            
            
            #illigating check
            kr01.boolbit(wet1.run(x.on?(2)),0)#signal switch
            
            kr01.boolbit(wet0.run(x.on?(0)),1)#illigate line1
            
            kr01.boolbit(wet1.run(x.on?(2)),2)#illigate line2          ##
            
            pomp = wet0.run(x.on?(0)) | wet1.run(x.on?(2))
            kr01.boolbit(pomp,3)#pomp signal
            
            
            #motor bit
            step_str=""
            if thermo.class==Array && thermo.size==thermo_N
              
              #set_temp from N_dan_thermo
              a.set_value=house1.set_now
              b.set_value=house2.set_now
              
              ##
              #Controll
              bit1=a.value_controll(thermo[0][0]) if thermo[0][0] != nil
              bit2=!a.switch
              bit3=b.value_controll(thermo[1][0]) if thermo[1][0] != nil
              bit4=!b.switch
              check_step=[a.now_step,b.now_step]
              
              if check_step != change_step
                string=[a.set_value,b.set_value].inspect#+check_step.inspect
                #p [bit1,bit2,bit3,bit4]
                #p thermo
                
                step_str=",step:"+a.now_step.to_s+","+"step:"+b.now_step.to_s+","+string
                yaml_db("change_step",check_step,config+"/change_step")
                
                change_step=check_step.dup
              end
            else
              #2010.4.28 changed 
              
              ##
              #commonly open if thrmo error.
              #house1
              bit1=true #open signal
              bit2=false#motor_off_trap
              
              ##
              #commonly open if thrmo error.
              #house2
              bit3=true #open signal
              bit4=false#motor_off_trap
              
              #dat=Time.now.to_s+":thermo_error!! open full time."+thermo.inspect
            end
            #house1 motor
            kr02.boolbit(bit1,0)
            kr02.boolbit(bit2,1)
            #house2 motor
            kr02.boolbit(bit3,2)
            kr02.boolbit(bit4,3)
            
            ##
            #RESULT BIT OUTPUT IF CHANGED
            #sum bits and check changes
        #KR01 auto
        #p change1
        
        #p config+"/manual_bool01.txt"
        #p yaml_dbr("manual_bool01.txt",config+"/manual_bool01.txt")
        if yaml_dbr("manual_bool01.txt",config+"/manual_bool01.txt")==nil
            if change1 != kr01.bit
              change1=kr01.bit
              
              str=kr01.tos(4,2)
              if thermo!=nil
                p data="KR01:"+str+","+time+","+
              thermo[0][0].to_s+","+thermo[0][1].to_s+","+thermo[1][0].to_s+","+thermo[2][0].to_s
              else
                p data="KR01:"+str+"   "+time
              end
              #logging thread
              #Thread.start(data){|dat|
                
                Loger::loger(config+"/last_bit.txt",data+"<br/>","w")
                Loger::loger(log+"/log.txt",data)
              #}
             #str=bits.tos(24,2)
             #str_size=str.size
            
            ##
            #command output
             hex0=str
             hex=Bit.new(hex0.to_i(2)).tos(1,16)
              command="%01#WCCY000000000#{hex}00**\r"
              q.push(command)
            end
          #KR01 manual
          else
            if yaml_dbr("kr01_readable.txt",config+"/kr01_readable.txt")=="OK"
              p "manual KR01"
              #kr1
              hex=yaml_dbr("kr1_bit.txt",config+"/kr1_bit.txt").upcase
              command="%01#WCCY000000000#{hex}00**\r"
              q.push(command)
              
              p data="KR01_manual:"+hex+","+time
              #logging thread
              #Thread.start(data) do |dat|
                Loger::loger(config+"/last_bit.txt",data+"<br/>","w")
                Loger::loger(log+"/log.txt",data)
              #end
              yaml_db("kr01_readable.txt",nil,config+"/kr01_readable.txt")
            end
          end
        #KR02 auto
        if yaml_dbr("manual_bool02.txt",config+"/manual_bool02.txt")==nil
          if change2 != kr02.bit
             change2=kr02.bit
            
             str=kr02.tos(8,2)
             if thermo!=nil
         p data="KR02:"+str+","+time+","+thermo[0][0].to_s+","+thermo[0][1].to_s+","+thermo[1][0].to_s+","+thermo[2][0].to_s+step_str
             else
               p data="KR02:"+str+"   "+time
             end
             #logging thread
             #Thread.start(data){|dat|
               Loger::loger(config+"/last_bit.txt",data+"<br/>","w")
               Loger::loger(log+"/log.txt",data)
             #}
             #str=bits.tos(24,2)
             #str_size=str.size
             
             hex1=str.slice(4,4)
             str
             hex2=str.slice(0,4)
             hex_w=Bit.new((hex2+hex1).to_i(2)).tos(2,16)
              command="%02#WCCY00000000#{hex_w}00**\r"
              q.push(command)
          end
        #manual
        else
          if yaml_dbr("kr02_readable.txt",config+"/kr02_readable.txt")=="OK"
            p "manual KR02"
            
            #kr2
            hex_w=yaml_dbr("kr2_bit.txt",config+"/kr2_bit.txt").upcase
            
            if hex_w.size==1
              hex_w="0"+hex_w
            end
            
            command="%02#WCCY00000000#{hex_w}00**\r"
            q.push(command)
            
            p data="KR02_manual:"+hex_w+","+time
            #logging thread
            #Thread.start(data) do |dat|
              Loger::loger(config+"/last_bit.txt",data+"<br/>","w")
              Loger::loger(log+"/log.txt",data)
            #end
            
            yaml_db("kr02_readable.txt",nil,config+"/kr02_readable.txt")
          end
          
        end
        if log_time.hour != t.hour
          log_time=t.dup
          p "watchdog:"+Time.now.to_s
        end
        #p Time.now-t
        
        sleep 0.3
        
        #p Thread.list
        #raise
      end #main_loop
    end   #catch reset_signal
  
  end     #reset_loop
  q.push nil
  queue.join
end

.rename_csv(dist, dist_dir, deli = ".csv.") ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/agri-controller/gruff/rename_csv.rb', line 4

def rename_csv(dist,dist_dir,deli=".csv.")
    if ARGV[0]=="-h"
       raise 
    end
    unless File.exist?(dist_dir)
      Dir.mkdir(dist_dir)
    end

    list=Dir.glob(dist+"/*#{deli}*")
    #p list
    list.each do |file|
      basename=File.basename(file)
      dir=File.dirname(file)
      name=basename.split(".")
      if name.last.to_i!=0
        new_name=name[0]+"."+name[2]+"."+name[1]
        #p file
        new_full_name=dist_dir+"/"+new_name
        FileUtils.cp(file,new_full_name)
      end
    end
end

.roten(t, rh) ⇒ Object

roten(Celsius_degree,Rh(%)) # =>Celsius_Degree



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/agri-controller/housa.rb', line 47

def roten(t,rh)
  ps=houwa_pressure(t)*100
  e =ps*rh/100.0
  y=Math.log(e/611.213)
  if y>=0
    p "1"
    td=13.715*y+8.4262*0.1*y**2+1.9048*0.01*y**3+7.8158*0.001*y**4
  elsif y<0
    p "2"
    td2=13.7204*y+7.36631*0.1*y**2+3.32136*0.01*y**3+7.78591*0.001*y**4
  else
    p "3"
    0
  end
end

.thermo_data_logger_thread(thermo_N = 3, config = "./cgi-bin/config", log = "./cgi-bin/log") ⇒ Object



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
# File 'lib/agri-controller/save_threads.rb', line 76

def thermo_data_logger_thread(thermo_N=3,config="./cgi-bin/config",log="./cgi-bin/log")
  
  Thread.start(thermo_N,config,log) do |n,c,l|
    #thermo number DEFINE
    
    #begin
      #mainloop
      before=nil
      day_before=Time.now.day
      loop do
        t=Time.now
        min=t.min
        if min!=before
          before=min
          
          t2=t.to_a
          t2[0]=0
          tt=Time.parse(t2[2].to_s+":"+t2[1].to_s+":"+t2[0].to_s)
          #p tt
          ##
          #save DATA LOGGER(default,each o'clock)
          thermo=yaml_dbr("last_thermo_data",c+"/last_thermo_data")
          word=tt.iso8601+","
          
          if thermo.class==Array && thermo.size >= n
            n.times do |a|
              word=word+thermo[a][0].to_s+","+thermo[a][1].to_s+","
            end
            
          else
            #nil data define ['c,%]=[0,false]
            n.times do |a|
              word=word+"0"+","+"false"+","
            end
          end
            #seve data each minute.
            str=word.chop+"\n"
            Loger::loger(l+"/thermo_data.csv",str,"a",150000)
            #thermo_gruff
            #yesterday = (Date.today-1).to_s
            #load yesterday_data
            #filename=l+"/"+yesterday+"_thermo.csv"
            #  if File.exist?(filename)
            #    yesterday_data=File.read(filename)
            #  else
            #    yesterday_data=""
            #  end
              #load today_data
           #   filename=l+"/thermo_data.csv"
           #   if File.exist?(filename)
           #     today_data=File.read(filename)
           #   else
           #     today_data=""
           #   end
            
           # dat=yesterday_data+today_data
           # open(l+"/data.tmp","w"){|io| io.print dat}
        end
        
        sleep 0.3
        day=t.day
        if day_before!=day
          day_before=day
          sleep 5
          Thread.start(l) do |ll|
            last_day=(Date.today-1).to_s
            #thermo_gruff(g+'/'+today+'.jpg',l+"/thermo_data.csv",n,"480x420")
            #p l+'/thermo/'+last_day+"_thermo.csv"
            
            #save data every day if needed.
            #__HERE!!__
            
            #
            File.rename(ll+"/thermo_data.csv",ll+'/thermo/'+last_day+"_thermo.csv")
            open(ll+"/thermo_data.csv","w"){|io| io.print("")}
          end
          sleep 1
        end
        #p Time.now
      end
      sleep 10
  end
end

.thermo_gruff_loop(verbose = false, dir_to = "./htdocs/thermo", dir_from = "./cgi-bin/log", num = 4, size1 = "480x420", size2 = "220x250", sec = 120) ⇒ Object



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
# File 'lib/agri-controller/gruff/thermo_gruff_loop.rb', line 8

def thermo_gruff_loop(verbose=false,dir_to="./htdocs/thermo",dir_from="./cgi-bin/log",num=4,size1="480x420",size2="220x250",sec=120)
  
  p "gruf_start:"+Time.now.iso8601 if verbose
  
  p input_csv_data=dir_from+"/thermo_data.csv"#"data.tmp"
  p output_filename=dir_to+"/thermo_data.jpg"
  
  loop do
    
      #bool=File.readable?(input_csv_data)
      #p "thermo_data_readable:"+bool.inspect
      #if bool
        
        #p input_csv_data
        
        thermo_gruff(output_filename,input_csv_data,num,size1)
        thermo_gruff(dir_to+"/thumb/thermo_data.jpg",input_csv_data,num,size2)
        
        Loger::loger(dir_from+"/thermo_graph_loop.txt",Time.now.iso8601,"w")
      #end
      
      #check 1day thermo_data and generate gruff(and small gruff) below
      thermo_gruff_generate("./htdocs/thermo","./cgi-bin/log/thermo",num)
    sleep sec
    p "gruff_loop:"+Time.now.iso8601 if verbose
  end
end

.thermo_loop_thread(yaml_file, thermo_port, thermo_N, sec = 3, logdir = "./cgi-bin/config", baud = 57600) ⇒ Object

WRの温度データを、logdirにyaml形式でN_secごとに記録する



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
# File 'lib/agri-controller/save_threads.rb', line 18

def thermo_loop_thread(yaml_file,thermo_port,thermo_N,sec=3,logdir="./cgi-bin/config",baud=57600)
  Thread.start(yaml_file,thermo_port,thermo_N,sec,logdir,baud) do |name,port,n,s,lo,bau|
    #p "start N:"+n.to_s
    unless RUBY_PLATFORM.include?("mswin")
      wr=Serial.new(WR1010::send_sample(n),port,bau,5)
      wr.set
    end
    
    
    loop do
      thermo=nil
      #load thermo DATA par :sec
      x=0
      
      9.times do |x|
        #p x
        begin
          #res=thermo_read()
          #thermo=eval(res) if res!=nil
          if RUBY_PLATFORM.include?("mswin")
            thermo=WR1010::list(port,n,0.3+x*0.1)#import WR1010 data
          else#linux
            res=wr.serial
            thermo=WR1010::toa(res)
          end
        rescue
          thermo=nil
        end
        Loger::loger(lo+"/../log/errors_.txt",Time.now.inspect+res.inspect+","+x.inspect) if x>2
        break if (thermo.class==Array && thermo.size==n)
        sleep 6
      end
      
      #read error
      #if thermo.class==Array && thermo.size>n
      #  thermo=thermo[0..n-1]
      #end
      
        yaml_db(name,thermo,lo+"/"+name)#save last data
      
      #judge right thermo data
      if thermo.class==Array && thermo.size==n
        #p thermo
        yaml_db("last_thermo_time",Time.now,lo+"/last_thermo_time")
      else
        #p "else"
        
        #error save and next_try after sleep
        dat=Time.now.iso8601+" |thermo_thread| "+thermo.inspect+"<br/>"
        #Loger::loger(lo+"/../log/errors.txt",dat)
        Loger::loger(lo+"/../log/errors_.txt",dat)
        #sleep 600
      end
      sleep s
    end
  end
end

.thermo_read(ref = "http://maru.selfip.com/cgi-bin/thermo.rb") ⇒ Object

ネット上のデータをサーモデータとして利用する



7
8
9
10
11
12
13
14
# File 'lib/agri-controller/save_threads.rb', line 7

def thermo_read(ref="http://maru.selfip.com/cgi-bin/thermo.rb")
  begin
    uri=URI(ref)
    dat=uri.read("Accept-Language" => "ja")
  rescue
    nil
  end
end

.ttyUSB_watchdogObject

++ watching ttyUSB* allow

AgriController::ttyUSB_watchdog() # =>‘chmod 777 /dev/ttyUSB*`



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/agri-controller/ttyUSB_watchdog.rb', line 7

def ttyUSB_watchdog
  ##
  #watching ttyUSB* allow
  str=`ls -l /dev/ttyUSB*`
  print str if $DEBUG

  flag=false
  str.each_line do |line|
    if line.include?("crwxrwxrwx")
      #OK. Nothing to do
    else
      #puts line
      #puts "ttyUSB ERROR"
      flag=true
    end
  end
  
  #/dev/ttyUSB* allow again
  `sudo chmod 777 /dev/ttyUSB*` if flag
end

.ttyUSB_watchdog_loop(sec = 30) ⇒ Object

++ ttyUSB_watchdog_loop(sec=30)

run each 30 seconds(default)
AgriController::ttyUSB_watchdog


30
31
32
33
34
35
36
# File 'lib/agri-controller/ttyUSB_watchdog.rb', line 30

def ttyUSB_watchdog_loop(sec=30)
  loop do
    print "." if $DEBUG
    ttyUSB_watchdog
    sleep sec
  end
end

.webrick_start(root = Dir.pwd, port = 10080) ⇒ Object



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
# File 'lib/agri-controller/webrick_test.rb', line 11

def webrick_start(root=Dir.pwd,port=10080)
  #p approot=Pathname.pwd.parent
  app_root =root#File.expand_path(File.dirname(__FILE__) + '/..')
  
  p document_root = app_root + '/htdocs'
  if RUBY_PLATFORM.include?("mswin")
    p rubybin = 'c:/ruby187/bin/rubyw.exe'
  else#liux
    rubybin = `which ruby`.chomp
  end
  server = WEBrick::HTTPServer.new({
    :DocumentRoot => document_root,
    :BindAddress => '127.0.0.1',
    :CGIInterpreter => rubybin,
    :Port => port
  })
  
  # App/cgi/
  app_root + '/cgi-bin/*.{cgi,rb}'
  cgilist = Dir.glob(app_root + '/cgi-bin/*.{cgi,rb}')
  
  cgilist.each {|cgi_file|
    p cgi_file
    cgi_file_name = File.basename(cgi_file)
    
    server.mount('/cgi-bin/' + cgi_file_name, WEBrick::HTTPServlet::CGIHandler, cgi_file)
    
  }
  
  ['INT', 'TERM'].each {|signal|
    Signal.trap(signal){ server.shutdown }
  }
  
  server.start
end

.yaml_db(name, value, file = name) ⇒ Object

“db.yaml”)



9
10
11
12
13
14
15
# File 'lib/agri-controller/setting_io.rb', line 9

def yaml_db(name,value,file=name)#"db.yaml")
  #p name
  db=YAML::Store.new(file)
  db.transaction do
    db[name.to_s]=value
  end
end

.yaml_dbr(name, file = name) ⇒ Object

“db.yaml”)



17
18
19
20
21
22
23
# File 'lib/agri-controller/setting_io.rb', line 17

def yaml_dbr(name,file=name)#"db.yaml")
  #p file
  db=YAML::Store.new(file)
  db.transaction(:read_only) do
    db[name.to_s]
  end
end

.yaml_dump(data, filename = "setting_io.yml") ⇒ Object



31
32
33
# File 'lib/agri-controller/setting_io.rb', line 31

def yaml_dump(data,filename="setting_io.yml")
  yaml_db(name=filename,value=data,"db.yaml")
end

.yaml_key(file) ⇒ Object



24
25
26
27
28
29
# File 'lib/agri-controller/setting_io.rb', line 24

def yaml_key(file)
  db=YAML::Store.new(file)
  db.transaction(:read_only) do
    db.roots.sort
  end
end

.yaml_load(filename = "setting_io.yml") ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/agri-controller/setting_io.rb', line 35

def yaml_load(filename="setting_io.yml")
  res=yaml_dbr(name=filename)#,"db.yaml")
  if res == nil
    if File.exist?(filename)
      a=File.read(filename)
      if a
        x=YAML.load(a)
        yaml_db(name=filename,x,name)
      end
      
      return x
    else
      return nil
    end
  else
    return res
  end
end

.zettai(t, rh) ⇒ Object



64
65
66
67
# File 'lib/agri-controller/housa.rb', line 64

def zettai(t,rh)
  x=houwa(t)
  x2=x*rh/100.0
end

Instance Method Details

#_24hourObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/agri-controller/multiple_pulse_timer.rb', line 160

def _24hour
  y=[]
  24.times do |x|
  z=x.to_s
  str=":00",10
  if x<10
  str="0"+z+str
  else
  str=z+str
  end
  y << [str]
  end
  y
end

#delete_comment(ary, comment_count) ⇒ Object



6
7
8
9
10
# File 'lib/agri-controller/gruff/join_csv.rb', line 6

def delete_comment(ary,comment_count)
  comment_count.times{ary.shift}
  
  ary
end

#error_caption(file = "error_ex") ⇒ Object

use block when functions must be caption if encountered error.

record error(file) *error_caption(file=“error_ex”)/RubyScripts/

and retry function.

infinite loop

*looperror_caption(file){/RubyScripts/}



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/agri-controller/error_caption.rb', line 12

def error_caption(file="error_ex")
  begin
    #p "initialize"
    
    #main program
        yield 
  rescue => ex
    #Error handling
    x0=ex.class.to_s
    x1=ex.message
    x2=ex.backtrace.to_s
    
    p x="MAIN :"+Time.now.to_s+","+x0+x1+x2
    Loger::loger(file+".txt",x+"<br/>")
    Loger::loger(file+"_.txt",x+"<br/>")
    sleep 5
    #retry
  #ensure
  end
end

#error_catch(file = "error_log.txt", sleep_sec = 5, cr_code = '</br>') ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/agri-controller/error_caption.rb', line 33

def error_catch(file="error_log.txt", sleep_sec=5 , cr_code= '</br>')
  begin
        yield 
  rescue => ex
    #Error handling
    x0=ex.class.to_s
    x1=ex.message
    x2=ex.backtrace.to_s
    
    p x="MAIN :"+Time.now.to_s+","+x0+x1+x2
    file.each do |f|
      Loger::loger(f,x+cr_code)
    end
    sleep sleep_sec
    #retry
  #ensure
  end
end

#multiple_pulse_timer(time_array, pomp_wait_sec = nil) ⇒ Object

modified 2013.10.17 multiple_pulse_timer([,time=,[“11:00”],,[“17:00”]],next_wait_sec=2) returns



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
# File 'lib/agri-controller/multiple_pulse_timer.rb', line 106

def multiple_pulse_timer(time_array,pomp_wait_sec=nil)#time_array=[[1,2],["12:52:00"],["12:53"],["7:57:30"],["6:00"],["11:00"],["14:00"],["17:00"]]
  bit=Bit.new
  #time_array=[[1,2,3],["12:52",10],["12:53"]]
  t0=time_array[0]
  size=t0.size
  
  #time expantion
  result=[]
  
  #each time
  (time_array.size-1).times do |i|
    list=time_array[i+1]
    time_table=[]       
    last_sec=0
    
    #each system,and wait
    size.times do |j|
      time_table[i] =[] unless time_table[i]
      #time_list has palse ["12:52",10]
      if list[1]!=nil
        #p i
        #p time_table[i]
        time_table[i] << [(Time.parse(list[0])+last_sec),list[1]]
        last_sec+=list[1]
      else#["12:52:15"]
        time_table[i] << [(Time.parse(list[0])+last_sec),t0[j]]
        last_sec+=t0[j]
      end
      
      if pomp_wait_sec!=nil
        time_table[i] << [(Time.parse(list[0])+last_sec),pomp_wait_sec]
        last_sec+=pomp_wait_sec
      end
    end
    result << time_table[i]       
  end 
  
  #Array.transpose each system
  result=result.transpose
  result.pop
  p result if $DEBUG
  
  x=0
  result.each do |time|
  
  #p time
    bool=pulse_timers(time)
    bit.boolbit(bool,x)
    x+=1
  end
  #p bit
  return bit
end

#next_pulse(time, sec) ⇒ Object

next list genarate



99
100
101
# File 'lib/agri-controller/multiple_pulse_timer.rb', line 99

def next_pulse(time,sec)
  Time.parse(time)+sec
end

#pulse_timer(time) ⇒ Object

pulse_timer() # =>nil ,if before

pulse_timer(["12:00:00",10]) # =>true  ,if time
pulse_timer(["12:00:00",10]) # =>false ,if over


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/agri-controller/multiple_pulse_timer.rb', line 54

def pulse_timer(time)#time=[start_time,seconds] ex["12:00:00",10]
  if time[0].class==Time
    start=time[0]
  else
    start=Time.parse(time[0])
  end
  end_time=start+time[1]
  if time?(start)
    #p "time!"
    if time?(end_time)
      #"on!"
      #yield true if block_given
      #p "over"
      yield false if block_given?
      return false
    else
      #p "time!"
      yield true if block_given?
      return true
    end
  end
  nil
end

#pulse_timers(time_array) ⇒ Object

pulse_timers([,[“11:00”,400],,[“17:00”,300]])



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/agri-controller/multiple_pulse_timer.rb', line 78

def pulse_timers(time_array)#time_array=[["12:52:00",10],["12:52:15",3],["7:57:30",10],["6:00",400],["11:00",400],["14:00",400],["17:00",300]]
  bit=false#return bool
  bool=false
    
  time_array.each do |time|
    #p time#
    a=pulse_timer(time)
    bool ||= a
  end
    
    
  #p bool
  if bool
    if bit==false
      bit=true
    end
  end
  return bit
end

#save_each_day(file = "_thermo_data.csv") ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/agri-controller/gruff/thermo_data_each_day.rb', line 49

def save_each_day(file="_thermo_data.csv")
  p result=thermo_data_each_day(file)
  x=0
  result[1].each do |date|
    i=date.year.to_s
    j=date.month.to_s
    k=date.day.to_s
    #save_each_day
    filename="./thermo_data/"+i+"-"+j+"-"+k+"_thermo.csv"
    open(filename,"w"){|io| io.print result[0][x]}
    
    x+=1
  end
end

#thermo_data_each_day(file) ⇒ Object



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
# File 'lib/agri-controller/gruff/thermo_data_each_day.rb', line 3

def thermo_data_each_day(file)
  if File.exist?(file)
    dat=File.read(file)
  else
    dat=file
  end
  x=[]
  y=[]
  line=0
  file_num=0
  last_day_data=0
  change=nil
  date=Time.now
  begin 
    dat.each_line do |str|
      line+=1
      #split day data
      #p str
      if str!=""
        data=str.chomp.split(",")
        date=Time.parse(data[0])
        last_day_data=date.day
        change=date if change==nil
      end
      #not change
      if change.day==last_day_data
      #day change
      elsif change.day!=last_day_data
        y[file_num] = change
        change=date
        file_num+=1
      else
        p "??"
      end
        x[file_num]="" if x[file_num]==nil
        x[file_num]+=str
      
      #normal end
      
    end
    y[file_num]=date
  end
  #return split array
  ret=[x,y]
  return ret
end

#thermo_gruff(output_filename = "thermo_data.jpg", input_csv_data = "thermo_data.csv", thermo_num = 3, view = "500x420") ⇒ Object

thrmo datas to jpg graph



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
# File 'lib/agri-controller/gruff/thermo_gruff.rb', line 7

def thermo_gruff(output_filename="thermo_data.jpg",input_csv_data="thermo_data.csv",thermo_num=3,view="500x420")
  #data ini
  data=File.read(input_csv_data)
  thermo_data_hash={}

  result=[]
  #arrays generate
  (2*thermo_num).times do
    result << []
  end
  result
  title_day=""
  line=0
  label={}
  hour=0
  day=0
  data.each_line do |str|
    line+=1#line+=
    dat=str.chomp.split(",")
    #p dat[0]
    begin
      time=Time.iso8601(dat[0]).localtime#DateTime
      #dat[0].inspect+","+time.to_s
    rescue
      line=line-1
      next
    end
    #title_day
    if line==1
      title_day=time.year.to_s+"/"+time.month.to_s+"/"+time.day.to_s
    end
    #line if day change
    time.hour
    if time.day!=day #(day change)
      day=time.day
      label[line]=":"#time.month.to_s+"/"+day.to_s
    elsif time.hour!=hour
         hour=time.hour
         label[line]="|"+hour.to_s
      if time.hour==12
        label[line]="|12"
      end
    else
    end
    
    #set thermo datas
    thermo_num.times do |n|
      begin
        x=dat[n*2+1].to_f
        if x>-100
        result[n*2] << x
        else
          result[n*2] << nil
        end
      rescue
        result[n*2] << nil
      end
      #Judge nil data
      if dat[n*2+2]!="false"
        result[n*2+1] << dat[n*2+2].to_f/3
      else
        result[n*2+1] << nil
      end
    end
  end
  #p result
  #p label
      #gruff main
      g = Gruff::Line.new(view)
      g.title="Thermo data since "+title_day
      #g.title_font_size =24
      g.theme_37signals
      g.maximum_value = 35
      g.minimum_value = 5
      g.y_axis_increment = 1
      #g.baseline_value=9
      #g.increment=5
      #dataset
      datasets=[]
      thermo_num.times do |i|
        x=i+1
        datasets[i*2  ]=[("'C:"+x.to_s).intern,result[i*2]]
        datasets[i*2+1]=[("%/3:"+x.to_s).intern,result[i*2+1]]
      end
      #% humidity data delete
      [9,7,5,3].each{|i| datasets.delete_at(i)}
      
      #dataset
      datasets.each do |data|
        g.data(*data)
      end
      g.labels =label
      # Default theme
      #p g
      g.write(output_filename)
end

#thermo_gruff2(output_filename = "thermo_data.jpg", input_csv_data = "thermo_data.csv", range = [1.0,1.0/3,1.0/100], legend = ["degree","humidty(1/3)","ppm(1/100)"], view = "640x480") ⇒ Object

thrmo datas to jpg graph



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
# File 'lib/agri-controller/gruff/thermo_gruff2.rb', line 11

def thermo_gruff2(output_filename="thermo_data.jpg",input_csv_data="thermo_data.csv",range=[1.0,1.0/3,1.0/100],legend=["degree","humidty(1/3)","ppm(1/100)"],view="640x480")
  #data ini
  data=File.read(input_csv_data)
  #delete "#" comments
  res=""
  i=0
  ##
  #day_data
  data.each_line do |str|
    i+=1
    unless (str =~ /\s*#/)==0 
      #p str
      unless str=="\n"
        #p str
        res+=str
      end
    end
  end
  
  #list delete if many
  n2=view.split("x")[0].to_i
  x=i/n2+2
  x=1 if i<n2
  data=res
  thermo_data_hash={}
        
  result=[]
  range.size.times{|i| result[i]=[]}
  
  title_day=""
  line=0
  title=0
  label={}
  hour=0
  day=0
  data.each_line do |str|
    line+=1
    if line%x==0
    dat=str.chomp.split(",")
    #delete if many array
    while dat.size > range.size+1 
       dat.pop
    end
    #p dat[0]
    begin
      
      begin
        time=Time.parse(dat[0]).localtime#DateTime
      rescue
        time=Time.iso8601(dat[0]).localtime#DateTime
      end
      #dat[0].inspect+","+time.to_s
      title=title+1
    rescue
      line=line-1
      next
    end
    
    #title_day
    if title==1
      title_day=time.year.to_s+"/"+time.month.to_s+"/"+time.day.to_s
    end
    
    #line if day change
    time.hour
    if time.day!=day #(day change)
      day=time.day
      label[line]=":"#time.month.to_s+"/"+day.to_s
    elsif time.hour!=hour
         hour=time.hour
         label[title]="|"+hour.to_s
      if time.hour==12
        label[title]="|12"
      end
    else
    end
    p dat
    dat.delete_at(0)
    dat.each_index{|i| 
      begin
        result[i] << dat[i].to_f
      rescue
        result[i] << 0
      end
      } 
    end

  end

  #p label
  result.each_index{|i| result[i]=[ legend[i],result[i].map!{|x|
  # x*range[i]
  begin
    if x > -50 
      x*range[i]
    else
      nil
    end
  rescue
    nil
  end
   } ]}
  
#    result.each_index{|i| result[i]=[ legend[i],result[i].map!{|x| x*range[i] rescue nil} ]}
  #delete no_need data
  legend.each_index do |i|
    unless range[i]
      result.delete_at(i)
      #p "delete #{i}." 
    end
  end
p result    
  #p result[0]
  #p thermo_num
#=begin    
  #p label
      #gruff main
      g = Gruff::Line.new(view)
      g.font = "/usr/share/fonts/vlgothic/VL-Gothic-Regular.ttf"
      g.title="環境データ "+title_day
      #g.title_font_size =24
      g.theme_37signals
      g.maximum_value = 40
      g.minimum_value = 0
      g.y_axis_increment =2
      
      #g.baseline_value=9
      #g.increment=5
      #dataset
      result.each do |data|
        g.data(*data)
      end
      g.labels =label
      # Default theme
      #p g
      g.write(output_filename)
#=end
end

#thermo_gruff_generate(dir_to = "./htdocs/thermo", dir_from = "./cgi-bin/log/thermo", num = 5) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/agri-controller/gruff/thermo_gruff_generate.rb', line 5

def thermo_gruff_generate(dir_to="./htdocs/thermo",dir_from="./cgi-bin/log/thermo",num=5)
  dir=dir_from+"/*_thermo.csv"
  list=Dir.glob(dir)
  
  list.each do |file|
    #p file
    name=File.basename(file).split(/_/)[0]
    new_name=dir_to+"/"+name+".jpg"
    
    #p name
    #p new_name
    
    unless File.exist?(new_name)
          thermo_gruff(new_name,file,thermo_num=num,"480x420")
    end
    
    new_name=dir_to+"/thumb/"+name+"_s.jpg"
    unless File.exist?(new_name)
          thermo_gruff(new_name,file,thermo_num=num,"240x320")
    end
    #p Time.now
  end
end

#time?(time) ⇒ Boolean

module Time_module

Returns:

  • (Boolean)


10
11
12
13
# File 'lib/agri-controller/multiple_pulse_timer.rb', line 10

def time?(time)
  
  time <= Time.now
end

#timer(_from, _to) ⇒ Object

_from=“12:00:00”,_to=“12:00:05”]



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
# File 'lib/agri-controller/multiple_pulse_timer.rb', line 14

def timer(_from,_to)#_from="12:00:00",_to="12:00:05"]
  if _from.class==Time
    start=_from
  else
    start=Time.parse(_from)
  end
  if _to.class==Time
    end_time=_to
  else
    end_time=Time.parse(_to)
  end
  
  if time?(start)
    #p "time!"
    if time?(end_time)
      #"on!"
      #yield true if block_given
      #p "over"
      yield false if block_given?
      return false
    else
      #p "time!"
      yield true if block_given?
      return true
    end
  end
  nil
end

#timers(array) ⇒ Object

timers([,[“13:00”,“18:00”]])



42
43
44
45
46
47
48
49
# File 'lib/agri-controller/multiple_pulse_timer.rb', line 42

def timers(array)# timers([["12:00","12:00:10"],["13:00","18:00"]])
  #p array
  bool=false
  array.each do |dat|
    bool=bool | timer(dat[0],dat[1])
  end
  return bool#(true,false,nil)
end

#to_h(ary, time_line = 0) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/agri-controller/gruff/join_csv.rb', line 12

def to_h(ary,time_line=0)
  hash={}
  ary.each do |list|
    t=list.delete_at(time_line=0)
    t=Time.parse(t)
    hash[t]=list
  end
  hash
end