Exemplo n.º 1
0
def Timechecking(time_min, container_id, sensor_id, activity_id, typing,
                 systemtype):
    target_time = datetime.datetime.now() + datetime.timedelta(
        minutes=time_min)
    while True:
        currentmisting = dbHandler.GetContainerStatus(container_id, typing)
        current_time = datetime.datetime.now()
        if (not currentmisting):
            dbHandler.UpdateUserActions('2', systemtype, 'true', container_id,
                                        '1')
            avetemp, avehumid, ts = dbHandler.GetAveTempHumid(container_id)
            endtime = str(datetime.datetime.now())
            sendhttp_request(sensor_id, "OFF", systemtype)
            dbHandler.UpdateActivity(activity_id, sensor_id, endtime, avetemp,
                                     avehumid)
            logging.info("TC: not currentmisting " + str(currentmisting) +
                         " endtime " + str(endtime) + " avetemp " +
                         str(avetemp) + " avehumid " + str(avehumid) +
                         " ts: " + str(ts))
            try:
                for each_user in user_id:
                    url = "http://{}/alert/{}/{} is STOPPED MANUALLY. Current temp is {} and humidity is {}".format(
                        wechat_url, each_user, typing, round(avetemp, 1),
                        round(avehumid, 1))
                    requests.get(url, timeout=2)
            except:
                logging.info("error with wechat sending")
            time.sleep(30)
            break

        if current_time > target_time:
            dbHandler.UpdateUserActions('2', systemtype, 'true', container_id,
                                        '1')
            avetemp, avehumid, ts = dbHandler.GetAveTempHumid(container_id)
            endtime = str(datetime.datetime.now())
            sendhttp_request(sensor_id, "OFF", systemtype)
            dbHandler.UpdateContainerStatus(container_id, typing, "false")
            dbHandler.UpdateActivity(activity_id, sensor_id, endtime, avetemp,
                                     avehumid)
            logging.info("TC: current_time > target_time " + " endtime " +
                         str(endtime) + " avetemp " + str(avetemp) +
                         " avehumid " + str(avehumid) + " ts: " + str(ts))
            try:
                for each_user in user_id:
                    url = "http://{}/alert/{}/{} is STOPPED due to time up. Current temp is {} and humidity is {}".format(
                        wechat_url, each_user, typing, round(avetemp, 1),
                        round(avehumid, 1))
                    requests.get(url, timeout=2)
            except:
                logging.info("error with wechat sending")
            time.sleep(30)
            break
        time.sleep(30)
Exemplo n.º 2
0
 def get(self,action):
     if action == 'now':
         avetemp, avehumid, ts = dbHandler.GetAveTempHumid('01579684047480')
         temp = round(avetemp, 1)
         humid = round(avehumid, 1)
         logging.info('fetch data from db')         
         return {'temp': temp, 'humid':humid, 'ts': ts}
     if action == 'config':
         start_humid, stop_humid, start_temp, stop_temp, fan_id, mist_id, fanning, auto_fanning, misting, auto_misting = dbHandler.GetContainerInfo('01579684047480')
         return {'hightemp': round(start_temp,1), 'lowtemp':round(stop_temp,1), 'highhumid':round(stop_humid,1), 'lowhumid':round(start_humid,1)}
                alarm_type, direction, measure_type, val, critical_sent, warning_sent = 'warning', 'above', 'Temperature', warning_value, critical_sent, True
        elif alert_type == 'highHumidity':
            if avehumid >= critical_value and not critical_sent:
                alarm_type, direction, measure_type, val, critical_sent, warning_sent = 'critical', 'above', 'Humidity', critical_value, True, warning_sent
            elif avehumid >= warning_value and not warning_sent:
                alarm_type, direction, measure_type, val, critical_sent, warning_sent = 'warning', 'above', 'Humidity', warning_value, critical_sent, True
        return alarm_type, direction, measure_type, val, critical_sent, warning_sent
    except Exception as e:
            logging.error("GetAlertMsg: " + str(e))

