daasiot_python
cli.py
Go to the documentation of this file.
1import typer
2import pydaasiot
3
4app = typer.Typer(help="DaaS-IoT Python CLI")
5
6# Nodo globale
7node = None
8
9class MyHandler(pydaasiot.IDaasApiEvent):
10 """
11 Gestore eventi per il nodo DaaS-IoT.
12 Tutti i metodi definiti in IDaasApiEvent sono implementati qui
13 e stampano le informazioni ricevute.
14 """
15 def __init__(self, node_ref=None):
16 super().__init__()
17 self.node = node_ref
18
19 def dinAcceptedEvent(self, din: int):
20 print(f"[EVENT] DIN accepted: {din}")
21
22 def ddoReceivedEvent(self, payload_size: int, typeset: int, din: int):
23 print(f"[EVENT] DDO available from DIN {din} (size={payload_size}, typeset={typeset})")
24 if self.node is None:
25 print("[ERROR] No node reference set in handler.")
26 return
27 # Pull immediato del DDO
28 err, ddo = self.node.pull(din)
29 if err == pydaasiot.daas_error_t.ERROR_NONE and ddo:
30 data = ddo.getPayloadAsBinary()
31 print(f"[PULL] Pulled data: {data}")
32 else:
33 print(f"[PULL] Pull failed with error {err}")
34
35 def frisbeeReceivedEvent(self, din: int):
36 print(f"[EVENT] FRISBEE from DIN {din}")
37
38 def nodeStateReceivedEvent(self, din: int):
39 print(f"[EVENT] Node state received for DIN {din}")
40
41 def atsSyncCompleted(self, din: int):
42 print(f"[EVENT] ATS sync completed for DIN {din}")
43
44 def frisbeeDperfCompleted(self, din: int, packets_sent: int, block_size: int):
45 print(f"[EVENT] Frisbee dperf completed for DIN {din} "
46 f"(packets_sent={packets_sent}, block_size={block_size})")
47
48
49@app.command()
50def init(sid: int, din: int, config: str = typer.Option("config.json", help="Path to config file")):
51 """
52 Initialize a DaaS-IoT node with given SID and DIN.
53 """
54 global node
55 handler = MyHandler()
56 node = pydaasiot.DaasWrapper(config, handler)
57 handler.node = node
58 node.doInit(sid, din)
59 typer.echo(f"Node initialized SID={sid} DIN={din}")
60
61@app.command()
62def enable_driver(link: str, uri: str):
63 """
64 Enable a driver on the node.
65
66 Example:
67 enable-driver _LINK_INET4 127.0.0.1:2222
68 """
69 global node
70 if node is None:
71 typer.echo("Node not initialized")
72 raise typer.Exit(1)
73 link_enum = getattr(pydaasiot.link_t, link)
74 node.enableDriver(link_enum, uri)
75 typer.echo(f"Driver enabled: {link} at {uri}")
76
77@app.command()
78def map(din: int, link: str, uri: str):
79 """
80 Map a remote node in the DaaS network.
81 """
82 global node
83 if node is None:
84 typer.echo("Node not initialized")
85 raise typer.Exit(1)
86 link_enum = getattr(pydaasiot.link_t, link)
87 node.map(din, link_enum, uri)
88 typer.echo(f"Mapped remote node DIN={din} at {uri}")
89
90@app.command()
91def perform(mode: str = typer.Option("PERFORM_CORE_THREAD", help="Execution mode")):
92 """
93 Start the internal processing loop.
94 """
95 global node
96 if node is None:
97 typer.echo("Node not initialized")
98 raise typer.Exit(1)
99 mode_enum = getattr(pydaasiot.performs_mode_t, mode)
100 node.doPerform(mode_enum)
101 typer.echo(f"doPerform started with mode {mode}")
102
103@app.command()
104def push(din: int, message: str):
105 """
106 Push a text message to a remote node.
107 """
108 global node
109 if node is None:
110 typer.echo("Node not initialized")
111 raise typer.Exit(1)
112 ddo = pydaasiot.DDO()
113 ddo.setOrigin(din)
114 ddo.setTypeset(1)
115 ddo.allocatePayload(len(message))
116 ddo.appendPayloadData(message.encode())
117 err = node.push(din, ddo)
118 typer.echo(f"Push result: {err}")
119
120@app.command()
121def pull(din: int):
122 """
123 Pull messages manually from a remote node.
124 """
125 global node
126 if node is None:
127 typer.echo("Node not initialized")
128 raise typer.Exit(1)
129 err, ddo = node.pull(din)
130 if err == pydaasiot.daas_error_t.ERROR_NONE and ddo:
131 typer.echo(f"Pulled data: {ddo.getPayloadAsBinary()}")
132 else:
133 typer.echo(f"Pull failed: {err}")
134
135if __name__ == "__main__":
136 app()
ddoReceivedEvent(self, int payload_size, int typeset, int din)
Definition cli.py:22
dinAcceptedEvent(self, int din)
Definition cli.py:19
nodeStateReceivedEvent(self, int din)
Definition cli.py:38
frisbeeReceivedEvent(self, int din)
Definition cli.py:35
atsSyncCompleted(self, int din)
Definition cli.py:41
__init__(self, node_ref=None)
Definition cli.py:15
frisbeeDperfCompleted(self, int din, int packets_sent, int block_size)
Definition cli.py:44
push(int din, str message)
Definition cli.py:104
init(int sid, int din, str config=typer.Option("config.json", help="Path to config file"))
Definition cli.py:50
pull(int din)
Definition cli.py:121
perform(str mode=typer.Option("PERFORM_CORE_THREAD", help="Execution mode"))
Definition cli.py:91
app
Definition cli.py:4
map(int din, str link, str uri)
Definition cli.py:78
enable_driver(str link, str uri)
Definition cli.py:62