Skip to content

Instantly share code, notes, and snippets.

@wiesson
Last active December 21, 2015 06:48
Show Gist options
  • Save wiesson/6266478 to your computer and use it in GitHub Desktop.
Save wiesson/6266478 to your computer and use it in GitHub Desktop.
Simple MTConnect sample reader
#!/usr/bin/pyton
import sys, os
import thread, threading
import requests
from lxml import objectify, etree
import dateutil.parser
from datetime import datetime, date
import time
line_storage = []
url = 'http://127.0.0.1:5000/sample'
payload = { 'interval': '1000', 'count':'50' }
class request(threading.Thread):
def __init__(self, thread_id, thread_name):
super(request, self).__init__()
self._stop = threading.Event()
self.thread_id = thread_id
self.name = thread_name
self.timer_status = time.time()
def run(self):
print "Starting thread " + self.name
self.mt_request()
def stop(self):
print "Stopping thread " + self.name
print "Elapsed Time: %ss for %s items" % (round(time.time() - self.timer_status,3), len(line_storage))
self.timer_status = time.time()
self._stop.set()
def stopped(self):
if self._stop.isSet():
return True
def mt_request(self):
self.timer_status = time.time()
xml = xml_builder()
requests_stream = requests.get(url, params=payload, stream=True)
for line in requests_stream.iter_lines():
if line:
xml_payload = xml.payload(line)
if xml_payload is not None:
line_storage.append(xml_payload)
# print line_storage[-1]
if self.stopped():
if line_storage[-1].endswith("</MTConnectStreams>"):
break
class xml_builder():
def __init__ (self):
self._buffer = []
def payload(self, raw_stream):
self._raw = raw_stream
filter_namespace = True
if filter_namespace:
if self._raw.startswith('<MTConnectStreams'):
self._raw = '<MTConnectStreams>'
if self._raw.startswith('<MTConnectAssets'):
self._raw = ' <MTConnectAssets> '
if self._raw.startswith('<MTConnectDevices'):
self._raw = ' <MTConnectDevices> '
# raw feed has to be separated into the header and the body
if self._raw.startswith('--'):
self._raw = ""
if self._raw.startswith('Content'):
self._raw = ""
else:
self._buffer.append(self._raw)
if self._raw.endswith('</MTConnectStreams>'):
body = ''.join(self._buffer)
self._buffer = [] # clear buffer
try:
return str(etree.tostring(objectify.fromstring(body), pretty_print=True))
except:
pass
if __name__ == '__main__':
command_list = ["start", "stop", "time", "buffer", "print" , "clear", "exit", "help"]
print "Available commands: ", command_list
while True:
todo = raw_input("> ")
if todo == 'start':
request_thread = request(1, "Request stream")
request_thread.start()
if todo == 'stop':
if request_thread.stopped():
print "Request is not active!"
else:
request_thread.stop()
request_thread.stopped()
if todo == 'test':
buffer = []
for el in line_storage:
xml = objectify.fromstring(el)
try:
for event in xml.Streams.DeviceStream.ComponentStream.Events.iterchildren():
if event.tag == 'Availability':
# print dateutil.parser.parse(event.attrib['timestamp'], ignoretz=True), "-", event.text, event.attrib['sequence']
if str(event.text) == 'AVAILABLE':
if 'AVAILABLE' not in buffer:
buffer.append(event.text)
buffer.append(event.attrib['timestamp'])
if str(event.text) == 'UNAVAILABLE':
if 'AVAILABLE' in buffer:
buffer.append(event.text)
buffer.append(event.attrib['timestamp'])
time_start = dateutil.parser.parse(buffer[1], ignoretz=True)
time_stop = dateutil.parser.parse(buffer[3], ignoretz=True)
print "uptime: ", time_stop.date(), " ", time_stop - time_start
# print buffer
buffer = []
except:
pass
if todo == 'cond':
buffer_cond = []
for el in line_storage:
xml = objectify.fromstring(el)
try:
for e in xml.Streams.DeviceStream.ComponentStream.Condition.iterchildren():
attributes = e.attrib
if attributes['type'] == 'SYSTEM':
print etree.tostring(e, pretty_print=True)
if e.tag == 'Availability' or e.tag == 'Execution':
# print str(e.attrib['timestamp']), "\t", str(e.text), "\t\t", str(e.attrib['sequence'])
if str(e.text) == 'AVAILABLE':
if 'AVAILABLE' not in buffer_cond:
buffer_cond.append(e.text)
buffer_cond.append(e.attrib['timestamp'])
if str(e.text) == 'UNAVAILABLE':
buffer_cond.append(e.text)
buffer_cond.append(e.attrib['timestamp'])
time_start = id.parse_date(buffer_cond[1])
time_stop = id.parse_date(buffer_cond[3])
print "uptime: ", time_stop - time_start
print buffer_cond
buffer_cond = []
except:
pass
if todo == 'buffer':
print "Items in buffer: ", len(line_storage)
if todo == 'print':
for item in line_storage:
print item
if todo == 'clear':
print "Clearing buffer"
line_storage = []
if todo == 'help' or todo not in command_list:
print "Available commands: ", command_list
if todo == 'exit':
print "Goodbye :)"
os._exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment