rabin 7 年 前
コミット
1e55ed4414

+ 2 - 2
admin/page/__load__.py

@@ -214,8 +214,8 @@ class Load(tornado.web.RequestHandler):
 	def menu(self):
 		parent = [['农场与设备管理', '网站管理', '基础设置'],['', '', '']]
 		child = [
-			[['/farm/farm','/device/gateway','/device/info','/work/work','/work/category','/work/land','/origin/batch','/farm/user','/device/printer','/farm/msg'],['农场列表', '网关管理', '设备管理', '农场农事管理', '农场农事方式', '农场地块管理', '溯源批次管理','农场用户管理','农场打印机','消息提醒']]
+			[['/farm/farm','/device/gateway','/device/info','/work/work','/work/category','/work/land','/origin/batch','/farm/user','/device/printer','/msg/msg'],['农场列表', '网关管理', '设备管理', '农场农事管理', '农场农事方式', '农场地块管理', '溯源批次管理','农场用户管理','农场打印机','消息提醒']]
 			,[['/content/info','/content/article','/content/category', '/content/set', '/sms/sms'],['首页内容管理', '文章管理', '文章分类', '网站基础设置', '短信通知']]
-			,[['/device/type','/admin/admin','/admin/role', '/admin/log'],['设备类型管理', '管理员设置', '管理权限设置', '系统日志']]
+			,[['/device/type','/hardware/type','/msg/type','/admin/admin','/admin/role','/hardware/hardware', '/admin/log'],['设备类型管理','硬件类型管理', '消息类型设置', '管理员设置', '管理权限设置','硬件设备记录', '系统日志']]
 			]
 		return (parent,child)

+ 21 - 12
admin/page/device.py

@@ -192,11 +192,27 @@ class gateway_update_path(Load):
 		self.commonView('update')
 	@tornado.web.authenticated
 	def post(self):
-		self.commonUpdate('device_gateway')
+		id = self.commonUpdate('device_gateway')
+		self.hardware(id)
+
 	@tornado.web.authenticated
 	def delete(self):
 		self.commonDelete('device_gateway')
 
+	@tornado.web.authenticated
+	def hardware(self, id):
+		hardware_id = self.data['update']['hardware_id']
+		if hardware_id and id > 0:
+			info = self.service('common').one('hardware', hardware_id=hardware_id)
+			if not info:
+				insert = {}
+				insert['farm_id'] = self.data['update']['farm_id']
+				insert['hardware_id'] = hardware_id
+				insert['gateway_id'] = id
+				insert['hardware_type'] = 1
+				self.service('common').update('hardware', None, insert)
+				
+
 class info_path(Load):
 	@tornado.web.authenticated
 	def get(self):
@@ -213,18 +229,10 @@ class info_path(Load):
 			,state = False
 		)
 		self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
-		hardware = json.loads(Demeter.config['hardware']['type'])
-		hardware_type = []
-		hardware_value = {}
-		for key,value in hardware.items():
-			send = {}
-			send['id'] = value
-			send['name'] = key
-			hardware_type.append(send)
-			hardware_value[value] = key
 		self.data['common']['search_type_id-select-'] = self.service('common').list('device_type')
 		self.data['common']['search_gateway_id-select-'] = self.service('common').list('device_gateway')
-		self.data['common']['search_hardware_type-select-'] = hardware_type
+		self.data['common']['search_hardware_type-select-'] = self.service('common').list('hardware_type')
+		del self.data['common']['search_hardware_type-select-'][0]
 		self.commonList('device_info')
 		if self.data['list']:
 			for key, value in enumerate(self.data['list']):
@@ -244,7 +252,8 @@ class info_path(Load):
 					self.data['list'][key]['show_num'] = value['show_num'] + '<br />更新时间:' + Demeter.date(value['cdate'])
 				else:
 					self.data['list'][key]['show_num'] = '0'
-				self.data['list'][key]['hardware_type'] = hardware_value[self.data['list'][key]['hardware_type']]
+				hardware_type = self.service('common').one('hardware_type', id=value['hardware_type'])
+				self.data['list'][key]['hardware_type'] = hardware_type['name']
 
 
 		self.commonView('list')

