完善汇多协议封装
修改webservice协议
diff --git a/supwisdom/protocol/amassprotocol.py b/supwisdom/protocol/amassprotocol.py
index fde1bbe..574e765 100644
--- a/supwisdom/protocol/amassprotocol.py
+++ b/supwisdom/protocol/amassprotocol.py
@@ -111,7 +111,7 @@
if t == IFT_USHORT or t == IFT_ULONG or t == IFT_NULL or t == IFT_BYTE or t == IFT_LONG:
if isinstance(value, int):
b = value
- elif isinstance(value, str):
+ elif isinstance(value, str) or isinstance(value, unicode):
if value.startswith('0x'):
b = int(value, 16)
else:
@@ -126,7 +126,9 @@
b = value + "00"
else:
if not isinstance(value, str):
- raise ValueError("field %s value type not string" % n)
+ if not isinstance(value, unicode):
+ raise ValueError("field %s value type not string" % n)
+ value = value.encode("utf-8")
if t == IFT_DATETIME:
l *= 2
elif value.startswith('0x'):
@@ -148,6 +150,11 @@
return self.value[n]
raise ValueError("Field %s not specified" % n)
+ def has_field(self, index):
+ field_def = self.get_field_def(index)
+ n = field_def[5]
+ return n in self.value
+
def encode_bitmap(self, bitmap):
buffer = []
offset = 0
@@ -322,8 +329,8 @@
#else:
# value = temp
value = self.buffer_2_string(data[offset:endpos], ftype)
- if fflag != 2 and ftype == IFT_BUFFER:
- value = '0x' + codecs.encode(value, 'hex')
+ # if fflag != 2 and ftype == IFT_BUFFER:
+ # value = '0x' + codecs.encode(value, 'hex')
self.set(findex, value)
#print "parse field[%s] OK =====" % fname
offset = endpos
@@ -355,7 +362,7 @@
raise ValueError("input data type error")
def get_2byte_int(self, data):
- return struct.unpack(">H", data)[0]
+ return struct.unpack("<H", data)[0]
def get_3byte_int(self, data):
temp = data + chr(0)
@@ -364,6 +371,17 @@
def get_4byte_int(self, data):
return struct.unpack("<I", data)[0]
+ def get_byte_int(self, data, signed=False):
+ dl = len(data)
+ if dl <= 0 or dl > 4:
+ raise ValueError(u"Data length must between 1 and 4")
+ if dl < 4:
+ data = data + chr(0) * (4 - dl)
+ if signed:
+ return struct.unpack('<i', data)[0]
+ else:
+ return struct.unpack('<I', data)[0]
+
def set_2byte_int(self, data):
return struct.pack('<H', data)