pymscada 0.2.0rc4__py3-none-any.whl → 0.2.0rc6__py3-none-any.whl

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.

Potentially problematic release.


This version of pymscada might be problematic. Click here for more details.

pymscada/alarms.py CHANGED
@@ -3,8 +3,8 @@ import logging
3
3
  import sqlite3 # note that sqlite3 has blocking calls
4
4
  import socket
5
5
  import time
6
- import atexit
7
6
  from pymscada.bus_client import BusClient
7
+ from pymscada.periodic import Periodic
8
8
  from pymscada.tag import Tag, TYPES
9
9
 
10
10
  ALM = 0
@@ -12,9 +12,32 @@ RTN = 1
12
12
  ACT = 2
13
13
  INF = 3
14
14
 
15
+ KIND = {
16
+ ALM: 'ALM',
17
+ RTN: 'RTN',
18
+ ACT: 'ACT',
19
+ INF: 'INF'
20
+ }
21
+
15
22
  NORMAL = 0
16
23
  ALARM = 1
17
24
 
25
+ """
26
+ Database schema:
27
+
28
+ alarms contains an event log of changes as they occur, this
29
+ includes information on actions taken by the alarm system.
30
+
31
+ CREATE TABLE IF NOT EXISTS alarms (
32
+ id INTEGER PRIMARY KEY ASC,
33
+ date_ms INTEGER,
34
+ alarm_string TEXT,
35
+ kind INTEGER, # one of ALM, RTN, ACT, INF
36
+ desc TEXT,
37
+ group TEXT
38
+ )
39
+ """
40
+
18
41
 
19
42
  def standardise_tag_info(tagname: str, tag: dict):
20
43
  """Correct tag dictionary in place to be suitable for modules."""
@@ -23,6 +46,8 @@ def standardise_tag_info(tagname: str, tag: dict):
23
46
  if 'desc' not in tag:
24
47
  logging.warning(f"Tag {tagname} has no description, using name")
25
48
  tag['desc'] = tag['name']
49
+ if 'area' not in tag:
50
+ tag['area'] = ''
26
51
  if 'multi' in tag:
27
52
  tag['type'] = int
28
53
  else:
@@ -40,51 +65,116 @@ def standardise_tag_info(tagname: str, tag: dict):
40
65
  tag['dp'] = 2
41
66
  if 'units' not in tag:
42
67
  tag['units'] = ''
68
+ if 'alarm' in tag:
69
+ if isinstance(tag['alarm'], str):
70
+ tag['alarm'] = [tag['alarm']]
71
+ if not isinstance(tag['alarm'], list):
72
+ logging.warning(f"Tag {tagname} has invalid alarm {tag['alarm']}")
73
+ del tag['alarm']
74
+
43
75
 
76
+ def split_operator(alarm: str) -> dict:
77
+ """Split alarm string into operator and value."""
78
+ tokens = alarm.split(' ')
79
+ alm_dict = {'for': 0}
80
+ if len(tokens) not in (2, 4):
81
+ raise ValueError(f"Invalid alarm {alarm}")
82
+ if tokens[0] not in ['>', '<', '==', '>=', '<=']:
83
+ raise ValueError(f"Invalid alarm {alarm}")
84
+ alm_dict['operator'] = tokens[0]
85
+ try:
86
+ alm_dict['value'] = float(tokens[1])
87
+ except ValueError:
88
+ raise ValueError(f"Invalid alarm {alarm}")
89
+ if len(tokens) == 4:
90
+ if tokens[2] != 'for':
91
+ raise ValueError(f"Invalid alarm {alarm}")
92
+ try:
93
+ alm_dict['for'] = int(tokens[3])
94
+ except ValueError:
95
+ raise ValueError(f"Invalid alarm {alarm}")
96
+ return alm_dict
44
97
 
45
- class Alarm:
46
- """Manages multiple alarm conditions for a single tag."""
47
- def __init__(self, tag: Tag, conditions: str | list[str]):
98
+
99
+ class Alarm():
100
+ """
101
+ Single alarm class.
102
+
103
+ Alarms are defined by a tag and a condition. Tags may have multiple
104
+ conditions, each combination of tag and condition is a separate Alarm.
105
+
106
+ Monitors tag value through the Tag callback. Tracks in alarm state.
107
+ Generates the ALM and RTN messages for Alarms to publish via rta_tag.
108
+ """
109
+
110
+ def __init__(self, tagname: str, tag: dict, alarm: str, area: str, rta_cb, alarms) -> None:
48
111
  """Initialize alarm with tag and condition(s)."""
49
- if tag.type not in (int, float):
50
- raise ValueError(f"Alarms only supported for numeric types, not {tag.type}")
51
-
52
- self.tag = tag
53
- self.tests: list[tuple[str, callable, float]] = []
54
-
55
- # Handle both string and list conditions
56
- if isinstance(conditions, str):
57
- conditions = [conditions]
58
-
59
- for condition in conditions:
60
- operator_str, value = condition.split(' ')
61
- self.tests.append((
62
- condition,
63
- {
64
- '==': (lambda x, y: x == y),
65
- '<': (lambda x, y: x < y),
66
- '>': (lambda x, y: x > y),
67
- '<=': (lambda x, y: x <= y),
68
- '>=': (lambda x, y: x >= y)
69
- }[operator_str],
70
- float(value)
71
- ))
72
-
73
- def check_conditions(self, in_alarm: set[str]) -> list[tuple[str, bool, float, int]]:
74
- """Check all conditions and return list of changes.
75
- Returns list of (alarm_ref, is_in_alarm, value, timestamp) for changed states."""
76
- changes = []
77
- for condition_str, test, value in self.tests:
78
- alarm_ref = f"{self.tag.name} {condition_str}"
79
- is_in_alarm = test(self.tag.value, value)
80
-
81
- if is_in_alarm and alarm_ref not in in_alarm:
82
- changes.append((alarm_ref, True, self.tag.value, self.tag.time_us))
83
-
84
- elif not is_in_alarm and alarm_ref in in_alarm:
85
- changes.append((alarm_ref, False, self.tag.value, self.tag.time_us))
86
-
87
- return changes
112
+ self.alarm_id = f'{tagname} {alarm}'
113
+ self.tag = Tag(tagname, tag['type'])
114
+ self.tag.desc = tag['desc']
115
+ self.tag.dp = tag['dp']
116
+ self.tag.units = tag['units']
117
+ self.tag.add_callback(self.callback)
118
+ self.area = area
119
+ self.rta_cb = rta_cb
120
+ self.alarms = alarms
121
+ self.alarm = split_operator(alarm)
122
+ self.in_alarm = False
123
+ self.checking = False
124
+
125
+ def callback(self, tag: Tag):
126
+ """Handle tag value changes and generate ALM/RTN messages."""
127
+ if tag.value is None:
128
+ return
129
+ value = float(tag.value)
130
+ time_us = tag.time_us
131
+ new_in_alarm = False
132
+ op = self.alarm['operator']
133
+ if op == '>':
134
+ new_in_alarm = value > self.alarm['value']
135
+ elif op == '<':
136
+ new_in_alarm = value < self.alarm['value']
137
+ elif op == '==':
138
+ new_in_alarm = value == self.alarm['value']
139
+ elif op == '>=':
140
+ new_in_alarm = value >= self.alarm['value']
141
+ elif op == '<=':
142
+ new_in_alarm = value <= self.alarm['value']
143
+ if new_in_alarm == self.in_alarm:
144
+ return
145
+ self.in_alarm = new_in_alarm
146
+ if self.in_alarm:
147
+ if self.alarm['for'] > 0:
148
+ if not self.checking:
149
+ self.checking = True
150
+ self.alarms.checking_alarms.append(self)
151
+ else:
152
+ self.generate_alarm(ALM, time_us, value)
153
+ else:
154
+ if self.checking:
155
+ self.checking = False
156
+ self.alarms.checking_alarms.remove(self)
157
+ self.generate_alarm(RTN, time_us, value)
158
+
159
+ def generate_alarm(self, kind: int, time_us: int, value: float):
160
+ """Generate alarm message."""
161
+ logging.warning(f'Alarm {self.alarm_id} {value} {KIND[kind]}')
162
+ self.rta_cb({
163
+ 'action': 'ADD',
164
+ 'date_ms': int(time_us / 1000),
165
+ 'alarm_string': self.alarm_id,
166
+ 'kind': kind,
167
+ 'desc': f'{self.tag.desc} {value:.{self.tag.dp}f}'
168
+ f' {self.tag.units}',
169
+ 'group': self.area
170
+ })
171
+
172
+ def check_duration(self, current_time_us: int):
173
+ """Check if alarm condition has been met for required duration."""
174
+ if current_time_us - self.tag.time_us >= self.alarm['for'] * 1000000:
175
+ self.generate_alarm(ALM, current_time_us, self.tag.value)
176
+ self.checking = False
177
+ self.alarms.checking_alarms.remove(self)
88
178
 
89
179
 
90
180
  class Alarms:
@@ -128,124 +218,56 @@ class Alarms:
128
218
  raise ValueError('table must be a non-empty string')
129
219
 
130
220
  logging.warning(f'Alarms {bus_ip} {bus_port} {db} {rta_tag}')
131
- self.connection = sqlite3.connect(db)
132
- self.tags: dict[str, Tag] = {}
133
- self.alarms: dict[str, Alarm] = {}
134
- self.in_alarm: dict[str, int] = {}
221
+ self.alarms: list[Alarm] = []
222
+ self.checking_alarms: list[Alarm] = []
135
223
  for tagname, tag in tag_info.items():
136
224
  standardise_tag_info(tagname, tag)
137
225
  if 'alarm' not in tag or tag['type'] not in (int, float):
138
226
  continue
139
- self.tags[tagname] = Tag(tagname, tag['type'])
140
- self.tags[tagname].desc = tag['desc']
141
- self.tags[tagname].dp = tag['dp']
142
- self.tags[tagname].units = tag['units']
143
- self.tags[tagname].add_callback(self.alarm_cb)
144
- self.alarms[tagname] = Alarm(self.tags[tagname], tag['alarm'])
145
- self.table = table
146
- self.cursor = self.connection.cursor()
227
+ area = tag['area']
228
+ for alarm in tag['alarm']:
229
+ new_alarm = Alarm(tagname, tag, alarm, area, self.rta_cb, self)
230
+ self.alarms.append(new_alarm)
147
231
  self.busclient = BusClient(bus_ip, bus_port, module='Alarms')
148
232
  self.rta = Tag(rta_tag, dict)
149
233
  self.rta.value = {}
150
234
  self.busclient.add_callback_rta(rta_tag, self.rta_cb)
151
- atexit.register(self.close)
152
-
153
- def alarm_cb(self, tag: Tag):
154
- """Callback for alarm tags."""
155
- if tag.name not in self.alarms:
156
- return
157
- changes = self.alarms[tag.name].check_conditions(self.in_alarm)
158
- for alarm_ref, is_in_alarm, value, time_us in changes:
159
- self._handle_alarm_change(
160
- alarm_ref,
161
- is_in_alarm,
162
- tag,
163
- value,
164
- time_us
165
- )
166
-
167
- def _handle_alarm_change(self, alarm_ref: str, is_in_alarm: bool,
168
- tag: Tag, value: float, time_us: int):
169
- """Handle alarm state changes and database updates."""
170
- if is_in_alarm:
171
- logging.warning(f'Alarm {alarm_ref} {value}')
172
- kind = ALM
173
- state = ALARM
174
- alarm_record = {
175
- 'action': 'ADD',
176
- 'date_ms': int(time_us / 1000),
177
- 'tag_alm': alarm_ref,
178
- 'kind': kind,
179
- 'desc': f'{tag.desc} {value:.{tag.dp}f} {tag.units}',
180
- 'in_alm': state
181
- }
182
- self.rta_cb(alarm_record)
183
- self.in_alarm[alarm_ref] = self.rta.value['id']
184
- else:
185
- logging.info(f'No alarm {alarm_ref} {value}')
186
- if alarm_ref in self.in_alarm:
187
- # First update the existing alarm record to NORMAL
188
- update_record = {
189
- 'action': 'UPDATE',
190
- 'id': self.in_alarm[alarm_ref],
191
- 'in_alm': NORMAL
192
- }
193
- self.rta_cb(update_record)
194
-
195
- # Then add the RTN record
196
- rtn_record = {
197
- 'action': 'ADD',
198
- 'date_ms': int(time_us / 1000),
199
- 'tag_alm': alarm_ref,
200
- 'kind': RTN,
201
- 'desc': f'{tag.desc} {value:.{tag.dp}f} {tag.units}',
202
- 'in_alm': NORMAL
203
- }
204
- self.rta_cb(rtn_record)
205
- del self.in_alarm[alarm_ref]
235
+ self._init_db(db, table)
236
+ self.periodic = Periodic(self.periodic_cb, 1.0)
206
237
 
