Sending data from a .txt file to another EG

If you have a question or need help, this is the place to be.
Post Reply
Zohan
Posts: 13
Joined: Sat Jul 06, 2019 2:08 am

Sending data from a .txt file to another EG

Post by Zohan » Mon Apr 27, 2020 8:39 am

I'm sure EG is capable of doing such task, but no idea on how to really begin.

If it has to be done using a python script, then i would really appreciate it if anyone can help me on that, or show me an example code that i could try to figure out myself, because i have never used python before.


A little more detail of what i want to do:

I want EG on computer 1 to read from a .txt file that was generated by a program and send the contents of the .txt file (not the file itself) to EG on computer 2 and then save it as a new .txt file.

Zohan
Posts: 13
Joined: Sat Jul 06, 2019 2:08 am

Re: Sending data from a .txt file to another EG

Post by Zohan » Wed Apr 29, 2020 7:59 am

I have been searching around about this, so far i came up with:

(Note: I'm doing this with the MQTT brokers plugins installed)


Now as i want to send data from a .txt file from PC1 to PC2

What i did on PC1 is i chose the "Publish a binary file as MQTT message" and i selected the .txt file
And as i understand, this would only send the data of the .txt file when i publish, which is exactly what i want.

And it did indeed send the data from the .txt successfully to PC2 and it shows up on the event log pane.


But i don't know how i could let EG save that particular event to a .txt file

I understand that python scripting is needed, which i already tried something like:

Code: Select all

f = open( 'C:\test.txt', 'w' )
f.write( eg.event.string )
f.close()
It does save an event to the .txt, but it only saves the latest event that has been logged, how can i make it save the event that has been sent by PC1?

And also, the event shows up as MQTT.command.data-i-want-saved
How can only save of what's after MQTT.command. ?

User avatar
kgschlosser
Site Admin
Posts: 5498
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Sending data from a .txt file to another EG

Post by kgschlosser » Wed Apr 29, 2020 2:43 pm

There is TCP Events which is a plugin for EG that can do what you need.
If you like the work I have been doing then feel free to Image

User avatar
kgschlosser
Site Admin
Posts: 5498
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Sending data from a .txt file to another EG

Post by kgschlosser » Wed Apr 29, 2020 2:50 pm

OK this code is almost there..

Code: Select all

f = open( 'C:\test.txt', 'w' )
f.write( eg.event.string )
f.close()

eg.event.string is the event name it's self. I am going to assume that the data is in the payload.

Code: Select all

with open( r'C:\test.txt', 'w' ) as f:
    f.write(eg.event.payload)
There are a couple of changes I made. It is good to use what is called a context when dealing with files. a context is going to start with "with" when the context exists it will close the file for you without you having to call close() explicitly.

The other thing is the addition of the r before the patch string. the r means "raw" we do this because a "\" is an indicator for escape characters. so \n is newline \r\n is a window carriage return and a new line. so we do not want to have python thinking you are trying to use escape codes. the r rells python that a \ is really a backslash and not an escape code.
If you like the work I have been doing then feel free to Image

User avatar
kgschlosser
Site Admin
Posts: 5498
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Sending data from a .txt file to another EG

Post by kgschlosser » Wed Apr 29, 2020 2:51 pm

Before you use that last code example. Lets test and see what the data type of the payload is.

Code: Select all

print type(eg.event.payload)
print eg.event.payload
copy and paste the output from the code above into a forum post.
If you like the work I have been doing then feel free to Image

Zohan
Posts: 13
Joined: Sat Jul 06, 2019 2:08 am

Re: Sending data from a .txt file to another EG

Post by Zohan » Wed Apr 29, 2020 3:25 pm

Thanks for the reply!

Ahh i didn't know about the "Payload" and indeed it was:
MQTT.command.test-123 u"test-123"
<EG2EG macro>
Python Script
<type 'unicode'>
test-123
It saved "test-123" in the .txt file successfully

The only problem remaining is how can i get that macro triggered on the receiving EG?
because the data is always different.

Zohan
Posts: 13
Joined: Sat Jul 06, 2019 2:08 am

Re: Sending data from a .txt file to another EG

Post by Zohan » Wed Apr 29, 2020 3:33 pm

This is how i'm doing it:

- PC1 has a .txt file with the data: test-123

- Data sent to PC2 via "MQTT Binary Message"

- On PC2, I had to add the event manually "MQTT.command.test-123" in the Macro for testing the script

Zohan
Posts: 13
Joined: Sat Jul 06, 2019 2:08 am

Re: Sending data from a .txt file to another EG

Post by Zohan » Wed Apr 29, 2020 4:03 pm

I think i got it!

I just created a new "MQTT Subscription" with a different topic
Added the event name without the payload inside the macro, and it's getting triggered :D

I will keep testing and make sure it's 100% working and will get back to confirm

User avatar
kgschlosser
Site Admin
Posts: 5498
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Sending data from a .txt file to another EG

Post by kgschlosser » Wed Apr 29, 2020 6:43 pm

ok when dealing with events it is always easier to drag and drop the event intop the macro. This way you will always know the event name is correct. So create your macro with your script to write the data. then cause the event to get triggered on the receiving machine.. drag and drop that event from the log into the macro.

it wasn't working because you have the event name incorrect MQTT.command.test-123 is not the event. This is what it probably looks like.
MQTT.command u"test-123"

and this is the event
MQTT.command

and this is the payload
u"test-123"


the payload is for any additional data the plugin may want the user to be able to access. It gets printed out in the log but it is not apart of the event string (name).

When dealing with payloads you almost always will need to use a python command or a python script. I really need to come up with a design for the payloads where a user would be able to access the information contained within the payload without having to know any python code. Payloads are a very important part of automation. I need to come up with a standard layout for payloads. so they would all follow the same data type and then I could design a UI interface so a user would be able to access the information contained without having to know any python code.
If you like the work I have been doing then feel free to Image

Post Reply