rabin 4 years ago
parent
commit
453132f393

+ 1 - 1
front/templates/mobile/device/alert/device.html

@@ -15,7 +15,7 @@
             <input type="hidden" name="function" id="function" value="msg"/>
             <input type="hidden" name="url" id="url" value="/device/data?type={{data['input']['type']}}&id={{data['input']['id']}}"/>
             <iframe id="f1" name="f1" style="display:none;"></iframe>
-            <input type="hidden" name="id" id="id" value="{% if 'set' in data and data['set'] and 'id' in data['set'] and data['set']['id'] > 0 %}{{data['set']['id']}}{% end %}" />
+            <input type="hidden" name="id" id="id" value="{% if 'set' in data and data['set'] and 'id' in data['set'] and data['set']['id'] %}{{data['set']['id']}}{% end %}" />
             <div class="form-group-head">单位:{{data['type']['unit']}}</div>
             <ul>
                 <li class="form-input">

+ 11 - 1
front/templates/mobile/device/inc/data.html

@@ -18,17 +18,27 @@
         </a>
     </li>
     {% elif value['hardware_type'] == 4 %}
+    {% if value['value'] %}
     {%set v = float(value['value']) %}
+    {% else %}
+    {%set v = 0 %}
+    {%set value['value'] = 0 %}
+    {% end %}
     <li class="form-input">
         <a href="/device/update_device?type={{data['input']['type']}}&id={{data['input']['id']}}&device_id={{value['id']}}">
             <i class="ico ico-7"></i><span>{{value['name']}}</span><p><span>{{value['value']}}{{value['unit']}}</span><i>&gt;</i></p>
         </a>
     </li>
     {% elif value['hardware_type'] == 2 %}
+    {% if value['value'] %}
     {%set v = float(value['value']) %}
+    {% else %}
+    {%set v = 0 %}
+    {%set value['value'] = 0 %}
+    {% end %}
     <li class="form-input">
         <a href="/device/update_device?type={{data['input']['type']}}&id={{data['input']['id']}}&device_id={{value['id']}}">
-            <span>{{value['name']}}<b class="txt-block">{{value['value']}}</b><u class="degree">{{value['unit']}}</u></span><p>{% if value['status'] == False %}<span class="disable">离线</span>{% elif value['max'] != None and v > value['max'] %}<span class="error">上限超标</span>{% elif value['min'] != None and v < value['min'] %}<span class="error">下限超标</span>{% else %}<span>正常</span>{% end %}<i>&gt;</i></p>
+            <span>{{value['name']}}<b class="txt-block">{{value['value']}}</b><u class="degree">{{value['unit']}}</u></span><p>{% if value['status'] == False %}<span class="disable">离线</span>{% elif value['max'] != None and value['max'] and v > value['max'] %}<span class="error">上限超标</span>{% elif value['min'] != None and value['min'] and v < value['min'] %}<span class="error">下限超标</span>{% else %}<span>正常</span>{% end %}<i>&gt;</i></p>
         </a>
     </li>
     {% elif value['hardware_type'] == 3 and value['unit'] == 'button' %}

+ 38 - 6
model/device_info.py

@@ -14,14 +14,11 @@ class Device_info(Model):
 	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必须是数字', unique=True)
-	hardware_id = Fields(type='bigint', comment='硬件id', match='number|硬件id必须是数字')
+	#hardware_id = Fields(type='bigint', comment='硬件id', match='number|硬件id必须是数字')
+	hardware_id = Fields(type='varchar(500)', comment='硬件id')
 	hardware_type = Fields(type='int', comment='硬件类型')
-	server_id = Fields(type='int', comment='服务id')
-	product_id = Fields(type='int', comment='产品id')
 	gateway_id = Fields(type='uuid', comment='网关id')
 	type_id = Fields(type='int', comment='设备类型id')
-	code_id = Fields(type='int', comment='命令码id')
-	code_index = Fields(type='int', comment='命令码索引key')
 	value = Fields(type='varchar(1000)', comment='设备传过来的值')
 	exp = Fields(type='varchar(500)', comment='表达式')
 	max = Fields(type='float', comment='最大上限', match='numberFloat|最大上限必须是数字')
@@ -36,8 +33,43 @@ class Device_info(Model):
 	inorder = Fields(type='int', default='0', comment='排序', match='number|排序必须是数字')
 	oper = Fields(type='boolean', default='False', comment='操作类型')
 	operdate = Fields(type='int', comment='操作时间')
+	server_id = Fields(type='int', comment='服务id')
+	product_id = Fields(type='int', comment='产品id')
+	code_id = Fields(type='int', comment='命令码id')
+	code_index = Fields(type='int', comment='命令码索引key')
 	##ALTER TABLE demeter_device_info ADD exp varchar(500); 
 	#ALTER TABLE demeter_device_info ADD inorder int default 100; 
 	#ALTER TABLE demeter_device_info ADD oper boolean; 
 	#ALTER TABLE demeter_device_info ADD operdate int; 
