Comments (4)
Curl doesn't have builtin support for mixed replace content, but many browsers do. The wikipedia link you sent is the correct reference. I have written software in C#, C, ruby, scala/java, and python to parse these messages.
The question back to you is what are you trying to do? If you can give a little more info I can provide a small util that will probably get you exactly what you want. In the current tools directory there is a module called long_pull.rb that parses the stream and blocks the data correctly. The agent dumper uses this module to dump the events in an adapter format.
The relevant code is:
require './long_pull'
require 'net/http'
client = Net::HTTP.new('agent.mtconnect.org', 80)
path = "/sample?interval=0&count=1000"
puller = LongPull.new(client)
puller.long_pull(path) do |xml|
# do something with the block of xml, this just prints....
puts xml
end
Will be happy to help...
Best,
- Will
from cppagent.
Hey Will,
maybe my approach is wrong. I'm reading the XML output from the Agent with Python to provide single elements with web sockets to a browser. (I tried pycurl). In addition, I'm currently displaying thinks like since when the machine is up, how many parts produced in what kind of time (productive time vs unproductive/idle time) or grab the error messages. It's a science machine that I'm monitoring so, there is no part number or so.
Further, I tried it with: http://docs.python-requests.org/en/latest/user/advanced.html#streaming-requests this works, due to its parsing line by line and I can easily filter --longnumber
and Content-...
Here is my workaround:
import requests
from lxml import objectify, etree
class Client:
def __init__(self):
r = requests.get('http://agent.mtconnectorg/sample?interval=0', stream=True)
self.buffer = ""
for line in r.iter_lines():
self.on_receive(line)
def on_receive(self, data):
if data.startswith("--"):
data = ""
if data.startswith("Content"):
data = ""
else:
self.buffer += data
if data.endswith('</MTConnectStreams>'):
xml = self.buffer
self.buffer = ""
try:
self.root = objectify.fromstring(xml)
# objectify.deannotate(self.root, xsi_nil=True)
except BaseException as myError:
print(myError)
pass
self.header = self.root.Header
self.streams = self.root.Streams
print etree.tostring(self.header, pretty_print=True)
client = Client()
I'm not limited to a programming language, but I personally like python ;) The advantage for me to use these interval=0 is that the Agent acts like its pushing and I can just use easy if-states to do something. I don't need to look every second at the XML output..
Thanks,
Arne
from cppagent.
You may want to check this out:
https://github.com/mtconnect/ros_bridge/tree/master/mtconnect/mtconnect_ros_bridge/scripts
The long pull source is in
https://github.com/mtconnect/ros_bridge/tree/master/mtconnect/mtconnect_ros_bridge/scripts/src
and an example is:
Here's a basic example, though I didn't pull in all the pieces. Check the module above for a complete example:
import urllib2
import httplib
import socket
def xml_get_response(data):
# Unpack data
url, url_port, port, conn, req = data
# Get response from url
rospy.logdebug('Attempting HTTP connection on url: %s:%s\tPort:%s' % (url, url_port, port))
conn.request("GET", req)
response = conn.getresponse()
if response.status != 200:
rospy.logerror("Request failed: %s - %d" % (response.reason, response.status))
sys.exit(0)
else:
rospy.logdebug('Request --> %s, Status --> %s' % (response.reason, response.status))
return response
# ... some __init__ stuff
self.conn = httplib.HTTPConnection('agent.mtconnect.org', 80)
response = xml_get_response(('localhost', 5000, None, self.conn, "/cnc/sample?interval=0&count=1000"))
lp = LongPull(response)
lp.long_pull(callback)
def callback(self, xml)
# do something with xml
from cppagent.
Thanks a lot! This is really nice and it was already there. I should look in more detail what you guys are posting.
Further I would suggest a faster method to enable cross-origin resource sharing than changing outgoing.headers["Access-Control-Allow-Origin"] = “*”;
below https://github.com/mtconnect/cppagent/blob/master/agent/agent.cpp#L232
from cppagent.
Related Issues (20)
- Change the schema version for the stylesheets to match the version of the schema being used for the agent.
- Make 'sender' configurable to support docker HOT 2
- Add * uuid: to the README and make sure all the other adapter commands are documented HOT 5
- `device` REST param does not work HOT 9
- Unclear logging at startup when using Docker HOT 1
- Ubuntu 20.04: Error building test package HOT 25
- * uuid command handling in agent not as expected HOT 19
- AgentConfiguration::monitorThread message reports wrong number of seconds
- Crash when editing device dataitem in DeviceModel HOT 4
- XML Schema validation with lxml HOT 1
- Simple WebSockets for subscriptions HOT 3
- Add validation transform to support 2.5 quality attribute for observations.
- MQTT INGRESS connection not working on V2.3.0.4 HOT 5
- How to set initial dataitem value in the stream HOT 2
- Agent Reconnecting to Broker repeatedly HOT 20
- Condition ID format and example not matching on Agent version 2.3? HOT 25
- DELETEing an already deleted asset responds with old asset value; expected MTConnectError HOT 10
- Check why asset counts are not being reported in agent HOT 3
- json schema for agent cfg HOT 8
- Add pretty handling to assets
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cppagent.