qx_base_info.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_Base_Info
  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. "861551056095367",
  30. "861551056102502",
  31. "861551056096621",
  32. "861551056101785",
  33. "861551056086549",
  34. "861551055354120",
  35. "861551055353536",
  36. "861551056092018",
  37. "861551055319586",
  38. "861551056088875",
  39. "861551055324651",
  40. "861551055313423"
  41. }
  42. def dsj_qxz_conf_info():
  43. user = parse.quote_plus("root")
  44. passwd = parse.quote_plus("yfkj@6020")
  45. # 账号密码方式连接MongoDB | "mongodb://用户名:密码@公网ip:端口/"
  46. myclient = pymongo.MongoClient("mongodb://{0}:{1}@8.136.98.49:57017/".format(user,passwd))
  47. # myclient = pymongo.MongoClient("mongodb://127.0.0.1:12514/")
  48. # 指定数据库
  49. db = myclient.smartfarming
  50. # 指定集合
  51. collection = db.sa_device
  52. collection1 = db.sa_qxz_base_info
  53. for k in qx_device:
  54. data = {'device_id': k}
  55. cursor = collection1.find(data,{'_id':0,'id':0})
  56. for i in cursor:
  57. qx_data, is_created = MongoQXZ_Base_Info.objects.get_or_create(
  58. device_id = i.get("device_id"),
  59. defaults={
  60. "volt": i.get("volt"),
  61. "rssi": i.get("rssi"),
  62. "iccid": i.get("iccid"),
  63. "lng": i.get("lng"),
  64. "lat": i.get("lat"),
  65. "led": i.get("led"),
  66. "ledinfo": i.get("ledinfo"),
  67. "dver": i.get("dver"),
  68. "uptime": i.get("uptime")
  69. }
  70. )
  71. print(f"设备: {k} 数据量 {cursor.count()} {qx_data.id, is_created} ")
  72. return True
  73. if __name__ == "__main__":
  74. dsj_qxz_conf_info()