+ 1 - 1
admin/page/farm.py

@@ -29,7 +29,7 @@ class farm_path(Load):
 			,{'name':'批次管理', 'url':'/origin/batch'}
 			,{'name':'用户管理', 'url':'/farm/user'}
 			,{'name':'打印机管理', 'url':'/device/printer'}
-			,{'name':'消息提醒', 'url':'/farm/msg'}
+			,{'name':'消息提醒', 'url':'/msg/msg'}
 			)
 		self.commonList('farm')
 		if self.data['list']:

+ 94 - 0
admin/page/hardware.py

@@ -0,0 +1,94 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+    demeter web page
+    name:hardware.py 硬件相关页面
+    author:rabin
+"""
+from __load__ import *
+
+class hardware_path(Load):
+	@tornado.web.authenticated
+	def get(self):
+		self.common(
+			name = u'硬件设备'
+			,path = '/hardware/hardware'
+			,width = '600'
+			,height = '600'
+			,add = False
+			,edit = False
+			,search = (('farm_id-select-','hardware_type-select-','hardware_id-input-mlike'), (u'选择农场',u'硬件类型', u'硬件id'))
+			,thead = (u'所属农场', u'硬件类型', u'硬件id',u'网关名',u'设备名', u'接入时间')
+			,tbody = ('farm','type', 'hardware_id', 'gateway', 'device', 'cdate')
+			,state = False
+		)
+		self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
+		self.data['common']['search_hardware_type-select-'] = self.service('common').list('hardware_type')
+		self.commonList('hardware')
+		if self.data['list']:
+			for key, value in enumerate(self.data['list']):
+				farm = self.service('common').one('farm', id=value['farm_id'])
+				self.data['list'][key]['farm'] = farm['name']
+				htype = self.service('common').one('hardware_type', id=value['hardware_type'])
+				self.data['list'][key]['type'] = htype['name']
+
+				gateway = self.service('common').one('device_gateway', id=value['gateway_id'])
+				self.data['list'][key]['gateway'] = gateway['name']
+
+				if value['device_id']:
+					device = self.service('common').one('device_info', id=value['device_id'])
+					self.data['list'][key]['device'] = device['name']
+				else:
+					self.data['list'][key]['device'] = ''
+		self.commonView('list')
+
+class hardware_update_path(Load):
+	@tornado.web.authenticated
+	def get(self):
+		self.common(
+			path = '/hardware/hardware'
+			,label = (u'硬件类型',u'硬件id')
+			,update = ('hardware_type-select-required','hardware_id-input-required')
+			,update_hardware_type = self.service('common').list('hardware_type')
+		)
+		self.commonOne('hardware')
+		self.commonView('update')
+	@tornado.web.authenticated
+	def post(self):
+		self.commonUpdate('hardware')
+	@tornado.web.authenticated
+	def delete(self):
+		self.commonDelete('hardware')
+
+class type_path(Load):
+	@tornado.web.authenticated
+	def get(self):
+		self.common(
+			name = u'硬件类型'
+			,path = '/hardware/type'
+			,width = '600'
+			,height = '300'
+			,search = (('name-input-mlike',), (u'类型名称',))
+			,thead = (u'类型名称',u'类型key', u'更新时间')
+			,tbody = ('name', 'key', 'cdate')
+			,state = False
+		)
+		self.commonList('hardware_type')
+		self.commonView('list')
+
+class type_update_path(Load):
+	@tornado.web.authenticated
+	def get(self):
+		self.common(
+			path = '/hardware/type'
+			,label = (u'类型名称', u'类型key')
+			,update = ('name-input-required', 'key-input-required')
+		)
+		self.commonOne('hardware_type')
+		self.commonView('update')
+	@tornado.web.authenticated
+	def post(self):
+		self.commonUpdate('hardware_type')
+	@tornado.web.authenticated
+	def delete(self):
+		self.commonDelete('hardware_type')

+ 101 - 0
admin/page/msg.py

@@ -0,0 +1,101 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+    demeter web page
+    name:msg.py 消息相关
+    author:rabin
+"""
+from __load__ import *
+
+push = ({'id':'sms', 'name':'短信'}, {'id':'weixin', 'name':'微信'})
+
+class msg_path(Load):
+	@tornado.web.authenticated
+	def get(self):
+		self.common(
+			name = u'消息'
+			,path = '/msg/msg'
+			,width = '600'
+			,height = '600'
+			#,add = False
+			,edit = False
+			,full = True
+			,search = (('farm_id-select-','type_id-select-','content-input-mlike'), (u'选择农场',u'硬件类型', u'消息内容'))
+			,thead = (u'所属农场', u'消息类型', u'接收人', u'消息内容', u'是否已读',u'发送时间')
+			,tbody = ('farm','type', 'user', 'content', 'status', 'cdate')
+			,state = False
+		)
+		self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
+		self.data['common']['search_type_id-select-'] = self.service('common').list('msg_type')
+		self.commonList('msg')
+		if self.data['list']:
+			for key, value in enumerate(self.data['list']):
+				farm = self.service('common').one('farm', id=value['farm_id'])
+				self.data['list'][key]['farm'] = farm['name']
+				htype = self.service('common').one('msg_type', id=value['hardware_type'])
+				self.data['list'][key]['type'] = htype['name']
+
+				user = self.service('common').one('farm_user', id=value['uid'])
+				self.data['list'][key]['user'] = user['username']
+
+				if value['status'] == True:
+					self.data['list'][key]['status'] = '已读'
+				else:
+					self.data['list'][key]['device'] = '未读'
+		self.commonView('list')
+
+class msg_update_path(Load):
+	@tornado.web.authenticated
+	def get(self):
+		self.common(
+			path = '/msg/msg'
+			,label = (u'所属农场',u'发送用户',u'消息类型',u'消息内容',u'消息链接',u'推送')
+			,update = ('farm_id-select--uid','uid-checkbox--farm_id','msg_type-select-required','content-editor-required','link-input-','push-checkbox-')
+			,update_msg_type = self.service('common').list('msg_type')
+			,update_farm_id = Demeter.config['setting']['farmList']
+			,update_uid = self.service('common').list('farm_user')
+			,update_push = push
+		)
+		self.commonOne('msg')
+		self.commonView('update')
+	@tornado.web.authenticated
+	def post(self):
+		# 发消息
+		self.commonUpdate('msg')
+	@tornado.web.authenticated
+	def delete(self):
+		self.commonDelete('msg')
+
+class type_path(Load):
+	@tornado.web.authenticated
+	def get(self):
+		self.common(
+			name = u'消息类型'
+			,path = '/msg/type'
+			,width = '600'
+			,height = '300'
+			,search = (('name-input-mlike',), (u'类型名称',))
+			,thead = (u'类型名称',u'类型key', u'更新时间')
+			,tbody = ('name', 'key', 'cdate')
+			,state = False
+		)
+		self.commonList('msg_type')
+		self.commonView('list')
+
+class type_update_path(Load):
+	@tornado.web.authenticated
+	def get(self):
+		self.common(
+			path = '/msg/type'
+			,label = (u'类型名称', u'类型key',u'推送')
+			,update = ('name-input-required', 'key-input-required','push-checkbox-')
+			,update_push = push
+		)
+		self.commonOne('msg_type')
+		self.commonView('update')
+	@tornado.web.authenticated
+	def post(self):
+		self.commonUpdate('msg_type')
+	@tornado.web.authenticated
+	def delete(self):
+		self.commonDelete('msg_type')

