-
Notifications
You must be signed in to change notification settings - Fork 0
/
learning_engine_daemon.py
132 lines (90 loc) · 3.07 KB
/
learning_engine_daemon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
# Gene Cheng(cgm). Nov. 2015
import os
import csv as csv
import numpy as np
import sys
import time
from sklearn.naive_bayes import GaussianNB
from daemon import runner
from lockfile import LockFile
from pyonep import onep
def start_to_learn(clf):
lock = LockFile('tmp/lockfile')
lock.acquire()
csvFileObj = csv.reader(open(('train.csv'), 'rb'))
data = []
for row in csvFileObj:
data.append(row)
data = np.array(data)
trainingData = data[0::, 0:3].astype(np.float)
labelData = data[0::, 3].astype(np.float)
print 'start to learn'
clf.fit(trainingData, labelData)
print 'learning finished'
lock.release()
class LearningEngine():
def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/tty'
self.stderr_path = '/dev/tty'
self.pidfile_path = '/tmp/learnEnging.pid'
self.default_path = os.path.dirname(os.path.abspath(sys.argv[0]))
self.pidfile_timeout = 5
self.clf = GaussianNB()
start_to_learn(self.clf)
self.onepInst = onep.OnepV1()
self.cik = 'c7daa5a76badd48e8b8a7b71560670f66ba23c4b'
self.learnCnt = 0
def run(self):
os.chdir(self.default_path)
while True:
print 'thread start'
self.learnCnt += 1
if self.learnCnt == 10:
self.learnCnt = 0
start_to_learn(self.clf)
while True:
print 'try to calc index'
isok, response = self.onepInst.read(self.cik, {'alias': 'indoor_temp'}, {'limit': 1, 'sort': 'desc', 'selection': 'all'})
if isok != True:
print 'indoor temp error'
break
indoorTemp = response[0][1]
isok, response = self.onepInst.read(self.cik, {'alias': 'indoor_humi'}, {'limit': 1, 'sort': 'desc', 'selection': 'all'})
if isok != True:
print 'indoor humi error'
break
indoorHumi = response[0][1]
indoorComfortIndex = self.clf.predict([[indoorTemp, indoorHumi, 0.0]])
print indoorComfortIndex
self.onepInst.write(self.cik,
{"alias": "indoor_comfort_index"},
indoorComfortIndex[0],
{})
break
while True:
isok, response = self.onepInst.read(self.cik, {'alias': 'outdoor_curTemp'}, {'limit': 1, 'sort': 'desc', 'selection': 'all'})
if isok != True:
break
outdoorTemp = response[0][1]
isok, response = self.onepInst.read(self.cik, {'alias': 'outdoor_curHumidity'}, {'limit': 1, 'sort': 'desc', 'selection': 'all'})
if isok != True:
break
outdoorHumi = response[0][1]
isok, response = self.onepInst.read(self.cik, {'alias': 'outdoor_windspeed'}, {'limit': 1, 'sort': 'desc', 'selection': 'all'})
if isok != True:
break
wind = response[0][1]
outdoorComfortIndex = self.clf.predict([[outdoorTemp, outdoorHumi, wind]])
print outdoorComfortIndex
self.onepInst.write(self.cik,
{"alias": "outdoor_comfort_index"},
outdoorComfortIndex[0],
{})
break
time.sleep(30)
if __name__ == '__main__':
learningEngine = LearningEngine()
daemonRunner = runner.DaemonRunner(learningEngine)
daemonRunner.do_action()