/dev/adei/kitcube-status

To get this branch, use:
bzr branch http://darksoft.org/webbzr/dev/adei/kitcube-status

« back to all changes in this revision

Viewing changes to scripts/hatpro.py

  • Committer: Chuan Miao
  • Date: 2014-03-24 08:44:49 UTC
  • Revision ID: chuan.miao@kit.edu-20140324084449-jwqklxh2x2zx4i3n
hatpro.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import re
 
2
import os
 
3
import ConfigParser
 
4
import adeitools as adei
 
5
from adeitools import DEBUG_
 
6
import simplejson as json
 
7
 
 
8
device_ = 'HATPRO'
 
9
deviceConfig = '../hatpro.ini'
 
10
serverConfig = '../server.ini'
 
11
 
 
12
def parseConfig(sensor_):
 
13
    # read server and sensor info from configuration
 
14
    # find sensor and group, then initialize adei server
 
15
    # there should be only one matched sensor, check?
 
16
 
 
17
    config = ConfigParser.ConfigParser()
 
18
    config.read(deviceConfig)
 
19
    config.read(serverConfig)
 
20
 
 
21
    sensors = [dict(config._sections[a]) 
 
22
            for a in config.sections() if re.match('^sensor\d+', a)]
 
23
 
 
24
    servers = [dict(config._sections[a]) 
 
25
            for a in config.sections() if re.match('^server', a)]
 
26
 
 
27
    try:
 
28
        sensor = [s for s in sensors if s['id'] == sensor_ ]
 
29
        sensor[0]
 
30
    except IndexError:
 
31
        print "sensor id %s is not found in configuration" % sensor_
 
32
        sys.exit()
 
33
    else:
 
34
        print "sensor id %s found in configuration" % sensor_
 
35
        sensor = sensor[0]
 
36
 
 
37
    server = [ s for s in servers 
 
38
            if s['__name__'] == sensor['server'] ]
 
39
    group = sensor['group']
 
40
 
 
41
    return (server[0], group)
 
42
 
 
43
 
 
44
class adeiReader(adei.adeiReader):
 
45
    def dostuff(self, group_, sensor_, date_):
 
46
 
 
47
        # find stamp
 
48
        stamp00 = self.groupLastStamp(group_)
 
49
        stamp0 =  self.groupLastDayStamp(group_)
 
50
        stampOptions = {
 
51
                '0d': stamp0, 
 
52
                '1d': stamp0 - 86400,
 
53
                '2d': stamp0 - 86400*2
 
54
                }
 
55
        filestamp = stampOptions[date_]
 
56
 
 
57
        # filenames 
 
58
        filename = "../cache/%s.%s.%s.json" % (
 
59
                device_, sensor_, filestamp)
 
60
 
 
61
        # update ? or not
 
62
        update = 0
 
63
        try:
 
64
            with open(filename, 'r') as f:
 
65
                oldstamp = json.loads(f.read())['time'][-1]
 
66
            if oldstamp > stamp0 and oldstamp < stamp00:
 
67
                update = 1
 
68
            print 'update ? ', stamp0, oldstamp, stamp00, update
 
69
        except:
 
70
            if DEBUG_:
 
71
                print filename
 
72
                adei.print_exc()
 
73
            update = 1
 
74
           
 
75
        return (filestamp, update)
 
76