def write_to_sensordb(s): s["datetime"] = dt_from_str_to_iso(s["datetime"]) time_interval = s["datetime"] + datetime.timedelta(seconds = 2) # 直近2秒以内に同じdevice_idのものがあれば統合 if(Sensor2.objects(datetime__gte=s["datetime"]).filter(datetime__lte=time_interval).filter(device_id=s["device_id"]).count()==1): save2mongo = Sensor2.objects(datetime__gte=s["datetime"]).filter(datetime__lte=time_interval).filter(device_id=s["device_id"]).get() else: save2mongo = Sensor2( box_id = s["box_id"], device_id = s["device_id"], pos_x = s["pos_x"], pos_y = s["pos_y"], # rssi = s["rssi"], date = s["date"], time = s["time"], datetime = s["datetime"] ) # RSSIがあれば登録 if (("rssi" in s) == True): save2mongo.rssi = float(s["rssi"]) # エラーフラグ追加 if(s["sensor_id"] == "Error"): save2mongo.error_flag = True else: save2mongo.error_flag = False # 各センサー計測値追加 if(s["sensor_id"] == "加速度"): save2mongo.ac = s["ac"] elif(s["sensor_id"] == "照度"): save2mongo.ilu = s["ilu"] elif(s["sensor_id"] == "赤外線"): save2mongo.tu = s["tu"] # 照度あり・なし分類 if(s["device_id"] in ilu_device_list): save2mongo.sensor_type = "照度あり" else: save2mongo.sensor_type = "照度なし" save2mongo.ilu = 0 # データ登録 save2mongo.save()
def update_db(request): # Sensor3.objects.all().delete() # error_db.objects.all().delete() limit = '1000' # 親機1台につき取得するデータの件数 datetime = '"201506010000"' # 取得時間帯 for num in "6789": # 親機ループ r = requests.get('http://api1.ginga-box.com:8080/ginga/sol?mode=getdata&v={box_id:"9CBD9D01000'+num+'", limit:'+limit+'}') t = r.json() # 最新データ取り出し latest_data = Sensor2.objects(box_id="9CBD9D01000"+num+"").order_by("-datetime").limit(1) if(latest_data.count() == 0): latest_datetime = str("2014-11-10 00:00:00.000") latest_datetime = dt_from_str_to_iso(latest_datetime) else: latest_datetime = latest_data[0]["datetime"] # データの加工 for i in range(len(t)): t[i] = convert_sensor_data(t[i]) dt = dt_from_str_to_iso(t[i]["datetime"]) latest_datetime = shift_time(dt) # データ登録 if(dt <= latest_datetime): break else: # 初期データ if(t[i]["sensor_id"] == "初期データ"): pass # エラーデータ elif(t[i]["sensor_id"] == "Error"): pass # 計測データ else: write_to_sensordb(t[i]) t = Sensor2.objects().all().order_by("-datetime").limit(30) return render_to_response('cms/update_db.html', # 使用するテンプレート {'t': t} )
def save_db(request): # DB初期化 Sensor2.objects.all().delete() # error_db.objects.all().delete() limit = '120000' # 親機1台につき取得するデータの件数 datetime = '"201411101700-201505280000000"' # データ取得時刻 for num in "67": # 親機ループ url = 'http://api1.ginga-box.com:8080/ginga/sol?mode=getdata&v={box_id:"9CBD9D01000'+num+'",limit:'+limit+',datetime:'+datetime+'}' r = requests.get(url) t = r.json() # データの加工 for i in range(len(t)): t[i] = convert_sensor_data(t[i]) # データ登録 if(t[i]["sensor_id"] == "初期データ"): pass else: write_to_sensordb(t[i]) t = Sensor2.objects().all().order_by("-date", "-time").limit(100) return render_to_response('cms/save_db.html', # 使用するテンプレート {'t': t} )
def d3jstest(request): t = [] for s in device_list: t += list(Sensor2.objects(device_id=s).order_by("-datetime").limit(1)) return render_to_response('cms/d3jstest.html', # 使用するテンプレート {'t':t} ) # テンプレートに渡すデータ
def data_list(request, limit=100, date_time=d): # 日付をstr12桁に合わせる --> 2014-11-20 19:40 date_time = datetime_to_12digits(date_time) # データベースから取り出し t = Sensor2.objects(datetime__gte="2015-06-29 12:40",device_id=66).order_by("-datetime").limit(5500) return render_to_response('cms/data_list.html', # 使用するテンプレート {'t': t, 'limit':limit, 'year':date_time[0:4],'month':date_time[5:7] ,'day':date_time[8:10],'hour':date_time[11:13],'minute':date_time[14:16]} )
def sensor_graph2(request, limit=100, date_time=999, type="ai01"): # センサーデータの取り出し if date_time == 999: lt = datetime.datetime.today() else: lt = dt_from_str_to_iso(datetime_to_12digits(date_time)) gt1 = lt - datetime.timedelta(days = 1) gt2 = lt - datetime.timedelta(days = 2) gt3 = lt - datetime.timedelta(days = 3) # データベースから取り出し t1 = Sensor2.objects(device_id=type[2:4],datetime__gt=gt1, datetime__lte=lt, error_flag=False).order_by("-datetime") t2 = Sensor2.objects(device_id=type[2:4],datetime__gt=gt2, datetime__lte=gt1, error_flag=False).order_by("-datetime") t3 = Sensor2.objects(device_id=type[2:4],datetime__gt=gt3, datetime__lte=gt2, error_flag=False).order_by("-datetime") return render_to_response('cms/sensor_graph2.html', # 使用するテンプレート {'t1': t1, 't2': t2, 't3': t3, 'limit':limit, 'year':lt.year,'month':lt.month ,'day':lt.day,'hour':lt.hour,'minute':lt.minute ,'sensor': type[0:2],'device_id':type[2:4]} )
def sensor_map_en(request, date_time=999, type="20"): # 最近の取得時間の取り出し recent = [] num = 20 # 最大取り出し件数 today = datetime.datetime.today() recent += Sensor2.objects(datetime__lt=today, error_flag=False).order_by("-datetime").limit(1).scalar("datetime") for i in range(0,num - 1): if len(recent) > i : lt = recent[i] - datetime.timedelta(hours = recent[i].hour) - datetime.timedelta(minutes = recent[i].minute + 5) recent += Sensor2.objects(datetime__lt=lt, error_flag=False).order_by("-datetime").limit(1).scalar("datetime") # センサーデータの取り出し if date_time == 999: lt = datetime.datetime.today() else: lt = dt_from_str_to_iso(datetime_to_12digits(date_time)) gt = lt - datetime.timedelta(hours = 1) # 一時間前までのデータを取得 t = [] t_ilu = [] exist_list = [] for s in device_list: t += Sensor2.objects(device_id=s, datetime__gt=gt, datetime__lt=lt, error_flag=False).order_by("-datetime").limit(1) exist_list += Sensor2.objects(device_id=s, datetime__gt=gt, datetime__lt=lt, error_flag=False).order_by("-datetime").limit(1).scalar("device_id") for s in ilu_device_list: t_ilu += Sensor2.objects(device_id=s, datetime__gt=gt, datetime__lt=lt, error_flag=False).order_by("-datetime").limit(1) # 位置情報の取り出し pos = [] for s in exist_list: pos += positionset.objects(device_id=s, datetime__lt=lt).order_by("-datetime").limit(1) return render_to_response('cms/sensor_map_en.html', # 使用するテンプレート {'t': t, 't_ilu': t_ilu,'recent': recent, 'year':date_time[0:4],'month':date_time[5:7] ,'day':date_time[8:10],'hour':date_time[11:13],'minute':date_time[14:16] ,'sensor':type[0:1],'visualize':type[1:2]} )
def sensor_map_en(request, date_time=d, type="20"): import datetime # 文字列を12桁に合わせる date_time = datetime_to_12digits(date_time) onehour_ago = datetime.datetime(int(date_time[0:4]),int(date_time[5:7]),int(date_time[8:10]),int(date_time[11:13]),int(date_time[14:16])) onehour_ago -= datetime.timedelta(hours = 1) onehour_ago = str(onehour_ago.year)+"-"+("0"+str(onehour_ago.month))[-2:]+"-"+("0"+str(onehour_ago.day))[-2:]+" "+("0"+str(onehour_ago.hour))[-2:]+":"+("0"+str(onehour_ago.minute))[-2:] # データベースから取り出し t = [] t_ilu = [] for s in device_list: t += Sensor2.objects(device_id=s, datetime__gt=onehour_ago, datetime__lt=date_time, error_flag=False).order_by("-datetime").limit(1) for s in ilu_device_list: t_ilu += Sensor2.objects(device_id=s, datetime__gt=onehour_ago, datetime__lt=date_time, error_flag=False).order_by("-datetime").limit(1) # 最近の取得時間の取り出し recent = [] num = 5 # 最大取り出し件数 today = datetime.datetime.today() recent += Sensor2.objects(datetime__lt=today, error_flag=False).order_by("-datetime").limit(1).scalar("datetime") for i in range(0,num - 1): if len(recent) > i : lt = recent[i] - datetime.timedelta(hours = recent[i].hour) - datetime.timedelta(minutes = recent[i].minute + 5) recent += Sensor2.objects(datetime__lt=lt, error_flag=False).order_by("-datetime").limit(1).scalar("datetime") return render_to_response('cms/sensor_map_en.html', # 使用するテンプレート {'t': t, 't_ilu': t_ilu,'recent': recent, 'year':date_time[0:4],'month':date_time[5:7] ,'day':date_time[8:10],'hour':date_time[11:13],'minute':date_time[14:16] ,'sensor':type[0:1],'visualize':type[1:2]} )
def sensor_graph_json(request, limit, date_time, type="101"): # 文字列を12桁に合わせる date_time = datetime_to_12digits(date_time) date_time = dt_from_str_to_iso(date_time) lt = date_time - datetime.timedelta(days = 3) gt = date_time - datetime.timedelta(days = int(limit)) num = 100 # 最大取り出し件数 t = [] # データベースから取り出し if type[0:1] == "0": t += Sensor2.objects(device_id=type[1:3],datetime__gt=gt, datetime__lt=lt, error_flag=False).order_by("-datetime").skip(int(limit)*2).limit(1).scalar("ac","datetime") for i in range(0,num - 1): if len(t) > i : lt = t[i][1] t += Sensor2.objects(device_id=type[1:3],datetime__gt=gt, datetime__lt=lt, error_flag=False).order_by("-datetime").skip(int(limit)*2).limit(1).scalar("ac","datetime") elif type[0:1] == "1": t += Sensor2.objects(device_id=type[1:3],datetime__gt=gt, datetime__lt=lt, error_flag=False).order_by("-datetime").skip(int(limit)*2).limit(1).scalar("ilu","datetime") for i in range(0,num - 1): if len(t) > i : lt = t[i][1] t += Sensor2.objects(device_id=type[1:3],datetime__gt=gt, datetime__lt=lt, error_flag=False).order_by("-datetime").skip(int(limit)*2).limit(1).scalar("ilu","datetime") else: t += Sensor2.objects(device_id=type[1:3],datetime__gt=gt, datetime__lt=lt, error_flag=False).order_by("-datetime").skip(int(limit)*2).limit(1).scalar("tu","datetime") for i in range(0,num - 1): if len(t) > i : lt = t[i][1] t += Sensor2.objects(device_id=type[1:3],datetime__gt=gt, datetime__lt=lt, error_flag=False).order_by("-datetime").skip(int(limit)*2).limit(1).scalar("tu","datetime") # Python辞書オブジェクトとしてdataに格納 data = [] if type[0:1] == "0": for i in range(0,len(t)): data.append({ 'ac':t[i][0], 'datetime':dt_from_iso_to_jap2(t[i][1]), }) elif type[0:1] == "1": for i in range(0,len(t)): data.append({ 'ilu':t[i][0], 'datetime':dt_from_iso_to_jap2(t[i][1]), }) else: for i in range(0,len(t)): data.append({ 'tu':t[i][0], 'datetime':dt_from_iso_to_jap2(t[i][1]), }) return render_json_response(request, data) # dataをJSONとして出力
def sensor_graph_en(request, limit=100, date_time=d, type="101"): # 文字列を12桁に合わせる date_time = datetime_to_12digits(date_time) # 直近2日分を取得 two_days_ago = dt_from_str_to_iso(date_time) two_days_ago -= datetime.timedelta(days = 2) two_days_ago = dt_from_iso_to_str(two_days_ago) two_days_ago = dt_insert_partition_to_min(two_days_ago) # データベースから取り出し t = Sensor2.objects(device_id=type[1:4],datetime__gt=two_days_ago, datetime__lte=date_time, error_flag=False).order_by("-datetime").limit(int(limit)) return render_to_response('cms/sensor_graph_en.html', # 使用するテンプレート {'t': t, 'limit':limit, 'year':date_time[0:4],'month':date_time[5:7] ,'day':date_time[8:10],'hour':date_time[11:13],'minute':date_time[14:16] ,'sensor': type[0:1],'device_id':type[1:3]} )
def sensor_map(request, date_time=999, type="20"): # 最近の取得時間の取り出し recent = [] num = 20 # 最大取り出し件数 today = datetime.datetime.today() # recent += Sensor2.objects(datetime__lt=today, error_flag=False).order_by("-datetime").limit(1).scalar("datetime") # for i in range(0,num - 1): # if len(recent) > i : # lt = recent[i] - datetime.timedelta(hours = recent[i].hour) - datetime.timedelta(minutes = recent[i].minute + 5) # recent += Sensor2.objects(datetime__lt=lt, error_flag=False).order_by("-datetime").limit(1).scalar("datetime") # センサーデータの取り出し if date_time == 999: lt = datetime.datetime.today() else: lt = dt_from_str_to_iso(datetime_to_12digits(date_time)) gt = lt - datetime.timedelta(minutes = 10) # 10分前までのデータを取得 exist_list = [] mongo_data = [] mongo_data += Sensor2.objects(datetime__gt=gt, datetime__lt=lt,error_flag=False).order_by("device_id","-datetime").limit(500) device_list = [False]*99 t = [] for i in range(0,len(mongo_data)): if device_list[mongo_data[i]["device_id"]] == False: device_list[mongo_data[i]["device_id"]] = True t.append(mongo_data[i]) # 位置情報の取り出し pos = [] for i in range(0,len(t)): pos += positionset.objects(device_id=t[i]["device_id"], datetime__lt=lt).order_by("-datetime").limit(1) return render_to_response('cms/sensor_map.html' , # 使用するテンプレート {'t': t, 'pos':pos, 'year':lt.year,'month':lt.month ,'day':lt.day,'hour':lt.hour,'minute':lt.minute ,'sensor':type[0:1],'visualize':type[1:2]} )
def data_check(request, date_time=d): # 文字列を12桁に合わせる date_time = datetime_to_12digits(date_time) # データベースから取り出し # t = Sensor2.objects(datetime__lt=date_time).order_by("-datetime").limit(int(limit)) error_data = Sensor2.objects(datetime__lt=date_time, error_flag=True).order_by("-datetime") total = Sensor2.objects(datetime__lt=date_time, error_flag=False).count() total_e = Sensor2.objects(datetime__lt=date_time, error_flag=True).count() total_ini = initial_db.objects(datetime__lt=date_time).count() data = [] t = [] for s in device_list: d0 = s d1 = Sensor2.objects(device_id=s, datetime__lt=date_time, error_flag=False).count() d2 = Sensor2.objects(device_id=s, datetime__lt=date_time, error_flag=True).count() d3 = initial_db.objects(device_id=s, datetime__lt=date_time).count() d4 = Sensor2.objects(device_id=s, datetime__lt=date_time, error_flag=False).order_by("-datetime").limit(1).scalar("datetime") for i in d4: d4 = i tmp_list = (d0, d1, d2, d3, d4), t += tmp_list for i in range(0,len(t)): data.append({ 'device_id':t[i][0], 'data':t[i][1], 'error':t[i][2], 'initial':t[i][3], 'datetime':t[i][4] }) return render_to_response('cms/data_check.html', # 使用するテンプレート {'data': data, 'total': total, 'total_e': total_e, 'total_ini': total_ini, 'error_data': error_data,'year':date_time[0:4],'month':date_time[5:7] ,'day':date_time[8:10],'hour':date_time[11:13],'minute':date_time[14:16]})
def response_json(request, date_time=d): import datetime if (date_time == d): _15min_ago = datetime.datetime.today() - datetime.timedelta(minutes = 15) _15min_ago = dt_from_iso_to_str(_15min_ago) # Sensor2.objects.all().delete() limit = '60' # 親機1台につき取得するデータの件数 datetime = _15min_ago + "-" + d date_time = dt_insert_partition_to_min(date_time) _15min_ago = dt_insert_partition_to_min(_15min_ago) _15min_ago = dt_from_str_to_iso(_15min_ago) # データベースから取り出し aggregated_data = db.sensor2.aggregate([{ "$match" :{"datetime":{"$gte":_15min_ago, "$lte":date_time}, "error_flag":False} }, { "$group" : {"device_id":"$device_id", "count" : {"$sum" : 1 } } }]) device_list = aggregated_data["result"] ilu_device_list = [4, 8, 9, 18, 20] print (device_list) t = [] up2date = Sensor2.objects(error_flag=False).order_by("-datetime").limit(1).scalar("datetime") # 最新時刻 for s in device_list: t += Sensor2.objects(device_id=s, datetime__gt=_15min_ago, error_flag=False).order_by("-datetime").limit(1).scalar("ac","ilu","tu","pos_x","pos_y","device_id","box_id","datetime") # Python辞書オブジェクトとしてdataに格納 data = [] for i in range(0,len(t)): data.append({ 'ac':t[i][0], 'ilu':t[i][1], 'tu':t[i][2], 'pos_x':t[i][3], 'pos_y':t[i][4], 'device_id':t[i][5], 'box_id':t[i][6], 'datetime':dt_from_iso_to_jap(t[i][7]), 'up2date':dt_from_iso_to_jap(up2date[0]), }) # r = requests.get('http://api1.ginga-box.com:8080/ginga/sol?mode=getdata&v={box_id:"9CBD9D010006", limit:50, datetime:"201411101700-201411101900"}') else: date_time = dt_insert_partition_to_min(date_time) onehour_ago = dt_from_str_to_iso(date_time) - datetime.timedelta(hours = 1) # onehour_ago = str(onehour_ago.year)+"-"+("0"+str(onehour_ago.month))[-2:]+"-"+("0"+str(onehour_ago.day))[-2:]+" "+("0"+str(onehour_ago.hour))[-2:]+":"+("0"+str(onehour_ago.minute))[-2:] # データベースから取り出し aggregated_data = db.sensor2.aggregate([{ "$match" :{"datetime":{"$gte":onehour_ago, "$lte":date_time}, "error_flag":False} }, { "$group" : {"device_id":"$device_id", "count" : {"$sum" : 1 } } }]) device_list = aggregated_data["result"] ilu_device_list = [4, 8, 9, 18, 20] print (device_list) t = [] for s in device_list: t += Sensor2.objects(device_id=s, datetime__gt=onehour_ago, datetime__lt=date_time, error_flag=False).order_by("-datetime").limit(1).scalar("ac","ilu","tu","pos_x","pos_y","device_id","box_id","datetime") # Python辞書オブジェクトとしてdataに格納 data = [] for i in range(0,len(t)): data.append({ 'ac':t[i][0], 'ilu':t[i][1], 'tu':t[i][2], 'pos_x':t[i][3], 'pos_y':t[i][4], 'device_id':t[i][5], 'box_id':t[i][6], 'datetime':dt_from_iso_to_jap(t[i][7]) }) return render_json_response(request, data) # dataをJSONとして出力
def sensor_map(request, date_time=d, type="20"): import datetime # 文字列を12桁に合わせる date_time = datetime_to_12digits(date_time) onehour_ago = datetime.datetime(int(date_time[0:4]),int(date_time[5:7]),int(date_time[8:10]),int(date_time[11:13]),int(date_time[14:16])) onehour_ago -= datetime.timedelta(hours = 1) onehour_ago = str(onehour_ago.year)+"-"+("0"+str(onehour_ago.month))[-2:]+"-"+("0"+str(onehour_ago.day))[-2:]+" "+("0"+str(onehour_ago.hour))[-2:]+":"+("0"+str(onehour_ago.minute))[-2:] # db.Sensor2.create_index([("device_id",ASCENDING),("datetime",DESCENDING)]) # データベースから取り出し t = [] t_ilu = [] # for s in device_list: # start = datetime.datetime.today() # if db.sensor2.find({"device_id":s, "datetime":{"$gte":onehour_ago, "$lte":date_time}, "error_flag":False}).count() != 0: # t += db.sensor2.find({"device_id":s, "datetime":{"$gte":onehour_ago, "$lte":date_time}, "error_flag":False}).sort("datetime")[1] # # t += Sensor2.objects(device_id=s, datetime__gt=onehour_ago, datetime__lt=date_time, error_flag=False).order_by("-datetime").limit(1) # end = datetime.datetime.today() # print (end - start) # for s in ilu_device_list: # start = datetime.datetime.today() # if db.sensor2.find({"device_id":s, "datetime":{"$gte":onehour_ago, "$lte":date_time}, "error_flag":False}).count() != 0: # t_ilu += db.sensor2.find({"device_id":s, "datetime":{"$gte":onehour_ago, "$lte":date_time}, "error_flag":False}).sort("-datetime")[1] # # t += Sensor2.objects(device_id=s, datetime__gt=onehour_ago, datetime__lt=date_time, error_flag=False).order_by("-datetime").limit(1) # end = datetime.datetime.today() # print (end - start) # 最近の取得時間の取り出し db.Sensor2.create_index([("datetime",DESCENDING)]) recent = [] num = 10 # 最大取り出し件数 today = datetime.datetime.today() lt = today - datetime.timedelta(hours = today.hour) - datetime.timedelta(minutes = today.minute + 5) gt = today - datetime.timedelta(days = 20) # print (lt, gt) # tmp_date = dt_from_iso_to_str(today)[:8] # dt_datas = list(db.sensor2.find({"datetime":{"$gte":gt, "$lte":lt}, "error_flag":False},{"datetime":1}).sort("datetime", DESCENDING)) # if len(dt_datas) > 0: # for i in range(0, len(dt_datas) - 1): # dt_data = dt_datas[i] # if str(dt_from_iso_to_str(dt_data["datetime"]))[:8] != tmp_date[:8]: # tmp = [dt_data["datetime"]] # recent += tmp # tmp_date = str(dt_from_iso_to_str(dt_data["datetime"]))[:8] # print (tmp_date) # else: # recent += [today] # print ("aaaaaaaaaaaaaaa", recent) # pass for i in range(0,num - 1): # start = datetime.datetime.today() if len(recent) > i : # lt = recent[i] - datetime.timedelta(hours = recent[i].hour) - datetime.timedelta(minutes = recent[i].minute + 5) # gt = recent[i] - datetime.timedelta(days = 30) # start = datetime.datetime.today() if db.sensor2.find({"datetime":{"$lte":lt, "$gte":gt}, "error_flag":False}).count() != 0: # end = datetime.datetime.today() # print ("if:", end - start) # start = datetime.datetime.today() tmp = db.sensor2.find({"datetime":{"$lte":lt, "$gte":gt}, "error_flag":False},{"datetime":1}).sort("-datetime")[1] # end = datetime.datetime.today() # print ("tmp:", end - start) tmp = [tmp["datetime"]] # print (type(tmp)) recent += tmp recent += Sensor2.objects(datetime__lt=lt, error_flag=False).order_by("-datetime").limit(1).scalar("datetime") # end = datetime.datetime.today() # print ("running:",end - start) return render_to_response('cms/sensor_map.html', # 使用するテンプレート {'t': t, 't_ilu': t_ilu,'recent': recent, 'year':date_time[0:4],'month':date_time[5:7] ,'day':date_time[8:10],'hour':date_time[11:13],'minute':date_time[14:16] ,'sensor':type[0:1],'visualize':type[1:2]} )
def response_json(request, date_time=999): import datetime if (date_time == 999): # リアルタイムビューではこちらを実行 _15min_ago = datetime.datetime.today() - datetime.timedelta(minutes = 15) _15min_ago = dt_from_iso_to_str(_15min_ago) # Sensor2.objects.all().delete() limit = '25' # 親機1台につき取得するデータの件数 datetime = _15min_ago + "-" + d for num in "6789": # 親機ループ r = requests.get('http://api1.ginga-box.com:8080/ginga/sol?mode=getdata&v={box_id:"9CBD9D01000'+num+'", limit:'+limit+'}') t = r.json() # 最新データ取り出し latest_data = Sensor2.objects(box_id="9CBD9D01000"+num+"").order_by("-datetime").limit(1) if(latest_data.count() == 0): latest_datetime = str("2014-11-10 00:00:00.000") latest_datetime = dt_from_str_to_iso(latest_datetime) else: latest_datetime = latest_data[0]["datetime"] # データの加工 for i in range(len(t)): t[i] = convert_sensor_data(t[i]) dt = dt_from_str_to_iso(t[i]["datetime"]) latest_datetime = shift_time(dt) # データ登録 if(dt <= latest_datetime): break else: # 初期データ if(t[i]["sensor_id"] == "初期データ"): pass # エラーデータ elif(t[i]["sensor_id"] == "Error"): pass # 計測データ else: write_to_sensordb(t[i]) date_time = dt_insert_partition_to_min(date_time) _15min_ago = dt_insert_partition_to_min(_15min_ago) _15min_ago = dt_from_str_to_iso(_15min_ago) # データベースから取り出し t = [] up2date = Sensor2.objects(error_flag=False).order_by("-datetime").limit(1).scalar("datetime") # 最新時刻 for s in device_list: t += Sensor2.objects(device_id=s, datetime__gt=_15min_ago, error_flag=False).order_by("-datetime").limit(1).scalar("ac","ilu","tu","pos_x","pos_y","device_id","box_id","datetime") # Python辞書オブジェクトとしてdataに格納 data = [] for i in range(0,len(t)): data.append({ 'ac':t[i][0], 'ilu':t[i][1], 'tu':t[i][2], 'pos_x':t[i][3], 'pos_y':t[i][4], 'device_id':t[i][5], 'box_id':t[i][6], 'datetime':dt_from_iso_to_jap(t[i][7]), 'up2date':dt_from_iso_to_jap(up2date[0]), }) # r = requests.get('http://api1.ginga-box.com:8080/ginga/sol?mode=getdata&v={box_id:"9CBD9D010006", limit:50, datetime:"201411101700-201411101900"}') else: date_time = dt_insert_partition_to_min(date_time) onehour_ago = dt_from_str_to_iso(date_time) - datetime.timedelta(minutes = 10) # onehour_ago = str(onehour_ago.year)+"-"+("0"+str(onehour_ago.month))[-2:]+"-"+("0"+str(onehour_ago.day))[-2:]+" "+("0"+str(onehour_ago.hour))[-2:]+":"+("0"+str(onehour_ago.minute))[-2:] # データベースから取り出し mongo_data = [] mongo_data += Sensor2.objects(datetime__gt=onehour_ago, datetime__lt=date_time,error_flag=False).order_by("device_id","-datetime").limit(500) device_list = [False]*99 t = [] for i in range(0,len(mongo_data)): if device_list[mongo_data[i]["device_id"]] == False: device_list[mongo_data[i]["device_id"]] = True t.append(mongo_data[i]) # Python辞書オブジェクトとしてdataに格納 data = [] for i in range(0,len(t)): tmp_pos = positionset.objects(device_id=t[i]["device_id"],datetime__lt=date_time).order_by("-datetime").limit(1).scalar("pos_x","pos_y") data.append({ 'ac':t[i]["ac"], 'ilu':t[i]["ilu"], 'tu':t[i]["tu"], 'pos_x':tmp_pos[0][0], 'pos_y':tmp_pos[0][1], 'device_id':t[i]["device_id"], 'box_id':t[i]["box_id"], 'datetime':dt_from_iso_to_jap(t[i]["datetime"]) }) return render_json_response(request, data) # dataをJSONとして出力