if __name__ == '__main__':
    ListOfAlerts = dbHandler.GetAlertList()
    logging.info(ListOfAlerts)
    alarm_type, direction, measure_type, val = None, None, None, None
    ts = datetime.datetime.now()
    for each_container in ListOfAlerts:
        try:
            alert_setting_id = each_container[0]
            container_name = dbHandler.GetContainerName(each_container[1])
            avetemp, avehumid, rdate = dbHandler.GetAveTempHumid(str(each_container[1]))
            warning_value, critical_value, alert_type, warning_sent, critical_sent =  each_container[2], each_container[3], each_container[4], each_container[5], each_container[6]
            logging.info('temp->' + str(avetemp) + ' humid->' + str(avehumid) + ' alert_type ->' + str(alert_type) + ' warning_value->' + str(warning_value) + ' critical_value->' + str(critical_value)
            + ' warning_sent->' + str(warning_sent) + ' critical_sent->' + str(critical_sent))
            alarm_type, direction, measure_type, val, critical_sent, warning_sent = GetAlertMsg(container_name, avetemp, avehumid, warning_value, critical_value, alert_type, warning_sent, critical_sent)
            if val is not None:
                logging.info("Output-->" + str(alarm_type) +" -- " + str(direction) +" -- " +  str(measure_type)+" -- " + str(val)+ " -- " + str(critical_sent)+" -- " + str(warning_sent))
                dbHandler.InsertAlert(alarm_type,direction,measure_type,val,ts,container_name,alert_setting_id) 
                dbHandler.UpdateAlertSettings(alert_setting_id,warning_sent,critical_sent)
        except Exception as e:
            logging.error("Main: " + str(e))
Exemplo n.º 4
0
def startfanning(container_id,
                 before_avetemp,
                 upper_target_temp,
                 lower_target_temp,
                 sensor_id,
                 manual=False,
                 time_duration=0):
    index = 0
    indexofstart = True
    start_time = str(datetime.datetime.now())
    activity_id = int('{0:%Y%m%d%H%M%S}'.format(datetime.datetime.now()))
    dbHandler.UpdateContainerStatus(container_id, "fanning", "true")
    time.sleep(2)
    if manual:
        try:
            for each_user in user_id:
                url = "http://{}/alert/{}/FANNING is STARTED MANUALLY. Current temperature at farm is {}".format(
                    wechat_url, each_user, round(before_avetemp, 1))
                requests.get(url, timeout=2)
        except:
            logging.info("error with wechat sending")
    else:
        for each in sensor_id:
            sendhttp_request(each, "ON", "FAN")
            time.sleep(30)
        logging.info(
            "5. Start Fanning ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
        )
        logging.info("indexofstart: " + str(indexofstart) +
                     " before_avetemp: " + str(before_avetemp) +
                     " lower_target_temp: " + str(lower_target_temp) +
                     " upper_target_temp: " + str(upper_target_temp))
        try:
            for each_user in user_id:
                url = "http://{}/alert/{}/FANNING is STARTED AUTOMATICALLY. Current temperature at farm is {}".format(
                    wechat_url, each_user, round(before_avetemp, 1))
                requests.get(url, timeout=2)
        except:
            logging.info("error with wechat sending")
    while True:
        avetemp, avehumid, ts = dbHandler.GetAveTempHumid(container_id)
        upper_target_temp, lower_target_temp = dbHandler.GetContainerTempInfo(
            container_id)
        currentfanning = dbHandler.GetContainerStatus(container_id, "fanning")
        auto_fanning = dbHandler.GetContainerStatus(container_id,
                                                    "auto_fanning")
        index = index + 1
        logging.info(
            "6." + str(index) +
            ".SF Inside While loop ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
        )
        logging.info("currentfanning: " + str(currentfanning) +
                     " indexofstart: " + str(indexofstart) + " avetemp: " +
                     str(avetemp) + " ts: " + str(ts) + " before_avetemp: " +
                     str(before_avetemp) + " lower_target_temp: " +
                     str(lower_target_temp) + " upper_target_temp: " +
                     str(upper_target_temp) + " auto_fanning: " +
                     str(auto_fanning))
        if indexofstart:
            for each in sensor_id:
                dbHandler.InsertActivity(
                    str(activity_id) + str(each), each, start_time, "FAN",
                    avetemp, avehumid)
            indexofstart = False
            logging.info("6." + str(index) + ".SF: indexofstart")

        if (not currentfanning) or (not auto_fanning and not manual):
            endtime = str(datetime.datetime.now())
            for each in sensor_id:
                dbHandler.UpdateActivity(
                    str(activity_id) + str(each), each, endtime, avetemp,
                    avehumid)
                dbHandler.UpdateContainerStatus(container_id, "fanning",
                                                "false")
                sendhttp_request(each, "OFF", "FAN")
                time.sleep(30)

            try:
                for each_user in user_id:
                    url = "http://{}/alert/{}/FANNING is STOPPED MANUALLY. Current temperature at farm is {}".format(
                        wechat_url, each_user, round(avetemp, 1))
                    requests.get(url, timeout=2)
            except:
                logging.info("error with wechat sending")
            time.sleep(60)
            break

        if manual:
            for each in sensor_id:
                sendhttp_request(each, "ON", "FAN")
                p = Process(target=Timechecking,
                            args=(
                                time_duration,
                                container_id,
                                each,
                                str(activity_id) + str(each),
                                "fanning",
                                "FAN",
                            ))
                p.start()
                time.sleep(30)
            break

        else:
            if (avetemp > upper_target_temp):
                #startmisting-switch keep on starting
                time.sleep(10)
                logging.info("6." + str(index) +
                             ".SF: avetemp > upper_target_temp")
            elif (avetemp < upper_target_temp and avetemp > lower_target_temp):
                time.sleep(10)
                logging.info(
                    "6." + str(index) +
                    ".SF: avetemp < upper_target_temp  and avetemp > lower_target_temp"
                )
            elif (avetemp < lower_target_temp):
                time.sleep(10)
                endtime = str(datetime.datetime.now())
                for each in sensor_id:
                    sendhttp_request(each, "OFF", "FAN")
                    time.sleep(30)
                    #update database
                    dbHandler.UpdateActivity(
                        str(activity_id) + str(each), each, endtime, avetemp,
                        avehumid)
                dbHandler.UpdateContainerStatus(container_id, "fanning",
                                                "false")
                logging.info(
                    "6." + str(index) +
                    ".SF: avetemp < lower_target_temp .. Success: Temperature has reached Lower Target limit"
                )
                try:
                    for each_user in user_id:
                        url = "http://{}/alert/{}/FANNING is STOPPED AUTOMATICALLY. Current temperature at farm is {}".format(
                            wechat_url, each_user, round(avetemp, 1))
                        requests.get(url, timeout=2)
                except:
                    logging.info("error with wechat sending")
                time.sleep(10)
                break
            else:
                logging.info("6." + str(index) + ".SF: finally else")

        if avetemp < before_avetemp:
            logging.info(
                "6." + str(index) +
                ".SF: avetemp < before_avetemp.. this is good. Temperature is decreasing"
            )
        elif avetemp > before_avetemp:
            logging.info(
                "6." + str(index) +
                ".SF: avetemp > before_avetemp.. this is NOT good. Temperature is increasing"
            )

        time.sleep(20)
