qx_base_info.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. }
  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_base_info
  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, is_created = MongoQXZ_Base_Info.objects.get_or_create(
  46. device_id = i.get("device_id"),
  47. defaults={
  48. "volt": i.get("volt"),
  49. "rssi": i.get("rssi"),
  50. "iccid": i.get("iccid"),
  51. "lng": i.get("lng"),
  52. "lat": i.get("lat"),
  53. "led": i.get("led"),
  54. "ledinfo": i.get("ledinfo"),
  55. "dver": i.get("dver"),
  56. "uptime": i.get("uptime")
  57. }
  58. )
  59. print(f"设备: {k} 数据量 {cursor.count()} {qx_data, is_created} ")
  60. return True
  61. if __name__ == "__main__":
  62. dsj_qxz_conf_info()