def get(self, request, *args, **kwargs): conditions = dict(request.query_params) #print parametesr["sensor_code"][0] conditions.update({"datetime": {}}) for key, value in conditions.iteritems(): if key == "start_time": # (conditions["datetime"]).update({"$gte": datetime.strptime(conditions[key][0], "%Y-%m-%d %H:%M:%S" )}) (conditions["datetime"]).update({"$gte": conditions[key][0]}) elif key == "end_time": # (conditions["datetime"]).update({"$lte": datetime.strptime(conditions[key][0], "%Y-%m-%d %H:%M:%S" )}) (conditions["datetime"]).update({"$lte": conditions[key][0]}) elif key == "datetime": pass else: conditions[key] = conditions[key][0] conditions.pop('start_time', None) conditions.pop('end_time', None) if conditions["datetime"] == {}: conditions.pop('datetime', None) if "number_of_data" in conditions.keys(): number_of_data = conditions["number_of_data"][0] print number_of_data conditions.pop('number_of_data', None) datavalues = MongoConnection.find_lastest_data( "DataValues", conditions, int(number_of_data)) else: datavalues = MongoConnection.find_data("DataValues", conditions=conditions) serializer = serializers.DataValueSerializer( instance=[DataValues(**element) for element in datavalues], many=True) return Response(serializer.data)
def put(self, request, *args, **kwargs): conditions = dict(request.query_params) for key, value in conditions.iteritems(): conditions[key] = conditions[key][0] parameters = request.data print conditions print parameters MongoConnection.update_data("Sites", parameters, conditions) return Response({"status": "ok"})
def get(self, request, *args, **kwargs): conditions = dict(request.query_params) for key, value in conditions.iteritems(): conditions[key] = conditions[key][0] if "number_of_data" in conditions.keys(): number_of_data = conditions["number_of_data"][0] conditions.pop('number_of_data', None) images = MongoConnection.find_lastest_data("Images", conditions, int(number_of_data)) else: images = MongoConnection.find_data("Images", conditions=conditions) serializer = serializers.ImageSerializer( instance=[Images(**element) for element in images], many=True) return Response(serializer.data)
def get(self, request, *args, **kwargs): conditions = dict(request.query_params) for key, value in conditions.iteritems(): conditions[key] = conditions[key][0] sites = MongoConnection.find_data("Sites", conditions=None) serializer = serializers.SiteSerializer( instance=[Sites(**element) for element in sites], many=True) return Response(serializer.data)
def overview(request): template = loader.get_template('sensor_data/sensor_monitoring.html') site_data = MongoConnection.find_data("Sites", conditions=None) site_code = [] for element in site_data: sensor_data = [] for sensor_pars in element["sensors"]: sensor_code = sensor_pars["sensor_code"] last_10_datavalue_cursor = MongoConnection.find_lastest_data( "DataValues", { "site_code": element["site_code"], "sensor_code": sensor_pars["sensor_code"], }, 10) last_10_datavalue = [] for data in last_10_datavalue_cursor: last_10_datavalue.append(data["value"]) if "LIGHT" in sensor_code: sensor_icon = "fa fa-sun-o fa-5x" pannel_color = "yellow" elif "WATER_TEMPERATURE" in sensor_code: sensor_icon = "fa fa-tint fa-5x" pannel_color = "primary" print sensor_pars sensor = { "sensor_code": sensor_pars["sensor_code"], "unit": sensor_pars["unit"], "sensor_icon": sensor_icon, "pannel_color": pannel_color } if last_10_datavalue != []: sensor.update({"value": round(last_10_datavalue[-1], 2)}) else: sensor.update({"value": "null"}) sensor_data.append(sensor) site_code.append({ "site_code": element["site_code"], "sensors": sensor_data, }) context = {} context.update({"site_code": site_code}) return HttpResponse(template.render(context, request))
def get(self, request, *args, **kwargs): conditions = dict(request.query_params) for key, value in conditions.iteritems(): conditions[key] = conditions[key][0] ip_cameras = MongoConnection.find_data("IpCameras", conditions=conditions) serializer = serializers.IpCameraSerializer( instance=[IpCameras(**element) for element in ip_cameras], many=True) return Response(serializer.data)
def detail(request): from bokeh.plotting import figure from bokeh.resources import CDN from bokeh.embed import components from datetime import datetime conditions = dict(request.GET) conditions.update({"datetime": {}}) for key, value in conditions.iteritems(): if key == "start_time": # (conditions["datetime"]).update({"$gte": datetime.strptime(conditions[key][0], "%Y-%m-%d %H:%M:%S" )}) (conditions["datetime"]).update({"$gte": conditions[key][0]}) elif key == "end_time": # (conditions["datetime"]).update({"$lte": datetime.strptime(conditions[key][0], "%Y-%m-%d %H:%M:%S" )}) (conditions["datetime"]).update({"$lte": conditions[key][0]}) elif key == "datetime": pass else: conditions[key] = conditions[key][0] conditions.pop('start_time', None) conditions.pop('end_time', None) if conditions["datetime"] == {}: conditions.pop('datetime', None) querry_string = '&'.join(["%s=%s" % (k, v) for k, v in conditions.items()]) url = "/api/datavalues?%s" % (querry_string) url = str(url) detail_data = MongoConnection.find_data("DataValues", conditions=conditions) ploting_data = [[], []] for data in detail_data: ploting_data[0].append( datetime.strptime(data["datetime"], '%Y-%m-%d %H:%M:%S.%f')) ploting_data[1].append(data["value"]) # plot = figure(x_axis_type="datetime", title="Example plot", plot_width=700, plot_height=400) plot.xaxis.axis_label = 'Datetime' plot.yaxis.axis_label = 'Celcius' plot.line(*ploting_data) script, div = components(plot, CDN) template = loader.get_template('sensor_data/sensor_detail.html') context = {"url": url} context.update({"site": conditions}) context.update({"the_script": script}) context.update({"the_div": div}) return HttpResponse(template.render(context, request))
def post(self, request, *args, **kwargs): conditions = dict(request.query_params) parameters = request.data created_time = str(time()) image_name = '%s_%s' % (created_time.replace('.', '_'), "test") print parameters["path"] try: image_path = parameters["path"] except: directory = os.path.join(settings.MEDIA_ROOT, "images", conditions["site_code"][0]) # create the folder for storing images if it doesn't exist. try: os.mkdir(directory) except: pass # write image to storage image = request.FILES.get("media") filename = '%s_%s' % (str(time()).replace('.', '_'), image.name) image_path = os.path.join(directory, image_name) output_image = open(image_path, 'wb+') image_content = ContentFile(image.read()) # iterate through the chunks. for chunk in image_content.chunks(): output_image.write(chunk) output_image.close() parameters = { "image_name": image_name, "site_code": conditions["site_code"][0], "datetime": unicode(created_time), "path": image_path, } MongoConnection.insert_data("Images", parameters) return Response({"status": "ok"})
def post(self, request, *args, **kwargs): parameters = request.data MongoConnection.insert_data("Sites", parameters) return Response({"status": "ok"})
def getData(): database = MongoConnection('traindata') collection = database.createCollection("traindetails") return collection.find()
def saveData(json): database = MongoConnection('traindata') collection = database.createCollection("traindetails") collection.insert(json)
def saveData(self, json): database = MongoConnection('traindata') collection = database.createCollection("traindetails") query = {'trainNo': json['trainNo']} collection.update(query, json, upsert=True)