Refactor code to be more readable #46
@@ -1,110 +1,68 @@
|
|||||||
from . import config
|
from . import config
|
||||||
from . import messages as msg
|
from . import messages as msg
|
||||||
#from . import convert
|
|
||||||
#from .DeviceData import DeviceData
|
|
||||||
#from .DeviceInfo import DeviceInfo
|
|
||||||
#from .InstantDemand import InstantDemand
|
|
||||||
#from .Reading import Reading
|
|
||||||
#from .Total import Total
|
|
||||||
import defusedxml.ElementTree as ET
|
import defusedxml.ElementTree as ET
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
#==========================================================================
|
|
||||||
def all():
|
def all():
|
||||||
# Newlines are required
|
|
||||||
xmlCmd = "<LocalCommand>\n<Name>get_device_data</Name>\n" \
|
xmlCmd = "<LocalCommand>\n<Name>get_device_data</Name>\n" \
|
||||||
"<MacId>%s</MacId>\n</LocalCommand>\n" % ( config.macAddress )
|
"<MacId>%s</MacId>\n</LocalCommand>\n" % config.macAddress
|
||||||
xmlData = sendXml(xmlCmd)
|
xmlData = sendXml(xmlCmd)
|
||||||
|
|
||||||
# Add fake wrapper for parsing list of elements
|
|
||||||
xmlData = "<root>%s</root>" % xmlData
|
xmlData = "<root>%s</root>" % xmlData
|
||||||
root = ET.fromstring(xmlData)
|
root = ET.fromstring(xmlData)
|
||||||
|
return msg.DeviceData(root)
|
||||||
|
|
||||||
return DeviceData( root )
|
|
||||||
|
|
||||||
#==========================================================================
|
|
||||||
def device():
|
def device():
|
||||||
# Newlines are required
|
|
||||||
xmlCmd = "<LocalCommand>\n<Name>list_devices</Name>\n</LocalCommand>\n"
|
xmlCmd = "<LocalCommand>\n<Name>list_devices</Name>\n</LocalCommand>\n"
|
||||||
xmlData = sendXml(xmlCmd)
|
xmlData = sendXml(xmlCmd)
|
||||||
root = ET.fromstring(xmlData)
|
root = ET.fromstring(xmlData)
|
||||||
|
|
||||||
return msg.DeviceInfo(root)
|
return msg.DeviceInfo(root)
|
||||||
|
|
||||||
#==========================================================================
|
|
||||||
def instant():
|
def instant():
|
||||||
# Newlines are required
|
|
||||||
xmlCmd = "<LocalCommand>\n<Name>get_instantaneous_demand</Name>\n" \
|
xmlCmd = "<LocalCommand>\n<Name>get_instantaneous_demand</Name>\n" \
|
||||||
"<MacId>%s</MacId>\n</LocalCommand>\n" % ( config.macAddress )
|
"<MacId>%s</MacId>\n</LocalCommand>\n" % config.macAddress
|
||||||
xmlData = sendXml(xmlCmd)
|
xmlData = sendXml(xmlCmd)
|
||||||
root = ET.fromstring(xmlData)
|
root = ET.fromstring(xmlData)
|
||||||
|
|
||||||
return msg.InstantaneousDemand(root)
|
return msg.InstantaneousDemand(root)
|
||||||
|
|
||||||
#==========================================================================
|
|
||||||
def history(start):
|
def history(start):
|
||||||
"start == datetime in utc"
|
|
||||||
startHex = convert.fromTime(start)
|
startHex = convert.fromTime(start)
|
||||||
|
|
||||||
# Newlines are required
|
|
||||||
xmlCmd = "<LocalCommand>\n<Name>get_history_data</Name>\n" \
|
xmlCmd = "<LocalCommand>\n<Name>get_history_data</Name>\n" \
|
||||||
"<MacId>%s</MacId>\n<StartTime>%s</StartTime>\n" \
|
"<MacId>%s</MacId>\n<StartTime>%s</StartTime>\n" \
|
||||||
"</LocalCommand>\n" % (config.macAddress, startHex)
|
"</LocalCommand>\n" % (config.macAddress, startHex)
|
||||||
xmlData = sendXml(xmlCmd)
|
xmlData = sendXml(xmlCmd)
|
||||||
|
|
||||||
# Add fake wrapper for parsing list of elements
|
|
||||||
root = ET.fromstring(xmlData)
|
root = ET.fromstring(xmlData)
|
||||||
|
return [msg.Total(child) for child in root]
|
||||||
|
|
||||||
return [ Total( child ) for child in root ]
|
|
||||||
|
|
||||||
#==========================================================================
|
|
||||||
def instantHistory(interval):
|
def instantHistory(interval):
|
||||||
"interval = 'hour', 'day', 'week'"
|
assert interval in ['hour', 'day', 'week']
|
||||||
assert( interval in [ 'hour', 'day', 'week' ] )
|
|
||||||
|
|
||||||
# Newlines are required
|
|
||||||
xmlCmd = "<LocalCommand>\n<Name>get_demand_values</Name>\n" \
|
xmlCmd = "<LocalCommand>\n<Name>get_demand_values</Name>\n" \
|
||||||
"<MacId>%s</MacId>\n</LocalCommand>\n" % ( config.macAddress )
|
"<MacId>%s</MacId>\n</LocalCommand>\n" % config.macAddress
|
||||||
xmlData = sendXml(xmlCmd)
|
xmlData = sendXml(xmlCmd)
|
||||||
|
|
||||||
# Add fake wrapper for parsing list of elements
|
|
||||||
xmlData = "<root>%s</root>" % xmlData
|
xmlData = "<root>%s</root>" % xmlData
|
||||||
root = ET.fromstring(xmlData)
|
root = ET.fromstring(xmlData)
|
||||||
|
|
||||||
return msg.Reading.xmlToList(root)
|
return msg.Reading.xmlToList(root)
|
||||||
|
|
||||||
#==========================================================================
|
|
||||||
def totalHistory(interval):
|
def totalHistory(interval):
|
||||||
"interval = 'day', 'week', 'month', 'year'"
|
assert interval in ['day', 'week', 'month', 'year']
|
||||||
assert( interval in [ 'day', 'week', 'month', 'year' ] )
|
|
||||||
|
|
||||||
# Newlines are required
|
|
||||||
xmlCmd = "<LocalCommand>\n<Name>get_summation_values</Name>\n" \
|
xmlCmd = "<LocalCommand>\n<Name>get_summation_values</Name>\n" \
|
||||||
"<MacId>%s</MacId>\n</LocalCommand>\n" % ( config.macAddress )
|
"<MacId>%s</MacId>\n</LocalCommand>\n" % config.macAddress
|
||||||
xmlData = sendXml(xmlCmd)
|
xmlData = sendXml(xmlCmd)
|
||||||
|
|
||||||
# Add fake wrapper for parsing list of elements
|
|
||||||
xmlData = "<root>%s</root>" % xmlData
|
xmlData = "<root>%s</root>" % xmlData
|
||||||
root = ET.fromstring(xmlData)
|
root = ET.fromstring(xmlData)
|
||||||
|
|
||||||
return msg.Reading.xmlToList(root)
|
return msg.Reading.xmlToList(root)
|
||||||
|
|
||||||
#==========================================================================
|
|
||||||
def sendXml(xmlCmd):
|
def sendXml(xmlCmd):
|
||||||
sock = socket.create_connection((config.host, config.port))
|
sock = socket.create_connection((config.host, config.port))
|
||||||
try:
|
try:
|
||||||
sock.send( xmlCmd )
|
sock.send(xmlCmd.encode())
|
||||||
|
|
||||||
buf = ""
|
buf = b""
|
||||||
while True:
|
while True:
|
||||||
s = sock.recv(1024)
|
s = sock.recv(1024)
|
||||||
if not s:
|
if not s:
|
||||||
break
|
break
|
||||||
|
|
||||||
buf += s
|
buf += s
|
||||||
finally:
|
finally:
|
||||||
sock.close()
|
sock.close()
|
||||||
|
|
||||||
return buf
|
return buf.decode()
|
||||||
|
|
||||||
#==========================================================================
|
|
||||||
Reference in New Issue
Block a user