node.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # 导入pymysql
  2. import socket
  3. import time
  4. import LedControl as lc
  5. import google.protobuf.text_format as tf
  6. import pymysql as psql
  7. #db参数
  8. db_ip="192.168.1.233"
  9. db_port=3306
  10. db_name="ct_project"
  11. db_user="zx"
  12. db_password="zx123456"
  13. #led参数
  14. led_ip="192.168.1.169"
  15. led_port=5005
  16. #LED消息发送器
  17. g_lc=lc.LedControl(led_ip,led_port)
  18. class DBQuery():
  19. def __init__(self,ip,port,database,user,password):
  20. self.ip=ip
  21. self.port=port
  22. self.database=database
  23. self.user=user
  24. self.password=password
  25. self.conn=psql.connect(host=self.ip,port=self.port,database=self.database,charset="utf8",user=self.user,passwd=self.password)
  26. self.unit_cmd_1=None
  27. self.unit_cmd_2=None
  28. self.unit_cmd_3=None
  29. def run(self):
  30. while True:
  31. #获取一个光标
  32. cursor = self.conn.cursor()
  33. #查询指令队列所有信息
  34. SQL1="select * from command_queue where unit = 1 and type = 2 order by queue_id ASC;"
  35. SQL2="select * from command_queue where unit = 2 and type = 2 order by queue_id ASC;"
  36. SQL3="select * from command_queue where unit = 3 and type = 2 order by queue_id ASC;"
  37. cmd_dict1 = {}
  38. cmd_dict2 = {}
  39. cmd_dict3 = {}
  40. #执行语句 返回结果数量
  41. command_count=cursor.execute(SQL1)
  42. self.conn.commit()
  43. #结果数量大于0
  44. if(command_count > 0):
  45. queue_cmd=cursor.fetchall()
  46. column=[index[0] for index in cursor.description ]# 列名
  47. cmd_dict1 = [dict(zip(column, row)) for row in queue_cmd] # row是数据库返回的一条一条记录,其中的每一天和column写成字典,最后就是字典数组
  48. command_count=cursor.execute(SQL2)
  49. self.conn.commit()
  50. #结果数量大于0
  51. if(command_count > 0):
  52. queue_cmd=cursor.fetchall()
  53. column=[index[0] for index in cursor.description ]# 列名
  54. cmd_dict2 = [dict(zip(column, row)) for row in queue_cmd] # row是数据库返回的一条一条记录,其中的每一天和column写成字典,最后就是字典数组
  55. command_count=cursor.execute(SQL3)
  56. self.conn.commit()
  57. #结果数量大于0
  58. if(command_count > 0):
  59. queue_cmd=cursor.fetchall()
  60. column=[index[0] for index in cursor.description ]# 列名
  61. cmd_dict3 = [dict(zip(column, row)) for row in queue_cmd] # row是数据库返回的一条一条记录,其中的每一天和column写成字典,最后就是字典数组
  62. if(cmd_dict1 != self.unit_cmd_1):
  63. self.unit_cmd_1 = cmd_dict1.copy()
  64. led_show_string = g_lc.getInput(self.unit_cmd_1)
  65. g_lc.sendLedMsg(0,led_show_string)
  66. if(cmd_dict2 != self.unit_cmd_2):
  67. self.unit_cmd_2 = cmd_dict2.copy()
  68. led_show_string = g_lc.getInput(self.unit_cmd_2)
  69. g_lc.sendLedMsg(1,led_show_string)
  70. if(cmd_dict3 != self.unit_cmd_3):
  71. self.unit_cmd_3 = cmd_dict3.copy()
  72. led_show_string = g_lc.getInput(self.unit_cmd_3)
  73. g_lc.sendLedMsg(2,led_show_string)
  74. # 关闭光标
  75. cursor.close()
  76. time.sleep(1)
  77. if __name__=="__main__":
  78. db_query = DBQuery(db_ip,db_port,db_name,db_user,db_password)
  79. db_query.run()