leancpms-cli 0.1.7__tar.gz → 0.1.8__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: leancpms-cli
3
- Version: 0.1.7
3
+ Version: 0.1.8
4
4
  Summary: The cpms command alone: a client of a LeanCPMS instance's JSON API
5
5
  License-Expression: AGPL-3.0-or-later
6
6
  License-File: LICENSE
@@ -36,7 +36,7 @@ cpms --help
36
36
  ```
37
37
 
38
38
  - **Upgrade:** `uv tool upgrade leancpms-cli`. To pin a version:
39
- `uv tool install leancpms-cli==0.1.7`.
39
+ `uv tool install leancpms-cli==0.1.8`.
40
40
  - **pipx** works too, if Python 3.14 is already installed:
41
41
  `pipx install leancpms-cli`.
42
42
  - Do not install it where the LeanCPMS server is installed: the server
@@ -98,6 +98,41 @@ carried charging current in the quarter (the most at once, at least 1 A,
98
98
  as the box measured it; empty when the box reported no currents in it). A session's quarters sum to
99
99
  exactly its `wh`. Energy only: there are no prices in it.
100
100
 
101
+ **When a driver is in trouble at a station**
102
+
103
+ ```bash
104
+ cpms chargepoints unlock cp1 cp1-2 -m "the driver's cable is stuck" # stop the charge, open the lock
105
+ cpms chargepoints restart cp1 -m "cp1-2 is faulted" # restart the box (soft)
106
+ cpms chargepoints restart cp1 --hard -m "a soft restart did not help"
107
+ cpms events --chargepoint SIM-0001 --since 1234 # what the box did afterwards
108
+ ```
109
+
110
+ The box is named by its key and the outlet by its key, the `CP` and
111
+ `OUTLET` columns of `cpms`. Both commands need a `write` key, act at once
112
+ and want a reason (`-m`), which is kept with who did it and when. They
113
+ come with leancpms-cli 0.1.8. The portal has the same two
114
+ as buttons on its overview.
115
+
116
+ - **`unlock`** is for a cable that is stuck. It asks the box to stop the
117
+ charge on that outlet, if one is running, and then to open the lock. The
118
+ session ends when the box reports its stop, with its last meter value,
119
+ and is paid for like any other session. The box answers `Unlocked`;
120
+ `UnlockFailed`, when it tried and the lock did not open (try again, then
121
+ restart the box); or `NotSupported`, when the outlet has no lock the box
122
+ can open. Anything but `Unlocked` exits 1.
123
+ - **`restart`** restarts the whole box, every outlet on it. A soft restart
124
+ asks the box to end its sessions first; `--hard` restarts it at once and
125
+ is for when a soft one did not help. If a session is running on any
126
+ outlet, the command names it and does nothing; `--interrupt` restarts
127
+ anyway. An interrupted session ends when the box reports its stop. If it
128
+ is still running when the box is back, close it:
129
+ `cpms session SIM-0001/42 close -m "…"`. A restart does not cure a cable
130
+ that did not lock: the driver pulls the plug and pushes it in again.
131
+ - **When the box says nothing.** "is not connected": nothing was sent.
132
+ "did not answer in time": the box may still act on the command, and the
133
+ `cpms events` line the command prints shows what it did.
134
+ - `--site` says which box when the same key is used at two sites.
135
+
101
136
  **Who is a member, and with which role**
102
137
 
103
138
  ```bash
@@ -259,7 +294,10 @@ environment; unset it.
259
294
  - `cpms manifest apply` never prompts without a TTY. It prints the plan
260
295
  and exits 2 unless you pass `--yes`. Show the plan to a person first,
261
296
  then apply with `--yes -m "why"`.
262
- - `session … stop|close` acts at once, with no confirmation step.
297
+ - `session … stop|close` and `chargepoints restart|unlock` act at once,
298
+ with no confirmation step. `restart` refuses (exit 1) while a session is
299
+ running on the box unless you pass `--interrupt`; with `--json` the
300
+ refusal lists the sessions.
263
301
  - Pass the key in the environment or the config file, never on the
264
302
  command line or in a transcript.
265
303
 
@@ -269,7 +307,7 @@ Raise `version` in both `cli/pyproject.toml` and the root `pyproject.toml`
269
307
  (a test keeps them equal), then tag and push:
270
308
 
271
309
  ```bash
272
- git tag cli-v0.1.7 && git push origin cli-v0.1.7
310
+ git tag cli-v0.1.8 && git push origin cli-v0.1.8
273
311
  ```