+ 1 - 3
admin/page/work.py

@@ -21,8 +21,6 @@ class work_path(Load):
 			,state = True
 		)
 		self.data['common']['search_farm_id-select-'] = Demeter.config['setting']['farmList']
-		self.commonList('device_gateway')
-
 		self.data['common']['search_category_id-select-'] = self.service('common').list('farm_work_category')
 		self.commonList('farm_work')
 		if self.data['list']:
@@ -41,7 +39,7 @@ class work_update_path(Load):
 		self.common(
 			path = '/work/work'
 			,label = (u'所属农场',u'劳作时间',u'劳作方式',u'劳作用量',u'劳作地块',u'图像记录',u'劳作内容')
-			,update = ('farm_id-select-required', 'workdate-date-required','category_id-select-required-method_id','amount-input-required','land_id-select-required','pic-pic-required','content-text-required')
+			,update = ('farm_id-select-required', 'workdate-date-required','category_id-select-required','amount-input-required','land_id-select-required','pic-pic-required','content-text-required')
 			,update_category_id = self.service('common').list('farm_work_category')
 			,update_land_id = self.service('common').list('farm_work_land')
 			,update_farm_id = Demeter.config['setting']['farmList']

+ 2 - 0
admin/static/js/main.js

@@ -38,8 +38,10 @@ function selectShow(e, id) {
 			if (parent != e.val()) {
 				$(this).html('');
 				$(this).attr('selected', false)
+				$(this).hide();
 			} else {
 				$(this).html($(this).attr('name'));
+				$(this).show();
 			}
 		})
 	}

+ 9 - 9
admin/templates/common/update.html

@@ -5,10 +5,10 @@
 
 {% block form %}
 {% for index,name in enumerate(data['common']['label']) %}
-	{%set value = data['common']['update'][index] %}
-	{%set value = value.split('-') %}
+  {%set value = data['common']['update'][index] %}
+  {%set value = value.split('-') %}
   {%set length = len(value) %}
-	<div class="layui-form-item {% if value[1] in ('text',)  %}layui-form-text{% end %}">
+  <div class="layui-form-item {% if value[1] in ('text',)  %}layui-form-text{% end %}">
       <label for="{{value[0]}}" class="layui-form-label">
         {% if value[2] or (value[1] == 'password'  and not data['info']) %}<span class="x-red">*</span>{% end %}
           {{name}}
@@ -27,9 +27,9 @@
           <input type="password" id="update_{{value[0]}}" name="update_{{value[0]}}" lay-verify="{% if not data['info'] %}required{% end %}"
           autocomplete="off" class="layui-input" value="" />
       {% elif value[1] == 'text' %}
-          <textarea placeholder="{{value[3]}}" class="layui-textarea" id="update_{{value[0]}}" name="update_{{value[0]}}" value="{% if data['info'] and value[0] in data['info'] %}{{data['info'][value[0]]}}{% end %}" lay-verify="{{value[2]}}">{% if data['info'] and value[0] in data['info'] %}{{data['info'][value[0]]}}{% end %}</textarea>
+          <textarea {% if length > 3 %}placeholder="{{value[3]}}"{% end %} class="layui-textarea" id="update_{{value[0]}}" name="update_{{value[0]}}" value="{% if data['info'] and value[0] in data['info'] %}{{data['info'][value[0]]}}{% end %}" lay-verify="{{value[2]}}">{% if data['info'] and value[0] in data['info'] %}{{data['info'][value[0]]}}{% end %}</textarea>
       {% elif value[1] == 'editor' %}
-          <textarea placeholder="{{value[3]}}" class="layui-form layui-textarea layui-editor" id="update_{{value[0]}}" name="update_{{value[0]}}" value="{% if data['info'] and value[0] in data['info'] %}{{data['info'][value[0]]}}{% end %}" lay-verify="{{value[2]}}">{% if data['info'] and value[0] in data['info'] %}{{data['info'][value[0]]}}{% end %}</textarea>
+          <textarea {% if length > 3 %}placeholder="{{value[3]}}"{% end %} class="layui-form layui-textarea layui-editor" id="update_{{value[0]}}" name="update_{{value[0]}}" value="{% if data['info'] and value[0] in data['info'] %}{{data['info'][value[0]]}}{% end %}" lay-verify="{{value[2]}}">{% if data['info'] and value[0] in data['info'] %}{{data['info'][value[0]]}}{% end %}</textarea>
       {% elif value[1] == 'pic' %}
           <div class="site-demo-upbar">
             <img id="upload_{{value[0]}}_show" width="200" src="{% if data['info'] and value[0] in data['info'] %}{{data['info'][value[0]]}}{% end %}" style="{% if  data['info'] and value[0] in data['info'] and data['info'][value[0]] %}display:;{% else %}display:none;{% end %}margin-left:10px;"/>
@@ -44,7 +44,7 @@
           {% if option in data['common'] %}
           {%set select = data['common'][option] %}
             {% for v in select %}
