1 Star 0 Fork 0

ReusLiu/python_example

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.py 2.10 KB
一键复制 编辑 原始数据 按行查看 历史
liuxq 提交于 2021-04-12 20:19 . 初始化
from datetime import datetime
from influxdb import InfluxDBClient
from sqlalchemy import create_engine, text
engine = create_engine(
'mysql+pymysql://isyscore:Isysc0re@172.16.200.218:23306/bap_data_warehouse?charset=utf8&connect_timeout=5')
client = InfluxDBClient('172.16.200.218', 28086, database='isc_dmc')
now = datetime.now()
def minute(t):
if 0 <= t.minute < 15:
return 0
elif 15 <= t.minute < 30:
return 15
elif 30 <= t.minute < 45:
return 30
elif 45 <= t.minute < 60:
return 45
start = datetime(now.year, now.month, now.day)
current = datetime(now.year, now.month, now.day, now.hour, minute(now))
total_consume = 0
try:
device_lst = ['BPD-DB-22', 'BPD-DB-19', 'BPD-DB-18', 'BPD-DB-17', 'BPD-DB-16', 'BPD-DB-8', 'BPD-DB-7', 'BPD-DB-4']
for i in device_lst:
start_sql = "select value from device_attr_double where devId='{}' and identifier = 'EPI' and time >= {} order by time asc limit 1".format(
i, int(start.timestamp() * 1000000000))
end_sql = "select value from device_attr_double where devId='{}' and identifier = 'EPI' and time >= {} order by time desc limit 1".format(
i, int(start.timestamp() * 1000000000))
zero_lst = [float(p['value']) for p in client.query(start_sql).get_points()]
curr_lst = [float(p['value']) for p in client.query(end_sql).get_points()]
if len(zero_lst) > 0 and len(curr_lst) > 0:
accum = curr_lst[0] - zero_lst[0]
else:
accum = 0
total_consume += accum
engine.execute('''
create table if not exists ST_ELEC_CONSUME
(
create_time datetime primary key not null,
total_consume double null
)
''')
v = {'create_time': current, 'total_consume': round(total_consume, 2)}
table_name = "ST_ELEC_CONSUME"
ks = ','.join(['`{}`'.format(k) for k in v.keys()])
vs = ','.join([':{}'.format(v) for v in v.keys()])
stmt = text("""replace into {} ({}) values ({})""".format(table_name, ks, vs))
# engine.execute(stmt, v)
except Exception as e:
raise e
finally:
client.close()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/reus_admin/python_example.git
git@gitee.com:reus_admin/python_example.git
reus_admin
python_example
python_example
master

搜索帮助