Example #1
0
def startup_xtest(ss, cmd):
    if cmd is None:
        trace("X test Arduino")
        ss.write(sserial.notSupported(ord('X')))
        return
        
    if sserial.is_not_supported(cmd) == True:
        trace("Arduino reply: %s"%(cmd.str()))
        return
    trace("Arduino ignore %s"%(cmd.str()))
Example #2
0
def startup_xtest(ss, cmd):
    if cmd is None:
        trace("X test Arduino")
        ss.write(sserial.notSupported(ord('X')))
        return

    if sserial.is_not_supported(cmd) == True:
        trace("Arduino reply: %s" % (cmd.str()))
        return
    trace("Arduino ignore %s" % (cmd.str()))
Example #3
0
def serve(ss, cmd, tt, te, tr, ht, he, hr, s, ot, oh):
    # process the events from Arduino.
    if cmd is not None:
        if sserial.is_not_supported(cmd) == True:
            trace("Ignore command %s"%(cmd.str()))
            return (s, ot, oh)
        elif sserial.is_unknown(cmd) == True:
            trace("Ignore command %s"%(cmd.str()))
            return (s, ot, oh)
        elif sserial.is_heater_closed(cmd) == True:
            trace("Arduino close the heater for warm enough.")
            return ('detect', ot, oh)
        if sserial.is_heater_opened(cmd) == True:
            trace("Arduino open heater ok, %d*C %d%%, target is %d*C"%(ot, oh, tt))
            return (s, ot, oh)
        if sserial.is_fan_closed(cmd) == True:
            trace("Arduino close the fan for humidity ok.")
            return ('detect', ot, oh)
        if sserial.is_fan_opened(cmd) == True:
            trace("Arduino open fan ok, %d*C %d%%, target is %d%%"%(ot, oh, ht))
            return (s, ot, oh)
            
    # use state machine to drive new event.
    if s == 'init':
        startup_ping(ss, cmd)
        return ('xtest', 0, 0)
    
    if s == 'xtest':
        startup_xtest(ss, cmd)
        return ('detect', 0, 0)
        
    if s == 'detect':
        (t, h) = detect(ss, cmd, ot, oh)
        if t == 0 and h == 0:
            return ('detect', ot, oh)
        if t < tt - tr:
            return ('heat', t, h)
        if h >= ht + hr:
            return ('fan', t, h)
        return ('detect', t, h)
        
    if s == 'heat':
        (t, h) = detect(ss, cmd, ot, oh)
        if t == 0 and h == 0:
            return ('heat', ot, oh)
        if t < tt:
            #trace("Notify Arduino to open heater.")
            ss.write(sserial.openHeater(tt, te))
            return ('heat', t, h)
        trace("Warm enough, no need to heat, %d*C %d%%"%(t, h))
        return ('detect', t, h)
        
    if s == 'fan':
        (t, h) = detect(ss, cmd, ot, oh)
        if t == 0 and h == 0:
            return ('fan', ot, oh)
        if h >= ht:
            #trace("Notify Arduino to open fan.")
            ss.write(sserial.openFan(ht, he))
            return ('fan', t, h)
        trace("Humidity ok, no need to fan, %d*C %d%%"%(t, h))
        return ('detect', t, h)
        
    return ('init', 0, 0)
Example #4
0
def serve(ss, cmd, tt, te, tr, ht, he, hr, s, ot, oh):
    # process the events from Arduino.
    if cmd is not None:
        if sserial.is_not_supported(cmd) == True:
            trace("Ignore command %s" % (cmd.str()))
            return (s, ot, oh)
        elif sserial.is_unknown(cmd) == True:
            trace("Ignore command %s" % (cmd.str()))
            return (s, ot, oh)
        elif sserial.is_heater_closed(cmd) == True:
            trace("Arduino close the heater for warm enough.")
            return ('detect', ot, oh)
        if sserial.is_heater_opened(cmd) == True:
            trace("Arduino open heater ok, %d*C %d%%, target is %d*C" %
                  (ot, oh, tt))
            return (s, ot, oh)
        if sserial.is_fan_closed(cmd) == True:
            trace("Arduino close the fan for humidity ok.")
            return ('detect', ot, oh)
        if sserial.is_fan_opened(cmd) == True:
            trace("Arduino open fan ok, %d*C %d%%, target is %d%%" %
                  (ot, oh, ht))
            return (s, ot, oh)

    # use state machine to drive new event.
    if s == 'init':
        startup_ping(ss, cmd)
        return ('xtest', 0, 0)

    if s == 'xtest':
        startup_xtest(ss, cmd)
        return ('detect', 0, 0)

    if s == 'detect':
        (t, h) = detect(ss, cmd, ot, oh)
        if t == 0 and h == 0:
            return ('detect', ot, oh)
        if t < tt - tr:
            return ('heat', t, h)
        if h >= ht + hr:
            return ('fan', t, h)
        return ('detect', t, h)

    if s == 'heat':
        (t, h) = detect(ss, cmd, ot, oh)
        if t == 0 and h == 0:
            return ('heat', ot, oh)
        if t < tt:
            #trace("Notify Arduino to open heater.")
            ss.write(sserial.openHeater(tt, te))
            return ('heat', t, h)
        trace("Warm enough, no need to heat, %d*C %d%%" % (t, h))
        return ('detect', t, h)

    if s == 'fan':
        (t, h) = detect(ss, cmd, ot, oh)
        if t == 0 and h == 0:
            return ('fan', ot, oh)
        if h >= ht:
            #trace("Notify Arduino to open fan.")
            ss.write(sserial.openFan(ht, he))
            return ('fan', t, h)
        trace("Humidity ok, no need to fan, %d*C %d%%" % (t, h))
        return ('detect', t, h)

    return ('init', 0, 0)