qx_switch_conf.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 (
  14. MongoQXZ_Switch,
  15. MongoQXZ_Auto_Switch,
  16. MongoQXZ_Timing_Switch,
  17. QXZ_Switch_Status
  18. )
  19. import json
  20. import requests
  21. import pymongo
  22. from urllib import parse
  23. my_client = pymongo.MongoClient(host="8.136.98.49", port=27017, username="root", password="yfkj@6020")
  24. my_col = my_client['smartfarming']['sa_device']
  25. qx_device = {
  26. "861551055315402",
  27. "861551056086671",
  28. "861551056088693",
  29. "861551056088479",
  30. "861551056088719",
  31. "861551055313365",
  32. "861551056086614",
  33. "861551056093800",
  34. "861551056095367",
  35. "861551056102502",
  36. "861551056096621",
  37. "861551056101785",
  38. "861551056086549",
  39. "861551055354120",
  40. "861551055353536",
  41. "861551056092018",
  42. "861551055319586",
  43. "861551056088875",
  44. "861551055324651",
  45. "861551055313423"
  46. }
  47. def dsj_qxz_conf_info():
  48. user = parse.quote_plus("root")
  49. passwd = parse.quote_plus("yfkj@6020")
  50. # 账号密码方式连接MongoDB | "mongodb://用户名:密码@公网ip:端口/"
  51. myclient = pymongo.MongoClient("mongodb://{0}:{1}@8.136.98.49:57017/".format(user,passwd))
  52. # myclient = pymongo.MongoClient("mongodb://127.0.0.1:12514/")
  53. # 指定数据库
  54. db = myclient.smartfarming
  55. # 指定集合
  56. collection = db.sa_device
  57. collection1 = db.sa_qxz_base_info
  58. for k in qx_device:
  59. data = {'device_id': k}
  60. cursor = collection1.find(data,{'_id':0,'id':0})
  61. for i in cursor:
  62. qx_data, is_created = MongoQXZ_Base_Info.objects.get_or_create(
  63. device_id = i.get("device_id"),
  64. defaults={
  65. "volt": i.get("volt"),
  66. "rssi": i.get("rssi"),
  67. "iccid": i.get("iccid"),
  68. "lng": i.get("lng"),
  69. "lat": i.get("lat"),
  70. "led": i.get("led"),
  71. "ledinfo": i.get("ledinfo"),
  72. "dver": i.get("dver"),
  73. "uptime": i.get("uptime")
  74. }
  75. )
  76. print(f"设备: {k} 数据量 {cursor.count()} {qx_data.id, is_created} ")
  77. return True
  78. if __name__ == "__main__":
  79. # dsj_qxz_conf_info()
  80. pass