-            <input class="layui-input" type="radio" id="update_{{value[0]}}" name="update_{{value[0]}}" lay-verify="{{value[2]}}"
+            <input {% if length > 3 %}parent="{% if value[3] in v %}{{v[value[3]]}}{% end %}"{% end %} class="layui-input" type="radio" id="update_{{value[0]}}" name="update_{{value[0]}}" lay-verify="{{value[2]}}"
             autocomplete="off" class="layui-input" value="{{v['id']}}" title="{{v['name']}}" {% if data['info'] and value[0] in data['info'] and data['info'][value[0]] == v['id'] %}checked{% end %}/>
             {% end %}
           {% end %}
@@ -54,7 +54,7 @@
           {% if option in data['common'] %}
           {%set select = data['common'][option] %}
             {% for v in select %}
-            <input class="layui-input" type="checkbox" id="update_{{value[0]}}" name="update_{{value[0]}}" lay-verify="{{value[2]}}"
+            <input {% if length > 3 %}parent="{% if value[3] in v %}{{v[value[3]]}}{% end %}"{% end %} class="layui-input" type="checkbox" id="update_{{value[0]}}" name="update_{{value[0]}}" lay-verify="{{value[2]}}"
             autocomplete="off" class="layui-input" value="{{v['id']}}" title="{{v['name']}}" {% if data['info'] and value[0] in data['info'] and data['info'][value[0]] and v['id'] and str(v['id'])+',' in data['info'][value[0]]+',' %}checked{% end %}/>
             {% end %}
           {% end %}
@@ -105,13 +105,13 @@
           <select class="layui-select selectLoad" name="update_{{value[0]}}" id="update_{{value[0]}}" lay-verify="{{value[2]}}" lay-search="" {% if length > 3 %} change="{{value[3]}}" lay-filter="yes"{% end %}>
                 <option value="">请选择</option>
             {% for v in select %}
-                <option value="{{v['id']}}" parent="{% if 'category_id' in v %}{{v['category_id']}}{% end %}" name="{{v['name']}}" {% if data['info'] and value[0] in data['info'] and int(data['info'][value[0]]) == v['id'] %}selected{% end %}>{{v['name']}}</option>
+                <option value="{{v['id']}}" {% if length > 3 %}parent="{% if value[3] in v %}{{v[value[3]]}}{% end %}"{% end %} name="{{v['name']}}" {% if data['info'] and value[0] in data['info'] and int(data['info'][value[0]]) == v['id'] %}selected{% end %}>{{v['name']}}</option>
                 {% end %}
             </select>
             {% end %}
       {% end %}
       </div>
-	</div>
+  </div>
 {% end %}
 {% end %}
 

+ 0 - 6
demeter.conf

@@ -8,12 +8,6 @@ site				= http://www.nongxiaohe.com/
 copyright			= 2017 nongxiaohe.com v1.0.0
 
 
-[hardware]
-;硬件配置
-type				= {"power":1,"sensor":2,"control":3,"pic":4,"status":5}
-station				= {'power':1,'sensor':2,'control':3,'pic':4,'status':5}
-
-
 [gateway]
 ;暂时写在这里 网关对应园区id 这里从数据库里读取
 gateway				= 1

+ 22 - 0
install.py

@@ -57,5 +57,27 @@ for value in device_type:
 		model.unit = value['unit']
 		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']
+	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()
+
 
 print 'install success!'

+ 13 - 19
model/data.py

@@ -9,40 +9,34 @@ from base import Base
 
 class Data(Base):
 	__table__ = 'data'
-	__type__ = 'influxdb'
+
+	def setTable(self, data):
+		self.table = self.__table__ + '_' + data['farm'] + '_' + data['device']
 
 	def insert(self, data):
-		json = self.model_insert(data)
+		self.setTable(data)
+		json = self.insert_json(data)
 		self.db.write_points(json)
 		self.select()
 
