FORUM CTRLX AUTOMATION
ctrlX World Partner Apps for ctrlX AUTOMATION
Dear Community User! We have started the migration process.
This community is now in READ ONLY mode.
Read more: Important
information on the platform change.
4 weeks ago
Hello @CodeShepherd
Hello all we have been trying to get the complex subscription working, following the guide. but
im able to create the complex subscribe node where i get the code 201 but if i then try to execute a get like the examplei get nothing back. Thanks for you help
With kind regards,
David Maenen
with kinde regards,
David
Solved! Go to Solution.
4 weeks ago - last edited 4 weeks ago
As mentioned in the the how to "How to subscribe to nodes of the ctrlX Data Layer using REST commands via CURL" you will receive a SSE (server sent event) response, so you also have to use a library supporting such kind of communication. As the screenshots are quite small and seems not to include information about the used library to do the the GET it is hard to tell what is going on.
Please also beware that unused subscription rule sets are automatically removed after a 60s timeout.
4 weeks ago
Hello @CodeShepherd ,
Thanks for the fast reply we are using the API requests "import requests" Hereby the code for the payload and the post. And for the .get "Motion_sub = s.get(BASE_URL + '/automation/api/v2/events/subID')". Yes we are of the fact that after 60s they get removed if unused.
Thanks for the help.
# Define the payload
payload = {
"properties": {
"id": "subID",
"keepaliveInterval": 60000,
"publishInterval": 1000,
"rules": [{
"rule_type": "Queueing",
"rule": {
"queueSize": 10
}
},
{
"rule_type": "Sampling",
"rule": {
"samplingInterval": 100000
}
},
{
"rule_type": "ChangeEvents",
"rule": {
"valueChange": "StatusValueTimestamp"
}
}
]
},
"nodes": ["framework/metrics/system/memavailable-mb"
]
}
# Make the POST request
motion_info = s.post(BASE_URL + '/automation/api/v2/events?format=json', json=payload)
# Check the response for the POST request
if motion_info.status_code == 201:
response_data = motion_info.json # Call the method to get JSON data
print(response_data) # Print the response data
else:
print(f"Error: {motion_info.status_code} - {motion_info.text}")
4 weeks ago
@David4 ,
Like @CodeShepherd said, you need a client that supports SSE. Python requests does not. See sseclient-py as an example.
Here is a modification of your code above.
# Make the POST request
motion_info = requests.post(url, json=payload, headers=head, verify=False)
# Check the response for the POST request
if motion_info.status_code == 201:
response_data = motion_info.json # Call the method to get JSON data
print(response_data) # Print the response data
else:
print(f"Error: {motion_info.status_code} - {motion_info.text}")
url = "https://10.0.2.2:8443/automation/api/v2/events/subID"
stream_response = requests.get(url, headers=head, verify=False, stream=True)
client = sseclient.SSEClient(stream_response)
# Loop forever (while connection "open")
for event in client.events():
print ("got a new event from server")
pprint.pprint(event.data)
4 weeks ago
Hello @Sgilk and @CodeShepherd
Thank for the help
4 weeks ago
Hi @Sgilk
Thanks for the help i do still have the problem where it gets stuck on the .get i think it has somthing to do with where it still uses the import reqeust lib. Again thanks for all the help for some context im still a student so still learning allot in this field so thanks for your patience.
import sseclient
import pprint
import ctypes
import json
import numpy as np
import time
from matplotlib import pyplot as plt
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
import requests
BASE_URL = "https://192.168.1.1"
USERNAME = "boschrexroth"
PASSWORD = "boschrexroth"
head = {'Content-Type': 'application/json'}
creds = {"name":USERNAME, "password": PASSWORD}
# sys.excepthook = customlogger.handle_exception
kernel32 = ctypes.windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-10), 439) #disable the quick edit function of terminal
access_token = None
################### Step 1 ##################### functions
with requests.post(BASE_URL + '/identity-manager/api/v2/auth/token',headers = head, verify= False, json=creds) as response:
data = json.loads(response.text)
token = data['access_token']
# 0.1 - authorization
head = {"accept": "application/json", 'Authorization':'Bearer {}'.format(token)}
payload = {
"properties": {
"id": "subID",
"keepaliveInterval": 60000,
"publishInterval": 1000,
"rules": [{
"rule_type": "Queueing",
"rule": {
"queueSize": 10
}
},
{
"rule_type": "Sampling",
"rule": {
"samplingInterval": 1000
}
},
{
"rule_type": "ChangeEvents",
"rule": {
"valueChange": "StatusValueTimestamp"
}
}
]
},
"nodes": ["motion/axs/X/state/values/actual/pos"
]
}
# Make the POST request
motion_info = requests.post(BASE_URL + '/automation/api/v2/events?format=json', json=payload,headers = head, verify = False)
# Check the response for the POST request
if motion_info.status_code == 201:
response_data = motion_info.json # Call the method to get JSON data
print(response_data) # Print the response data
else:
print(f"Error: {motion_info.status_code} - {motion_info.text}")
motion_sub = requests.get(BASE_URL + '/automation/api/v2/events/subID',headers = head, stream = True, verify= False)
client = sseclient.SSEClient(motion_sub)
if motion_sub.status_code == 200:
print(motion_sub.json()) # Call the method to get JSON data
else:
print(f"Error: {motion_sub.status_code} - {motion_sub.text}")
for event in client.events():
pprint.pprint(event.data)
4 weeks ago
@David4 ,
Can you please post the error messages you are seeing? Maybe you can debug and set a breakpoint on the problem line so you can step over it and view more information.
4 weeks ago
Thats the strange part. the post i get the message 201 so completed and then with the .get it dousnt give a error because it runs on the .get and dousnt move forward to the next line. Even if i run it via the line for line debug tool it stops at the .get and dousnt want to move forward and eventually it crashes.
4 weeks ago
and the strange part is that if i for example remove the headers so it dousnt have authentication it dous give the 401 so it is talking with the datalayer and on the correct position as well. same goes for if i type the path wrong
4 weeks ago
@David4 ,
Your problem is with parsing the response of the motion subscription as json.
#print(motion_sub.json()) # INCORRECT
print(motion_sub.json) # CORRECT
4 weeks ago
Hi, yea that was the problem thanks for you help i completely overlooked it. Stupid because i had the same iseu a while back where i programmed json() instead of json.