207
- def _init_table(self):
238
+ def _init_db(self, db, table):
208
239
  """Initialize the database table schema."""
240
+ self.connection = sqlite3.connect(db)
241
+ self.table = table
242
+ self.cursor = self.connection.cursor()
209
243
  query = (
210
- 'CREATE TABLE IF NOT EXISTS ' + self.table +
244
+ 'CREATE TABLE IF NOT EXISTS ' + self.table + ' '
211
245
  '(id INTEGER PRIMARY KEY ASC, '
212
246
  'date_ms INTEGER, '
213
- 'tag_alm TEXT, '
247
+ 'alarm_string TEXT, '
214
248
  'kind INTEGER, '
215
249
  'desc TEXT, '
216
- 'in_alm INTEGER)'
250
+ '"group" TEXT)'
217
251
  )
218
252
  self.cursor.execute(query)
253
+ self.connection.commit()
219
254
 
220
- # Clear any existing ALARM states
221
- try:
222
- with self.connection:
223
- # Update all alarm records to NORMAL
224
- self.cursor.execute(
225
- f'SELECT id, tag_alm FROM {self.table} WHERE in_alm = ?',
226
- (ALARM,))
227
- alarm_records = self.cursor.fetchall()
228
- for record_id, tag_alm in alarm_records:
229
- update_record = {
230
- 'action': 'UPDATE',
231
- 'id': record_id,
232
- 'in_alm': NORMAL
233
- }
234
- self.rta_cb(update_record)
235
- except sqlite3.Error as e:
236
- logging.error(f'Error clearing alarm states during startup: {e}')
237
-
238
- # Add startup record using existing ADD functionality
239
255
  startup_record = {
240
256
  'action': 'ADD',
241
257
  'date_ms': int(time.time() * 1000),
242
- 'tag_alm': self.rta.name,
258
+ 'alarm_string': self.rta.name,
243
259
  'kind': INF,
244
260
  'desc': 'Alarm logging started',
245
- 'in_alm': NORMAL
261
+ 'group': '__system__'
246
262
  }
247
263
  self.rta_cb(startup_record)
248
264
 
265
+ async def periodic_cb(self):
266
+ """Periodic callback to check alarms."""
267
+ current_time_us = int(time.time() * 1000000)
268
+ for alarm in self.checking_alarms[:]:
269
+ alarm.check_duration(current_time_us)
270
+
249
271
  def rta_cb(self, request):
250
272
  """Respond to Request to Author and publish on rta_tag as needed."""
251
273
  if 'action' not in request:
@@ -256,18 +278,18 @@ class Alarms:
256
278
  with self.connection:
257
279
  self.cursor.execute(
258
280
  f'INSERT INTO {self.table} '
259
- '(date_ms, tag_alm, kind, desc, in_alm) '
260
- 'VALUES(:date_ms, :tag_alm, :kind, :desc, :in_alm) '
281
+ '(date_ms, alarm_string, kind, desc, "group") '
282
+ 'VALUES(:date_ms, :alarm_string, :kind, :desc, :group) '
261
283
  'RETURNING *;',
262
284
  request)
263
285
  res = self.cursor.fetchone()
264
286
  self.rta.value = {
265
287
  'id': res[0],
266
288
  'date_ms': res[1],
267
- 'tag_alm': res[2],
289
+ 'alarm_string': res[2],
268
290
  'kind': res[3],
269
291
  'desc': res[4],
270
- 'in_alm': res[5]
292
+ 'group': res[5]
271
293
  }
272
294
  except sqlite3.IntegrityError as error:
273
295
  logging.warning(f'Alarms rta_cb {error}')
@@ -284,10 +306,10 @@ class Alarms:
284
306
  self.rta.value = {
285
307
  'id': res[0],
286
308
  'date_ms': res[1],
287
- 'tag_alm': res[2],
309
+ 'alarm_string': res[2],
288
310
  'kind': res[3],
289
311
  'desc': res[4],
290
- 'in_alm': res[5]
312
+ 'group': res[5]
291
313
  }
292
314
  except sqlite3.IntegrityError as error:
293
315
  logging.warning(f'Alarms rta_cb update {error}')
@@ -303,10 +325,10 @@ class Alarms:
303
325
  '__rta_id__': request['__rta_id__'],
304
326
  'id': res[0],
305
327
  'date_ms': res[1],
306
- 'tag_alm': res[2],
328
+ 'alarm_string': res[2],
307
329
  'kind': res[3],
308
330
  'desc': res[4],
309
- 'in_alm': res[5]
331
+ 'group': res[5]
310
332
  }
311
333
  except sqlite3.IntegrityError as error:
312
334
  logging.warning(f'Alarms rta_cb {error}')
@@ -329,30 +351,4 @@ class Alarms:
329
351
  async def start(self):
330
352
  """Async startup."""
331
353
  await self.busclient.start()
332
- self._init_table()
333
-
334
- def close(self):
335
- """Clean shutdown of alarms logging."""
336
- for alarm_ref, record_id in self.in_alarm.items():
337
- update_record = {
338
- 'action': 'UPDATE',
339
- 'id': record_id,
340
- 'in_alm': NORMAL
341
- }
342
- try:
343
- self.rta_cb(update_record)
344
- except sqlite3.Error as e:
345
- logging.error(f'Error clearing alarm {alarm_ref}: {e}')
346
-
347
- shutdown_record = {
348
- 'action': 'ADD',
349
- 'date_ms': int(time.time() * 1000),
350
- 'tag_alm': self.rta.name,
351
- 'kind': INF,
352
- 'desc': 'Alarm logging stopped',
353
- 'in_alm': NORMAL
354
- }
355
- try:
356
- self.rta_cb(shutdown_record)
357
- except sqlite3.Error as e:
358
- logging.error(f'Error during alarm shutdown: {e}')
354
+ await self.periodic.start()
pymscada/console.py CHANGED
@@ -3,7 +3,8 @@ import asyncio
3
3
  import logging
