pymscada 0.1.11b10__py3-none-any.whl → 0.2.0rc2__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/bus_client.py CHANGED
@@ -12,8 +12,9 @@ class BusClient:
12
12
  """
13
13
  Connects to a Bus Server.
14
14
 
15
- await client.connect() to make the connection. If bus server connection
16
- fails, die.
15
+ The client is created without a connection. client.start() creates the
16
+ connection and checks the tags at that time. When the connection fails
17
+ the client dies. A connection is mandatory for the client to run.
17
18
  """
18
19
 
19
20
  def __init__(self, ip: str = '127.0.0.1', port: int = 1324, tag_info=None,
@@ -125,7 +126,6 @@ class BusClient:
125
126
 
126
127
  async def read(self):
127
128
  """Read forever."""
128
- await self.open_connection()
129
129
  while True:
130
130
  try:
131
131
  head = await self.reader.readexactly(14)
@@ -223,4 +223,5 @@ class BusClient:
223
223
 
224
224
  async def start(self):
225
225
  """Start async."""
226
+ await self.open_connection()
226
227
  self.read_task = asyncio.create_task(self.read())
pymscada/bus_server.py CHANGED
@@ -3,6 +3,7 @@ import asyncio
3
3
  from struct import pack, unpack
4
4
  import time
5
5
  import logging
6
+ import socket
6
7
  import pymscada.protocol_constants as pc
7
8
 
8
9
 
@@ -143,8 +144,12 @@ class BusServer:
143
144
 
144
145
  __slots__ = ('ip', 'port', 'server', 'connections', 'bus_tag')
145
146
 
146
- def __init__(self, ip: str = '127.0.0.1', port: int = 1324,
147
- bus_tag: str = '__bus__'):
147
+ def __init__(
148
+ self,
149
+ ip: str = '127.0.0.1',
150
+ port: int = 1324,
151
+ bus_tag: str = '__bus__'
152
+ ) -> None:
148
153
  """
149
154
  Serve Tags on ip:port, echoing changes to any subscribers.
150
155
 
@@ -154,6 +159,13 @@ class BusServer:
154
159
 
155
160
  Event loop must be running.
156
161
  """
162
+ try:
163
+ socket.gethostbyname(ip)
164
+ except socket.gaierror as e:
165
+ raise ValueError(f'Cannot resolve IP/hostname: {e}')
166
+ if not isinstance(bus_tag, str):
167
+ raise ValueError('bus_tag must be a string')
168
+
157
169
  self.ip = ip
158
170
  self.port = port
159
171
  self.server = None
pymscada/checkout.py CHANGED
@@ -6,100 +6,98 @@ import sys
6
6
  from pymscada.config import get_demo_files, get_pdf_files
7
7
 
8
8
 
9
- PATH = {
10
- '__PYTHON__': Path(f'{sys.exec_prefix}/bin/python').absolute(),
11
- '__PYMSCADA__': Path(sys.argv[0]).absolute(),
12
- '__DIR__': Path('.').absolute(),
13
- '__HOME__': Path.home().absolute(),
14
- '__USER__': getpass.getuser()
15
- }
16
- if sys.platform == "win32":
17
- PATH = {
18
- '__PYTHON__': Path(f'{sys.exec_prefix}/python.exe').absolute(),
19
- '__PYMSCADA__': Path(sys.argv[0]).absolute(),
20
- '__DIR__': Path('.').absolute(),
21
- '__HOME__': Path.home(),
22
- '__USER__': getpass.getuser()
23
- }
9
+ class Checkout:
10
+ """Create and manage configuration files."""
11
+
12
+ def __init__(self, **kwargs):
13
+ """Initialize paths and settings."""
14
+ self.path = {
15
+ '__PYTHON__': Path(f'{sys.exec_prefix}/bin/python').absolute(),
16
+ '__PYMSCADA__': Path(sys.argv[0]).absolute(),
17
+ '__DIR__': Path('.').absolute(),
18
+ '__HOME__': Path.home().absolute(),
19
+ '__USER__': getpass.getuser()
20
+ }
21
+ if sys.platform == "win32":
22
+ self.path['__PYTHON__'] = Path(f'{sys.exec_prefix}/python.exe').absolute()
23
+
24
+ self.overwrite = kwargs.get('overwrite', False)
25
+ self.diff = kwargs.get('diff', False)
24
26
 
25
- def make_history():
26
- """Make the history folder if missing."""
27
- history_dir = PATH['__DIR__'].joinpath('history')
28
- if not history_dir.exists():
29
- print("making 'history' folder")
30
- history_dir.mkdir()
27
+ def make_history(self):
28
+ """Make the history folder if missing."""
29
+ history_dir = self.path['__DIR__'].joinpath('history')
30
+ if not history_dir.exists():
31
+ print("making 'history' folder")
32
+ history_dir.mkdir()
31
33
 
34
+ def make_pdf(self):
35
+ """Make the pdf folder if missing."""
36
+ pdf_dir = self.path['__DIR__'].joinpath('pdf')
37
+ if not pdf_dir.exists():
38
+ print('making pdf dir')
39
+ pdf_dir.mkdir()
40
+ for pdf_file in get_pdf_files():
41
+ target = pdf_dir.joinpath(pdf_file.name)
42
+ target.write_bytes(pdf_file.read_bytes())
32
43
 
33
- def make_pdf():
34
- """Make the pdf folder if missing."""
35
- pdf_dir = PATH['__DIR__'].joinpath('pdf')
36
- if not pdf_dir.exists():
37
- print('making pdf dir')
38
- pdf_dir.mkdir()
39
- for pdf_file in get_pdf_files():
40
- target = pdf_dir.joinpath(pdf_file.name)
41
- target.write_bytes(pdf_file.read_bytes())
44
+ def make_config(self):
45
+ """Make the config folder, if missing, and copy files in."""
46
+ config_dir = self.path['__DIR__'].joinpath('config')
47
+ if not config_dir.exists():
48
+ print('making config dir')
49
+ config_dir.mkdir()
50
+ for config_file in get_demo_files():
51
+ target = config_dir.joinpath(config_file.name)
52
+ rt = 'Creating '
53
+ if target.exists():
54
+ if self.overwrite:
55
+ rt = 'Replacing '
56
+ target.unlink()
57
+ else:
58
+ continue
59
+ print(f'{rt} {target}')
60
+ rd_bytes = config_file.read_bytes()
61
+ if target.name.lower() != 'readme.md':
62
+ for k, v in self.path.items():
63
+ rd_bytes = rd_bytes.replace(k.encode(), str(v).encode())
64
+ target.write_bytes(rd_bytes)
42
65
 
66
+ def read_with_subst(self, file: Path):
67
+ """Read the file and replace DIR markers."""
68
+ rd = file.read_bytes().decode()
69
+ for k, v in self.path.items():
70
+ rd = rd.replace(k, str(v))
71
+ lines = rd.splitlines()
72
+ return lines
43
73
 
44
- def make_config(overwrite: bool):
45
- """Make the config folder, if missing, and copy files in."""
46
- config_dir = PATH['__DIR__'].joinpath('config')
47
- if not config_dir.exists():
48
- print('making config dir')
49
- config_dir.mkdir()
50
- for config_file in get_demo_files():
51
- target = config_dir.joinpath(config_file.name)
52
- rt = 'Creating '
53
- if target.exists():
54
- if overwrite:
55
- rt = 'Replacing '
56
- target.unlink()
74
+ def compare_config(self):
75
+ """Compare old and new config."""
76
+ config_dir = self.path['__DIR__'].joinpath('config')
77
+ if not config_dir.exists():
78
+ print('No config dir, are you in the right directory')
79
+ return
80
+ for config_file in get_demo_files():
81
+ target = config_dir.joinpath(config_file.name)
82
+ if target.exists():
83
+ new_lines = self.read_with_subst(config_file)
84
+ old_lines = self.read_with_subst(target)
85
+ diff = list(difflib.unified_diff(old_lines, new_lines,
86
+ fromfile=str(target), tofile=str(config_file)))
87
+ if len(diff):
88
+ print('\n'.join(diff), '\n')
57
89
  else:
58
- continue
59
- print(f'{rt} {target}')
60
- rd_bytes = config_file.read_bytes()
61
- if target.name.lower() != 'readme.md':
62
- for k, v in PATH.items():
63
- rd_bytes = rd_bytes.replace(k.encode(), str(v).encode())
64
- target.write_bytes(rd_bytes)
90
+ print(f'\n--- MISSING FILE\n\n+++ {config_file}')
65
91
 
66
-
67
- def read_with_subst(file: Path):
68
- """Read the file and replace DIR markers."""
69
- rd = file.read_bytes().decode()
70
- for k, v in PATH.items():
71
- rd = rd.replace(k, str(v))
72
- lines = rd.splitlines()
73
- return lines
74
-
75
-
76
- def compare_config():
77
- """Compare old and new config."""
78
- config_dir = PATH['__DIR__'].joinpath('config')
79
- if not config_dir.exists():
80
- print('No config dir, are you in the right directory')
81
- return
82
- for config_file in get_demo_files():
83
- target = config_dir.joinpath(config_file.name)
84
- if target.exists():
85
- new_lines = read_with_subst(config_file)
86
- old_lines = read_with_subst(target)
87
- diff = list(difflib.unified_diff(old_lines, new_lines,
88
- fromfile=str(target), tofile=str(config_file)))
89
- if len(diff):
90
- print('\n'.join(diff), '\n')
92
+ async def start(self):
93
+ """Execute checkout process."""
94
+ for name in ['__PYTHON__', '__PYMSCADA__', '__DIR__', '__HOME__']:
95
+ if not self.path[name].exists():
96
+ raise SystemExit(f'{self.path[name]} is missing')
97
+
98
+ if self.diff:
99
+ self.compare_config()
91
100
  else:
92
- print(f'\n--- MISSING FILE\n\n+++ {config_file}')
93
-
94
-
95
- def checkout(overwrite=False, diff=False):
96
- """Do it."""
97
- for name in ['__PYTHON__', '__PYMSCADA__', '__DIR__', '__HOME__']:
98
- if not PATH[name].exists():
99
- raise SystemExit(f'{PATH[name]} is missing')
100
- if diff:
101
- compare_config()
102
- else:
103
- make_history()
104
- make_pdf()
105
- make_config(overwrite)
101
+ self.make_history()
102
+ self.make_pdf()
103
+ self.make_config()
pymscada/console.py CHANGED
@@ -3,7 +3,7 @@ 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
6
+ from pymscada.tag import Tag, tag_for_web, TagInfo
7
7
  try:
8
8
  import termios
9
9
  import tty
@@ -153,7 +153,7 @@ class Console:
153
153
  """Provide a text console to interact with a Bus."""
154
154
 
155
155
  def __init__(self, bus_ip: str = '127.0.0.1', bus_port: int = 1324,
156
- tag_info: dict = {}):
156
+ tag_info: dict[str, TagInfo] = {}) -> None:
157
157
  """
158
158
  Connect to bus_ip:bus_port and provide console interaction with a Bus.
159
159
 
@@ -2,7 +2,7 @@ bus_ip: 127.0.0.1
2
2
  bus_port: 1324
3
3
  proxy:
4
4
  api:
5
- api_key: ${OPENWEATHER_API_KEY}
5
+ api_key: ${OPENWEATHERMAP_API_KEY}
6
6
  units: metric
7
7
  locations:
8
8
  Murupara:
@@ -1,11 +1,11 @@
1
1
  [Unit]
2
- Description=pymscada - AccuWeather client
2
+ Description=pymscada - Open Weather client
3
3
  BindsTo=pymscada-bus.service
4
4
  After=pymscada-bus.service
5
5
 
6
6
  [Service]
7
7
  WorkingDirectory=__DIR__
8
- ExecStart=__PYMSCADA__ accuweatherclient --config __DIR__/config/accuweather.yaml
8
+ ExecStart=__PYMSCADA__ openweatherclient --config __DIR__/config/openweather.yaml
9
9
  Restart=always
10
10
  RestartSec=5
11
11
  User=__USER__
pymscada/files.py CHANGED
@@ -10,13 +10,13 @@ class Files():
10
10
  """Connect to bus_ip:bus_port, store and provide a value history."""
11
11
 
12
12
  def __init__(self, bus_ip: str = '127.0.0.1', bus_port: int = 1324,
13
- path: str = '', files: list = None,
13
+ path: str = '', files: list[dict] = [],
14
14
  rta_tag: str = '__files__') -> None:
15
15
  """
16
16
  Connect to bus_ip:bus_port, serve and update files.
17
17
 
18
- TODO
19
- Write something.
18
+ Scans paths in files at the root defined by path. These should
19
+ be readable by wwwserver so that download links work.
20
20
 
21
21
  Event loop must be running.
22
22
  """
pymscada/history.py CHANGED
@@ -1,11 +1,34 @@
1
- """Store and provide history."""
1
+ """Store and provide history.
2
+
3
+ History File Structure
4
+ ---------------------
5
+ History files are binary files stored as <tagname>_<time_us>.dat where time_us
6
+ is the microsecond timestamp of the first entry in that file.
7
+
8
+ Each file contains a series of fixed-size records (16 bytes each):
9
+ - For integer tags: 8 bytes timestamp (uint64) + 8 bytes value (int64)
10
+ - For float tags: 8 bytes timestamp (uint64) + 8 bytes value (double)
11
+
12
+ Files are organized in chunks:
13
+ - Each chunk is 1024 records (16KB)
14
+ - Each file contains up to 64 chunks (1MB)
15
+ - New files are created when:
16
+ 1. Current file reaches max size (64 chunks)
17
+ 2. Manual flush() is called
18
+ 3. Application shutdown
19
+
20
+ Timestamps are stored as microseconds since epoch in network byte order (big-endian).
21
+ Values are also stored in network byte order.
22
+ """
2
23
  import atexit
3
24
  import logging
4
25
  from pathlib import Path
5
26
  from struct import pack, pack_into, unpack_from, error
6
27
  import time
28
+ import socket
29
+ from typing import TypedDict, Optional
7
30
  from pymscada.bus_client import BusClient
8
- from pymscada.tag import Tag, TYPES
31
+ from pymscada.tag import Tag, TagInfo, TYPES
9
32
 
10
33
 
11
34
  ITEM_SIZE = 16 # Q + q, Q or d
@@ -14,6 +37,16 @@ CHUNK_SIZE = ITEM_COUNT * ITEM_SIZE
14
37
  FILE_CHUNKS = 64
15
38
 
16
39
 
40
+ class Request(TypedDict, total=False):
41
+ """Type definition for request dictionary."""
42
+ tagname: str
43
+ start_ms: Optional[int] # Allow web client to use native ms
44
+ start_us: Optional[int] # Native for pymscada server
45
+ end_ms: Optional[int]
46
+ end_us: Optional[int]
47
+ __rta_id__: Optional[int] # Empty for a change that must be broadcast
48
+
49
+
17
50
  def tag_for_history(tagname: str, tag: dict):
18
51
  """Correct tag dictionary in place to be suitable for web client."""
19
52
  tag['name'] = tagname
@@ -188,9 +221,14 @@ class TagHistory():
188
221
  class History():
189
222
  """Connect to bus_ip:bus_port, store and provide a value history."""
190
223
 
191
- def __init__(self, bus_ip: str = '127.0.0.1', bus_port: int = 1324,
192
- path: str = 'history', tag_info: dict = {},
193
- rta_tag: str = '__history__') -> None:
224
+ def __init__(
225
+ self,
226
+ bus_ip: str = '127.0.0.1',
227
+ bus_port: int = 1324,
228
+ path: str = 'history',
229
+ tag_info: TagInfo = {},
230
+ rta_tag: str | None = '__history__'
231
+ ) -> None:
194
232
  """
195
233
  Connect to bus_ip:bus_port, store and provide a value history.
196
234
 
@@ -200,6 +238,23 @@ class History():
200
238
 
201
239
  Event loop must be running.
202
240
  """
241
+ if not isinstance(bus_ip, str):
242
+ raise ValueError("bus_ip must be a string")
243
+ try:
244
+ socket.gethostbyname(bus_ip)
245
+ except socket.gaierror:
246
+ raise ValueError(f"Invalid bus_ip: {bus_ip}")
247
+ if not isinstance(bus_port, int):
248
+ raise ValueError("bus_port must be an integer")
249
+ if not 1024 <= bus_port <= 65535:
250
+ raise ValueError(f"bus_port must be between 1024 and 65535")
251
+ if not isinstance(path, str):
252
+ raise ValueError("path must be a string")
253
+ if not isinstance(tag_info, dict):
254
+ raise ValueError("tag_info must be a dictionary")
255
+ if not isinstance(rta_tag, (str, type(None))):
256
+ raise ValueError("rta_tag must be a string or None")
257
+
203
258
  self.busclient = BusClient(bus_ip, bus_port, module='History')
204
259
  self.path = path
205
260
  self.tags: dict[str, Tag] = {}
@@ -217,7 +272,7 @@ class History():
217
272
  self.rta.value = b'\x00\x00\x00\x00\x00\x00'
218
273
  self.busclient.add_callback_rta(rta_tag, self.rta_cb)
219
274
 
220
- def rta_cb(self, request):
275
+ def rta_cb(self, request: Request):
221
276
  """Respond to bus requests for data to publish on rta."""
222
277
  if 'start_ms' in request:
223
278
  request['start_us'] = request['start_ms'] * 1000
@@ -2,6 +2,7 @@
2
2
  import asyncio
3
3
  import aiohttp
4
4
  import logging
5
+ import socket
5
6
  from time import time
6
7
  from pymscada.bus_client import BusClient
7
8
  from pymscada.periodic import Periodic
@@ -10,8 +11,14 @@ from pymscada.tag import Tag
10
11
  class OpenWeatherClient:
11
12
  """Get weather data from OpenWeather Current and Forecast APIs."""
12
13
 
13
- def __init__(self, bus_ip: str = '127.0.0.1', bus_port: int = 1324,
14
- proxy: str = None, api: dict = {}, tags: dict = {}) -> None:
14
+ def __init__(
15
+ self,
16
+ bus_ip: str | None = '127.0.0.1',
17
+ bus_port: int = 1324,
18
+ proxy: str | None = None,
19
+ api: dict = {},
20
+ tags: list = []
21
+ ) -> None:
15
22
  """