274
312
 
275
313
  `.github/workflows/release-cli.yml` builds the wheel and sdist, smoke-tests
@@ -24,7 +24,7 @@ cpms --help
24
24
  ```
25
25
 
26
26
  - **Upgrade:** `uv tool upgrade leancpms-cli`. To pin a version:
27
- `uv tool install leancpms-cli==0.1.7`.
27
+ `uv tool install leancpms-cli==0.1.8`.
28
28
  - **pipx** works too, if Python 3.14 is already installed:
29
29
  `pipx install leancpms-cli`.
30
30
  - Do not install it where the LeanCPMS server is installed: the server
@@ -86,6 +86,41 @@ carried charging current in the quarter (the most at once, at least 1 A,
86
86
  as the box measured it; empty when the box reported no currents in it). A session's quarters sum to
87
87
  exactly its `wh`. Energy only: there are no prices in it.
88
88
 
89
+ **When a driver is in trouble at a station**
90
+
91
+ ```bash
92
+ cpms chargepoints unlock cp1 cp1-2 -m "the driver's cable is stuck" # stop the charge, open the lock
93
+ cpms chargepoints restart cp1 -m "cp1-2 is faulted" # restart the box (soft)
94
+ cpms chargepoints restart cp1 --hard -m "a soft restart did not help"
95
+ cpms events --chargepoint SIM-0001 --since 1234 # what the box did afterwards
96
+ ```
97
+
98
+ The box is named by its key and the outlet by its key, the `CP` and
99
+ `OUTLET` columns of `cpms`. Both commands need a `write` key, act at once
100
+ and want a reason (`-m`), which is kept with who did it and when. They
101
+ come with leancpms-cli 0.1.8. The portal has the same two
102
+ as buttons on its overview.
103
+
104
+ - **`unlock`** is for a cable that is stuck. It asks the box to stop the
105
+ charge on that outlet, if one is running, and then to open the lock. The
106
+ session ends when the box reports its stop, with its last meter value,
107
+ and is paid for like any other session. The box answers `Unlocked`;
108
+ `UnlockFailed`, when it tried and the lock did not open (try again, then
109
+ restart the box); or `NotSupported`, when the outlet has no lock the box
110
+ can open. Anything but `Unlocked` exits 1.
111
+ - **`restart`** restarts the whole box, every outlet on it. A soft restart
112
+ asks the box to end its sessions first; `--hard` restarts it at once and
113
+ is for when a soft one did not help. If a session is running on any
114
+ outlet, the command names it and does nothing; `--interrupt` restarts
115
+ anyway. An interrupted session ends when the box reports its stop. If it
116
+ is still running when the box is back, close it:
117
+ `cpms session SIM-0001/42 close -m "…"`. A restart does not cure a cable
118
+ that did not lock: the driver pulls the plug and pushes it in again.
119
+ - **When the box says nothing.** "is not connected": nothing was sent.
120
+ "did not answer in time": the box may still act on the command, and the
121
+ `cpms events` line the command prints shows what it did.
122
+ - `--site` says which box when the same key is used at two sites.
123
+
89
124
  **Who is a member, and with which role**
90
125
 
91
126
  ```bash
@@ -247,7 +282,10 @@ environment; unset it.
247
282
  - `cpms manifest apply` never prompts without a TTY. It prints the plan
248
283
  and exits 2 unless you pass `--yes`. Show the plan to a person first,
249
284
  then apply with `--yes -m "why"`.
250
- - `session … stop|close` acts at once, with no confirmation step.
285
+ - `session … stop|close` and `chargepoints restart|unlock` act at once,
286
+ with no confirmation step. `restart` refuses (exit 1) while a session is
287
+ running on the box unless you pass `--interrupt`; with `--json` the
288
+ refusal lists the sessions.
251
289
  - Pass the key in the environment or the config file, never on the
252
290
  command line or in a transcript.
253
291
 
@@ -257,7 +295,7 @@ Raise `version` in both `cli/pyproject.toml` and the root `pyproject.toml`
257
295
  (a test keeps them equal), then tag and push:
258
296
 
259
297
  ```bash
260
- git tag cli-v0.1.7 && git push origin cli-v0.1.7
298
+ git tag cli-v0.1.8 && git push origin cli-v0.1.8
261
299
  ```
262
300
 
263
301
  `.github/workflows/release-cli.yml` builds the wheel and sdist, smoke-tests
