qx_conf.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import os
  2. import sys
  3. import time
  4. import django
  5. local_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  6. print(local_path)
  7. if local_path not in sys.path:
  8. sys.path.append(local_path)
  9. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "kedong.settings")
  10. django.setup()
  11. from django.conf import settings
  12. from smartfarming.models.device import MongoDevice
  13. from smartfarming.models.weather import QXZdata_New, MongoQXZ_Conf
  14. import json
  15. import requests
  16. import pymongo
  17. from urllib import parse
  18. my_client = pymongo.MongoClient(host="8.136.98.49", port=27017, username="root", password="yfkj@6020")
  19. my_col = my_client['smartfarming']['sa_device']
  20. qx_device = {
  21. "861551055315402",
  22. "861551056086671",
  23. "861551056088693",
  24. "861551056088479",
  25. "861551056088719",
  26. "861551055313365",
  27. "861551056086614",
  28. "861551056093800"
  29. }
  30. def dsj_qxz_conf_info():
  31. user = parse.quote_plus("root")
  32. passwd = parse.quote_plus("yfkj@6020")
  33. # 账号密码方式连接MongoDB | "mongodb://用户名:密码@公网ip:端口/"
  34. myclient = pymongo.MongoClient("mongodb://{0}:{1}@8.136.98.49:57017/".format(user,passwd))
  35. # myclient = pymongo.MongoClient("mongodb://127.0.0.1:12514/")
  36. # 指定数据库
  37. db = myclient.smartfarming
  38. # 指定集合
  39. collection = db.sa_device
  40. collection1 = db.sa_qxz_conf
  41. for k in qx_device:
  42. data = {'device_id': k}
  43. cursor = collection1.find(data,{'_id':0,'id':0})
  44. for i in cursor:
  45. qx_data = MongoQXZ_Conf.objects.create(
  46. device_id = i['device_id'],
  47. uptime = i['uptime'],
  48. e1 = i['e1'],
  49. e2 = i['e2'],
  50. e3 = i['e3'],
  51. e4 = i['e4'],
  52. e5 = i['e5'],
  53. e6 = i['e6'],
  54. e7 = i['e7'],
  55. e8 = i['e8'],
  56. e9 = i['e9'],
  57. e10 = i['e10'],
  58. e11 = i['e11'],
  59. e12 = i['e12'],
  60. e13 = i['e13'],
  61. e14 = i['e14'],
  62. e15 = i['e15'],
  63. e16 = i['e16'],
  64. e17 = i['e17'],
  65. e18 = i['e18'],
  66. e19 = i['e19'],
  67. e20 = i['e20'],
  68. e21 = i['e21'],
  69. e22 = i['e22'],
  70. e23 = i['e23'],
  71. e24 = i['e24'],
  72. e25 = i['e25'],
  73. e26 = i['e26'],
  74. e27 = i['e27'],
  75. e28 = i['e28'],
  76. e29 = i['e29'],
  77. e30 = i['e30']
  78. )
  79. print(f"设备: {k} 数据量 {cursor.count()}")
  80. return True
  81. if __name__ == "__main__":
  82. dsj_qxz_conf_info()