Exemplo n.º 5
0
def startmisting(container_id,
                 before_avehumid,
                 upper_target_humid,
                 lower_target_humid,
                 sensor_id,
                 manual=False,
                 time_duration=0):
    index = 0
    indexofstart = True
    dbHandler.UpdateContainerStatus(container_id, "misting", "true")
    time.sleep(2)
    start_time = str(datetime.datetime.now())
    activity_id = int('{0:%Y%m%d%H%M%S}'.format(datetime.datetime.now()))
    logging.info(
        "5. Start misting ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
    )
    logging.info("indexofstart: " + str(indexofstart) + " before_avehumid: " +
                 str(before_avehumid) + " lower_target_humid: " +
                 str(lower_target_humid) + " upper_target_humid: " +
                 str(upper_target_humid))
    if manual:
        try:
            for each_user in user_id:
                url = "http://{}/alert/{}/Misting is STARTED MANUALLY. Current humidity at farm is {}".format(
                    wechat_url, each_user, round(before_avehumid, 1))
                requests.get(url, timeout=2)
        except:
            logging.info("error with wechat sending")
    else:
        sendhttp_request(sensor_id, "ON", "MIST")
        try:
            for each_user in user_id:
                url = "http://{}/alert/{}/Misting is STARTED AUTOMATICALLY. Current humidity at farm is {}".format(
                    wechat_url, each_user, round(before_avehumid, 1))
                requests.get(url, timeout=2)
        except:
            logging.info("error with wechat sending")

    while True:
        avetemp, avehumid, ts = dbHandler.GetAveTempHumid(container_id)
        upper_target_humid, lower_target_humid = dbHandler.GetContainerHumidInfo(
            container_id)
        currentmisting = dbHandler.GetContainerStatus(container_id, "misting")
        auto_misting = dbHandler.GetContainerStatus(container_id,
                                                    "auto_misting")
        index = index + 1
        logging.info(
            "6." + str(index) +
            ".SM Inside While loop ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
        )
        logging.info("currentmisting: " + str(currentmisting) +
                     " indexofstart: " + str(indexofstart) + " avehumid: " +
                     str(avehumid) + " ts: " + str(ts) + " before_avehumid: " +
                     str(before_avehumid) + " lower_target_humid: " +
                     str(lower_target_humid) + " upper_target_humid: " +
                     str(upper_target_humid) + " auto_misting: " +
                     str(auto_misting))
        if indexofstart:
            dbHandler.InsertActivity(activity_id, sensor_id, start_time,
                                     "MIST", avetemp, avehumid)
            indexofstart = False
            logging.info("6." + str(index) + ".SM: indexofstart")
        if (not currentmisting) or (not auto_misting and not manual):
            endtime = str(datetime.datetime.now())
            sendhttp_request(sensor_id, "OFF", "MIST")
            dbHandler.UpdateContainerStatus(container_id, "misting", "false")
            dbHandler.UpdateActivity(activity_id, sensor_id, endtime, avetemp,
                                     avehumid)
            try:
                for each_user in user_id:
                    url = "http://{}/alert/{}/Misting is STOPPED MANUALLY. Current humidity at farm is {}".format(
                        wechat_url, each_user, round(avehumid, 1))
                    requests.get(url, timeout=2)
            except:
                logging.info("error with wechat sending")
            time.sleep(60)
            break

        if manual:
            sendhttp_request(sensor_id, "ON", "MIST")
            p = Process(target=Timechecking,
                        args=(time_duration, container_id, sensor_id,
                              start_time, "misting", "MIST"))
            p.start()
            break

        else:
            if (avehumid < lower_target_humid):
                #startmisting-switch keep on starting
                time.sleep(10)
                logging.info("6." + str(index) +
                             ".SM: avehumid < lower_target_humid")
            elif (avehumid > lower_target_humid
                  and avehumid < upper_target_humid):
                time.sleep(10)
                logging.info(
                    "6." + str(index) +
                    ".SM: avehumid > lower_target_humid and avehumid < upper_target_humid"
                )
            elif (avehumid > upper_target_humid):
                endtime = str(datetime.datetime.now())
                sendhttp_request(sensor_id, "OFF", "MIST")
                #update database
                dbHandler.UpdateContainerStatus(container_id, "misting",
                                                "false")
                dbHandler.UpdateActivity(activity_id, sensor_id, endtime,
                                         avetemp, avehumid)
                logging.info(
                    "6." + str(index) +
                    ".SM: avehumid > upper_target_humid .. Success: Humidity has reached Upper Target limit"
                )
                try:
                    for each_user in user_id:
                        url = "http://{}/alert/{}/Misting is STOPPED AUTOMATICALLY. Current humidity at farm is {}".format(
                            wechat_url, each_user, round(avehumid, 1))
                        requests.get(url, timeout=2)
                except:
                    logging.info("error with wechat sending")
                time.sleep(10)
                break
            else:
                logging.info("6." + str(index) + ".SM: finally else")

        if avehumid > before_avehumid:
            logging.info(
                "6." + str(index) +
                ".SM: avehumid > before_avehumid.. this is good. humidity is increasing"
            )
        elif avehumid < before_avehumid:
            logging.info(
                "6." + str(index) +
                ".SM: avehumid < before_avehumid .. this is NOT good. humidity is decreasing"
            )

        time.sleep(20)