16
23
  Connect to bus on bus_ip:bus_port.
17
24
 
@@ -21,6 +28,18 @@ class OpenWeatherClient:
21
28
  - times: list of hours ahead to fetch forecast data for
22
29
  - units: optional units (standard, metric, imperial)
23
30
  """
31
+ if bus_ip is not None:
32
+ try:
33
+ socket.gethostbyname(bus_ip)
34
+ except socket.gaierror:
35
+ raise ValueError(f"Invalid bus_ip: {bus_ip}")
36
+ if not isinstance(proxy, str) and proxy is not None:
37
+ raise ValueError("proxy must be a string or None")
38
+ if not isinstance(api, dict):
39
+ raise ValueError("api must be a dictionary")
40
+ if not isinstance(tags, list):
41
+ raise ValueError("tags must be a list")
42
+
24
43
  self.busclient = None
25
44
  if bus_ip is not None:
26
45
  self.busclient = BusClient(bus_ip, bus_port, module='OpenWeather')
@@ -41,82 +60,144 @@ class OpenWeatherClient:
41
60
 
42
61
  def update_tags(self, location, data, suffix):
43
62
  """Update tags for forecast weather."""
63
+ if 'dt' not in data:
64
+ logging.error(f'No timestamp in data for {location}, skipping update')
65
+ return
44
66
  for parameter in self.parameters:
45
67
  tagname = f"{location}_{parameter}{suffix}"
46
- if parameter == 'Temp':
47
- value = data['main']['temp']
48
- elif parameter == 'WindSpeed':
49
- value = data['wind']['speed']
50
- elif parameter == 'WindDir':
51
- value = data['wind'].get('deg', 0)
52
- elif parameter == 'Rain':
53
- value = data.get('rain', {}).get('1h', 0)
54
68
  try:
55
- self.tags[tagname].value = value
56
- except KeyError:
57
- logging.warning(f'{tagname} not found setting weather value')
69
+ if parameter == 'Temp':
70
+ main_data = data.get('main', {})
71
+ value = main_data.get('temp', 0)
72
+ elif parameter == 'WindSpeed':
73
+ wind_data = data.get('wind', {})
74
+ value = wind_data.get('speed', 0)
75
+ elif parameter == 'WindDir':
76
+ wind_data = data.get('wind', {})
77
+ value = wind_data.get('deg', 0)
78
+ elif parameter == 'Rain':
79
+ rain_data = data.get('rain', {})
80
+ value = rain_data.get('1h', 0)
81
+ else:
82
+ logging.warning(f'Unknown parameter {parameter} for {tagname}')
83
+ continue
84
+ time_us = int(data['dt'] * 1_000_000)
85
+ self.tags[tagname].value = value, time_us, self.map_bus
86
+ logging.debug(f'Updated {tagname} = {value} at timestamp {data["dt"]}')
87
+ except Exception as e:
88
+ logging.error(
89
+ f'Error updating {tagname}: {type(e).__name__} - {str(e)}'
90
+ )
58
91
 
59
92
  async def handle_response(self):
60
93
  """Handle responses from the API."""
61
94
  while True:
62
- location, data = await self.queue.get()
63
- now = int(time())
64
- if 'dt' in data:
65
- self.update_tags(location, data, '')
66
- elif 'list' in data:
67
- for forecast in data['list']:
68
- hours_ahead = int((forecast['dt'] - now) / 3600)
69
- if hours_ahead not in self.times:
70
- continue
71
- suffix = f'_{hours_ahead:02d}'
72
- self.update_tags(location, forecast, suffix)
95
+ try:
96
+ location, data = await self.queue.get()
97
+ logging.debug(f'Processing data for {location}')
98
+
99
+ if 'dt' in data: # Current weather data
100
+ self.update_tags(location, data, '')
101
+ elif 'list' in data: # Forecast data
102
+ now = int(time())
103
+ for forecast in data['list']:
104
+ hours_ahead = int((forecast['dt'] - now) / 3600)
105
+ if hours_ahead in self.times:
106
+ suffix = f'_{hours_ahead:02d}'
107
+ self.update_tags(location, forecast, suffix)
108
+
109
+ self.queue.task_done()
110
+ except Exception as e:
111
+ logging.error(f'Error handling response: {type(e).__name__} - {str(e)}')
73
112
 
74
113
  async def fetch_current_data(self):
75
114
  """Fetch current weather data for all locations."""
76
- logging.info('fetching current')
77
- if self.session is None:
78
- self.session = aiohttp.ClientSession()
79
- for location, coords in self.locations.items():
80
- base_params = {'lat': coords['lat'], 'lon': coords['lon'],
81
- 'appid': self.api_key, 'units': self.units }
82
- try:
83
- async with self.session.get(self.current_url,
84
- params=base_params, proxy=self.proxy) as resp:
85
- if resp.status == 200:
86
- self.queue.put_nowait((location, await resp.json()))
87
- else:
88
- logging.warning('OpenWeather current API error for '
89
- f'{location}: {resp.status}')
90
- except Exception as e:
91
- logging.warning('OpenWeather current API error for '
92
- f'{location}: {e}')
115
+ try:
116
+ if self.session is None:
117
+ self.session = aiohttp.ClientSession()
118
+
119
+ for location, coords in self.locations.items():
120
+ base_params = {
121
+ 'lat': coords.get('lat'),
122
+ 'lon': coords.get('lon'),
123
+ 'appid': self.api_key,
124
+ 'units': self.units
125
+ }
126
+
127
+ # Validate required parameters
128
+ if not all(base_params.values()):
129
+ logging.error(
130
+ f'Missing required parameters for {location}: '
131
+ f'{[k for k, v in base_params.items() if not v]}'
132
+ )
133
+ continue
134
+
135
+ try:
136
+ async with self.session.get(
137
+ self.current_url,
138
+ params=base_params,
139
+ proxy=self.proxy,
140
+ timeout=30 # Add timeout
141
+ ) as resp:
142
+ if resp.status == 200:
143
+ data = await resp.json()
144
+ logging.debug(
145
+ f'Received current weather data for {location}'
146
+ )
147
+ await self.queue.put((location, data))
148
+ else:
149
+ error_text = await resp.text()
150
+ logging.error(
151
+ f'OpenWeather API error for {location}: '
152
+ f'Status: {resp.status}, Response: {error_text[:200]}'
153
+ )
154
+
155
+ except asyncio.TimeoutError:
156
+ logging.error(f'Timeout fetching data for {location}')
157
+ except aiohttp.ClientError as e:
158
+ logging.error(
159
+ f'Network error for {location}: {type(e).__name__} - {str(e)}'
160
+ )
161
+ except Exception as e:
162
+ logging.error(
163
+ f'Unexpected error for {location}: {type(e).__name__} - {str(e)}'
164
+ )
165
+
166
+ except Exception as e:
167
+ logging.error(f'Fatal error in fetch_current_data: {type(e).__name__} - {str(e)}')
93
168
 
94
169
  async def fetch_forecast_data(self):
95
170
  """Fetch forecast weather data for all locations."""
96
- logging.info('fetching forecast')
97
171
  if self.session is None:
98
172
  self.session = aiohttp.ClientSession()
99
173
  for location, coords in self.locations.items():
100
- base_params = {'lat': coords['lat'], 'lon': coords['lon'],
101
- 'appid': self.api_key, 'units': self.units }
174
+ base_params = {
175
+ 'lat': coords['lat'],
176
+ 'lon': coords['lon'],
177
+ 'appid': self.api_key,
178
+ 'units': self.units
179
+ }
102
180
  try:
103
181
  async with self.session.get(self.forecast_url,
104
182
  params=base_params, proxy=self.proxy) as resp:
105
183
  if resp.status == 200:
106
- self.queue.put_nowait((location, await resp.json()))
184
+ data = await resp.json()
185
+ logging.info(f'Queue forecast {location} {data}')
186
+ await self.queue.put((location, data))
107
187
  else:
108
- logging.warning('OpenWeather forecast API error '
109
- f'for {location}: {resp.status}')
188
+ logging.error(f'OpenWeather forecast API error for '
189
+ f'{location}: Status:{resp.status}, '
190
+ f'Response:{await resp.text()}')
110
191
  except Exception as e:
111
- logging.warning('OpenWeather forecast API error for '
112
- f'{location}: {e}')
192
+ logging.error(f'OpenWeather forecast API error for {location}: '
193
+ f'Exception:{type(e).__name__}, Message:{str(e)}')
113
194
 
114
195
  async def poll(self):
115
196
  """Poll OpenWeather APIs every 10 minutes."""
116
197
  now = int(time())
117
198
  if now % 600 == 0: # Every 10 minutes
118
199
  asyncio.create_task(self.fetch_current_data())
119
- if now % 10800 == 60: # Every 3 hours, offset by 1 minute
200
+ if now % 3600 == 60: # Every 3 hours, offset by 1 minute
120
201
  asyncio.create_task(self.fetch_forecast_data())
121
202
 
122
203
  async def start(self):