@@ -114,7 +114,94 @@ def _key_column(r: dict[str, Any]) -> str:
114
114
  return f"{state} (refused: {live['refused']})" if live.get("refused") else state
115
115
 
116
116
 
117
+ def _box_action(a: argparse.Namespace) -> tuple[str, dict[str, Any]]:
118
+ """What `chargepoints restart|unlock` need beside the verb: the box, and a reason."""
119
+ if not a.key:
120
+ raise UsageError(f"{a.verb} needs a charge point key: cpms chargepoints {a.verb} <key>"
121
+ f"{' <outlet>' if a.verb == 'unlock' else ''} -m \"why\"")
122
+ if a.verb == "unlock" and not a.outlet:
123
+ raise UsageError("unlock needs an outlet key: cpms chargepoints unlock <key> <outlet> -m \"why\"")
124
+ if not a.message or not a.message.strip():
125
+ raise UsageError(f"{a.verb} needs a reason: -m \"why\"")
126
+ return a.message, {"organization": a.org, "site": a.site}
127
+
128
+
129
+ def _events_hint(r: dict[str, Any]) -> str:
130
+ return f"what the box does now: cpms events --chargepoint {r['ocpp_id']} --since {r['events_since']}"
131
+
132
+
133
+ def cmd_restart(api: Api, a: argparse.Namespace) -> int:
134
+ reason, where = _box_action(a)
135
+ body = {"reason": reason, "type": "hard" if a.hard else "soft", "interrupt": a.interrupt}
136
+ try:
137
+ r = api.call("POST", f"/chargepoints/{a.key}/restart", params=where, json=body)
138
+ except ApiError as e:
139
+ running = e.detail.get("sessions") if e.status == 409 and isinstance(e.detail, dict) else None
140
+ if not running:
141
+ raise
142
+ if a.json:
143
+ _dump({"error": str(e), "detail": e.detail})
144
+ for s in running:
145
+ _err(f"cpms: a session is running on {a.key}: outlet {s['outlet'] or s['connector']}, "
146
+ f"{s['ocpp_id']}/{s['transaction_id']}, since {_hm(s['started_at'])} UTC, {s['wh']} Wh")
147
+ _err("cpms: not restarted. A restart restarts every outlet on the box and interrupts it: "
148
+ "add --interrupt to restart anyway")
149
+ return 1
150
+ if a.json:
151
+ _dump(r)
152
+ elif r["accepted"]:
153
+ _print(f"{r['charge_point']} ({r['ocpp_id']}): the box answered Accepted to a {r['type'].lower()} restart; "
154
+ "it disconnects and is back within a few minutes")
155
+ for s in r["sessions"]:
156
+ ref = f"{s['ocpp_id']}/{s['transaction_id']}"
157
+ _print(f"interrupted: outlet {s['outlet'] or s['connector']}, {ref}. It ends when the box reports its "
158
+ f"stop; if it still runs when the box is back: cpms session {ref} close -m \"…\"")
159
+ _print(_events_hint(r))
160
+ else:
161
+ _err(f"cpms: {r['charge_point']} ({r['ocpp_id']}) refused the {r['type'].lower()} restart: "
162
+ f"it answered {r['status']}")
163
+ return 0 if r["accepted"] else 1
164
+
165
+
166
+ UNLOCK_REFUSALS = {
167
+ "UnlockFailed": "the box tried and the lock did not open (UnlockFailed). Try again; the next step is a restart "
168
+ "of the box",
169
+ "NotSupported": "the box says this outlet has no lock it can open (NotSupported)",
170
+ }
171
+
172
+
173
+ def cmd_unlock(api: Api, a: argparse.Namespace) -> int:
174
+ reason, where = _box_action(a)
175
+ try:
176
+ r = api.call("POST", f"/chargepoints/{a.key}/outlets/{a.outlet}/unlock", params=where,
177
+ json={"reason": reason})
178
+ except ApiError as e:
179
+ if isinstance(e.detail, dict) and "stop" in e.detail:
180
+ e.detail = None # the box's answer to the stop: the error's text says it already
181
+ raise
182
+ if a.json:
183
+ _dump(r)
184
+ return 0 if r["unlocked"] else 1
185
+ stop = r["stop"]
186
+ if stop and stop.get("error"):
187
+ _print(f"{r['outlet']}: the box answered the stop with an error: {stop['error']}")
188
+ elif stop:
189
+ _print(f"{r['outlet']}: stop requested for {r['ocpp_id']}/{stop.get('transaction_id')} (the box answered "
190
+ f"{stop.get('status')}); the session ends when the box reports it")
191
+ if r["unlocked"]:
192
+ _print(f"{r['outlet']}: unlocked (the box answered Unlocked)")
193
+ else:
194
+ _err(f"cpms: {r['outlet']} is NOT unlocked: "
195
+ f"{UNLOCK_REFUSALS.get(r['status'], 'the box answered ' + str(r['status']))}")
196
+ _print(_events_hint(r))
197
+ return 0 if r["unlocked"] else 1
198
+
199
+
117
200
  def cmd_chargepoints(api: Api, a: argparse.Namespace) -> int:
201
+ if a.verb == "restart":
202
+ return cmd_restart(api, a)
203
+ if a.verb == "unlock":
204
+ return cmd_unlock(api, a)
118
205
  if a.verb == "rekey":
119
206
  if not a.key:
120
207
  raise UsageError("rekey needs a charge point key: cpms chargepoints rekey <key>")
@@ -883,9 +970,20 @@ def parser() -> argparse.ArgumentParser:
883
970
  s.add_argument("--site")
884
971
  s.add_argument("--org", help="operator: one organization")
885
972
 
886
- s = cmd("chargepoints", cmd_chargepoints, "declared charge points with live state [rekey KEY]")
887
- s.add_argument("verb", nargs="?", choices=["rekey"], help="rekey: a new key for a box declared auth: basic")
888
- s.add_argument("key", nargs="?")
973
+ s = cmd("chargepoints", cmd_chargepoints, "declared charge points with live state "
974
+ "[restart KEY -m REASON | unlock KEY OUTLET -m REASON | rekey KEY]")
975
+ s.add_argument("verb", nargs="?", choices=["restart", "unlock", "rekey"],
976
+ help="restart: restart the box, every outlet on it (soft unless --hard). "
977
+ "unlock: stop the charge on an outlet, if one runs, and unlock its cable. "
978
+ "rekey: a new key for a box declared auth: basic")
979
+ s.add_argument("key", nargs="?", help="the charge point's key, the CP column of cpms status")
980
+ s.add_argument("outlet", nargs="?", help="unlock: the outlet's key, the OUTLET column of cpms status")
981
+ s.add_argument("-m", "--message", help="restart, unlock: why, in a few words (kept in the actions journal)")
982
+ s.add_argument("--hard", action="store_true", help="restart: the box restarts at once, without ending its "
983
+ "sessions first; for when a soft restart did not help")
984
+ s.add_argument("--interrupt", action="store_true", help="restart: go ahead although a session is running on "
985
+ "the box")
986
+ s.add_argument("--site", help="restart, unlock: the site, when the key is used at more than one")
889
987
  s.add_argument("--unclaimed", action="store_true", help="operator: boxes nobody has declared")
890
988
  s.add_argument("--org")
891
989
 
@@ -1041,8 +1139,8 @@ def parser() -> argparse.ArgumentParser:
1041
1139
  if name != "remove":
1042
1140
  s.add_argument("--role", choices=["read", "write"], default="read" if name == "add" else None,
1043
1141
  required=name == "set-role",
1044
- help="read (default for add): cpms login gives read keys only; write: may act too. "
1045
- "The portal only reads, whatever the role")
1142
+ help="read (default for add): cpms login gives read keys only; write: may act too, "
1143
+ "and may restart a box and unlock an outlet in the portal")
1046
1144
  return p
1047
1145
 
1048
1146
 
@@ -1052,8 +1150,8 @@ def main(argv: list[str] | None = None) -> int:
1052
1150
  a = parser().parse_args(argv)
1053
1151
  a.json = getattr(a, "json", False)
1054
1152
  fn = getattr(a, "fn", None)
1055
- alone = fn is None
1056
- if alone: # `cpms` alone: the live state
1153
+ if fn is None: # `cpms` alone: the live state
1154
+ alone = True
1057
1155
  a.site = a.org = None
1058
1156
  fn = cmd_status
1059
1157
  if fn in (cmd_validate, cmd_schema, cmd_login): # offline, or a login: no key yet
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "leancpms-cli"
3
- version = "0.1.7"
3
+ version = "0.1.8"
4
4
  description = "The cpms command alone: a client of a LeanCPMS instance's JSON API"
5
5
  readme = "README.md"
6
6
  license = "AGPL-3.0-or-later"
File without changes
File without changes