rabin 7 years ago
parent
commit
189e66165d

+ 9 - 3
admin/templates/theme/list.html

@@ -3,6 +3,12 @@
 <html>
     <head>
         {%include '../inc/header.html'%}
+        <style>
+        .layui-form-switch
+        {
+            margin-top: 0px;
+        }
+        </style>
     </head>
     <body>
         <div class="x-nav">
@@ -40,7 +46,7 @@
                     </div>
                   </div>
                 </div> 
-            </form>
+            
             <xblock>{% block button %}<button class="layui-btn" onclick="show('添加问题','question-add.html','600','500')"><i class="layui-icon">&#xe608;</i>添加</button><button class="layui-btn layui-btn-danger" onclick="showDel('')"><i class="layui-icon"></i>回收站</button>{% end %}<span class="x-right" style="line-height:40px">{% block total %}共有数据:{{data['page']['totalNum']}} 条 [每页数据:{{data['page']['num']}} 条{% end %}]</span></xblock>
             <table class="layui-table">
                 <thead>
@@ -115,7 +121,7 @@
                     {% if data['page']['ajax'] == False %}
                 </tbody>
             </table>
-
+            </form>
             <div id="page"></div>
         </div>
         {%include '../inc/footer.html'%}
@@ -217,7 +223,7 @@ laypage({
     url = url + link;
     url = url.replace('&&', '&')
     
-    if (curr != {{data['page']['current']}}) {
+    if (curr) {
       url = url + 'page=' + curr
       var index = layer.msg('loading...', {icon: 4});
       request('get', url, {}, function(msg)

+ 139 - 62
install.py

@@ -1,83 +1,160 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 from demeter.core import *
-model = Demeter.model('manage_admin')
-model.id = 1
-info = model.select(type='fetchone')
-if not info:
-	model.role_id = 1
-	model.username = 'admin'
-	model.mobile = '15810090845'
-	model.password = '123456'
-	model.insert()
 
-model = Demeter.model('manage_role')
-model.id = 1
-info = model.select(type='fetchone')
-if not info:
-	model.name = '管理员'
-	model.insert()
+def manage():
+	model = Demeter.model('manage_admin')
+	model.id = 1
+	info = model.select(type='fetchone')
+	if not info:
+		model.role_id = 1
+		model.username = 'admin'
+		model.mobile = '15810090845'
+		model.password = '123456'
+		model.insert()
 
-model = Demeter.model('content')
-model.id = 1
-info = model.select(type='fetchone')
-if not info:
-	model.name = '农小盒'
-	model.subname = '农作物生长监测与产品溯源'
-	model.insert()
+	model = Demeter.model('manage_role')
+	model.id = 1
+	info = model.select(type='fetchone')
+	if not info:
+		model.name = '管理员'
+		model.insert()
 
+def web():
+	model = Demeter.model('web_set')
+	model.id = 1
+	info = model.select(type='fetchone')
+	if not info:
+		model.name = '农小盒'
+		model.subname = '农作物生长监测与产品溯源'
+		model.keywords = '农小盒,智慧云平台,智能硬件,大数据,农业,农业互联网'
+		model.description = '农小盒'
+		model.icp = '京ICP备12045252号-2'
+		model.tel = '18701029660'
+		model.passtel = '18701029660'
+		model.mobile = ''
+		model.buy = ''
+		model.insert()
 
-model = Demeter.model('farm')
-model.id = 1
-info = model.select(type='fetchone')
-if not info:
-	model.name = '默认园区'
-	model.insert()
+	model = Demeter.model('web_article_category')
+	data = ('站点介绍', '焦点图', '系统优势')
+	for value in data:
+		model.name = value
+		info = model.select(type='fetchone')
+		if not info:
+			model.name = value
+			model.insert()
 
-# 初始化网关,测试用
-model = Demeter.model('device_gateway')
-model.hardware_id = 10086
-info = model.select(type='fetchone')
-if not info:
-	model.farm_id = 1
-	model.hardware_id = 10086
-	model.name = '默认网关'
-	model.insert()
+	model = Demeter.model('web_article')
+	data = ('合作伙伴', '免责声明', '联系我们', '帮助文档', '团队介绍')
+	for value in data:
+		model.name = value
+		info = model.select(type='fetchone')
+		if not info:
+			model.category_id = 1
+			model.name = value
+			if model.name == '帮助文档':
+				model.link = '/help'
+			model.insert()
 
-# 初始化设备类型
-device_type = ({'name':'温度', 'key':'temprature', 'unit':'℃'},{'name':'湿度', 'key':'humidity','unit':'%RH'})
-model = Demeter.model('device_type')
-for value in device_type:
-	model.key = value['key']
+	model = Demeter.model('web_menu')
+	data = (
+		{'name':'新农宝盒首页','link':'/main'}
+		,{'name':'灌溉商城','link':'http://www.guangaishangcheng.com/'}
+		,{'name':'帮助中心','link':'/help'}
+		)
+	for value in data:
+		model.link = value['link']
+		info = model.select(type='fetchone')
+		if not info:
+			model.name = value['name']
+			model.link = value['link']
+			model.insert()
+
+def farm():
+	farm_id = 1
+	model = Demeter.model('farm')
+	model.id = farm_id
 	info = model.select(type='fetchone')
 	if not info:
-		model.key = value['key']
-		model.name = value['name']
-		model.exp = '{n}'
-		model.unit = value['unit']
+		model.name = '默认园区'
 		model.insert()
 
-# 初始化硬件类型
-hardware_type = ({'name':'网关','key':'gateway'},{'name':'传感器', 'key':'sensor'},{'name':'控制器', 'key':'control'},{'name':'电源', 'key':'power'},{'name':'摄像机', 'key':'pic'},{'name':'状态器', 'key':'status'})
-model = Demeter.model('hardware_type')
-for value in hardware_type:
-	model.key = value['key']
+	# 初始化网关,测试用
+	hardware_id = 10086
+	model = Demeter.model('device_gateway')
+	model.hardware_id = hardware_id
 	info = model.select(type='fetchone')
 	if not info:
+		model.farm_id = farm_id
+		model.hardware_id = hardware_id
+		model.name = '默认网关'
+		id = model.insert()
+		Demeter.service('device').upGatewayStatus(farm_id, id, hardware_id)
+
+def device():
+	# 以下类型只能新增,不能随意更改或删除
+	# 初始化设备类型
+	device_type = (
+		{'name':'温度', 'key':'temprature', 'unit':'℃'}
+		,{'name':'湿度', 'key':'humidity','unit':'%RH'}
+		,{'name':'光照', 'key':'light','unit':'LUX'}
+		,{'name':'二氧化碳', 'key':'co2','unit':'PPM'}
+		,{'name':'风速', 'key':'speed','unit':'km/h'}
+		,{'name':'风向', 'key':'direction','unit':''}
+		,{'name':'PM2.5', 'key':'pm2.5','unit':'μg/m3'}
+		,{'name':'电源', 'key':'power','unit':'V'}
+		,{'name':'阀门', 'key':'valve','unit':''}
+		,{'name':'放风机', 'key':'blower','unit':''}
+		)
+	model = Demeter.model('device_type')
+	for value in device_type:
 		model.key = value['key']
-		model.name = value['name']
-		model.insert()
+		info = model.select(type='fetchone')
+		if not info:
+			model.key = value['key']
+			model.name = value['name']
+			#model.exp = '{n}'
+			model.unit = value['unit']
+			model.insert()
 
-# 初始化消息类型
-msg_type = ({'name':'系统消息', 'key':'system'},{'name':'警告提醒', 'key':'notice'},{'name':'操作提示', 'key':'oper'},{'name':'新闻', 'key':'news'})
-model = Demeter.model('msg_type')
-for value in msg_type:
-	model.key = value['key']
-	info = model.select(type='fetchone')
-	if not info:
+	# 初始化硬件类型 1/1/2/3/4/5/6
+	hardware_type = (
+		{'name':'网关', 'key':'gateway'}
+		,{'name':'传感器', 'key':'sensor'}
+		,{'name':'控制器', 'key':'control'}
+		,{'name':'电源', 'key':'power'}
+		,{'name':'状态', 'key':'status'}
+		,{'name':'摄像头', 'key':'pic'}
+		,{'name':'摄像机', 'key':'camera'}
+		)
+	model = Demeter.model('hardware_type')
+	for value in hardware_type:
 		model.key = value['key']
-		model.name = value['name']
-		model.insert()
+		info = model.select(type='fetchone')
+		if not info:
+			model.key = value['key']
+			model.name = value['name']
+			model.insert()
 
+	# 初始化消息类型
+	msg_type = (
+		{'name':'系统消息', 'key':'system'}
+		,{'name':'警告提醒', 'key':'notice'}
+		,{'name':'操作提示', 'key':'oper'}
+		,{'name':'新闻', 'key':'news'}
+		)
+	model = Demeter.model('msg_type')
+	for value in msg_type:
+		model.key = value['key']
+		info = model.select(type='fetchone')
+		if not info:
+			model.key = value['key']
+			model.name = value['name']
+			model.insert()
 
+manage()
+web()
+farm()
+device()
 print 'install success!'

+ 0 - 21
model/content.py

@@ -1,21 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-"""
-    demeter database
-    name:content.py
-    author:rabin
-"""
-from __load__ import *
-
-class Content(Model):
-	__table__ = 'content'
-	__comment__ = '首页资料表'
-	id = Fields(type='int', primaryKey=True, autoIncrement=True, comment='内容ID')
-	name = Fields(type='varchar(255)', comment='标题')
-	subname = Fields(type='varchar(255)', comment='首页小标题')
-	product = Fields(type='text', comment='内容描述')
-	weixin = Fields(type='text', comment='内容描述')
-	demo = Fields(type='text', comment='内容描述')
-	pic = Fields(type='varchar(255)', comment='图片')
-	state = Fields(type='boolean', default='True', comment='数据存在状态')
-	cdate = Fields(type='int', default='time', comment='创建时间')

+ 3 - 3
model/data.py

@@ -31,8 +31,8 @@ class Data(Model):
 			},
 			#"time": data['time'],
 			"fields": {
-				'source' : int(data['source']),
-				'value' : int(data['value'])
+				'source' : float(data['source']),
+				'value' : float(data['value'])
 			}
 		}]
 		return json
@@ -54,7 +54,7 @@ class Data(Model):
 		if 'series' in data.raw and data.raw['series'][0]['values']:
 			for values in data.raw['series'][0]['values']:
 				if values[1]:
-					v = int(round(values[1]))
+					v = float(round(values[1], 2))
 					temp = values[0].split('.')
 					uformat = utc_format
 					if len(temp) <= 1:

+ 4 - 4
model/device_gateway.py

@@ -13,9 +13,9 @@ class Device_gateway(Model):
 	id = Fields(type='uuid', primaryKey=True, comment='网关ID', uuid='farm_id')
 	farm_id = Fields(type='int', default='setting.farm', comment='园区ID', match='not')
 	name = Fields(type='varchar(50)', comment='网关名')
-	hardware_id = Fields(type='bigint', comment='硬件id')
-	power_num = Fields(type='smallint', comment='电源数值')
-	power_status = Fields(type='boolean', default='False', comment='电源连接状态')
-	status = Fields(type='boolean', default='False', comment='网关连接状态')
+	hardware_id = Fields(type='bigint', comment='硬件id', match='number|硬件id必须是数字')
+	pic = Fields(type='varchar(255)', comment='设备分布图纸')
+	camera = Fields(type='varchar(500)', comment='摄像头实时流地址')
+	stat = Fields(type='boolean', default='False', comment='是否显示趋势图')
 	state = Fields(type='boolean', default='True', comment='数据存在状态')
 	cdate = Fields(type='int', default='time', comment='创建时间')

+ 0 - 19
model/device_group.py

@@ -1,19 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-"""
-    demeter database
-    name:device_group.py
-    author:rabin
-"""
-from __load__ import *
-
-class Device_group(Model):
-	__table__ = 'device_group'
-	__comment__ = '设备分组管理'
-	id = Fields(type='uuid', primaryKey=True, comment='分组ID', uuid='farm_id')
-	farm_id = Fields(type='int', default='setting.farm', comment='园区ID', match='not')
-	name = Fields(type='varchar(50)', comment='分组名')
-	page_id = Fields(type='uuid', comment='页面ID')
-	devices = Fields(type='text', comment='设备id,逗号隔开')
-	state = Fields(type='boolean', default='True', comment='数据存在状态')
-	cdate = Fields(type='int', default='time', comment='创建时间')

+ 29 - 25
model/device_info.py

@@ -1,25 +1,29 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-"""
-    demeter database
-    name:device_info.py
-    author:rabin
-"""
-from __load__ import *
-
-class Device_info(Model):
-	__table__ = 'device_info'
-	__comment__ = '设备管理'
-	id = Fields(type='uuid', primaryKey=True, comment='设备ID', uuid='farm_id')
-	farm_id = Fields(type='int', default='setting.farm', comment='园区ID', match='not')
-	name = Fields(type='varchar(50)', comment='设备名')
-	hardware_id = Fields(type='bigint', comment='硬件id')
-	hardware_type = Fields(type='int', comment='硬件类型')
-	hardware_station = Fields(type='smallint', comment='硬件所属站:1气象站、2温室、3土壤商情、4阀门灌溉')
-	gateway_id = Fields(type='uuid', comment='网关id')
-	type_id = Fields(type='int', comment='设备类型')
-	num = Fields(type='int', default='0', comment='设备数据')
-	show = Fields(type='varchar(500)', comment='设备数据计算之后的数值')
-	status = Fields(type='boolean', default='False', comment='设备连接状态')
-	state = Fields(type='boolean', default='True', comment='数据存在状态')
-	cdate = Fields(type='int', default='time', comment='创建时间')
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+    demeter database
+    name:device_info.py
+    author:rabin
+"""
+from __load__ import *
+
+class Device_info(Model):
+	__table__ = 'device_info'
+	__comment__ = '设备管理'
+	id = Fields(type='uuid', primaryKey=True, comment='设备ID', uuid='farm_id')
+	farm_id = Fields(type='int', default='setting.farm', comment='园区ID', match='not')
+	name = Fields(type='varchar(50)', comment='设备名')
+	hardware_id = Fields(type='bigint', comment='硬件id', match='number|硬件id必须是数字')
+	hardware_type = Fields(type='int', comment='硬件类型')
+	gateway_id = Fields(type='uuid', comment='网关id')
+	type_id = Fields(type='int', comment='设备类型')
+	value = Fields(type='varchar(1000)', comment='设备传过来的值')
+	max = Fields(type='float', comment='最大上限', match='number|最大上限必须是数字')
+	min = Fields(type='float', comment='最小下限', match='number|最小下限必须是数字')
+	cxnum = Fields(type='int', comment='超限次数')
+	cxtype = Fields(type='int', comment='超限类型,1为超过上限,2为超过下限')
+	cxdate = Fields(type='int', comment='首次超限时间')
+	status = Fields(type='boolean', default='False', comment='设备连接状态')
+	state = Fields(type='boolean', default='True', comment='数据存在状态')
+	cdate = Fields(type='int', default='time', comment='创建时间')
+	udate = Fields(type='int', default='time', comment='接入时间')

+ 0 - 18
model/device_mul.py

@@ -1,18 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-"""
-    demeter database
-    name:device_mul.py
-    author:rabin
-"""
-from __load__ import *
-
-class Device_mul(Model):
-	__table__ = 'device_mul'
-	__comment__ = '设备批量控制'
-	id = Fields(type='uuid', primaryKey=True, comment='分组ID', uuid='farm_id')
-	farm_id = Fields(type='int', default='setting.farm', comment='园区ID', match='not')
-	name = Fields(type='varchar(50)', comment='分组名')
-	devices = Fields(type='text', comment='设备id,逗号隔开')
-	state = Fields(type='boolean', default='True', comment='数据存在状态')
-	cdate = Fields(type='int', default='time', comment='创建时间')

+ 0 - 17
model/device_page.py

@@ -1,17 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-"""
-    demeter database
-    name:device_page.py
-    author:rabin
-"""
-from __load__ import *
-class Device_page(Model):
-	__table__ = 'device_page'
-	__comment__ = '设备页面管理'
-	id = Fields(type='uuid', primaryKey=True, comment='页面ID', uuid='farm_id')
-	farm_id = Fields(type='int', default='setting.farm', comment='园区ID', match='not')
-	#name = Fields(type='varchar(50)', comment='页面名', match='not', manage='list,update', index='using gin(name gin_trgm_ops)', indexs='yes')
-	name = Fields(type='varchar(50)', comment='页面名', match='not', manage='list,update')
-	state = Fields(type='boolean', default='True', comment='数据存在状态')
-	cdate = Fields(type='int', default='time', comment='创建时间', manage='list')

+ 20 - 18
model/device_pic.py

@@ -1,19 +1,21 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-"""
-    demeter database
-    name:device_pic.py
-    author:rabin
-"""
-from __load__ import *
-
-class Device_pic(Model):
-	__table__ = 'device_pic'
-	__comment__ = '设备图片记录表'
-	id = Fields(type='uuid', primaryKey=True, comment='图片ID', uuid='farm_id')
-	farm_id = Fields(type='int', default='setting.farm', comment='园区ID', match='not')
-	gateway_id = Fields(type='uuid', comment='网关id')
-	device_id = Fields(type='uuid', comment='设备id')
-	pic = Fields(type='text', comment='图像记录')
-	state = Fields(type='boolean', default='True', comment='数据存在状态')
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+    demeter database
+    name:device_pic.py
+    author:rabin
+"""
+from __load__ import *
+
+class Device_pic(Model):
+	__table__ = 'device_pic'
+	__comment__ = '摄像头图片记录表'
+	id = Fields(type='uuid', primaryKey=True, comment='图片ID', uuid='farm_id')
+	farm_id = Fields(type='int', default='setting.farm', comment='园区ID', match='not')
+	gateway_id = Fields(type='uuid', comment='网关id')
+	device_id = Fields(type='uuid', comment='设备id')
+	pic = Fields(type='text', comment='图像记录')
+	spic = Fields(type='text', comment='原图地址')
+	sdate = Fields(type='int', comment='原图创建时间')
+	state = Fields(type='boolean', default='True', comment='数据存在状态')
 	cdate = Fields(type='int', default='time', comment='创建时间')

+ 21 - 0
model/device_printer_log.py

@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+    demeter database
+    name:device_printer_log.py
+    author:rabin
+"""
+from __load__ import *
+class Device_printer_log(Model):
+	__table__ = 'device_printer_log'
+	__comment__ = '设备打印机日志管理'
+	id = Fields(type='uuid', primaryKey=True, comment='日志ID', uuid='farm_id')
+	farm_id = Fields(type='int', default='setting.farm', comment='园区ID', match='not')
+	uid = Fields(type='uuid', comment='用户ID')
+	printer_id = Fields(type='uuid', comment='打印机id')
+	table_name = Fields(type='varchar(150)', comment='表名')
+	table_id = Fields(type='uuid', comment='表id')
+	content = Fields(type='varchar(500)', comment='日志内容')
+	num = Fields(type='int', comment='打印张数')
+	state = Fields(type='boolean', default='True', comment='数据存在状态')
+	cdate = Fields(type='int', default='time', comment='创建时间', manage='list')

+ 0 - 19
model/device_set.py

@@ -1,19 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-"""
-    demeter database
-    name:device_set.py
-    author:rabin
-"""
-from __load__ import *
-
-class Device_set(Model):
-	__table__ = 'device_set'
-	__comment__ = '设备设置'
-	id = Fields(type='uuid', primaryKey=True, comment='设备设置ID', uuid='farm_id')
-	farm_id = Fields(type='int', default='setting.farm', comment='园区ID', match='not')
-	device_id = Fields(type='uuid', comment='设备id')
-	max = Fields(type='smallint', comment='最大上限')
-	min = Fields(type='smallint', comment='最小下限')
-	state = Fields(type='boolean', default='True', comment='数据存在状态')
-	cdate = Fields(type='int', default='time', comment='创建时间')

+ 6 - 3
model/farm.py

@@ -12,10 +12,13 @@ class Farm(Model):
 	__comment__ = '园区主表'
 	id = Fields(type='int', primaryKey=True, autoIncrement=True, comment='园区ID')
 	name = Fields(type='varchar(200)', comment='园区名')
-	info = Fields(type='varchar(500)', comment='园区介绍')
+	info = Fields(type='text', comment='园区介绍')
 	pic = Fields(type='varchar(255)', comment='园区图片')
+	location = Fields(type='varchar(200)', comment='园区位置')
 	tel = Fields(type='varchar(50)', comment='联系电话')
-	address = Fields(type='varchar(200)', comment='农场地址')
-	master_name = Fields(type='varchar(50)', comment='联系人')
+	address = Fields(type='varchar(200)', comment='联系地址')
+	master_name = Fields(type='varchar(50)', comment='农场主姓名')
+	master_pic = Fields(type='varchar(255)', comment='农场主照片')
+	gateway = Fields(type='text', comment='网关ID')
 	state = Fields(type='boolean', default='True', comment='数据存在状态')
 	cdate = Fields(type='int', default='time', comment='创建时间')

+ 1 - 2
model/farm_work.py

@@ -14,9 +14,8 @@ class Farm_work(Model):
 	farm_id = Fields(type='int', default='setting.farm', comment='园区ID', match='not')
 	amount = Fields(type='varchar(150)', comment='劳作用量')
 	category_id = Fields(type='uuid', comment='劳作分类id')
-	content = Fields(type='varchar(300)', comment='劳作具体内容')
 	land_id = Fields(type='uuid', comment='地块id')
-	pic = Fields(type='text', comment='图像记录')
+	pic = Fields(type='varchar(255)', comment='图像记录')
 	workdate = Fields(type='int', comment='劳作时间')
 	state = Fields(type='boolean', default='True', comment='数据存在状态')
 	cdate = Fields(type='int', default='time', comment='创建时间')

+ 2 - 2
model/origin_batch.py

@@ -15,8 +15,8 @@ class Origin_batch(Model):
 	name = Fields(type='varchar(150)', comment='产品名')
 	zzdate = Fields(type='int', comment='种植时间')
 	csdate = Fields(type='int', comment='采收时间')
-	gateway_id = Fields(type='uuid', comment='网关id')
 	land_id = Fields(type='uuid', comment='地块id')
-	pic = Fields(type='text', comment='图片')
+	gateway_id = Fields(type='uuid', comment='网关id')
+	number = Fields(type='varchar(32)', comment='批号')
 	state = Fields(type='boolean', default='True', comment='数据存在状态')
 	cdate = Fields(type='int', default='time', comment='创建时间')

+ 6 - 5
model/article.py → model/web_article.py

@@ -7,13 +7,14 @@
 """
 from __load__ import *
 
-class Article(Model):
-	__table__ = 'article'
+class Web_article(Model):
+	__table__ = 'web_article'
 	__comment__ = '文章表'
-	id = Fields(type='uuid', primaryKey=True, comment='文章ID', uuid='farm_id')
-	farm_id = Fields(type='int', default='setting.farm', comment='园区ID', match='not')
-	category_id = Fields(type='uuid', comment='分类ID', match='not')
+	id = Fields(type='int', primaryKey=True, autoIncrement=True, comment='ID')
+	category_id = Fields(type='int', comment='分类ID', match='not')
 	name = Fields(type='varchar(255)', comment='文章名')
+	link = Fields(type='varchar(255)', comment='文章外链')
 	content = Fields(type='text', comment='文章内容')
+	pic = Fields(type='varchar(255)', comment='文章配图')
 	state = Fields(type='boolean', default='True', comment='数据存在状态')
 	cdate = Fields(type='int', default='time', comment='创建时间')

+ 3 - 4
model/article_category.py → model/web_article_category.py

@@ -7,11 +7,10 @@
 """
 from __load__ import *
 
-class Article_category(Model):
-	__table__ = 'article_category'
+class Web_article_category(Model):
+	__table__ = 'web_article_category'
 	__comment__ = '文章分类表'
-	id = Fields(type='uuid', primaryKey=True, comment='分类ID', uuid='farm_id')
-	farm_id = Fields(type='int', default='setting.farm', comment='园区ID', match='not')
+	id = Fields(type='int', primaryKey=True, autoIncrement=True, comment='ID')
 	name = Fields(type='varchar(50)', comment='分类名')
 	state = Fields(type='boolean', default='True', comment='数据存在状态')
 	cdate = Fields(type='int', default='time', comment='创建时间')

+ 18 - 0
model/web_help.py

@@ -0,0 +1,18 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+    demeter database
+    name:help.py
+    author:rabin
+"""
+from __load__ import *
+
+class Web_help(Model):
+	__table__ = 'web_help'
+	__comment__ = '帮助内容表'
+	id = Fields(type='int', primaryKey=True, autoIncrement=True, comment='ID')
+	category_id = Fields(type='int', comment='分类ID', match='not')
+	name = Fields(type='varchar(255)', comment='内容名')
+	content = Fields(type='text', comment='文章内容')
+	state = Fields(type='boolean', default='True', comment='数据存在状态')
+	cdate = Fields(type='int', default='time', comment='创建时间')

+ 6 - 6
model/set.py → model/web_help_category.py

@@ -2,15 +2,15 @@
 # -*- coding: utf-8 -*-
 """
     demeter database
-    name:set.py
+    name:web_help_category.py
     author:rabin
 """
 from __load__ import *
 
-class Set(Model):
-	__table__ = 'set'
-	__comment__ = '设置表'
-	id = Fields(type='int', primaryKey=True, autoIncrement=True, comment='设置ID')
-	name = Fields(type='varchar(255)', comment='网站标题')
+class Web_help_category(Model):
+	__table__ = 'web_help_category'
+	__comment__ = '帮助分类表'
+	id = Fields(type='int', primaryKey=True, autoIncrement=True, comment='ID')
+	name = Fields(type='varchar(50)', comment='分类名')
 	state = Fields(type='boolean', default='True', comment='数据存在状态')
 	cdate = Fields(type='int', default='time', comment='创建时间')

+ 18 - 0
model/web_menu.py

@@ -0,0 +1,18 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+    demeter database
+    name:web_menu.py
+    author:rabin
+"""
+from __load__ import *
+
+class Web_menu(Model):
+	__table__ = 'web_menu'
+	__comment__ = '菜单表'
+	id = Fields(type='int', primaryKey=True, autoIncrement=True, comment='ID')
+	name = Fields(type='varchar(255)', comment='菜单名')
+	link = Fields(type='varchar(500)', comment='菜单链接')
+	reorder = Fields(type='int', comment='菜单排序')
+	state = Fields(type='boolean', default='True', comment='数据存在状态')
+	cdate = Fields(type='int', default='time', comment='创建时间')

+ 24 - 0
model/web_set.py

@@ -0,0 +1,24 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+    demeter database
+    name:web_set.py
+    author:rabin
+"""
+from __load__ import *
+
+class Web_set(Model):
+	__table__ = 'web_set'
+	__comment__ = '网站设置表'
+	id = Fields(type='int', primaryKey=True, autoIncrement=True, comment='内容ID')
+	name = Fields(type='varchar(255)', comment='标题')
+	subname = Fields(type='varchar(255)', comment='子标题')
+	keywords = Fields(type='varchar(255)', comment='关键词')
+	description = Fields(type='varchar(255)', comment='描述')
+	passtel = Fields(type='varchar(255)', comment='忘记密码电话')
+	tel = Fields(type='varchar(255)', comment='联系电话')
+	icp = Fields(type='varchar(255)', comment='icp备案')
+	buy = Fields(type='text', comment='首页购买')
+	mobile = Fields(type='text', comment='首页手机')
+	state = Fields(type='boolean', default='True', comment='数据存在状态')
+	cdate = Fields(type='int', default='time', comment='创建时间')

+ 35 - 14
pub.py

@@ -7,25 +7,47 @@
 """
 from demeter.mqtt import *
 from demeter.tcp import *
+import ConfigParser
 import random
 import time
 import base64
+import oss2
 
-def sensor():
-	# 模拟设备数据
-	gateway = '10086'
-	def device():
-		return str(random.randint(100000, 100010))
-	def val():
-		return str(random.randint(1, 50))
+def push():
+	# 模拟传感器设备数据
+	gateway = 10086
+	def device(min):
+		max = min + 9
+		return str(random.randint(min, max))
+	def val(min, max):
+		return str(round(random.uniform(min, max), 2))
+
+	def switch():
+		return str(random.randint(0, 1))
 
 	hard = 'sensor'
-	pubType = ('temperature', 'humidity')
+	pubType = (
+		{'hard':'sensor','type':'temprature','device':100000,'value':(-20, 30)}
+		,{'hard':'sensor','type':'humidity','device':100010,'value':(30, 80)}
+		,{'hard':'sensor','type':'light','device':100020,'value':(3000, 13000)}
+		,{'hard':'power','type':'power','device':100090,'value':(1, 50)}
+		,{'hard':'control','type':'valve','device':100100}
+		,{'hard':'control','type':'blower','device':100110}
+		,{'hard':'power','type':'power','device':10086,'value':(1, 50)}
+		)
 	pub = Pub()
 	while True:
+		config = Demeter.temp()
 		for v in pubType:
-			key = hard+'/'+v+'/'+gateway+'/'+device()
-			value = val()
+			if v['device'] == gateway:
+				id = v['device']
+			else:
+				id = device(v['device'])
+			key = v['hard']+'/'+v['type']+'/'+str(gateway)+'/'+str(id)
+			if 'value' in v:
+				value = val(v['value'][0], v['value'][1])
+			else:
+				value = config[v['type']][id]
 			print 'key:' + key + ' value:' + value
 			pub.push(key, value)
 		time.sleep(1)
@@ -33,10 +55,9 @@ def sensor():
 def pic():
 	gateway = '10086'
 	pub = Pub()
-	key = 'pic/'
 	key = 'pic/pic_'
 	gateway = '/' + gateway + '/200001'
-	with open('admin/static/images/banner.png', 'rb') as f:
+	with open('front/static/pc/images/376a00e6.tmp2.jpg', 'rb') as f:
 		data = f.readlines()
 		total = len(data)
 		key = key + str(total) + '_'
@@ -48,7 +69,7 @@ def pic():
 			print k
 			i = i+1
 
-#sensor()
+push()
 #pic()
 
 def tcp_sensor():
@@ -92,5 +113,5 @@ def tcp_pic():
 			i = i+1
 	client.close()
 
-tcp_sensor()
+#tcp_sensor()
 #tcp_pic()

+ 78 - 0
service/device.py

@@ -0,0 +1,78 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+    demeter service
+    name:device.py 处理设备
+    author:rabin
+"""
+from demeter.core import *
+from demeter.mqtt import *
+class Device(object):
+
+	# 新增网关时,增加状态和电源
+	def upGatewayStatus(self, farm_id, gateway_id, hardware_id):
+		if hardware_id and gateway_id > 0:
+			info = Demeter.service('common').list('device_info', search={'hardware_id':hardware_id})
+			if not info:
+				# 网关状态
+				insert = {}
+				insert['farm_id'] = farm_id
+				insert['name'] = '网关状态'
+				insert['hardware_id'] = hardware_id
+				insert['gateway_id'] = gateway_id
+				insert['hardware_type'] = 5
+				insert['value'] = '0'
+				insert['type_id'] = 0
+				insert['status'] = False
+				Demeter.service('common').update('device_info', None, insert)
+
+				# 电源
+				insert = {}
+				insert['farm_id'] = farm_id
+				insert['name'] = '电源状态'
+				insert['hardware_id'] = hardware_id
+				insert['gateway_id'] = gateway_id
+				insert['hardware_type'] = 4
+				insert['value'] = '0'
+				insert['type_id'] = 8
+				insert['status'] = False
+				Demeter.service('common').update('device_info', None, insert)
+			
+	# 批量处理开关
+	def switchMul(self, value):
+		if value['devices']:
+			model = Demeter.model('device_info')
+			value['devices'] = tuple(eval(value['devices']))
+			if value['oper'] == 1:
+				switch = 1
+			else:
+				switch = 0
+			for v in value['devices']:
+				self.switch(v, switch)
+	# 处理开关
+	def switch(self, device_id, switch=1):
+		model = Demeter.model('device_info')
+		model.id = device_id
+		info = model.select(type='fetchone')
+		if info:
+			model.id = device_id
+			model.update(value=switch)
+			model = Demeter.model('device_gateway')
+			model.id = info['gateway_id']
+			gateway_info = model.select(type='fetchone')
+
+			if info['type_id'] > 0:
+				model = Demeter.model('device_type')
+				model.id = info['type_id']
+				type_info = model.select(type='fetchone')
+				if type_info:
+					info['hardware_id'] = str(info['hardware_id'])
+					switch = str(switch)
+					gateway_info['hardware_id'] = str(gateway_info['hardware_id'])
+					# 这里要向服务器发送pub请求了,暂时使用临时文件代替
+					Demeter.temp(key=type_info['key'], name=info['hardware_id'], value=switch)
+					#pub
+					#pub = Pub()
+					key = 'update/' + type_info['key'] + '/' + gateway_info['hardware_id']  + '/' + info['hardware_id']
+					value = switch
+					#pub.push(key, value, 2)

+ 200 - 2
service/record.py

@@ -6,14 +6,212 @@
     author:rabin
 """
 from demeter.core import *
+import json
 class Record(object):
 
 	def topic(self, key):
 		array = key.split('/')
-		return {'method':array[0], 'type':array[1], 'parent':array[2], 'child':array[3]}
+		slen = len(array)
+		if len >= 4:
+			return {'method':array[0], 'type':array[1], 'parent':array[2], 'child':array[3]}
+		else:
+			return {'method':array[0], 'type':array[1], 'parent':array[2], 'child':array[2]}
 
 	def push(self, key, value):
 		config = self.topic(key)
 		method = 'record_' + config['method']
 		cls = Demeter.getClass(method, 'service.')
-		cls().handle(config, value)
+		state = False
+		if config['method'] != 'pic':
+			if '{' in value:
+				value = Demeter.isJson(value)
+				if value:
+					state = True
+					for k,v in value.items():
+						config['child'] = k
+						cls().handle(self, config, v)
+		if state == False:
+			cls().handle(self, config, value)
+
+	def save(self, config, value, name='', device_type=None):
+		model = Demeter.model('device_gateway')
+		model.hardware_id = config['parent']
+		gateway = model.select(type='fetchone')
+		if gateway:
+			#更新网关状态
+			self.gateway(config['parent'], gateway['farm_id'], gateway['id'])
+			model_type = Demeter.model('hardware_type')
+			model_type.key = config['method']
+			hard = model_type.select(type='fetchone')
+			model = Demeter.model('device_info')
+			model.hardware_id = config['child']
+			model.hardware_type = hard['id']
+			device = model.select(type='fetchone')
+			if device:
+				#update
+				model.id = device['id']
+				id = device['id']
+				model.update(farm_id=gateway['farm_id'], value=value,status=True,cdate='time')
+				# 上下限判断
+				# 条件判断
+				if device['type_id'] > 0 and (hard['id'] == 2 or hard['id'] == 4):
+					value = float(value)
+					content = ''
+					if device['max']:
+						if value > device['max']:
+							if device['cxtype'] == 1:
+								if device['cxnum']:
+									device['cxnum'] = device['cxnum'] + 1
+								else:
+									device['cxnum'] = 1
+								if device['cxnum'] > 3:
+									device['cxnum'] = 0
+									content = '从' + Demeter.date(device['cxdate']) + '到' + Demeter.date(Demeter.time()) + ',' + gateway['name'] + ','+device['name']+'超过'+str(device['max'])+device_type['unit']+',当前值'+str(value)+device_type['unit']+',请及时控制'+device_type['name']+'进行处理。'
+									self.msg(id, content, gateway['farm_id'], 2)
+							else:
+								device['cxtype'] = 1
+								device['cxnum'] = 1
+							model.id = device['id']
+							if device['cxnum'] == 1:
+								model.update(farm_id=gateway['farm_id'], cxnum=device['cxnum'],cxtype=device['cxtype'], cxdate='time')
+							else:
+								model.update(farm_id=gateway['farm_id'], cxnum=device['cxnum'],cxtype=device['cxtype'])
+
+					if device['min']:
+						if value < device['min']:
+							if device['cxtype'] == 2:
+								if device['cxnum']:
+									device['cxnum'] = device['cxnum'] + 1
+								else:
+									device['cxnum'] = 1
+								if device['cxnum'] > 3:
+									device['cxnum'] = 0
+									content = '从' + Demeter.date(device['cxdate']) + '到' + Demeter.date(Demeter.time()) + ',' + gateway['name'] + ','+device['name']+'少于'+str(device['min'])+device_type['unit']+',当前值'+str(value)+device_type['unit']+',请及时控制'+device_type['name']+'进行处理。'
+									self.msg(id, content, gateway['farm_id'], 2)
+							else:
+								device['cxtype'] = 2
+								device['cxnum'] = 1
+							model.id = device['id']
+							if device['cxnum'] == 1:
+								model.update(farm_id=gateway['farm_id'], cxnum=device['cxnum'],cxtype=device['cxtype'], cxdate='time')
+							else:
+								model.update(farm_id=gateway['farm_id'], cxnum=device['cxnum'],cxtype=device['cxtype'])
+
+					content = ''
+					condition_model = Demeter.model('device_set_condition')
+					condition_model.device_id = id
+					condition_model.status = True
+					condition = condition_model.select()
+					if condition:
+						
+						for cv in condition:
+							state = False
+							text = ''
+							cv['value'] = float(cv['value'])
+							if cv['condition'] == 1 and value > cv['value']:
+								state = True
+								text = '超过'
+							elif cv['condition'] == 2 and value < cv['value']:
+								state = True
+								text = '少于'
+							elif value == cv['value']:
+								state = True
+								text = '等于'
+							if state:
+								if cv['notice'] == True:
+									content = gateway['name'] + ','+device['name']+text+str(cv['value'])+device_type['unit']+',当前值'+str(value)+device_type['unit']+',请及时控制'+device_type['name']+'进行处理。'
+									self.msg(id, content, gateway['farm_id'], 2)
+								if cv['oper'] == 2:
+									cv['oper'] = 1
+									Demeter.service('device').switchMul(cv)
+								elif cv['oper'] == 3:
+									cv['oper'] = 0
+									Demeter.service('device').switchMul(cv)
+						
+			else:
+				#insert
+				model.hardware_id = config['child']
+				model.farm_id = gateway['farm_id']
+				model.name = name
+				model.hardware_type = hard['id']
+				model.gateway_id = gateway['id']
+				if device_type:
+					model.type_id = device_type['id']
+				else:
+					model.type_id = 0
+				model.value = value
+				model.status = True
+				id = model.insert()
+				self.hardware(id, config['child'], gateway['farm_id'],gateway['id'], hard['id'])
+
+			if config['method'] == 'pic':
+				model_pic = Demeter.model('device_pic')
+				model_pic.farm_id = gateway['farm_id']
+				model_pic.gateway_id = gateway['id']
+				model_pic.device_id = id
+				model_pic.pic = value
+				model_pic.insert()
+			else:
+				data = {}
+				data['type'] = config['type']
+				data['gateway'] = config['parent']
+				data['device'] = config['child']
+				data['hard'] = config['method']
+				data['farm'] = gateway['farm_id']
+				data['source'] = value
+				data['value'] = value
+				data['time'] = Demeter.time()
+				Demeter.model('data', 'tsdb').insert(data)
+
+	def msg(self, device_id, content, farm_id, type_id):
+		model = Demeter.model('msg')
+		model.farm_id = farm_id
+		model.content = content
+		model.device_id = device_id
+		model.type_id = type_id
+		info = model.select(type='fetchone')
+		if not info:
+			model.farm_id = farm_id
+			model.content = content
+			model.device_id = device_id
+			model.type_id = type_id
+			model.insert()
+		else:
+			model.id = info['id']
+			model.update(content=content, cdate='time')
+
+	def gateway(self, gateway, farm, id):
+		# 更新网关状态
+		hardware_type = 5
+		model = Demeter.model('device_info')
+		model.hardware_id = gateway
+		model.hardware_type = hardware_type
+		device = model.select(type='fetchone')
+		if device:
+			model.id = device['id']
+			model.update(value='1',status=True,cdate='time')
+		else:
+			model.hardware_id = gateway
+			model.farm_id = farm
+			model.name = '网关状态'
+			model.status = True
+			model.hardware_type = hardware_type
+			model.gateway_id = id
+			model.type_id = 0
+			model.value = '1'
+			model.insert()
+			self.hardware(id, gateway, farm, id, 1)
+
+	def hardware(self, id, hardware_id, farm, gateway, hard):
+		# 记录硬件设备
+		if hardware_id and id > 0:
+			model = Demeter.model('hardware')
+			model.hardware_id = hardware_id
+			info = model.select(type='fetchone')
+			if not info:
+				model.farm_id = farm
+				model.hardware_id = hardware_id
+				model.gateway_id = gateway
+				model.device_id = id
+				model.hardware_type = hard
+				model.insert()

+ 15 - 0
service/record_control.py

@@ -0,0 +1,15 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+    demeter service
+    name:record_control.py 处理控制器的状态数据
+    author:rabin
+    key:control/type/gateway/device
+"""
+from demeter.core import *
+from record_sensor import Record_sensor
+class Record_control(object):
+
+	def handle(self, record, config, value):
+		sensor = Record_sensor()
+		sensor.handle(record, config, value)

+ 1 - 1
service/record_msg.py

@@ -11,7 +11,7 @@ import json
 from demeter.core import *
 class Record_msg(object):
 
-	def handle(self, config, value):
+	def handle(self, record, config, value):
 		model = Demeter.model('msg_type')
 		model.key = config['type']
 		msg_type = model.select(type='fetchone')

+ 3 - 55
service/record_pic.py

@@ -13,7 +13,8 @@ from datetime import *
 from demeter.core import *
 class Record_pic(object):
 	pic = {}
-	def handle(self, config, value):
+	def handle(self, record, config, value):
+		self.record = record
 		method = config['type'].split('_')
 		name = method[0] + '.jpg'
 		if name not in self.pic:
@@ -40,57 +41,4 @@ class Record_pic(object):
 			f.writelines(data)
 			del self.pic[name]
 		file_path = file_path.replace(Demeter.path + 'runtime', '')
-		self.write(file_path, name, config)
-
-	def write(self, file, name, config):
-		show_value = file
-		value = 0
-		model = Demeter.model('device_gateway')
-		model.hardware_id = config['parent']
-		gateway = model.select(type='fetchone')
-		if gateway:
-			model.id = gateway['id']
-			model.update(status=True,farm_id=gateway['farm_id'])
-			model = Demeter.model('device_info')
-			model.hardware_id = config['child']
-			device = model.select(type='fetchone')
-			model_type = Demeter.model('hardware_type')
-			model_type.key = config['method']
-			hard = model_type.select(type='fetchone')
-			if device:
-				#update
-				model.id = device['id']
-				id = device['id']
-				model.update(farm_id=gateway['farm_id'], num=value,show=show_value,status=True,cdate='time')
-			else:
-				#insert
-				model.hardware_id = config['child']
-				model.farm_id = gateway['farm_id']
-				model.name = name
-				model.hardware_type = hard['id']
-				model.gateway_id = gateway['id']
-				model.num = value
-				model.show = show_value
-				model.status = True
-				id = model.insert()
-			self.hardware(id, config['child'], gateway['farm_id'],gateway['id'], hard['id'])
-
-			model_pic = Demeter.model('device_pic')
-			model_pic.farm_id = gateway['farm_id']
-			model_pic.gateway_id = gateway['id']
-			model_pic.device_id = id
-			model_pic.pic = file
-			model_pic.insert()
-
-	def hardware(self, id, hardware_id, farm, gateway, hard):
-		if hardware_id and id > 0:
-			model = Demeter.model('hardware')
-			model.hardware_id = hardware_id
-			info = model.select(type='fetchone')
-			if not info:
-				model.farm_id = farm
-				model.hardware_id = hardware_id
-				model.gateway_id = gateway
-				model.device_id = id
-				model.hardware_type = hard
-				model.insert()
+		self.record.save(config, file_path, name)

+ 15 - 0
service/record_power.py

@@ -0,0 +1,15 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+    demeter service
+    name:record_power.py 处理电源数据
+    author:rabin
+    key:power/type/gateway/device
+"""
+from demeter.core import *
+from record_sensor import Record_sensor
+class Record_power(object):
+
+	def handle(self, record, config, value):
+		sensor = Record_sensor()
+		sensor.handle(record, config, value)

+ 4 - 57
service/record_sensor.py

@@ -11,64 +11,11 @@ import json
 from demeter.core import *
 class Record_sensor(object):
 
-	def handle(self, config, value):
+	def handle(self, record, config, value):
+		self.record = record
 		model = Demeter.model('device_type')
 		model.key = config['type']
 		device_type = model.select(type='fetchone')
 		if device_type:
-			show_value = value
-			model = Demeter.model('device_gateway')
-			model.hardware_id = config['parent']
-			gateway = model.select(type='fetchone')
-			if gateway:
-				model.id = gateway['id']
-				model.update(status=True,farm_id=gateway['farm_id'])
-				model = Demeter.model('device_info')
-				model.hardware_id = config['child']
-				device = model.select(type='fetchone')
-				model_type = Demeter.model('hardware_type')
-				model_type.key = config['method']
-				hard = model_type.select(type='fetchone')
-				if device:
-					#update
-					model.id = device['id']
-					id = device['id']
-					model.update(farm_id=gateway['farm_id'], num=value,show=show_value,status=True,cdate='time')
-				else:
-					#insert
-					model.hardware_id = config['child']
-					model.farm_id = gateway['farm_id']
-					model.name = device_type['name'] + '#' + str(config['child'])
-					model.hardware_type = hard['id']
-					model.gateway_id = gateway['id']
-					model.type_id = device_type['id']
-					model.num = value
-					model.show = show_value
-					model.status = True
-					id = model.insert()
-				self.hardware(id, config['child'], gateway['farm_id'],gateway['id'], hard['id'])
-
-				data = {}
-				data['type'] = config['type']
-				data['gateway'] = config['parent']
-				data['device'] = config['child']
-				data['hard'] = config['method']
-				data['farm'] = gateway['farm_id']
-				data['source'] = value
-				data['value'] = show_value
-				data['time'] = Demeter.time()
-
-				Demeter.model('data', 'tsdb').insert(data)
-
-	def hardware(self, id, hardware_id, farm, gateway, hard):
-		if hardware_id and id > 0:
-			model = Demeter.model('hardware')
-			model.hardware_id = hardware_id
-			info = model.select(type='fetchone')
-			if not info:
-				model.farm_id = farm
-				model.hardware_id = hardware_id
-				model.gateway_id = gateway
-				model.device_id = id
-				model.hardware_type = hard
-				model.insert()
+			name = device_type['name'] + '#' + str(config['child'])
+			self.record.save(config, value, name, device_type)