4
4
  import sys
5
5
  from pymscada.bus_client import BusClient
6
- from pymscada.tag import Tag, tag_for_web, TagInfo
6
+ from pymscada.tag import Tag
7
+ from pymscada.www_server import standardise_tag_info
7
8
  try:
8
9
  import termios
9
10
  import tty
@@ -153,7 +154,7 @@ class Console:
153
154
  """Provide a text console to interact with a Bus."""
154
155
 
155
156
  def __init__(self, bus_ip: str = '127.0.0.1', bus_port: int = 1324,
156
- tag_info: dict[str, TagInfo] = {}) -> None:
157
+ tag_info: dict = {}) -> None:
157
158
  """
158
159
  Connect to bus_ip:bus_port and provide console interaction with a Bus.
159
160
 
@@ -177,7 +178,7 @@ class Console:
177
178
  self.busclient = BusClient(bus_ip, bus_port, module='Console')
178
179
  self.tags: dict[str, Tag] = {}
179
180
  for tagname, tag in tag_info.items():
180
- tag_for_web(tagname, tag)
181
+ standardise_tag_info(tagname, tag)
181
182
  self.tags[tagname] = Tag(tagname, tag['type'])
182
183
 
183
184
  def write_tag(self, tag: Tag):
@@ -0,0 +1,17 @@
1
+ bus_ip: 127.0.0.1
2
+ bus_port: 1324
3
+ proxy:
4
+ api:
5
+ url: 'https://api.electricityinfo.co.nz'
6
+ client_id: ${WITS_CLIENT_ID}
7
+ client_secret: ${WITS_CLIENT_SECRET}
8
+ gxp_list:
9
+ - MAT1101
10
+ - CYD2201
11
+ - BEN2201
12
+ back: 1
13
+ forward: 12
14
+ tags:
15
+ - MAT1101_RTD
16
+ - CYD2201_RTD
17
+ - BEN2201_RTD
@@ -0,0 +1,217 @@
1
+ """Poll WITS GXP pricing real time dispatch and forecast."""
2
+ import asyncio
3
+ import aiohttp
4
+ import datetime
5
+ import logging
6
+ import socket
7
+ from time import time
8
+ from pymscada.bus_client import BusClient
9
+ from pymscada.periodic import Periodic
10
+ from pymscada.tag import Tag
11
+
12
+ class WitsAPIClient:
13
+ """Get pricing data from WITS GXP APIs."""
14
+
15
+ def __init__(
16
+ self,
17
+ bus_ip: str | None = '127.0.0.1',
18
+ bus_port: int = 1324,
19
+ proxy: str | None = None,
20
+ api: dict = {},
21
+ tags: list = []
22
+ ) -> None:
23
+ """
24
+ Connect to bus on bus_ip:bus_port.
25
+
26
+ api dict should contain:
27
+ - client_id: WITS API client ID
28
+ - client_secret: WITS API client secret
29
+ - url: WITS API base URL
30
+ - gxp_list: list of GXP nodes to fetch prices for
31
+ - schedules: list of schedule types to fetch
32
+ - back: number of periods to look back
33
+ - forward: number of periods to look forward
34
+ """
35
+ if bus_ip is not None:
36
+ try:
37
+ socket.gethostbyname(bus_ip)
38
+ except socket.gaierror:
39
+ raise ValueError(f"Invalid bus_ip: {bus_ip}")
40
+ if not isinstance(proxy, str) and proxy is not None:
41
+ raise ValueError("proxy must be a string or None")
42
+ if not isinstance(api, dict):
43
+ raise ValueError("api must be a dictionary")
44
+ if not isinstance(tags, list):
45
+ raise ValueError("tags must be a list")
46
+
47
+ self.busclient = None
48
+ if bus_ip is not None:
49
+ self.busclient = BusClient(bus_ip, bus_port, module='WitsAPI')
50
+ self.proxy = proxy
51
+ self.map_bus = id(self)
52
+ self.tags = {tagname: Tag(tagname, float) for tagname in tags}
53
+
54
+ # API configuration
55
+ self.client_id = api['client_id']
56
+ self.client_secret = api['client_secret']
57
+ self.base_url = api['url']
58
+ self.gxp_list = api.get('gxp_list', [])
59
+ self.back = api.get('back', 2)
60
+ self.forward = api.get('forward', 72)
61
+
62
+ self.session = None
63
+ self.handle = None
64
+ self.periodic = None
65
+ self.queue = asyncio.Queue()
66
+
67
+ async def get_token(self):
68
+ """Get a new OAuth token"""
69
+ auth_url = f"{self.base_url}/login/oauth2/token"
70
+ data = {
71
+ "grant_type": "client_credentials",
72
+ "client_id": self.client_id,
73
+ "client_secret": self.client_secret
74
+ }
75
+ headers = {"Content-Type": "application/x-www-form-urlencoded"}
76
+ try:
77
+ async with self.session.post(auth_url, data=data, headers=headers) as response:
78
+ if response.status == 200:
79
+ result = await response.json()
80
+ self.session.headers.update({
81
+ "Authorization": f"Bearer {result['access_token']}"
82
+ })
83
+ return result["access_token"]
84
+ else:
85
+ error_text = await response.text()
86
+ logging.error(f'WITS API auth error: {error_text}')
87
+ return None
88
+ except Exception as e:
89
+ logging.error(f'WITS API auth error: {type(e).__name__} - {str(e)}')
90
+ return None
91
+
92
+ async def get_multi_schedule_prices(self):
93
+ """Get prices across multiple schedules"""
94
+ endpoint = "api/market-prices/v1/prices"
95
+ params = {
96
+ 'schedules': 'RTD,PRSS,PRSL',
97
+ 'marketType': 'E',
98
+ 'offset': 0
99
+ }
100
+ if self.gxp_list:
101
+ params['nodes'] = ','.join(self.gxp_list)
102
+ if self.back:
103
+ params['back'] = min(self.back, 48)
104
+ if self.forward:
105
+ params['forward'] = min(self.forward, 48)
106
+
107
+ query = '&'.join(f"{k}={v}" for k, v in params.items())
108
+ url = f"{self.base_url}/{endpoint}?{query}"
109
+
110
+ try:
111
+ async with self.session.get(url, proxy=self.proxy) as response:
112
+ if response.status == 200:
113
+ return await response.json()
114
+ else:
115
+ error_text = await response.text()
116
+ logging.error(f'WITS API error: {error_text}')
117
+ return None
118
+ except Exception as e:
119
+ logging.error(f'WITS API error: {type(e).__name__} - {str(e)}')
120
+ return None
121
+
122
+ def parse_prices(self, response):
123
+ """Parse API response into structured price dictionary"""
124
+ if not response:
125
+ return {}
126
+ prices = {}
127
+ for schedule_data in response:
128
+ schedule = schedule_data['schedule']
129
+ if 'prices' not in schedule_data:
130
+ continue
131
+ for price in schedule_data['prices']:
132
+ node = price['node']
133
+ trading_time = int(datetime.datetime.fromisoformat(
134
+ price['tradingDateTime'].replace('Z', '+00:00')
135
+ ).timestamp())
136
+ last_run = int(datetime.datetime.fromisoformat(
137
+ price['lastRunTime'].replace('Z', '+00:00')
138
+ ).timestamp())
139
+
140
+ if node not in prices:
141
+ prices[node] = {}
142
+ if trading_time not in prices[node]:
143
+ prices[node][trading_time] = {}
144
+ prices[node][trading_time][schedule] = [price['price'], last_run]
145
+ return prices
146
+
147
+ def update_tags(self, prices):
148
+ """Update tags with price data"""
149
+ for node in prices:
150
+ rtd = {}
151
+ for trading_time in prices[node]:
152
+ if 'RTD' in prices[node][trading_time]:
153
+ rtd_price, _ = prices[node][trading_time]['RTD']
154
+ rtd[trading_time] = rtd_price
155
+ continue
156
+ prss_price = None
157
+ prsl_price = None
158
+ if 'PRSS' in prices[node][trading_time]:
159
+ prss_price, prss_last_run = prices[node][trading_time]['PRSS']
160
+ if 'PRSL' in prices[node][trading_time]:
161
+ prsl_price, prsl_last_run = prices[node][trading_time]['PRSL']
162
+ if prsl_price is not None and prss_price is not None:
163
+ if prss_last_run > prsl_last_run:
164
+ rtd[trading_time] = prss_price
165
+ else:
166
+ rtd[trading_time] = prsl_price
167
+ continue
168
+ if prss_price is not None:
169
+ rtd[trading_time] = prss_price
170
+ elif prsl_price is not None:
171
+ rtd[trading_time] = prsl_price
172
+ tagname = f"{node}_RTD"
173
+ if tagname in self.tags:
174
+ for trading_time in sorted(rtd.keys()):
175
+ time_us = int(trading_time * 1_000_000)
176
+ self.tags[tagname].value = rtd[trading_time], time_us, self.map_bus
177
+
178
+ async def handle_response(self):
179
+ """Handle responses from the API."""
180
+ while True:
181
+ try:
182
+ prices = await self.queue.get()
183
+ if prices:
184
+ parsed_prices = self.parse_prices(prices)
185
+ self.update_tags(parsed_prices)
186
+ self.queue.task_done()
187
+ except Exception as e:
188
+ logging.error(f'Error handling response: {type(e).__name__} - {str(e)}')
189
+
190
+ async def fetch_data(self):
191
+ """Fetch price data from WITS API."""
192
+ try:
193
+ if self.session is None:
194
+ self.session = aiohttp.ClientSession()
195
+ token = await self.get_token()
196
+ if token:
197
+ prices = await self.get_multi_schedule_prices()
198
+ if prices:
199
+ await self.queue.put(prices)
200
+ except Exception as e:
201
+ logging.error(f'Error fetching data: {type(e).__name__} - {str(e)}')
202
+
203
+ async def poll(self):
204
+ """Poll WITS API every 5 minutes."""
205
+ now = int(time())
206
+ if now % 300 == 0: # Every 5 minutes
207
+ asyncio.create_task(self.fetch_data())
208
+
209
+ async def start(self):
210
+ """Start bus connection and API polling."""
211
+ if self.busclient is not None:
212
+ await self.busclient.start()
213
+ self.handle = asyncio.create_task(self.handle_response())
214
+ self.periodic = Periodic(self.poll, 1.0)
215
+ await self.periodic.start()
216
+
217
+
@@ -0,0 +1,246 @@
1
+ import aiohttp
2
+ import asyncio
3
+ import datetime
4
+
5
+ class WitsAPIClient:
6
+ def __init__(self, url, client_id, client_secret):
7
+ self.client_id = client_id
8
+ self.client_secret = client_secret
9
+ self.base_url = url
10
+ self.session = None
11
+
12
+ async def __aenter__(self):
13
+ """Create session and get token on entry"""
14
+ self.session = aiohttp.ClientSession()
15
+ return self
16
+
17
+ async def __aexit__(self, exc_type, exc_val, exc_tb):
18
+ """Close session on exit"""
19
+ if self.session:
20
+ await self.session.close()
21
+
22
+ async def get_token(self):
23
+ """Get a new OAuth token"""
24
+ auth_url = f"{self.base_url}/login/oauth2/token"
25
+ data = {
26
+ "grant_type": "client_credentials",
27
+ "client_id": self.client_id,
28
+ "client_secret": self.client_secret
29
+ }
30
+
31
+ headers = {
32
+ "Content-Type": "application/x-www-form-urlencoded"
33
+ }
34
+
35
+ try:
36
+ async with self.session.post(auth_url, data=data, headers=headers) as response:
37
+ if response.status == 200:
38
+ result = await response.json()
39
+ self.session.headers.update({
40
+ "Authorization": f"Bearer {result['access_token']}"
41
+ })
42
+ return result["access_token"]
43
+ else:
44
+ error_text = await response.text()
45
+ return None
46
+ except Exception as e:
47
+ return None
48
+
49
+ async def get(self, endpoint):
50
+ """Make a GET request to the WITS API"""
51
+ url = f"{self.base_url}/{endpoint}"
52
+ try:
53
+ async with self.session.get(url) as response:
54
+ if response.status == 200:
55
+ result = await response.json()
56
+ return result
57
+ else:
58
+ error_text = await response.text()
59
+ return None
60
+ except Exception as e:
61
+ return None
62
+
63
+ async def get_schedules(self):
64
+ """Get list of schedules for which pricing data is available"""
65
+ endpoint = "api/market-prices/v1/schedules"
66
+ return await self.get(endpoint)
67
+
68
+ async def get_nodes(self):
69
+ """Get list of GXP/GIP nodes supported by this API"""
70
+ endpoint = "api/market-prices/v1/nodes"
71
+ return await self.get(endpoint)
72
+
73
+ async def get_schedule_prices(self, schedule='RTD', market_type='E', nodes=None,
74
+ back=None, forward=None, from_date=None, to_date=None,
75
+ island=None, offset=0):
76
+ """Get prices for a single schedule
77
+ Args:
78
+ schedule: Schedule type (e.g. 'RTD' for Real Time Dispatch)
79
+ market_type: 'E' for energy prices, 'R' for reserve prices
80
+ nodes: List of node IDs to filter by
81
+ back: Number of trading periods to look back (1-48)
82
+ forward: Number of trading periods to look ahead (1-48)
83
+ from_date: Start datetime (RFC3339 format)
84
+ to_date: End datetime (RFC3339 format)
85
+ island: Filter by island ('NI' or 'SI')
86
+ offset: Pagination offset
87
+ """
88
+ endpoint = f"api/market-prices/v1/schedules/{schedule}/prices"
89
+ params = {
90
+ 'marketType': market_type,
91
+ 'offset': offset
92
+ }
93
+ if nodes:
94
+ params['nodes'] = ','.join(nodes) if isinstance(nodes, list) else nodes
95
+ if back:
96
+ params['back'] = min(back, 48)
97
+ if forward:
98
+ params['forward'] = min(forward, 48)
99
+ if from_date:
100
+ params['from'] = from_date
101
+ if to_date:
102
+ params['to'] = to_date
103
+ if island:
104
+ params['island'] = island
105
+ query = '&'.join(f"{k}={v}" for k, v in params.items())
106
+ return await self.get(f"{endpoint}?{query}")
107
+
108
+ async def get_multi_schedule_prices(self, schedules, market_type='E', nodes=None,
109
+ back=None, forward=None, from_date=None,
110
+ to_date=None, island=None, offset=0):
111
+ """Get prices across multiple schedules
112
+ Args:
113
+ schedules: List of schedule types
114
+ market_type: 'E' for energy prices, 'R' for reserve prices
115
+ nodes: List of node IDs to filter by
116
+ back: Number of trading periods to look back (1-48)
117
+ forward: Number of trading periods to look ahead (1-48)
118
+ from_date: Start datetime (RFC3339 format)
119
+ to_date: End datetime (RFC3339 format)
120
+ island: Filter by island ('NI' or 'SI')
121
+ offset: Pagination offset
122
+ """
123
+ endpoint = "api/market-prices/v1/prices"
124
+ params = {
125
+ 'schedules': ','.join(schedules) if isinstance(schedules, list) else schedules,
126
+ 'marketType': market_type,
127
+ 'offset': offset
128
+ }
129
+ if nodes:
130
+ params['nodes'] = ','.join(nodes) if isinstance(nodes, list) else nodes
131
+ if back:
132
+ params['back'] = min(back, 48)
133
+ if forward:
134
+ params['forward'] = min(forward, 48)
135
+ if from_date:
136
+ params['from'] = from_date
137
+ if to_date:
138
+ params['to'] = to_date
139
+ if island:
140
+ params['island'] = island
141
+ query = '&'.join(f"{k}={v}" for k, v in params.items())
142
+ return await self.get(f"{endpoint}?{query}")
143
+
144
+ def parse_prices(self, response):
145
+ """Parse API response into structured price dictionary
146
+ Returns dict in format:
147
+ {node: {trading_time_utc: {schedule: [price, last_run_utc]}}}
148
+ """
149
+ if not response:
150
+ return {}
151
+ prices = {}
152
+ for schedule_data in response:
153
+ schedule = schedule_data['schedule']
154
+ if 'prices' not in schedule_data:
155
+ continue
156
+ for price in schedule_data['prices']:
157
+ node = price['node']
158
+ trading_time = int(datetime.datetime.fromisoformat(
159
+ price['tradingDateTime'].replace('Z', '+00:00')
160
+ ).timestamp())
161
+ last_run = int(datetime.datetime.fromisoformat(
162
+ price['lastRunTime'].replace('Z', '+00:00')
163
+ ).timestamp())
164
+
165
+ if node not in prices:
166
+ prices[node] = {}
167
+ if trading_time not in prices[node]:
168
+ prices[node][trading_time] = {}
169
+ prices[node][trading_time][schedule] = [price['price'], last_run]
170
+
171
+ # Create RTD_forecast schedule
172
+ for node in prices:
173
+ for trading_time in prices[node]:
174
+ if 'RTD' in prices[node][trading_time]:
175
+ prices[node][trading_time]['RTD_forecast'] = prices[node][trading_time]['RTD']
176
+ else:
177
+ # Find most recent schedule by last run time
178
+ latest_schedule = None
179
+ latest_last_run = 0
180
+ for schedule in prices[node][trading_time]:
181
+ if prices[node][trading_time][schedule][1] > latest_last_run:
182
+ latest_last_run = prices[node][trading_time][schedule][1]
183
+ latest_schedule = schedule
184
+ if latest_schedule:
185
+ prices[node][trading_time]['RTD_forecast'] = \
186
+ prices[node][trading_time][latest_schedule]
187
+
188
+ return prices
189
+
190
+ def print_prices(self, prices):
191
+ """Print prices in structured format with time information"""
192
+ now = datetime.datetime.now(datetime.timezone.utc)
193
+ now_ts = now.timestamp()
194
+ for node in sorted(prices.keys()):
195
+ print(f" - {node}:")
196
+ for trading_time in sorted(prices[node].keys()):
197
+ time_diff = trading_time - now_ts
198
+ # For future times on 30 minute boundaries, show half-hour intervals
199
+ if time_diff > 0 and trading_time % 1800 == 0:
200
+ half_hours = int(time_diff / 1800)
201
+ time_str = f"(+{half_hours})"
202
+ else:
203
+ # For past times or non-30min intervals, show actual time
204
+ dt = datetime.datetime.fromtimestamp(trading_time,
205
+ datetime.timezone.utc)
206
+ time_str = f"({dt.strftime('%Y-%m-%d %H:%M:%S')})"
207
+
208
+ print(f" - Trading Time UTC: {trading_time} {time_str}")
209
+ for schedule in sorted(prices[node][trading_time].keys()):
210
+ if schedule in ['RTD', 'PRSS', 'PRSL', 'RTD_forecast']:
211
+ price, last_run = prices[node][trading_time][schedule]
212
+ last_run_dt = datetime.datetime.fromtimestamp(
213
+ last_run, datetime.timezone.utc)
214
+ print(f" {schedule:12} Price: {price:8.2f}, "
215
+ f"Last Run: {last_run_dt.strftime('%H:%M:%S')}")
216
+
217
+
218
+ async def main(config):
219
+ async with WitsAPIClient(url=config['url'],client_id=config['client_id'],
220
+ client_secret=config['client_secret']) as client:
221
+ token = await client.get_token()
222
+ if token:
223
+ multi_prices = await client.get_multi_schedule_prices(
224
+ schedules=config['schedules'],
225
+ nodes=config['gxp_list'],
226
+ back=config['back'],
227
+ forward=config['forward']
228
+ )
229
+ if multi_prices:
230
+ prices_dict = client.parse_prices(multi_prices)
231
+ client.print_prices(prices_dict)
232
+ await asyncio.sleep(1)
233
+
234
+
235
+ CONFIG = {
236
+ 'url': 'https://api.electricityinfo.co.nz',
237
+ 'gxp_list': ['MAT1101', 'CYD2201', 'BEN2201'],
238
+ 'schedules': ['RTD', 'PRSS', 'PRSL'],
239
+ 'back': 2,
240
+ 'forward': 72,
241
+ 'client_id': 'xx',
242
+ 'client_secret': 'xx'
243
+ }
244
+
245
+ if __name__ == "__main__":
246
+ asyncio.run(main(CONFIG))
pymscada/module_config.py CHANGED
@@ -150,6 +150,18 @@ def create_module_registry():
150
150
  module_class='pymscada.iodrivers.snmp_client:SnmpClient',
151
151
  tags=False
152
152
  ),
153
+ ModuleDefinition(
154
+ name='witsapi',
155
+ help='poll WITS GXP pricing real time dispatch and forecast',
156
+ module_class='pymscada.iodrivers.witsapi:WitsAPIClient',
157
+ tags=False,
158
+ epilog=dedent("""
159
+ WITS_CLIENT_ID and WITS_CLIENT_SECRET can be set in the wits.yaml
160
+ or as environment variables:
161
+ vi ~/.bashrc
162
+ export WITS_CLIENT_ID='your_client_id'
163
+ export WITS_CLIENT_SECRET='your_client_secret'""")
164
+ ),
153
165
  ModuleDefinition(
154
166
  name='console',
155
167
  help='interactive bus console',
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: pymscada
3
- Version: 0.2.0rc4
3
+ Version: 0.2.0rc6
4
4
  Summary: Shared tag value SCADA with python backup and Angular UI
5
5
  Author-email: Jamie Walton <jamie@walton.net.nz>
6
6
  License: GPL-3.0-or-later
@@ -21,6 +21,7 @@ Requires-Dist: pymscada-html==0.2.0rc4
21
21
  Requires-Dist: cerberus>=1.3.5
22
22
  Requires-Dist: pycomm3>=1.2.14
23
23
  Requires-Dist: pysnmplib>=5.0.24
24
+ Dynamic: license-file
24
25
 
25
26
  # pymscada
26
27
  #### [Docs](https://github.com/jamie0walton/pymscada/blob/main/docs/README.md)
@@ -1,16 +1,16 @@
1
1
  pymscada/__init__.py,sha256=NV_cIIwe66Ugp8ns426rtfJIIyskWbqwImD9p_5p0bQ,739
2
2
  pymscada/__main__.py,sha256=WcyVlrYOoDdktJhOoyubTOycMwpayksFdxwelRU5xpQ,272
3
- pymscada/alarms.py,sha256=KiGws42xu8Bcmy_hhZkKoYhfNzUzICSV7f8xQcY7M8c,13715
3
+ pymscada/alarms.py,sha256=iGRf_ybe2KGiFpN7EwAHzqiCyyPYaWgL5zd34SSdrU8,12895
4
4
  pymscada/bus_client.py,sha256=ROShMcR2-y_i5CIvPxRdCRr-NpnMANjKFdLjKjMTRwo,9117
5
5
  pymscada/bus_server.py,sha256=k7ht2SAr24Oab0hBOPeW4NRDF_RK-F46iE0cMzh7K4w,12323
6
6
  pymscada/checkout.py,sha256=RLuCMTEuUI7pp1hIRAUPbo8xYFta8TjArelx0SD4gOY,3897
7
7
  pymscada/config.py,sha256=vwGxieaJBYXiHNQEOYVDFaPuGmnUlCnbNm_W9bugKlc,1851
8
- pymscada/console.py,sha256=zM6TNXJY8ROcVzO3UBzi1qDSiit7mfLr2YprRoxv2oQ,8824
8
+ pymscada/console.py,sha256=EEsJLCvn8AFimN8qGNilX0ks6t3OFcGW5nw6OVAXfac,8850
9
9
  pymscada/files.py,sha256=iouEOPfEkVI0Qbbf1p-L324Y04zSrynVypLW0-1MThA,2499
10
10
  pymscada/history.py,sha256=7UEOeMnlSMv0LoWTqLWx7QwOW1FZZ4wAvzH6v6b0_vI,11592
11
11
  pymscada/main.py,sha256=d6EnK4-tEcvM5AqMHYhvqlnSh-E_wd0Tuxk-kXYSiDw,1854
12
12
  pymscada/misc.py,sha256=0Cj6OFhQonyhyk9x0BG5MiS-6EPk_w6zvavt8o_Hlf0,622
13
- pymscada/module_config.py,sha256=r1JBjOXtMk7n09kvlvnmA3d_BySEmJpdefFhRNKPdAY,8824
13
+ pymscada/module_config.py,sha256=sEoLUhMUFJfalH3CbhNPIqQd1bAL7bWCyPSMUKs6HJ4,9370
14
14
  pymscada/opnotes.py,sha256=MKM51IrB93B2-kgoTzlpOLpaMYs-7rPQHWmRLME-hQQ,7952
15
15
  pymscada/periodic.py,sha256=MLlL93VLvFqBBgjO1Us1t0aLHTZ5BFdW0B__G02T1nQ,1235
16
16
  pymscada/protocol_constants.py,sha256=lPJ4JEgFJ_puJjTym83EJIOw3UTUFbuFMwg3ohyUAGY,2414
@@ -47,6 +47,7 @@ pymscada/demo/pymscada-opnotes.service,sha256=TlrTRgP3rzrlXT8isAGT_Wy38ScDjT1Vvn
47
47
  pymscada/demo/pymscada-wwwserver.service,sha256=7Qy2wsMmFEsQn-b5mgAcsrAQZgXynkv8SpHD6hLvRGc,370
48
48
  pymscada/demo/snmpclient.yaml,sha256=z8iACrFvMftYUtqGrRjPZYZTpn7aOXI-Kp675NAM8cU,2013
49
49
  pymscada/demo/tags.yaml,sha256=9xydsQriKT0lNAW533rz-FMVgoedn6Lwc50AnNig7-k,2733
50
+ pymscada/demo/wits.yaml,sha256=B8F136jvLIYU8t-pOdsEU_j97qMo3RgGQ1Rs4ExhmeE,289
50
51
  pymscada/demo/wwwserver.yaml,sha256=mmwvSLUXUDCIPaHeCJdCETAp9Cc4wb5CuK_aGv01KWk,2759
51
52
  pymscada/demo/__pycache__/__init__.cpython-311.pyc,sha256=tpxZoW429YA-2mbwzOlhBmbSTcbvTJqgKCfDRMrhEJE,195
52
53
  pymscada/iodrivers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -61,15 +62,17 @@ pymscada/iodrivers/ping_client.py,sha256=UOQgUfoIcYqy5VvKyJ8XGHHjeSRTfjmrhyWEojh
61
62
  pymscada/iodrivers/ping_map.py,sha256=EbOteqfEYKIOMqPymROJ4now2If-ekEj6jnM5hthoSA,1403
62
63
  pymscada/iodrivers/snmp_client.py,sha256=66-IDzddeKcSnqOzNXIZ8wuuAqhIxZjyLNrDwDvHCvw,2708
63
64
  pymscada/iodrivers/snmp_map.py,sha256=sDdIR5ZPAETpozDfBt_XQiZ-f4t99UCPlzj7BxFxQyM,2369
65
+ pymscada/iodrivers/witsapi.py,sha256=Ga6JpEQRUciT_LxWW36LsVGkUeWjModtzPoWYIzyzHs,8381
66
+ pymscada/iodrivers/witsapi_POC.py,sha256=dQcR2k1wsLb_cnNqvAB4kJ7FdY0BlcnxiMoepr28Ars,10132
64
67
  pymscada/pdf/__init__.py,sha256=WsDDgkWnZBJbt2-cJCdc2NvRAv_T4a7WOC1Q0k_l0gI,29
65
68
  pymscada/pdf/one.pdf,sha256=eoJ45DrAjVZrwmwdA_EAz1fwmT44eRnt_tkc2pmMrKY,1488
66
69
  pymscada/pdf/two.pdf,sha256=TAuW5yLU1_wfmTH_I5ezHwY0pxhCVuZh3ixu0kwmJwE,1516
67
70
  pymscada/pdf/__pycache__/__init__.cpython-311.pyc,sha256=4KTfXrV9bGDbTIEv-zgIj_LvzLbVTj77lEC1wzMh9e0,194
68
71
  pymscada/tools/snmp_client2.py,sha256=pdn5dYyEv4q-ubA0zQ8X-3tQDYxGC7f7Xexa7QPaL40,1675
69
72
  pymscada/tools/walk.py,sha256=OgpprUbKLhEWMvJGfU1ckUt_PFEpwZVOD8HucCgzmOc,1625
70
- pymscada-0.2.0rc4.dist-info/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
71
- pymscada-0.2.0rc4.dist-info/METADATA,sha256=F9_ab9hHNXQhJaoVfC7CtuEPKgMh5eDAaZOt8GdIr1M,2371
72
- pymscada-0.2.0rc4.dist-info/WHEEL,sha256=A3WOREP4zgxI0fKrHUG8DC8013e3dK3n7a6HDbcEIwE,91
73
- pymscada-0.2.0rc4.dist-info/entry_points.txt,sha256=2UJBi8jrqujnerrcXcq4F8GHJYVDt26sacXl94t3sd8,56
74
- pymscada-0.2.0rc4.dist-info/top_level.txt,sha256=LxIB-zrtgObJg0fgdGZXBkmNKLDYHfaH1Hw2YP2ZMms,9
75
- pymscada-0.2.0rc4.dist-info/RECORD,,
73
+ pymscada-0.2.0rc6.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
74
+ pymscada-0.2.0rc6.dist-info/METADATA,sha256=QdAhMpE2X7HmhVO0H6XvXjnjQO7JZka7zlWoXKle2vg,2393
75
+ pymscada-0.2.0rc6.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
76
+ pymscada-0.2.0rc6.dist-info/entry_points.txt,sha256=2UJBi8jrqujnerrcXcq4F8GHJYVDt26sacXl94t3sd8,56
77
+ pymscada-0.2.0rc6.dist-info/top_level.txt,sha256=LxIB-zrtgObJg0fgdGZXBkmNKLDYHfaH1Hw2YP2ZMms,9
78
+ pymscada-0.2.0rc6.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.7.0)
2
+ Generator: setuptools (80.9.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5