-	def model_insert(self, data):
-		if 'farm' in data:
-			json = self.model_farm(data)
-		else:
-			json = self.model_json(data)
-		return json
-
-	def model_json(self, data):
-		return data
-
-	def model_farm(self, data):
+	def insert_json(self, data):
 		json = [
 		{
 			"measurement": self.table,
 			"tags": {
-				"farm": data['farm'],
-				"method": data['method'],
-				"gateway": data['gateway'],
-				"device": data['device']
+				"hard": data['hard'],
+				"type":data['type'],
+				"gateway": data['gateway']
 			},
 			#"time": data['time'],
 			"fields": {
-				data['type']: data['value']
+				'source' : data['source'],
+				'value' : data['value']
 			}
 		}]
 		return json
 
-	def select(self):
+	def select(self, data):
+		self.setTable(data)
 		result = self.db.query('select * from ' + self.table)    
 		print("Result: {0}".format(result))

+ 1 - 1
model/device_info.py

@@ -14,7 +14,7 @@ class Device_info(Base):
 	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='smallint', comment='硬件类型')
+	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='设备类型')

+ 20 - 0
model/hardware.py

@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+    demeter database
+    name:hardware.py
+    author:rabin
+"""
+from __load__ import *
+
+class Hardware(Base):
+	__table__ = 'hardware'
+	__comment__ = '硬件设备表'
+	id = Fields(type='uuid', primaryKey=True, comment='硬件设备表ID', uuid='farm_id')
+	farm_id = Fields(type='int', default='setting.farm', comment='园区ID', match='not')
+	hardware_id = Fields(type='bigint', comment='硬件id', unique=True)
+	hardware_type = Fields(type='int', comment='硬件类型')
+	device_id = Fields(type='uuid', comment='设备id')
+	gateway_id = Fields(type='uuid', comment='网关id')
+	state = Fields(type='boolean', default='True', comment='数据存在状态')
+	cdate = Fields(type='int', default='time', comment='创建时间')

+ 7 - 7
model/content_article.py → model/hardware_type.py

@@ -2,16 +2,16 @@
 # -*- coding: utf-8 -*-
 """
     demeter database
-    name:content_category.py
+    name:hardware_type.py
     author:rabin
 """
 from __load__ import *
 
-class Content_article(Base):
-	__table__ = 'content_article'
-	__comment__ = '内容文章表'
-	id = Fields(type='int', primaryKey=True, autoIncrement=True, comment='文章ID')
-	name = Fields(type='varchar(255)', comment='文章名')
-	content = Fields(type='text', comment='文章内容')
+class Hardware_type(Base):
+	__table__ = 'hardware_type'
+	__comment__ = '硬件类型'
+	id = Fields(type='int', primaryKey=True, autoIncrement=True, comment='硬件类型ID')
+	name = Fields(type='varchar(50)', comment='硬件类型名')
+	key = Fields(type='varchar(50)', comment='硬件类型标识', unique=True)
 	state = Fields(type='boolean', default='True', comment='数据存在状态')
 	cdate = Fields(type='int', default='time', comment='创建时间')

+ 24 - 0
model/msg.py

@@ -0,0 +1,24 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+    demeter database
+    name:msg.py
+    author:rabin
+    mqtt里的key:msg/type_key/farm_id/uid,value
+
+"""
+from __load__ import *
+
+class Msg(Base):
+	__table__ = 'msg'
+	__comment__ = '消息表'
+	id = Fields(type='uuid', primaryKey=True, comment='消息ID', uuid='farm_id')
+	farm_id = Fields(type='int', default='setting.farm', comment='园区ID')
+	uid = Fields(type='uuid', comment='用户ID')
+	type_id = Fields(type='int', comment='分类ID')
+	link = Fields(type='varchar(255)', comment='消息链接')
+	content = Fields(type='text', comment='消息内容')
+	push = Fields(type='varchar(50)', comment='推送')
+	status = Fields(type='boolean', default='False', comment='是否已读')
+	state = Fields(type='boolean', default='True', comment='数据存在状态')
+	cdate = Fields(type='int', default='time', comment='创建时间')

+ 18 - 0
model/msg_type.py

