get_unread_alarm.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from abstract_api import AbstractApi
  2. from apis.operate_mysql import OperateMysql
  3. class Get_unread_alarm(AbstractApi):
  4. '''查询未读,可根据设备名来进行部分查询'''
  5. def operation(self, request):
  6. operate_mysql = OperateMysql()
  7. device_name = request['device']
  8. # 查报警
  9. if device_name is None:
  10. sql1 = "SELECT id, CAST(times AS CHAR) as times,name,data FROM alarm_data_tbl WHERE is_cancel=0 order by times desc;"
  11. res1 = operate_mysql.execute_sql(sql1)
  12. else:
  13. sql = "SELECT (serial_number) FROM data_point_tbl WHERE device_name=\'%s\';" % (device_name)
  14. res = operate_mysql.execute_sql(sql)
  15. basic_datas = []
  16. for each in res:
  17. basic_datas.append('c' + str(each['serial_number']))
  18. sql1 = "SELECT id, CAST(times AS CHAR) as times,name,data FROM alarm_data_tbl WHERE is_cancel=0 and name in %s order by times desc;" % (format(tuple(basic_datas)))
  19. res1 = operate_mysql.execute_sql(sql1)
  20. if len(res1) != 0:
  21. # 查报警对应的点和上下限
  22. list_alarm = []
  23. for each in res1:
  24. serial_number = each['name'].replace('c', '')
  25. sql2 = "SELECT io_point_name, alarm_low_limit, alarm_up_limit FROM data_point_tbl WHERE serial_number=%s;" % (serial_number)
  26. res2 = operate_mysql.execute_sql(sql2)
  27. if len(res2) == 0:
  28. io_point_name = "相关信息关联失败"
  29. alarm_low_limit = "相关信息关联失败"
  30. alarm_up_limit = "相关信息关联失败"
  31. else:
  32. io_point_name = res2[0]['io_point_name']
  33. alarm_low_limit = res2[0]['alarm_low_limit']
  34. alarm_up_limit = res2[0]['alarm_up_limit']
  35. dict_alarm = {'id': each['id'], 'times': each['times'], 'io_point_name': io_point_name, 'data': each['data'], 'alarm_low_limit': alarm_low_limit, 'alarm_up_limit': alarm_up_limit}
  36. list_alarm.append(dict_alarm)
  37. return list_alarm
  38. else:
  39. return None