检测炉子打开和关闭之间的时间差

2024-06-16 14:29:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据库表,它存储了从熔炉输出的空气的温度。每行有一个时间戳(ms)和温度(degF)。为了捕获系统上的一些使用指标,我正在尝试编写sql或python来执行以下操作

A)检测温度上升期间的时间(毫秒)(系统开启) B) 检测温度值下降(系统关闭)期间的时间(毫秒)

理想情况下,我会尝试测量每个加热周期,例如开12分钟,关28分钟,开14分钟等等

样本数据如下

timestamp | F
1544154123|117.28
1544154063|116.15
1544154003|112.66
1544153943|107.26
1544153883|99.84
1544153823|92.08
1544153763|93.2
1544153703|104.79
1544153643|115.81
1544153584|116.83
1544153523|113.67
1544153463|109.17
1544153404|102.99
1544153343|94.66
1544153283|89.26
1544153223|98.94
1544153163|110.86
1544153103|115.7
1544153043|112.1
1544152983|106.7
1544152923|98.26
1544152863|85.55
1544152803|72.61

我的代码到了我现在困惑的地方

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import glob
import time
import sqlite3 as lite
import re
import requests
import json
import datetime
from datetime import timedelta
import traceback

# Import settings.

try:
    from settings import settings
except ImportError:
    from default_settings import settings

try:
    con = lite.connect(settings['db'])
    db = con.cursor()
    timestamp = int(time.time())
    db.execute('SELECT Timestamp, F from Temperature where DeviceID = 3 ORDER by Timestamp DESC limit 100'
               )

    rows = db.fetchall()

    lastValue = 0
    latestValue = 0
    firstrun = True

    refTime = datetime.datetime(2200, 9, 16, 0, 0)
    increasingTime = datetime.datetime(1978, 9, 16, 0, 0)
    decreasingTime = datetime.datetime(1978, 9, 16, 0, 0)

    periods = {}

    waitingForRise = False
    waitingForFall = False

    for row in rows:
        state = ''
        ts = datetime.datetime.fromtimestamp(int(row[0]))

   # .strtime('%Y-%m-%d %H:%M:%S')

        latestValue = row[1]

   # we capture time between the event where it transitions from inc to dec

        if firstrun == True:
            lastValue = latestValue
            firstrun = False
            decreasing = True
            waitingForRise = True
            waitingForFall = False
        elif latestValue <= lastValue:

                                  # decreasing

            if waitingForFall == True and decreasingTime < refTime:  # going down, expecting a down but not stored it yet
                waitingForFall = False  # no longer waiting for a fall
                decreasingTime = ts  # store the edge point
            else:

          # going down, do nothing, just reinforce we need to wait

                waitingForFall = True
        elif latestValue > lastValue:

                                 # increasing

            if waitingForRise == True and increasingTime < refTime:

                waitingForRise = False  # on the way down, lets record the next up
            else:
                print 'transition Down to Up -> ' + str(lastValue) \
                    + '->' + str(latestValue)
                increasingTime = ts
                waitingForFall = True
                waitingForRise = False

        lastValue = latestValue
except:

   # print ts + " > " + str(row[1]) + " > " + state

    e = sys.exc_info()[0]
    print e
    print traceback.format_exc()

Tags: fromimportfalsetruedbdatetimesettingstime