scd_device_data.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import os
  2. import sys
  3. import datetime
  4. import django
  5. local_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  6. if local_path not in sys.path:
  7. sys.path.append(local_path)
  8. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "kedong.settings")
  9. django.setup()
  10. from django.conf import settings
  11. from smartfarming.models.device import MongoDevice, MongoSCDData
  12. import json
  13. import requests
  14. import pymongo
  15. from urllib import parse
  16. from tqdm import tqdm
  17. my_client = pymongo.MongoClient(host="8.136.98.49", port=27017, username="root", password="yfkj@6020")
  18. my_col = my_client['smartfarming']['sa_device']
  19. scd_device = {
  20. "865328061478136",
  21. "865328061471529",
  22. "865328061466933",
  23. "865328061428701",
  24. "865328061466404",
  25. "865328061449913",
  26. "865328061455027",
  27. "865328061271879",
  28. "865328061253190",
  29. "865328061460159",
  30. "865328061438890",
  31. "865328061429451",
  32. "869298052295540",
  33. "865328061465679",
  34. "865328061450044"
  35. }
  36. def dsj_qxz_conf_info():
  37. user = parse.quote_plus("root")
  38. passwd = parse.quote_plus("yfkj@6020")
  39. # 账号密码方式连接MongoDB | "mongodb://用户名:密码@公网ip:端口/"
  40. myclient = pymongo.MongoClient("mongodb://{0}:{1}@8.136.98.49:57017/".format(user,passwd))
  41. # myclient = pymongo.MongoClient("mongodb://127.0.0.1:12514/")
  42. # 指定数据库
  43. db = myclient.smartfarming
  44. # 指定集合
  45. collection = db.sa_device
  46. collection1 = db.sa_device_scd_data
  47. for d_id in scd_device:
  48. device = collection.find_one({"device_id": d_id}, {"id": 1, "_id": 0})
  49. start = "2023-10-22 13:23:05"
  50. end = "2023-12-06 14:43:00"
  51. start = int(datetime.datetime.strptime(start, "%Y-%m-%d %H:%M:%S").timestamp())
  52. end = int(datetime.datetime.strptime(end, "%Y-%m-%d %H:%M:%S").timestamp())
  53. data = {'device_id': device.get("id"), 'addtime':{'$gt':start, "$lt": end}}
  54. cursor = collection1.find(data,{'_id':0,'id':0,'device_id':0})
  55. device_id = MongoDevice.objects.get(device_id=d_id).id
  56. print("数据量", cursor.count())
  57. for i in tqdm(list(cursor)):
  58. MongoSCDData.objects.create(
  59. device_id=device_id,
  60. device_data=i["device_data"],
  61. addtime=i['addtime'],
  62. device_status=i['device_status']
  63. )
  64. return True
  65. if __name__ == "__main__":
  66. dsj_qxz_conf_info()