Exemplo n.º 6
0
        start_humid, stop_humid, start_temp, stop_temp, fan_id, mist_id, fanning, auto_fanning, misting, auto_misting = dbHandler.GetContainerInfo(
            each_container)
        logging.info(
            "1.Containers info   +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
        )
        logging.info("start_humid:" + str(start_humid) + " stop_humid: " +
                     str(stop_humid) + " start_temp: " + str(start_temp) +
                     " stop_temp: " + str(stop_temp) + " fan_id: " +
                     str(fan_id) + " mist_id: " + str(mist_id) + " fanning: " +
                     str(fanning) + " auto_fanning: " + str(auto_fanning) +
                     " misting: " + str(misting) + " auto_misting: " +
                     str(auto_misting))
        logging.info(
            "2. Average Temperature  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
        )
        avetemp, avehumid, ts = dbHandler.GetAveTempHumid(each_container)
        logging.info("avetemp: " + str(avetemp) + " avehumid: " +
                     str(avehumid) + " ts: " + str(ts))
        logging.info(
            "3. Fan Mist +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
        )
        #   should_fanning - Make decision based on the Temp inside the GreenHouse
        #   should_misting - Make decision based on the Humidity inside the GreenHouse
        should_misting, should_fanning = ShoudStartFanMist(
            start_humid, start_temp, avetemp, avehumid)
        logging.info("should_misting:" + str(should_misting) +
                     " should_fanning: " + str(should_fanning))
        fan_id_list = fan_id.split(',')

        manual_start_misting = dbHandler.GetSystemStatus(
            'MIST', each_container, '(0,1)', 'true')