检测开炉和关炉之间的时间差

时间:2018-12-07 03:56:29

标签: python sql

我有一个数据库表,用于存储熔炉输出空气的温度。每行都有一个时间戳(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()

0 个答案:

没有答案