@@ -0,0 +1,18 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+    demeter database
+    name:msg_type.py
+    author:rabin
+"""
+from __load__ import *
+
+class Msg_type(Base):
+	__table__ = 'msg_type'
+	__comment__ = '消息类型表'
+	id = Fields(type='int', primaryKey=True, autoIncrement=True, comment='消息类型ID')
+	name = Fields(type='varchar(50)', comment='类型名')
+	key = Fields(type='varchar(50)', comment='类型key')
+	push = Fields(type='varchar(50)', comment='推送')
+	state = Fields(type='boolean', default='True', comment='数据存在状态')
+	cdate = Fields(type='int', default='time', comment='创建时间')

+ 6 - 7
model/content_help.py → model/set.py

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

+ 1 - 0
runtime/postgresql/hardware

@@ -0,0 +1 @@
+[["id", 1], ["farm_id", 2], ["hardware_id", 3], ["hardware_type", 4], ["device_id", 5], ["gateway_id", 6], ["state", 7], ["cdate", 8]]

+ 1 - 0
runtime/postgresql/hardware_type

@@ -0,0 +1 @@
+[["id", 1], ["name", 2], ["key", 3], ["state", 4], ["cdate", 5]]

+ 1 - 0
runtime/postgresql/manage_log

@@ -0,0 +1 @@
+[["id", 1], ["admin_id", 2], ["model", 3], ["method", 4], ["data", 5], ["state", 6], ["cdate", 7]]

+ 1 - 0
runtime/postgresql/msg

@@ -0,0 +1 @@
+[["id", 1], ["farm_id", 2], ["uid", 3], ["type_id", 4], ["link", 5], ["content", 6], ["push", 7], ["status", 8], ["state", 9], ["cdate", 10]]

+ 1 - 0
runtime/postgresql/msg_type

@@ -0,0 +1 @@
+[["id", 1], ["name", 2], ["key", 3], ["sms", 4], ["state", 5], ["cdate", 6]]

+ 0 - 90
service/device_group.py

@@ -1,90 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-"""
-    demeter service
-    name:device_group.py 设备分组管理
-    author:rabin
-"""
-from demeter import *
-
-class Device_group(object):
-
-	# 获取农场下的页面数据、页面下的分组数据、分组中的设备
-	def getPage(self, type = False, state = True, search = None):
-		model = Demeter.model('device_page')
-		model.state = state
-		if search:
-			if 'name' in search and search['name']:
-				model.name.mlike(search['name'])
-			if 'start' in search and search['start']:
-				model.cdate.start(search['start'])
-			if 'end' in search and search['end']:
-				model.cdate.end(search['end'])
-		page = model.select(page=True)
-		if type:
-			return self.__getGroup(page)
-		return page
-
-	def __getGroup(self, page):
-		if page:
-			device = Demeter.model('device_info')
-			for key, value in page:
-				page[key]['group'] = self.__getDevice(device, self.getGroup(value['id']))
-		return page
-
-	def __getDevice(self, device, group):
-		if group:
-			for key, value in group:
-				device.id = value['devices']
-				device.exp('in')
-				group[key]['device'] = device.select()
-
-		return group
-
-	def getPageOne(self, id):
-		model = Demeter.model('device_page')
-		model.id = id
-		page = model.select(type='fetchone')
-		return page
-
-	def getGroup(self, page_id):
-		group = Demeter.model('device_group')
-		group.page_id = page_id
-		return group.select()
-
-	# 更新页面
-	def updatePage(self, id, name):
-		model = Demeter.model('device_page')
-		if id:
-			model.id = id
-			model.update(name=name, cdate='time')
-			return id
-		else:
-			model.name = name
-			return model.insert()
-
-	# 删除页面
-	def deletePage(self, id, state = False):
-		model = Demeter.model('device_page')
-		model.id = id
-		return model.update(state=state)
-
-	# 添加分组
-	def addGroup(self, page_id, name, devices):
-		model = Demeter.model('device_group')
-		model.page_id = page_id
-		model.name = name
-		model.devices = devices
-		return model.insert()
-
-	# 修改分组
-	def updateGroup(self, id, name, devices):
-		model = Demeter.model('device_group')
-		model.id = id
-		return model.update(name=name, devices=devices, cdate='time')
-
-	# 删除分组
-	def deleteGroup(self, id, state = False):
-		model = Demeter.model('device_group')
-		model.id = id
-		return model.update(state=state)

+ 5 - 46
service/record.py

@@ -5,56 +5,15 @@
     name:record.py 用于记录设备过来的数据
     author:rabin
 """
