-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
206 lines (180 loc) · 5.95 KB
/
config.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import redis
# coding=utf-8
from pymongo import MongoClient
import datetime
import logging
import itchatmp
from itchatmp.content import TEXT,SAFE
logging.basicConfig(level=logging.DEBUG, format='[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s')
itchatmp.update_config(itchatmp.WechatConfig(
token='xxx',
copId='xxx',
appSecret='xxx',
encryptMode=itchatmp.content.SAFE,
encodingAesKey='xxxxx', ))
def getDate():
now = datetime.datetime.now().strftime("%Y%m%d")
return now
def getTime():
now = datetime.datetime.now().strftime("%Y-%m-%d ")
return now
class LOG:
@staticmethod # 中间件--tcp服务
def middle():
logger = logging.getLogger('%s:%d' % ("MIDDLE", 9876))
return logger
@staticmethod # 后台---websocket服务
def end():
logger = logging.getLogger('%s:%d' % ('END', 9999))
return logger
@staticmethod # 后台---web服务
def web():
logger = logging.getLogger('%s:%d' % ('WEB', 9999))
return logger
class DBSENSOR:
'''
存储数据模型,此模型只适合用来存储数据,不适合记录下载图片情形
{"date":"2017-03-06",
"data":
{
"id":"",
"tem":"",
"wet":"",
"time":"",}
}
'''
def __init__(self):
self.time = getTime()
self.table = "sensor" + getDate()
self.MONGODB_URI = "mongodb://localhost:27017/"
self.client = MongoClient(self.MONGODB_URI)
self.db = self.client["sensor"]
self.collection = self.db[self.table]
def save(self, record):
self.collection.save(record)
def query(self):
try:
res = {}
# todo find({"flag":true})验证 再做数据修改{"flag":false}
rc = self.collection.find_one()
r = rc[self.table]
res["tem"] = r["tem"]
res["wet"] = r["wet"]
res["time"] = r["time"]
print(res)
except Exception as e:
print("查询出错")
res = {self.time: {}}
return res
#微信报警模块
class WX:
'''
微信企业号配置
token='W4jcMkLSnO3jAiFs434MaG',
copId='wx6ea71abb13481848',
appSecret='pimbBsWhKSX7OnODx3v0pLN8cocR5SOBmeMAP5jdLIQ',
encryptMode=itchatmp.content.SAFE,
encodingAesKey='p1pINcRhEjRBnPXaeaUgrL6Nag8fixm4gvzOwxf8hTR',
'''
def __init__(self):
self.token = "W4jcMkLSnO3jAiFs434MaG"
self.copId = "wx6ea71abb13481848"
self.appSecret = "pimbBsWhKSX7OnODx3v0pLN8cocR5SOBmeMAP5jdLIQ"
self.encryptMode= SAFE
self.encodingAesKey = 'p1pINcRhEjRBnPXaeaUgrL6Nag8fixm4gvzOwxf8hTR',
def CALL(self,message):
#加载配置
result = itchatmp.messages.send_some(TEXT, message, targetIdList=['snt_qihongtao'], agentId=0)
return result
# class DBSENSOR:
# '''
# 存储数据模型
# 一天一个表 table20170306
# {
# "id":"",
# "tem":"",
# "wet":"",
# "datetime":""
# "flag":True
# }
# '''
#
# def __init__(self):
# self.date = "sensor" + getDate()
# self.MONGODB_URI = "mongodb://localhost:27017/"
# self.client = MongoClient(self.MONGODB_URI)
# self.db = self.client["sensordb"]
# self.collection = self.db[self.date]
#
# def insert(self, record):
# # 数据在外部查询结束后,再插入数据库
# self.collection.insert(record)
#
# def insert_many(self, recordList):
# self.collection.insert_many(recordList)
#
# def count(self):
# count = self.collection.count()
# return count
#
# def tcheck(self):
# # 针对一条记录存储所有数据list
# # 判断数据是否存在,判断key的存在,再判断value的长度
# # self.collection.
# pass
#
# def query(self):
# results = []
# try:
# records = self.collection.find()
# # 可以再优化
# for r in records:
# print(r)
# res = {}
# res["tem"] = r["tem"]
# res["wet"] = r["wet"]
# res["datetime"] = r["datetime"]
# res["flag"]=r["flag"]
# results.append(res)
# return results
# except Exception as e:
# print("DBHistory query error:{}".format(e))
# def check(self):
#
# print("start")
# try:
# num = self.collection.find().count()
# print(num)
# if num == 0:
# return saveHistory()
# else:
# return self.query()
# except Exception as e:
# print(e)
class RedisHelper(object):
def __init__(self):
self.__conn = redis.Redis(host='127.0.0.1', port=6379) # 连接Redis
self.channel = 'monitor' # 定义名称
self.video_channel = "video" # 摄像头监控主题
def publish(self, msg): # 定义发布方法
self.__conn.publish(self.channel, msg)
return True
def publish_video(self, msg): # 摄像头监控发布方法
self.__conn.publish(self.video_channel, msg)
return True
def subscribe(self): # 定义订阅方法
pub = self.__conn.pubsub()
pub.subscribe(self.channel)
pub.parse_response()
return pub
def subscribe_video(self): # 定义订阅方法
pub = self.__conn.pubsub()
pub.subscribe(self.video_channel)
pub.parse_response()
return pub
if __name__ == "__main__":
pass
# data={"tem":"tem","wet":"wet","time":getTime(),"flag":True}
# data = {DBSENSOR().table: {"tem": "tem", "wet": "wet", "time": getTime(), "flag": True}}
# DBSENSOR().save(data)
DBSENSOR().query()