rabin hace 6 años
padre
commit
470d022068

+ 35 - 2
front/page/__load__.py

@@ -290,7 +290,7 @@ class Device(object):
 				self.data['pic'][key]['cdates'] = Demeter.date(value['sdate'])
 
 	@staticmethod
-	def statBase(self, deviceInfo={}):
+	def statBase(self, deviceInfo={}, state=False):
 		device = self.input('device')
 		method = self.input('method', 'maxmin')
 		if not device:
@@ -324,7 +324,9 @@ class Device(object):
 		search['type'] = type
 		search['id'] = id
 		self.data['search'] = search
-		self.data['statList'] = model.getData(method, search, self.data['deviceInfo'])
+		out = self.input('out')
+		if not out:
+			self.data['statList'] = model.getData(method, search, self.data['deviceInfo'])
 	@staticmethod
 	def stat(self):
 		id, type = Device.init(self, search={'hardware_type--ins':[2,4]})
@@ -333,6 +335,36 @@ class Device(object):
 		Device.statBase(self, self.data['device'][0])
 		self.data['url'] = Device.url('type='+type+'&id=' + id)
 
+	@staticmethod
+	def out(self):
+		id, type = Device.init(self, search={'hardware_type--ins':[2,4]})
+		if not id:
+			return
+		Device.statBase(self, self.data['device'][0])
+		self.data['url'] = Device.url('type='+type+'&id=' + id)
+
+		if 'statList' in self.data:
+			import csv
+			from datetime import *
+			import uuid
+			self.set_header("Content-Type","text/csv,charset=UTF-8")
+			datas = [['name', 'age'],
+			['Bob', 14],
+			['Tom', 23],
+			['Jerry', '18']]
+
+			day = str(date.today())
+			day = day.split('-')
+			file_name =  self.data['deviceInfo']['name']
+			file_path = day[0] + '/' + day[1] + '/' + day[2]
+			file_path = File.mkdirs(os.path.join(Demeter.path, 'runtime','upload', file_path)) + '/' + file_name + '.csv'
+			with open(file_path, 'w', newline='') as f:
+				writer = csv.writer(f)
+				for row in datas:
+					writer.writerow(row)
+			self.finish()
+			return
+
 	@staticmethod
 	def log(self):
 		id, type = Device.init(self)
@@ -386,6 +418,7 @@ class Device(object):
 		config['stat'] = '/device/stat?' + param;
 		config['log'] = '/device/log?' + param;
 		config['pic'] = '/device/pic?' + param;
+		config['out'] = '/device/out?' + param + '&out=1';
 		return config
 
 	@staticmethod

+ 8 - 0
front/page/device.py

@@ -236,6 +236,14 @@ class stat_path(Load):
 		Device.stat(self)
 		self.view("device/stat.html")
 
+#数据导出
+class out_path(Load):
+	@Web.auth
+	@Web.setting
+	def get(self):
+		Device.out(self)
+		self.view("device/out.html")
+
 #设备日志
 class log_path(Load):
 	@Web.auth

+ 2 - 0
front/templates/pc/device/inc/menu.html

@@ -11,6 +11,8 @@
         <li {% if data['device_cur'] == 'stat' %}class="cur"{% end %}><a href="{{data['url']['stat']}}">数据统计</a></li>
         <li><span>/</span></li>
         <li {% if data['device_cur'] == 'log' %}class="cur"{% end %}><a href="{{data['url']['log']}}">设备记录</a></li>
+        <li><span>/</span></li>
+        <li {% if data['device_cur'] == 'out' %}class="cur"{% end %}><a href="{{data['url']['out']}}">数据导出</a></li>
     </ul>
 </div>
 {% elif 'pic' in data and data['pic'] %}

+ 77 - 0
front/templates/pc/device/out.html

@@ -0,0 +1,77 @@
+{% extends "../theme.html" %}
+{% block content %}
+{%include 'inc/menu.html'%}
+<div class="pannel">
+    <form action="/device/out" method="get">
+    <input type="hidden" name="type" id="type" value="{{data['input']['type']}}" />
+    <input type="hidden" name="id" id="id" value="{{data['input']['id']}}" />
+    <input type="hidden" name="method" id="method" value="mx" />
+        <select name="device" id="device">
+            {% for v in data['device'] %}
+            <option value="{{v['id']}}" {% if 'device' in data['search'] and data['search']['device'] == v['id'] %}selected{% end %}>{{v['name']}}</option>
+            {% end %}
+        </select>   
+        <select name="group" id="group" {% if 'start' in data['search'] or 'end' in data['search'] %}style="display:none;"{% end %}>
+            {% for v in data['group'] %}
+            <option value="{{v['id']}}" {% if 'group' in data['search'] and data['search']['group'] == v['id'] %}selected{% end %}>{{v['name']}}</option>
+            {% end %}
+        </select>     
+        <input type="text" name="start" id="start" readonly class="form_datetime" value="{% if 'start' in data['search'] %}{{data['search']['start']}}{% end %}" style="width:150px;">
+        ~     
+        <input type="text" name="end" id="end" readonly class="form_datetime" value="{% if 'end' in data['search'] %}{{data['search']['end']}}{% end %}" style="width:150px;">
+        <input type="submit" value="导出报表">  
+    </form>
+    <div class="box" id="main" style="width: 100%;height:400px;">
+    </div>
+</div>
+{% end %}
+
+{% block script %}
+{%include 'out/page.html'%}
+<script type="text/javascript" src="{{static_url('pc/lib/laydate/laydate.js')}}"></script>
+<script type="text/javascript">
+dateTheme = '#247ed0'
+laydate.render({
+  elem: '#start'
+  ,theme: dateTheme
+  ,max: 0
+  ,type: 'datetime'
+  /*
+  ,mark: {
+    '0-9-14': '浇水'
+    ,'0-9-26': '施肥' //每年的日期
+  }
+  */
+  ,done: function(value, date){
+    /*
+    if(date.year === 2017 && date.month === 9 && date.date === 14){
+        alert('我都干了什么啊');
+    }
+    */
+    var start = value
+    var end = $("#end").val()
+    if (start || end) {
+        $("#group").hide();
+    } else {
+        $("#group").show();
+    }
+  }
+});
+laydate.render({
+  elem: '#end'
+  ,theme: dateTheme
+  //,min: $('#start').val()
+  ,max: 0
+  ,type: 'datetime'
+  ,done: function(value, date){
+    var start = $("#start").val()
+    var end = value
+    if (start || end) {
+        $("#group").hide();
+    } else {
+        $("#group").show();
+    }
+  }
+});
+</script>
+{% end %}