-import time
-import json
 from demeter import *
 class Record(object):
 
 	def topic(self, key):
 		array = key.split('/')
-		return {'hard':array[0], 'type':array[1], 'gateway':array[2], 'device':array[3]}
+		return {'method':array[0], 'type':array[1], 'parent':array[2], 'child':array[3]}
 
 	def push(self, key, value):
-		data = self.topic(key)
-		model = Demeter.model('device_type')
-		model.key = data['type']
-		device_type = model.select(type='fetchone')
-		if device_type:
-			model = Demeter.model('device_gateway')
-			model.hardware_id = data['gateway']
-			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 = data['device']
-				device = model.select(type='fetchone')
-				if device:
-					#update
-					model.id = device['id']
-					model.update(farm_id=gateway['farm_id'], num=value,show_num=value,status=True,cdate='time')
-				else:
-					hard = json.loads(Demeter.config['hardware']['type'])
-					#insert
-					model.hardware_id = data['device']
-					model.farm_id = gateway['farm_id']
-					model.name = device_type['name'] + '#' + str(data['device'])
-					model.hardware_type = hard[data['hard']]
-					model.gateway_id = gateway['id']
-					model.type_id = device_type['id']
-					model.num = value
-					model.show_num = value
-					model.status = True
-					id = model.insert()
-
-				#now = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
-				#data['farm'] = gateway['farm_id']
-				#data['time'] = now
-				#data['value'] = value
-
-				#print data
-
-				#Demeter.model('data', 'tsdb').insert(data)
-				# 上报数据
-				#Demeter.up(data)
-		
+		config = self.topic(key)
+		method = 'record_' + config['method']
+		cls = Demeter.getClass(method)
+		cls.handle(config, value)

+ 75 - 0
service/record_sensor.py

@@ -0,0 +1,75 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+    demeter service
+    name:record_sensor.py 处理传感器数据
+    author:rabin
+    key:sensor/type/gateway/device
+"""
+import time
+import json
+from demeter import *
+class Record_sensor(object):
+
+	def handle(self, config, value):
+		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')
+				if device:
+					#update
+					model.id = device['id']
+					model.update(farm_id=gateway['farm_id'], num=value,show_num=show_value,status=True,cdate='time')
+				else:
+					model_type = Demeter.model('hardware_type')
+					model_type.key = config['method']
+					hard = model_type.select(type='fetchone')
+					#insert
+					model.hardware_id = config['child']
+					model.farm_id = gateway['farm_id']
+					model.name = device_type['name'] + '#' + str(data['device'])
+					model.hardware_type = hard['id']
+					model.gateway_id = gateway['id']
+					model.type_id = device_type['id']
+					model.num = value
+					model.show_num = 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['time'] = now
+				data['source'] = value
+				data['value'] = show_value
+
+				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:
+				insert = {}
+				insert['farm_id'] = farm
+				insert['hardware_id'] = hardware_id
+				insert['gateway_id'] = gateway
+				insert['device_id'] = id
+				insert['hardware_type'] = hard
+				model.insert()
+		

+ 40 - 0
service/record_sms.py

@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+    demeter service
+    name:record_sms.py 处理消息数据
+    author:rabin
+    key:msg/type_key/farm_id/uid
+"""
+import time
+import json
+from demeter import *
+class Record_sms(object):
+
+	def handle(self, config, value):
+		model = Demeter.model('msg_type')
+		model.key = config['type']
+		msg_type = model.select(type='fetchone')
+		if msg_type:
+			data = value.split('||')
+			model = Demeter.model('msg')
+			model.farm_id = config['parent']
+			model.uid = config['child']
+			model.type_id = config['type']
+			model.content = data[0]
+			model.link = data[1]
+			if not data[2]:
+				data[2] = msg_type['push']
+			model.push = data[2]
+			if 'sms' in model.push:
+				self.sendSms(model)
+			if 'weixin' in model.push:
+				self.sendWeixin(model)
+			id = model.insert()
+
+	def sendSms(self, model):
+		return
+
+	def sendWeixin(self, model):
+		return
+