Giter VIP home page Giter VIP logo

Comments (4)

wsobel avatar wsobel commented on June 19, 2024

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.

wiesson avatar wiesson commented on June 19, 2024

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.

wsobel avatar wsobel commented on June 19, 2024

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:

https://github.com/mtconnect/ros_bridge/tree/master/mtconnect/mtconnect_ros_bridge/scripts/robot_sim.py

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.

wiesson avatar wiesson commented on June 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.