-	#ALTER TABLE demeter_device_info DROP inorder;   
+	#ALTER TABLE demeter_device_info DROP inorder;   
+	#ALTER TABLE demeter_device_info ALTER hardware_id type varchar(500);
+	#ALTER TABLE demeter_device_info ADD server_id int; 
+	#ALTER TABLE demeter_device_info ADD product_id int; 
+	#ALTER TABLE demeter_device_info ADD code_id int; 
+	#ALTER TABLE demeter_device_info ADD code_index int; 
+	'''
+	1、增加一列ALTER TABLE table_name ADD column_name datatype;
+
+	2、删除一列
+	ALTER TABLE table_name DROP column_name;
+
+	3、更改列的数据类型
+	ALTER TABLE table_name ALTER column_name TYPE datatype;
+
+	4、表的重命名
+	ALTER TABLE table_name RENAME TO new_name;
+
+	5、更改列的名字
+	ALTER TABLE table_name RENAME column_name to new_column_name;
+
+	6、字段的not null设置
+	ALTER TABLE table_name ALTER column_name {SET|DROP} NOT NULL;
+
+	7、给列添加default
+	ALTER TABLE table_name ALTER column_name SET DEFAULT expression;
+
+	作者:暗香抚动
+	链接:https://www.jianshu.com/p/fe824d6ecfb0
+	来源:简书
+	著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
+	'''

+ 14 - 5
service/device.py

@@ -68,15 +68,23 @@ class Device(object):
 			if hardware_id < 1:
 				device = Demeter.service('common').list('device_info', search={'gateway_id':gateway_id}, order='hardware_id desc', limit='0,1')
 				if device:
-					hardware_id = device[0]['hardware_id'] + 1
+					if '_' in device[0]['hardware_id']:
+						temp = device[0]['hardware_id'].split('_')
+						hardware_id = int(temp[1]) + 1
+					else:
+						hardware_id = int(device[0]['hardware_id']) + 1
 				else:
-					hardware_id = 10001
-			else:
-				hardware_id = (hardware_id * 10000) + 1
+					hardware_id = 100001
+
+				hardware_id = 's' + str(server_id) + '_' + str(hardware_id)
+
+			device_info = Demeter.service('common').one('device_info', hardware_id=str(hardware_id))
+			if device_info:
+				return
 			insert = {}
 			insert['farm_id'] = farm_id
 			insert['name'] = name + '#' + str(hardware_id)
-			insert['hardware_id'] = hardware_id
+			insert['hardware_id'] = str(hardware_id)
 			insert['gateway_id'] = gateway_id
 			insert['server_id'] = server_id
 			insert['product_id'] = product_id
@@ -404,6 +412,7 @@ class Device(object):
 			model.update(content=content, cdate='time')
 
 	def gateway(self, gateway, farm, id):
+		gateway = str(gateway)
 		# 更新网关状态
 		hardware_type = 5
 		model = Demeter.model('device_info')

+ 8 - 8
service/modbus/handle.py

@@ -91,17 +91,17 @@ class Handle(object):
 	def getValue(self, code, master, info):
 
 		temp = master.execute(int(code['slave_id']), int(code['register_type']), int(code['register_address']), int(code['register_length']))
-		#value = struct.unpack('>f', struct.pack('>HH', temp[0], temp[1]))
+		value = struct.unpack('>f', struct.pack('>HH', temp[0], temp[1]))
 		value = str(round(temp[0], 2))
 		#value = str(temp)
+		if value and value != '0':
+			if 'exp' in code and code['exp']:
+				value = Demeter.exp(code['exp'], value)
+			if 'exp' in info and info['exp'] and info['exp'] != '-1':
+				value = Demeter.exp(info['exp'], value)
+			value = str(round(float(value), 2))
+		#Demeter.echo('value:' + str(value) + '||temp:' + str(temp) + '||size:' + str(len(temp)))
 		#print(value)
-
-		if 'exp' in code and code['exp']:
-			value = Demeter.exp(code['exp'], value)
-		if 'exp' in info and info['exp'] and info['exp'] != '-1':
-			value = Demeter.exp(info['exp'], value)
-		value = str(round(float(value), 2))
-		Demeter.echo('value:' + str(value) + '||temp:' + str(temp) + '||size:' + str(len(temp)))
 		return value
 
 	def getCode(self, address, code, type = 1):

+ 8 - 8
service/modbus/old.py

@@ -18,17 +18,17 @@ class Old(object):
 		slave_1.add_block('block1', modbus_tk.defines.HOLDING_REGISTERS, 0, 16)
 
 		while 1:
-			tempr = round(((random.random()*1000)+10000), 2)
-			#value = struct.unpack('>HH', struct.pack('>f', tempr))
-			value = tempr
-			#Demeter.echo('tempr:' + str(tempr))
+			tempr = round(((random.random()*1000)), 2)
+			value = struct.unpack('>HH', struct.pack('>f', tempr))
+			#value = tempr
+			Demeter.echo('tempr:' + str(tempr))
 			Demeter.echo('value:' + str(value))
 			slave_1.set_values('block1', 0, value)
 
-			shidu = round(((random.random()*1000)+10000), 2)
-			#value = struct.unpack('>HH', struct.pack('>f', shidu))
-			value = shidu
-			#Demeter.echo('shidu:' + str(shidu))
+			shidu = round(((random.random()*1000)), 2)
+			value = struct.unpack('>HH', struct.pack('>f', shidu))
+			#value = shidu
+			Demeter.echo('shidu:' + str(shidu))
 			Demeter.echo('value:' + str(value))
 			slave_1.set_values('block1', 2, value)