Skip to content

Master read classes output to json? #23

@Saleem344

Description

@Saleem344

Hi I'm new to dnp3 protocol I'm reading an outstation using pydnp3 I want to take the required classes output in the program so that I can make the required output format can anyone help me??

my Code :

`
from pydnp3 import asiodnp3, asiopal, opendnp3, openpal
import time

    FILTERS = opendnp3.levels.NORMAL | opendnp3.levels.ALL_COMMS
    HOST = "192.168.0.69"
    LOCAL = "0.0.0.0"
    PORT = 20000


    class TestMaster:

        def config_master(self):
            # Callback interface for log messages
            self.handler = asiodnp3.ConsoleLogger().Create()

            # Root DNP3 object used to create channels and sessions
            self.manager = asiodnp3.DNP3Manager(1, self.handler)

            # Connect via a TCPClient socket to an outstation
            self.channel_listener = asiodnp3.PrintingChannelListener().Create()
            self.channel = self.manager.AddTCPClient("tcpclient",
                                                    FILTERS,
                                                    asiopal.ChannelRetry(),
                                                    HOST,
                                                    LOCAL,
                                                    PORT,
                                                    self.channel_listener)

            # Master config object for a master
            stack_config = asiodnp3.MasterStackConfig()
            stack_config.master.responseTimeout = openpal.TimeDuration().Seconds(2)
            stack_config.link.RemoteAddr = 1
            stack_config.link.LocalAddr = 2

            # Add a master to a communication channel
            self.master_application = asiodnp3.DefaultMasterApplication().Create()
            self.master = self.channel.AddMaster("master",
                                                asiodnp3.PrintingSOEHandler().Create(),
                                                self.master_application,
                                                stack_config)

            # Do an integrity poll (Class 3/2/1/0) once per minute
            self.integrity_scan = self.master.AddClassScan(opendnp3.ClassField().AllClasses(),
                                                        openpal.TimeDuration().Minutes(1))

            # Do a Class 1 exception poll every 5 seconds
            self.exception_scan = self.master.AddClassScan(opendnp3.ClassField(opendnp3.ClassField.CLASS_1),
                                                        openpal.TimeDuration().Seconds(2))

            # Enable the master. This will start communications.
            self.master.Enable()

        def run_master(self, cmd=None):
            self.config_master()


            if cmd == "a":
                self.master.ScanRange(opendnp3.GroupVariationID(1, 2), 0, 3)
            if cmd == "i":
                self.integrity_scan.Demand()
                print("self.integrity_scan.Demand()------",self.integrity_scan.Demand())
            if cmd == "e":
                self.exception_scan.Demand()
                print("self.exception_scan.Demand()-------",self.exception_scan.Demand())
            if cmd == "c1":
                crob = opendnp3.ControlRelayOutputBlock(opendnp3.ControlCode.LATCH_ON)
                self.master.SelectAndOperate(crob, 0, command_callback)
            if cmd == "c2":
                crob = opendnp3.ControlRelayOutputBlock(opendnp3.ControlCode.LATCH_ON)
                commands = opendnp3.CommandSet([opendnp3.WithIndex(crob, 0),opendnp3.WithIndex(crob, 1)])
                self.master.SelectAndOperate(commands, command_callback)
            if cmd == "d1":
               self.master.DirectOperate(opendnp3.ControlRelayOutputBlock(opendnp3.ControlCode.LATCH_ON)
                                                                                                                                          ,4,command_callback)
            if cmd == "d2":
                crob = opendnp3.ControlRelayOutputBlock(opendnp3.ControlCode.LATCH_ON)
                commands = opendnp3.CommandSet([opendnp3.WithIndex(crob, 0),opendnp3.WithIndex(crob, 1)])
                self.master.DirectOperate(commands, command_callback)
            if cmd == "r":
                self.master.Restart(opendnp3.RestartType.COLD, restart_callback)

            
            time.sleep(1)

        

    if __name__ == "__main__":
        app = TestMaster().run_master(cmd="i")`

Output:

(env) root@ubuntu:/home/saleem/Saleem/DNP3_Python/pydnp3# python3 ./examples/test.py ms(1609218902174) INFO manager - Starting thread (0) channel state change: OPENING ms(1609218902175) INFO tcpclient - Connecting to: 192.168.0.69 self.integrity_scan.Demand()------ None ms(1609218902177) INFO tcpclient - Connected to: 192.168.0.69 channel state change: OPEN ms(1609218902177) INFO master - Begining task: Disable Unsolicited ms(1609218902177) --AL-> master - C0 15 3C 02 06 3C 03 06 3C 04 06 ms(1609218902178) --AL-> master - FIR: 1 FIN: 1 CON: 0 UNS: 0 SEQ: 0 FUNC: DISABLE_UNSOLICITED ms(1609218902179) --AL-> master - 060,002 - Class Data - Class 1 - all objects ms(1609218902179) --AL-> master - 060,003 - Class Data - Class 2 - all objects ms(1609218902179) --AL-> master - 060,004 - Class Data - Class 3 - all objects ms(1609218902179) --TL-> master - FIR: 1 FIN: 1 SEQ: 0 LEN: 11 ms(1609218902179) --LL-> master - Function: PRI_UNCONFIRMED_USER_DATA Dest: 1 Source: 2 Length: 12 ms(1609218902188) <-LL-- tcpclient - Function: PRI_UNCONFIRMED_USER_DATA Dest: 2 Source: 1 Length: 10 ms(1609218902188) <-TL-- master - FIR: 1 FIN: 1 SEQ: 7 LEN: 4 ms(1609218902188) <-AL-- master - FIR: 1 FIN: 1 CON: 0 UNS: 0 SEQ: 0 FUNC: RESPONSE IIN: [0x10, 0x01] ms(1609218902188) WARN master - Task was explicitly rejected via response with error IIN bit(s): Disable Unsolicited ms(1609218902188) INFO master - Begining task: Startup Integrity Poll ms(1609218902188) --AL-> master - C1 01 3C 02 06 3C 03 06 3C 04 06 3C 01 06 ms(1609218902188) --AL-> master - FIR: 1 FIN: 1 CON: 0 UNS: 0 SEQ: 1 FUNC: READ ms(1609218902188) --AL-> master - 060,002 - Class Data - Class 1 - all objects ms(1609218902188) --AL-> master - 060,003 - Class Data - Class 2 - all objects ms(1609218902188) --AL-> master - 060,004 - Class Data - Class 3 - all objects ms(1609218902188) --AL-> master - 060,001 - Class Data - Class 0 - all objects ms(1609218902188) --TL-> master - FIR: 1 FIN: 1 SEQ: 1 LEN: 14 ms(1609218902188) --LL-> master - Function: PRI_UNCONFIRMED_USER_DATA Dest: 1 Source: 2 Length: 15 ms(1609218902207) <-LL-- tcpclient - Function: PRI_UNCONFIRMED_USER_DATA Dest: 2 Source: 1 Length: 42 ms(1609218902207) <-TL-- master - FIR: 1 FIN: 1 SEQ: 8 LEN: 36 ms(1609218902207) <-AL-- master - FIR: 1 FIN: 1 CON: 0 UNS: 0 SEQ: 1 FUNC: RESPONSE IIN: [0x10, 0x00] ms(1609218902207) <-AL-- master - 001,002 Binary Input - With Flags, 8-bit start stop [0, 0] ms(1609218902207) <-AL-- master - 030,005 Analog Input - Single-precision With Flag, 8-bit start stop [0, 0] ms(1609218902207) <-AL-- master - 010,002 Binary Output - Output Status With Flags, 8-bit start stop [0, 0] ms(1609218902207) <-AL-- master - 040,003 Analog Output Status - Single-precision With Flag, 8-bit start stop [0, 0] [0] : 1 : 129 : 0 [0] : 55.55 : 1 : 0 [0] : 1 : 129 : 0 [0] : 65.76 : 1 : 0 ms(1609218902207) INFO master - Begining task: Enable Unsolicited ms(1609218902207) --AL-> master - C2 14 3C 02 06 3C 03 06 3C 04 06 ms(1609218902207) --AL-> master - FIR: 1 FIN: 1 CON: 0 UNS: 0 SEQ: 2 FUNC: ENABLE_UNSOLICITED ms(1609218902207) --AL-> master - 060,002 - Class Data - Class 1 - all objects ms(1609218902207) --AL-> master - 060,003 - Class Data - Class 2 - all objects ms(1609218902207) --AL-> master - 060,004 - Class Data - Class 3 - all objects ms(1609218902207) --TL-> master - FIR: 1 FIN: 1 SEQ: 2 LEN: 11 ms(1609218902208) --LL-> master - Function: PRI_UNCONFIRMED_USER_DATA Dest: 1 Source: 2 Length: 12 ms(1609218902228) <-LL-- tcpclient - Function: PRI_UNCONFIRMED_USER_DATA Dest: 2 Source: 1 Length: 10 ms(1609218902229) <-TL-- master - FIR: 1 FIN: 1 SEQ: 9 LEN: 4 ms(1609218902229) <-AL-- master - FIR: 1 FIN: 1 CON: 0 UNS: 0 SEQ: 2 FUNC: RESPONSE IIN: [0x10, 0x01] ms(1609218902229) WARN master - Task was explicitly rejected via response with error IIN bit(s): Enable Unsolicited ms(1609218902229) INFO master - Begining task: Application Poll ms(1609218902229) --AL-> master - C3 01 3C 02 06 3C 03 06 3C 04 06 3C 01 06 ms(1609218902229) --AL-> master - FIR: 1 FIN: 1 CON: 0 UNS: 0 SEQ: 3 FUNC: READ ms(1609218902229) --AL-> master - 060,002 - Class Data - Class 1 - all objects ms(1609218902229) --AL-> master - 060,003 - Class Data - Class 2 - all objects ms(1609218902229) --AL-> master - 060,004 - Class Data - Class 3 - all objects ms(1609218902229) --AL-> master - 060,001 - Class Data - Class 0 - all objects ms(1609218902229) --TL-> master - FIR: 1 FIN: 1 SEQ: 3 LEN: 14 ms(1609218902229) --LL-> master - Function: PRI_UNCONFIRMED_USER_DATA Dest: 1 Source: 2 Length: 15 ms(1609218902247) <-LL-- tcpclient - Function: PRI_UNCONFIRMED_USER_DATA Dest: 2 Source: 1 Length: 42 ms(1609218902247) <-TL-- master - FIR: 1 FIN: 1 SEQ: 10 LEN: 36 ms(1609218902247) <-AL-- master - FIR: 1 FIN: 1 CON: 0 UNS: 0 SEQ: 3 FUNC: RESPONSE IIN: [0x10, 0x00] ms(1609218902247) <-AL-- master - 001,002 Binary Input - With Flags, 8-bit start stop [0, 0] ms(1609218902247) <-AL-- master - 030,005 Analog Input - Single-precision With Flag, 8-bit start stop [0, 0] ms(1609218902247) <-AL-- master - 010,002 Binary Output - Output Status With Flags, 8-bit start stop [0, 0] ms(1609218902247) <-AL-- master - 040,003 Analog Output Status - Single-precision With Flag, 8-bit start stop [0, 0] **[0] : 1 : 129 : 0 [0] : 55.55 : 1 : 0 [0] : 1 : 129 : 0 [0] : 65.76 : 1 : 0** ms(1609218902247) INFO master - Begining task: Application Poll ms(1609218902247) --AL-> master - C4 01 3C 02 06 ms(1609218902247) --AL-> master - FIR: 1 FIN: 1 CON: 0 UNS: 0 SEQ: 4 FUNC: READ ms(1609218902247) --AL-> master - 060,002 - Class Data - Class 1 - all objects ms(1609218902247) --TL-> master - FIR: 1 FIN: 1 SEQ: 4 LEN: 5 ms(1609218902247) --LL-> master - Function: PRI_UNCONFIRMED_USER_DATA Dest: 1 Source: 2 Length: 6 ms(1609218902266) <-LL-- tcpclient - Function: PRI_UNCONFIRMED_USER_DATA Dest: 2 Source: 1 Length: 10 ms(1609218902266) <-TL-- master - FIR: 1 FIN: 1 SEQ: 11 LEN: 4 ms(1609218902266) <-AL-- master - FIR: 1 FIN: 1 CON: 0 UNS: 0 SEQ: 4 FUNC: RESPONSE IIN: [0x10, 0x00] channel state change: CLOSED channel state change: SHUTDOWN

can anyone help me with how can I take the classes output in my program so that I can make the output in my required structure like a dictionary or list?

Thank you for your help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions