Skip to content

Shared Module

allsky_shared

allsky_shared.py

Part of allsky postprocess.py modules. https://github.com/AllskyTeam/allsky

This module is a common dumping ground for shared variables and functions used by various Allsky components.

It provides helpers for:

  • Reading environment variables and Allsky configuration
  • Managing small on-disk "databases" used as debug stores
  • Filesystem utilities (paths, permissions, extra-data files)
  • Database connection helpers and automatic purge routines
  • Overlay "extra data" JSON formatting and persistence

configure_tacho(tacho_id, gpio_pin, pulses_per_revolution=2, sample_seconds=10, stale_seconds=5, debounce_ms=0, name='', enabled=True)

Configure a server-side tachometer monitor via the Allsky HTTP API.

The server owns the GPIO interrupt and keeps it running between module executions. This helper is idempotent: calling it repeatedly with the same tacho_id and settings will not restart the interrupt.

Parameters:

Name Type Description Default
tacho_id str

Unique monitor identifier. Use a stable value such as "allsky_fans-1" so multiple modules can use separate monitors.

required
gpio_pin int | str

BCM GPIO number connected to the tacho signal.

required
pulses_per_revolution int | float

Number of tacho pulses per fan revolution, usually 2.

2
sample_seconds int | float

Rolling sample window used for RPM calculation.

10
stale_seconds int | float

Return 0 RPM after no pulses are seen for this long.

5
debounce_ms int

GPIO edge debounce time in milliseconds.

0
name str

Optional display name for GPIO status screens.

''
enabled bool

If false, the server stops the named tacho monitor.

True

Returns:

Name Type Description
dict

Parsed server status, including rpm and changed.

Raises:

Type Description
HTTPError

If the HTTP request fails (via raise_for_status).

Source code in stable/scripts/modules/allsky_shared.py
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
def configure_tacho(
    tacho_id,
    gpio_pin,
    pulses_per_revolution=2,
    sample_seconds=10,
    stale_seconds=5,
    debounce_ms=0,
    name="",
    enabled=True,
):
    """
    Configure a server-side tachometer monitor via the Allsky HTTP API.

    The server owns the GPIO interrupt and keeps it running between module
    executions. This helper is idempotent: calling it repeatedly with the same
    ``tacho_id`` and settings will not restart the interrupt.

    Args:
        tacho_id (str):
            Unique monitor identifier. Use a stable value such as
            ``"allsky_fans-1"`` so multiple modules can use separate monitors.
        gpio_pin (int | str):
            BCM GPIO number connected to the tacho signal.
        pulses_per_revolution (int | float):
            Number of tacho pulses per fan revolution, usually ``2``.
        sample_seconds (int | float):
            Rolling sample window used for RPM calculation.
        stale_seconds (int | float):
            Return ``0`` RPM after no pulses are seen for this long.
        debounce_ms (int):
            GPIO edge debounce time in milliseconds.
        name (str):
            Optional display name for GPIO status screens.
        enabled (bool):
            If false, the server stops the named tacho monitor.

    Returns:
        dict:
            Parsed server status, including ``rpm`` and ``changed``.

    Raises:
        requests.HTTPError:
            If the HTTP request fails (via ``raise_for_status``).
    """
    api_url = get_api_url()
    response = requests.put(
        f'{api_url}/gpio/tacho',
        json={
            'tacho_id': str(tacho_id),
            'pin': str(gpio_pin),
            'pulses_per_revolution': pulses_per_revolution,
            'sample_seconds': sample_seconds,
            'stale_seconds': stale_seconds,
            'debounce_ms': debounce_ms,
            'name': name,
            'enabled': enabled,
        },
        timeout=2
    )
    response.raise_for_status()
    return response.json()

convert_lat_lon(input)

Helper for converting latitude/longitude strings.

This wrapper calls the legacy :func:convertLatLon. New code should use this snake_case name; the camelCase function is kept so existing callers continue to work.

See :func:convertLatLon for details.

Source code in stable/scripts/modules/allsky_shared.py
482
483
484
485
486
487
488
489
490
491
492
def convert_lat_lon(input):
    """
    Helper for converting latitude/longitude strings.

    This wrapper calls the legacy :func:`convertLatLon`. New code should
    use this snake_case name; the camelCase function is kept so existing
    callers continue to work.

    See :func:`convertLatLon` for details.
    """
    return convertLatLon(input)

count_starts_in_image(image, mask_file_name=None)

Detect stars in an image using Photutils' DAOStarFinder.

The image is converted to grayscale if needed, optionally masked with :func:mask_image, and then processed with sigma-clipped statistics and DAOStarFinder to locate star centroids.

Parameters:

Name Type Description Default
image ndarray

Input image (grayscale or BGR).

required
mask_file_name str | None

Optional mask file name to apply before detection.

None

Returns:

Type Description

tuple[list[tuple[float, float]], numpy.ndarray]: A tuple containing:

  • A list of (x, y) star coordinates.
  • The (possibly masked) image used for detection.
Source code in stable/scripts/modules/allsky_shared.py
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
def count_starts_in_image(image, mask_file_name=None):
    """
    Detect stars in an image using Photutils' DAOStarFinder.

    The image is converted to grayscale if needed, optionally masked with
    :func:`mask_image`, and then processed with sigma-clipped statistics
    and DAOStarFinder to locate star centroids.

    Args:
        image (numpy.ndarray):
            Input image (grayscale or BGR).
        mask_file_name (str | None, optional):
            Optional mask file name to apply before detection.

    Returns:
        tuple[list[tuple[float, float]], numpy.ndarray]:
            A tuple containing:

              * A list of ``(x, y)`` star coordinates.
              * The (possibly masked) image used for detection.
    """
    from photutils.detection import DAOStarFinder
    from astropy.stats import sigma_clipped_stats
    import cv2

    # Convert to grayscale if it's RGB
    if image.ndim == 3:
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    else:
        gray = image

    if mask_file_name is not None and mask_file_name != '':
        gray = mask_image(gray, mask_file_name)

    # Convert to float for processing
    image_data = gray.astype(float)

    # Estimate background stats
    mean, median, std = sigma_clipped_stats(image_data, sigma=3.0)

    # Detect stars. An image without stars (clouds, twilight) is a normal result here,
    # not something to warn about in the log on every frame.
    import warnings
    daofind = DAOStarFinder(fwhm=3.0, threshold=5.0 * std)
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", message="No sources were found")
        sources = daofind(image_data - median)

    # Convert to list of (x, y) tuples if sources were found.
    # photutils 3.0 renamed the columns to x_centroid/y_centroid; the old names still
    # work there but log a deprecation warning, and will be removed in 4.0.
    coords = []
    if sources is not None and len(sources) > 0:
        new_names = 'x_centroid' in sources.colnames
        x = sources['x_centroid' if new_names else 'xcentroid'].tolist()
        y = sources['y_centroid' if new_names else 'ycentroid'].tolist()
        coords = list(zip(x, y))

    return coords, image

create_cardinal(degrees)

Convert a wind direction in degrees into a cardinal point.

Parameters:

Name Type Description Default
degrees

Direction in degrees (0–360). North is 0°/360°, east is 90°, and so on.

required

Returns:

Type Description

A string containing one of the 16-point compass directions

(e.g., 'N', 'NE', 'SW'), or 'N/A' if the input

cannot be interpreted.

Source code in stable/scripts/modules/allsky_shared.py
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
def create_cardinal(degrees):
    """
    Convert a wind direction in degrees into a cardinal point.

    Args:
        degrees: Direction in degrees (0–360). North is 0°/360°, east is
            90°, and so on.

    Returns:
        A string containing one of the 16-point compass directions
        (e.g., ``'N'``, ``'NE'``, ``'SW'``), or ``'N/A'`` if the input
        cannot be interpreted.
    """
    try:
        cardinals = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW','W', 'WNW', 'NW', 'NNW', 'N']
        cardinal = cardinals[round(degrees / 22.5)]
    except Exception:
        cardinal = 'N/A'

    return cardinal

create_device(import_name, class_name, bus_number, i2c_address='')

Instantiate an I2C device class on a given bus.

The device class is imported dynamically and initialised with an Adafruit Blinka busio.I2C object constructed from a known set of SCL/SDA pins for the requested bus number.

Parameters:

Name Type Description Default
import_name str

Module path to import (e.g. "adafruit_bme280").

required
class_name str

Name of the device class in that module.

required
bus_number int

I2C bus number (e.g. 1, 3, 4, 5, 6).

required
i2c_address str

Optional I2C address string (e.g. "0x76"). If omitted, the device class is constructed without an explicit address.

''

Returns:

Name Type Description
Any

An instance of the requested device class.

Raises:

Type Description
ImportError

If the module or class cannot be imported.

ValueError

If no pin mapping exists for the given bus number.

Source code in stable/scripts/modules/allsky_shared.py
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
def create_device(import_name: str, class_name: str, bus_number: int, i2c_address: str = ""):
    """
    Instantiate an I2C device class on a given bus.

    The device class is imported dynamically and initialised with an
    Adafruit Blinka ``busio.I2C`` object constructed from a known set of
    SCL/SDA pins for the requested bus number.

    Args:
        import_name (str):
            Module path to import (e.g. ``"adafruit_bme280"``).
        class_name (str):
            Name of the device class in that module.
        bus_number (int):
            I2C bus number (e.g. 1, 3, 4, 5, 6).
        i2c_address (str, optional):
            Optional I2C address string (e.g. ``"0x76"``). If omitted, the
            device class is constructed without an explicit address.

    Returns:
        Any:
            An instance of the requested device class.

    Raises:
        ImportError:
            If the module or class cannot be imported.
        ValueError:
            If no pin mapping exists for the given bus number.
    """
    bus_number = int(bus_number)

    # Define SCL/SDA pins for each bus
    I2C_BUS_PINS = {
        1: (board.SCL, board.SDA),
        3: (board.D5, board.D4),
        4: (board.D9, board.D8),
        5: (board.D13, board.D12),
        6: (board.D23, board.D22)
    }

    # Dynamically import the module and get the class
    try:
        module = importlib.import_module(import_name)
        cls = getattr(module, class_name)
    except (ImportError, AttributeError) as e:
        raise ImportError(f"Could not import '{class_name}' from '{import_name}': {e}")

    try:
        scl, sda = I2C_BUS_PINS[bus_number]
    except KeyError:
        raise ValueError(f"No pin mapping defined for I2C bus {bus_number}")

    i2c = busio.I2C(scl, sda)

    # Instantiate device
    if i2c_address:
        return cls(i2c, int(i2c_address, 0))
    else:
        return cls(i2c)

delete_extra_data(fileName)

Preferred wrapper for removing extra data files.

This is the underscore version and should be used in new code. It simply delegates to :func:deleteExtraData, which contains the legacy implementation.

Parameters:

Name Type Description Default
fileName str

File name to remove from all configured extra data directories.

required
Source code in stable/scripts/modules/allsky_shared.py
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
def delete_extra_data(fileName):
    """
    Preferred wrapper for removing extra data files.

    This is the underscore version and should be used in new code. It simply
    delegates to :func:`deleteExtraData`, which contains the legacy
    implementation.

    Args:
        fileName (str):
            File name to remove from all configured extra data directories.
    """
    deleteExtraData(fileName)

get_all_allsky_variables(show_empty=True, module='', indexed=False, raw=False)

Retrieve all known Allsky variables via the ALLSKYVARIABLES helper.

Parameters:

Name Type Description Default
show_empty bool

Whether to include variables that have no value. Defaults to True.

True
module str

Optional module filter, depending on the ALLSKYVARIABLES implementation.

''
indexed bool

If True, variables may be returned in an indexed form.

False
raw bool

If True, return raw data structures from ALLSKYVARIABLES.

False

Returns:

Name Type Description
Any

Whatever is returned by ALLSKYVARIABLES().get_variables(...).

Source code in stable/scripts/modules/allsky_shared.py
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
def get_all_allsky_variables(show_empty=True, module='', indexed=False, raw=False):
    """
    Retrieve all known Allsky variables via the ALLSKYVARIABLES helper.

    Args:
        show_empty (bool, optional):
            Whether to include variables that have no value. Defaults to True.
        module (str, optional):
            Optional module filter, depending on the ALLSKYVARIABLES
            implementation.
        indexed (bool, optional):
            If True, variables may be returned in an indexed form.
        raw (bool, optional):
            If True, return raw data structures from ALLSKYVARIABLES.

    Returns:
        Any:
            Whatever is returned by ``ALLSKYVARIABLES().get_variables(...)``.
    """
    allskyvariables = ALLSKYVARIABLES()
    return allskyvariables.get_variables(show_empty, module, indexed, raw)

get_allsky_variable(variable)

Look up a single Allsky variable from environment or extra-data files.

The lookup order is:

  1. Environment via :func:getEnvironmentVariable.
  2. JSON extra-data files in all extra-data directories.
  3. Text extra-data files in all extra-data directories.

Parameters:

Name Type Description Default
variable str

The variable name to retrieve.

required

Returns:

Name Type Description
Any

The variable value if found, otherwise None.

Source code in stable/scripts/modules/allsky_shared.py
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
def get_allsky_variable(variable):
    """
    Look up a single Allsky variable from environment or extra-data files.

    The lookup order is:

      1. Environment via :func:`getEnvironmentVariable`.
      2. JSON extra-data files in all extra-data directories.
      3. Text extra-data files in all extra-data directories.

    Args:
        variable (str):
            The variable name to retrieve.

    Returns:
        Any:
            The variable value if found, otherwise None.
    """
    result = getEnvironmentVariable(variable)
    if result is None:

        extra_data_paths = get_extra_dir()
        for extra_data_path in extra_data_paths:
            directory = Path(extra_data_path)

            for file_path in directory.iterdir():
                if file_path.is_file() and isFileReadable(file_path):

                    file_extension = Path(file_path).suffix

                    if file_extension == '.json':
                        result = _get_value_from_json_file(file_path, variable)

                    if file_extension == '.txt':
                        result = _get_value_from_text_file(file_path, variable)

                if result is not None:
                    break

    return result

get_allsky_version()

Convenience helper to retrieve and parse the Allsky version.

The version file path is taken from the ALLSKY_VERSION_FILE environment variable and passed to :func:parse_version.

Returns:

Name Type Description
dict

Parsed version info as returned by :func:parse_version.

Source code in stable/scripts/modules/allsky_shared.py
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
def get_allsky_version():
    """
    Convenience helper to retrieve and parse the Allsky version.

    The version file path is taken from the ``ALLSKY_VERSION_FILE``
    environment variable and passed to :func:`parse_version`.

    Returns:
        dict:
            Parsed version info as returned by :func:`parse_version`.
    """
    version_file = os.environ['ALLSKY_VERSION_FILE']
    version_info = parse_version(version_file)

    return version_info

get_api_url()

Resolve the Allsky API base URL from the environment.

If ALLSKY_API_URL is not present in the current environment, this helper calls :func:setupForCommandLine to load variables from the usual variables.json file, and then re-reads the environment.

Returns:

Name Type Description
str

The API base URL from ALLSKY_API_URL.

Source code in stable/scripts/modules/allsky_shared.py
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
def get_api_url():
    """
    Resolve the Allsky API base URL from the environment.

    If ``ALLSKY_API_URL`` is not present in the current environment, this
    helper calls :func:`setupForCommandLine` to load variables from the
    usual ``variables.json`` file, and then re-reads the environment.

    Returns:
        str:
            The API base URL from ``ALLSKY_API_URL``.
    """
    try:
        api_url = os.environ['ALLSKY_API_URL']
    except KeyError:
        setupForCommandLine()
        api_url = os.environ['ALLSKY_API_URL']

    return api_url

get_camera_gain()

Get the current camera gain.

For Raspberry Pi cameras, this reads the AnalogueGain value from the Pi metadata. For other camera types it uses the AS_GAIN environment variable.

If no gain can be determined, 0.0 is returned.

Returns:

Name Type Description
float

Camera gain value.

Source code in stable/scripts/modules/allsky_shared.py
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
def get_camera_gain():
    """
    Get the current camera gain.

    For Raspberry Pi cameras, this reads the ``AnalogueGain`` value from the
    Pi metadata. For other camera types it uses the ``AS_GAIN`` environment
    variable.

    If no gain can be determined, 0.0 is returned.

    Returns:
        float:
            Camera gain value.
    """
    gain = 0
    camera_type = get_camera_type()

    if camera_type == 'rpi':
        gain = get_rpi_meta_value('AnalogueGain')
    else:
        gain = get_environment_variable('AS_GAIN')

    if gain == None:
        gain = 0

    return asfloat(gain)

get_camera_type()

Get the configured camera type from the environment.

Returns:

Name Type Description
str

Lowercase camera type string (e.g. "rpi").

Source code in stable/scripts/modules/allsky_shared.py
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
def get_camera_type():
    """
    Get the configured camera type from the environment.

    Returns:
        str:
            Lowercase camera type string (e.g. ``"rpi"``).
    """
    camera_type = get_environment_variable('CAMERA_TYPE')
    return camera_type.lower()

get_ecowitt_data(api_key, app_key, mac_address, temp_unitid=1, pressure_unitid=3)

Fetch live weather data from the remote Ecowitt cloud API.

If all of the required credentials are non-empty, the function builds an API URL and attempts to parse a range of fields such as outdoor and indoor temperatures, humidity, rainfall, wind, pressure, and lightning.

All fields are returned in a nested dict with sensible defaults of None.

Parameters:

Name Type Description Default
api_key str

Ecowitt API key.

required
app_key str

Ecowitt application key.

required
mac_address str

Device MAC address registered with Ecowitt.

required
temp_unitid int

Temperature unit ID expected by the API. Defaults to 1.

1
pressure_unitid int

Pressure unit ID expected by the API. Defaults to 3.

3

Returns:

Type Description

dict | str: On success, a nested dictionary of parsed values. On HTTP error, a descriptive error string may be returned instead.

Source code in stable/scripts/modules/allsky_shared.py
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
def get_ecowitt_data(api_key, app_key, mac_address, temp_unitid=1, pressure_unitid=3):
    """
    Fetch live weather data from the remote Ecowitt cloud API.

    If all of the required credentials are non-empty, the function builds
    an API URL and attempts to parse a range of fields such as outdoor and
    indoor temperatures, humidity, rainfall, wind, pressure, and lightning.

    All fields are returned in a nested dict with sensible defaults of None.

    Args:
        api_key (str):
            Ecowitt API key.
        app_key (str):
            Ecowitt application key.
        mac_address (str):
            Device MAC address registered with Ecowitt.
        temp_unitid (int, optional):
            Temperature unit ID expected by the API. Defaults to 1.
        pressure_unitid (int, optional):
            Pressure unit ID expected by the API. Defaults to 3.

    Returns:
        dict | str:
            On success, a nested dictionary of parsed values. On HTTP error,
            a descriptive error string may be returned instead.
    """
    result = {
        'outdoor': {
            'temperature': None,
            'feels_like': None,
            'humidity': None,
            'app_temp': None,
            'dew_point': None
        },
        'indoor': {
            'temperature': None,
            'humidity': None
        },
        'solar_and_uvi': {
            'solar': None,
            'uvi': None
        },
        'rainfall': {
            'rain_rate': None,
            'daily': None,
            'event': None,
            'hourly': None,
            'weekly': None,
            'monthly': None,
            'yearly': None
        },
        'wind': {
            'wind_speed': None,
            'wind_gust': None,
            'wind_direction': None
        },
        'pressure': {
            'relative': None,
            'absolute': None
        },
        'lightning': {
            'distance': None,
            'count': None
        }
    }
    if all(var.strip() for var in (app_key, api_key, mac_address)):
        ECOWITT_API_URL = f'https://api.ecowitt.net/api/v3/device/real_time?application_key={app_key}&api_key={api_key}&mac={mac_address}&call_back=all&temp_unitid={temp_unitid}&pressure_unitid={pressure_unitid}'

        log(4, f"INFO: Reading Ecowitt API from - {ECOWITT_API_URL}")
        try:
            response = requests.get(ECOWITT_API_URL)
            if response.status_code == 200:
                raw_data = response.json()

                result['outdoor']['temperature'] = _get_nested_value(raw_data, 'data.outdoor.temperature.value', float)
                result['outdoor']['feels_like'] = _get_nested_value(raw_data, 'data.outdoor.feels_like.value', float)
                result['outdoor']['humidity'] = _get_nested_value(raw_data, 'data.outdoor.humidity.value', float)
                result['outdoor']['app_temp'] = _get_nested_value(raw_data, 'data.outdoor.app_temp.value', float)
                result['outdoor']['dew_point'] = _get_nested_value(raw_data, 'data.outdoor.dew_point.value', float)

                result['indoor']['temperature'] = _get_nested_value(raw_data, 'data.indoor.temperature.value', float)
                result['indoor']['humidity'] = _get_nested_value(raw_data, 'data.indoor.humidity.value', float)

                result['solar_and_uvi']['solar'] = _get_nested_value(raw_data, 'data.solar_and_uvi.solar.value', float)
                result['solar_and_uvi']['uvi'] = _get_nested_value(raw_data, 'data.solar_and_uvi.uvi.value', float)

                result['rainfall']['rain_rate'] = _get_nested_value(raw_data, 'data.rainfall.rain_rate.value', float)
                result['rainfall']['daily'] = _get_nested_value(raw_data, 'data.rainfall.daily.value', float)
                result['rainfall']['event'] = _get_nested_value(raw_data, 'data.rainfall.event.value', float)
                result['rainfall']['hourly'] = _get_nested_value(raw_data, 'data.rainfall.hourly.value', float)
                result['rainfall']['weekly'] = _get_nested_value(raw_data, 'data.rainfall.weekly.value', float)
                result['rainfall']['monthly'] = _get_nested_value(raw_data, 'data.rainfall.monthly.value', float)
                result['rainfall']['yearly'] = _get_nested_value(raw_data, 'data.rainfall.yearly.value', float)

                result['wind']['wind_speed'] = _get_nested_value(raw_data, 'data.wind.wind_speed.value', float)
                result['wind']['wind_gust'] = _get_nested_value(raw_data, 'data.wind.wind_gust.value', float)
                result['wind']['wind_direction'] = _get_nested_value(raw_data, 'data.wind.wind_direction.value', int)

                result['pressure']['relative'] = _get_nested_value(raw_data, 'data.pressure.relative.value', float)
                result['pressure']['absolute'] = _get_nested_value(raw_data, 'data.pressure.absolute.value', float)

                result['lightning']['distance'] = _get_nested_value(raw_data, 'data.lightning.distance.value', float)
                result['lightning']['count'] = _get_nested_value(raw_data, 'data.lightning.count.value', int)

                log(1, f'INFO: Data read from Ecowitt API')
            else:
                result = f'Got error from the Ecowitt API. Response code {response.status_code}'
                log(0, f'ERROR: {result}')
        except Exception as e:
            me = os.path.basename(__file__)
            eType, eObject, eTraceback = sys.exc_info()
            log(0, f'ERROR: Failed to read data from Ecowitt on line {eTraceback.tb_lineno} in {me} - {e}')
    else:
        log(0, 'ERROR: Missing Ecowitt Application Key, API Key or MAC Address')

    return result

get_ecowitt_local_data(address, password=None)

Fetch live weather data directly from a local Ecowitt gateway.

This variant talks to the gateway's local HTTP API and parses a variety of metrics such as temperatures, humidity, rainfall, wind, pressure and lightning. Values are returned in a nested dict with None defaults.

Units are parsed from the API response and temperature values are converted to Celsius when needed.

Parameters:

Name Type Description Default
address str

Base URL or IP address of the Ecowitt gateway.

required
password str

Reserved for password-protected gateways (currently unused).

None

Returns:

Name Type Description
dict

Nested dictionary of parsed values.

Source code in stable/scripts/modules/allsky_shared.py
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
def get_ecowitt_local_data(address, password=None):
    """
    Fetch live weather data directly from a local Ecowitt gateway.

    This variant talks to the gateway's local HTTP API and parses a variety
    of metrics such as temperatures, humidity, rainfall, wind, pressure and
    lightning. Values are returned in a nested dict with None defaults.

    Units are parsed from the API response and temperature values are
    converted to Celsius when needed.

    Args:
        address (str):
            Base URL or IP address of the Ecowitt gateway.
        password (str, optional):
            Reserved for password-protected gateways (currently unused).

    Returns:
        dict:
            Nested dictionary of parsed values.
    """
    '''
    Temp 0 - C, 1 - F
    Pressure 0 - hPA, 1 - inHg, 2 - mmHg
    Wind 0 - m/s, 1 - km/h, 2 - mph, 3 - knots, 5 - Beaufort
    Rain 0 - mm, 1 - in
    Irradiance 0 - Klux, 1 - W/m2, 2 - Kfc
    Capacity 0 - L, 1 - m3, 2 - Gal
    '''

    result = {
        'outdoor': {
            'temperature': None,
            'feels_like': None,
            'humidity': None,
            'app_temp': None,
            'dew_point': None
        },
        'indoor': {
            'temperature': None,
            'humidity': None
        },
        'solar_and_uvi': {
            'solar': None,
            'uvi': None
        },
        'rainfall': {
            'rain_rate': None,
            'daily': None,
            'event': None,
            'hourly': None,
            'weekly': None,
            'monthly': None,
            'yearly': None
        },
        'wind': {
            'wind_speed': None,
            'wind_gust': None,
            'wind_direction': None
        },
        'pressure': {
            'relative': None,
            'absolute': None
        },
        'lightning': {
            'distance': None,
            'count': None
        }
    }

    def parse_val(val, as_type=float, unit=None):
        """
        Extract numeric part from a value string and optionally convert °F to °C.

        Args:
            val (Any):
                Raw value from the Ecowitt JSON (may contain unit text).
            as_type (callable):
                Type to cast the numeric part to (e.g. float, int).
            unit (str | None):
                Optional unit string; when reported as Fahrenheit, values are
                converted to Celsius.

        Returns:
            Any | None:
                Parsed and optionally converted value, or None on failure.
        """
        if val is None:
            return None
        try:
            num_str = str(val).strip().split()[0].strip('%')
            value = as_type(num_str)
            if unit:
                unit = unit.lower()
                if unit in ['f', '°f']:
                    value = round((value - 32) * 5 / 9, 2)
            return value
        except (ValueError, TypeError):
            return None

    def get_val_and_unit(data_list, target_id):
        """
        Helper to locate a record in an Ecowitt list and extract (value, unit).

        Args:
            data_list (list[dict]):
                List of readings as returned by the Ecowitt gateway.
            target_id (str):
                Identifier to match in each dict's ``"id"`` field.

        Returns:
            tuple:
                ``(value, unit)`` where both may be None if the ID is not found.
        """
        for item in data_list:
            if item.get("id") == target_id:
                return item.get("val"), item.get("unit", None)
        return None, None

    LIVE_URL = f'{address}/get_livedata_info?'

    try:
        response = requests.get(LIVE_URL)
        if response.status_code == 200:
            live_data = response.json()

            common = live_data.get("common_list", [])
            val, unit = get_val_and_unit(common, "0x02")
            result['outdoor']['temperature'] = parse_val(val, float, unit)

            val, unit = get_val_and_unit(common, "0x07")
            result['outdoor']['humidity'] = parse_val(val, int, unit)

            val, unit = get_val_and_unit(common, "3")
            result['outdoor']['feels_like'] = parse_val(val, float, unit)

            val, unit = get_val_and_unit(common, "0x03")
            result['outdoor']['dew_point'] = parse_val(val, float, unit)

            val, unit = get_val_and_unit(common, "0x0B")
            result['wind']['wind_speed'] = parse_val(val, float, unit)

            val, unit = get_val_and_unit(common, "0x0C")
            result['wind']['wind_gust'] = parse_val(val, float, unit)

            val, unit = get_val_and_unit(common, "0x0A")
            result['wind']['wind_direction'] = parse_val(val, int, unit)

            val, unit = get_val_and_unit(common, "0x15")
            result['solar_and_uvi']['solar'] = parse_val(val, float, unit)

            val, unit = get_val_and_unit(common, "0x17")
            result['solar_and_uvi']['uvi'] = parse_val(val, int, unit)

            # --- Rain ---
            rain = live_data.get("rain", [])
            for rid, key in {
                "0x0D": "event",
                "0x0E": "rain_rate",
                "0x10": "hourly",
                "0x11": "daily",
                "0x12": "weekly",
                "0x13": "yearly"
            }.items():
                val, unit = get_val_and_unit(rain, rid)
                result['rainfall'][key] = parse_val(val, float, unit)

            # --- WH25 Indoor Sensor ---
            wh25 = live_data.get("wh25", [{}])[0]
            result['indoor']['temperature'] = parse_val(wh25.get("intemp"), float, wh25.get("unit"))
            result['indoor']['humidity'] = parse_val(wh25.get("inhumi"), int)

            result['pressure']['absolute'] = parse_val(wh25.get("abs"), float)
            result['pressure']['relative'] = parse_val(wh25.get("rel"), float)

            # --- Lightning ---
            lightning = live_data.get("lightning", [{}])[0]
            result['lightning']['distance'] = parse_val(lightning.get("distance"), float)
            result['lightning']['count'] = parse_val(lightning.get("count"), int)

    except Exception as e:
        me = os.path.basename(__file__)
        eType, eObject, eTraceback = sys.exc_info()
        log(0, f'ERROR: Failed to read live data from the local Ecowitt gateway on line {eTraceback.tb_lineno} in {me} - {e}')

    return result

get_environment_variable(name, fatal=False, debug=False, try_allsky_debug_file=False)

Helper for reading an environment variable.

This is the modern, snake_case wrapper around the legacy :func:getEnvironmentVariable implementation. New code should call this function rather than the camelCase version.

Parameters:

Name Type Description Default
name

Name of the environment variable to read.

required
fatal

If True and the variable cannot be resolved, the process will terminate with an error.

False
debug

If True, values are read from the debug database instead of the real environment.

False
try_allsky_debug_file

When False (default), the function will fall back to loading variables from variables.json. When True, it will instead try to look up the value in the overlay debug data file.

False

Returns:

Type Description

The resolved value as a string, or None if not found (and fatal

is False).

Source code in stable/scripts/modules/allsky_shared.py
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
def get_environment_variable(name, fatal=False, debug=False, try_allsky_debug_file=False):
    """
    Helper for reading an environment variable.

    This is the modern, snake_case wrapper around the legacy
    :func:`getEnvironmentVariable` implementation. New code should call this
    function rather than the camelCase version.

    Args:
        name: Name of the environment variable to read.
        fatal: If True and the variable cannot be resolved, the process will
            terminate with an error.
        debug: If True, values are read from the debug database instead of
            the real environment.
        try_allsky_debug_file: When False (default), the function will fall
            back to loading variables from ``variables.json``. When True,
            it will instead try to look up the value in the overlay debug
            data file.

    Returns:
        The resolved value as a string, or None if not found (and ``fatal``
        is False).
    """
    return getEnvironmentVariable(name, fatal, debug)

get_extra_dir(current_only=False)

Helper to get the "extra data" directory or directories.

This simply calls :func:getExtraDir. New code should use this snake_case name.

Source code in stable/scripts/modules/allsky_shared.py
1699
1700
1701
1702
1703
1704
1705
1706
def get_extra_dir(current_only:bool = False) -> list[str] | str:
    """
    Helper to get the "extra data" directory or directories.

    This simply calls :func:`getExtraDir`. New code should use this
    snake_case name.
    """
    return getExtraDir(current_only)

get_flows_with_module(module_name)

Scan module flow files and return those containing a given module.

Only postprocessing_*.json files that are not debug variants are considered. Files that fail to parse are quietly ignored.

Parameters:

Name Type Description Default
module_name str

Name of the module to search for in the flow definitions.

required

Returns:

Name Type Description
dict

Mapping of filename to parsed JSON content for flows that contain the given module.

Source code in stable/scripts/modules/allsky_shared.py
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
def get_flows_with_module(module_name):
    """
    Scan module flow files and return those containing a given module.

    Only ``postprocessing_*.json`` files that are not debug variants are
    considered. Files that fail to parse are quietly ignored.

    Args:
        module_name (str):
            Name of the module to search for in the flow definitions.

    Returns:
        dict:
            Mapping of filename to parsed JSON content for flows that contain
            the given module.
    """
    folder = Path(ALLSKY_MODULES)
    found: Dict[str, Any] = {}

    for file in folder.glob("*.json"):
        if not file.name.endswith("-debug.json"):
            if file.name.startswith("postprocessing_"):
                try:
                    with file.open("r", encoding="utf-8") as f:
                        data = json.load(f)

                    if module_name in data:
                        found[file.name] = data

                except (json.JSONDecodeError, OSError) as e:
                    pass

    return found

get_gpio_pin(gpio_pin, pi=None, show_errors=False)

Read the logical state of a GPIO pin via the Allsky API (legacy alias).

This definition simply calls :func:read_gpio_pin. It is kept for backward compatibility with existing code that expects get_gpio_pin to return a pin value rather than a board pin object.

Parameters:

Name Type Description Default
gpio_pin int | str

Pin identifier understood by the Allsky API.

required
pi Any

Unused placeholder kept for interface compatibility.

None
show_errors bool

Currently unused; kept for interface compatibility.

False

Returns:

Name Type Description
bool

True if the GPIO is reported as "on", False otherwise.

Source code in stable/scripts/modules/allsky_shared.py
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
def get_gpio_pin(gpio_pin, pi=None, show_errors=False):
    """
    Read the logical state of a GPIO pin via the Allsky API (legacy alias).

    This definition simply calls :func:`read_gpio_pin`. It is kept for
    backward compatibility with existing code that expects ``get_gpio_pin``
    to return a pin value rather than a board pin object.

    Args:
        gpio_pin (int | str):
            Pin identifier understood by the Allsky API.
        pi (Any, optional):
            Unused placeholder kept for interface compatibility.
        show_errors (bool, optional):
            Currently unused; kept for interface compatibility.

    Returns:
        bool:
            True if the GPIO is reported as ``"on"``, False otherwise.
    """
    return read_gpio_pin(gpio_pin, pi=None, show_errors=False)

get_gpio_pin_details(pin)

Get the CircuitPython board pin object for a given numeric pin.

This is a convenience wrapper around :func:getGPIOPin and should be used by new code.

Parameters:

Name Type Description Default
pin int

Numeric pin index (0–27) corresponding to a board pin.

required

Returns:

Name Type Description
Any

The matching board.Dx constant, or None if the pin is unknown.

Source code in stable/scripts/modules/allsky_shared.py
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
def get_gpio_pin_details(pin):
    """
    Get the CircuitPython ``board`` pin object for a given numeric pin.

    This is a convenience wrapper around :func:`getGPIOPin` and should be
    used by new code.

    Args:
        pin (int):
            Numeric pin index (0–27) corresponding to a board pin.

    Returns:
        Any:
            The matching ``board.Dx`` constant, or None if the pin is unknown.
    """
    return getGPIOPin(pin)

get_hass_sensor_value(ha_url, ha_ltt, ha_sensor)

Query a Home Assistant sensor and return its numeric state.

A GET request is sent to the Home Assistant REST API using the supplied long-lived token. The sensor's state is parsed as a float on success.

Parameters:

Name Type Description Default
ha_url str

Base URL of the Home Assistant instance (e.g. "http://host:8123").

required
ha_ltt str

Long-lived access token for Home Assistant.

required
ha_sensor str

Entity ID of the sensor (e.g. "sensor.outdoor_temp").

required

Returns:

Type Description

float | None: The sensor state as a float, or None if the sensor cannot be read.

Source code in stable/scripts/modules/allsky_shared.py
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
def get_hass_sensor_value(ha_url, ha_ltt, ha_sensor):
    """
    Query a Home Assistant sensor and return its numeric state.

    A GET request is sent to the Home Assistant REST API using the supplied
    long-lived token. The sensor's state is parsed as a float on success.

    Args:
        ha_url (str):
            Base URL of the Home Assistant instance (e.g. ``"http://host:8123"``).
        ha_ltt (str):
            Long-lived access token for Home Assistant.
        ha_sensor (str):
            Entity ID of the sensor (e.g. ``"sensor.outdoor_temp"``).

    Returns:
        float | None:
            The sensor state as a float, or None if the sensor cannot be read.
    """
    result = None

    headers = {
        'Authorization': f'Bearer {ha_ltt}',
        'Content-Type': 'application/json',
    }

    try:
        response = requests.get(f'{ha_url}/api/states/{ha_sensor}', headers=headers)

        if response.status_code == 200:
            result = float(response.json().get('state'))
        else:
            if response.status_code == 404:
                log(0, f'ERROR: Unable to read {ha_sensor} from {ha_url}. homeassistant reports the sensor does not exist')
            else:
                if response.status_code == 401:
                    log(0, f'ERROR: Unable to read {ha_sensor} from {ha_url}. homeassistant reports the token is unauthorised')
                else:
                    log(0, f'ERROR: Unable to read {ha_sensor} from {ha_url}. Error code {response.status_code}')

    except Exception as e:
        me = os.path.basename(__file__)
        eType, eObject, eTraceback = sys.exc_info()
        log(0, f'ERROR: Failed to read data from Homeassistant {eTraceback.tb_lineno} in {me} - {e}')
        result = None

    return result

get_lat_lon()

Read latitude and longitude from settings and return them as floats.

The settings latitude and longitude may be stored in a variety of formats supported by :func:convert_lat_lon (for example, 51.5N or -0.13). If a value is empty, the corresponding return value is None.

Returns:

Type Description

Tuple of (lat, lon) where each element is either a float or

None if not defined.

Source code in stable/scripts/modules/allsky_shared.py
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
def get_lat_lon():
    """
    Read latitude and longitude from settings and return them as floats.

    The settings ``latitude`` and ``longitude`` may be stored in a variety
    of formats supported by :func:`convert_lat_lon` (for example,
    ``51.5N`` or ``-0.13``). If a value is empty, the corresponding
    return value is ``None``.

    Returns:
        Tuple of ``(lat, lon)`` where each element is either a float or
        None if not defined.
    """
    lat = None
    lon = None

    temp_lat = get_setting('latitude')
    if temp_lat != '':
        lat = convert_lat_lon(temp_lat)
    temp_lon = get_setting('longitude')
    if temp_lon != '':
        lon = convert_lat_lon(temp_lon)

    return lat, lon

get_pi_info(info)

Query simple hardware details about the Raspberry Pi.

Parameters:

Name Type Description Default
info

One of the predefined constants:

  • PI_INFO_MODEL – return the board model string.
  • Pi_INFO_CPU_TEMPERATURE – return the CPU temperature in °C.
required

Returns:

Type Description

The requested value, or None if info does not match a

supported constant.

Source code in stable/scripts/modules/allsky_shared.py
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
def get_pi_info(info):
    """
    Query simple hardware details about the Raspberry Pi.

    Args:
        info:
            One of the predefined constants:

            - ``PI_INFO_MODEL`` – return the board model string.
            - ``Pi_INFO_CPU_TEMPERATURE`` – return the CPU temperature in °C.

    Returns:
        The requested value, or None if ``info`` does not match a
        supported constant.
    """
    from gpiozero import CPUTemperature
    result = None

    if info == PI_INFO_MODEL:
        pi_info = get_pi_board_info()
        if pi_info is not None:
            result = pi_info.model

    if info == Pi_INFO_CPU_TEMPERATURE:
        result = CPUTemperature().temperature

    return result

get_rpi_meta_value(key)

Read a single value from the Raspberry Pi camera metadata file.

The metadata file format can be either JSON or simple key=value text. This helper tries JSON first and falls back to line-based parsing.

Parameters:

Name Type Description Default
key str

Metadata key to retrieve.

required

Returns:

Name Type Description
Any

The value if found, or 0 if the file is missing, unreadable, or the key is not present.

Source code in stable/scripts/modules/allsky_shared.py
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
def get_rpi_meta_value(key):
    """
    Read a single value from the Raspberry Pi camera metadata file.

    The metadata file format can be either JSON or simple ``key=value`` text.
    This helper tries JSON first and falls back to line-based parsing.

    Args:
        key (str):
            Metadata key to retrieve.

    Returns:
        Any:
            The value if found, or 0 if the file is missing, unreadable, or the
            key is not present.
    """
    result = None
    metadata_path = get_rpi_metadata()

    if metadata_path is not None:
        try:
            with open(metadata_path, 'r') as file:
                metadata = json.load(file)
                if key in metadata:
                    result = metadata[key]
        except json.JSONDecodeError as e:
            with open(metadata_path, 'r') as f:
                for line in f:
                    if line.startswith(key + "="):
                        result = line.split("=", 1)[1].strip()
        except FileNotFoundError as e:
            result = 0
        except Exception as e:
            result = 0

    return result

get_rpi_metadata()

Determine the path to the Raspberry Pi camera metadata file.

The metadata path is extracted from the extraargs in the main settings file if a --metadata argument is present. If no explicit path is found, a default of metadata.txt in the current directory is used.

Returns:

Name Type Description
str

Path to the metadata file.

Source code in stable/scripts/modules/allsky_shared.py
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
def get_rpi_metadata():
    """
    Determine the path to the Raspberry Pi camera metadata file.

    The metadata path is extracted from the ``extraargs`` in the main
    settings file if a ``--metadata`` argument is present. If no explicit
    path is found, a default of ``metadata.txt`` in the current directory
    is used.

    Returns:
        str:
            Path to the metadata file.
    """
    with open(ALLSKY_SETTINGS_FILE) as file:
        config = json.load(file)

    extraargs = config.get('extraargs', '')
    args = shlex.split(extraargs)

    metadata_path = None
    for i, arg in enumerate(args):
        if arg == '--metadata' and i + 1 < len(args):
            metadata_path = args[i + 1]
            break
    if metadata_path is None:
        metadata_path = os.path.join(ALLSKY_CURRENT_DIR, 'metadata.txt')

    return metadata_path

get_secrets(keys)

Load one or more secrets from env.json in ALLSKYPATH.

The file is expected to contain a flat JSON object mapping key names to secret values. Only the requested keys are returned; missing keys are silently ignored.

Parameters:

Name Type Description Default
keys Union[str, List[str]]

Either a single key (string) or a list of key names to fetch.

required

Returns:

Type Description
Union[str, Dict[str, str], None]

If a single key was requested, the value as a string (or None if

Union[str, Dict[str, str], None]

it is not present).

Union[str, Dict[str, str], None]

If multiple keys were requested, a dict mapping each key to

Union[str, Dict[str, str], None]

its value. Keys that are not found are omitted from the mapping.

Union[str, Dict[str, str], None]

If the file cannot be read or parsed, returns None (single key) or

Union[str, Dict[str, str], None]

an empty dict (multiple keys).

Source code in stable/scripts/modules/allsky_shared.py
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
def get_secrets(keys: Union[str, List[str]]) -> Union[str, Dict[str, str], None]:
    """
    Load one or more secrets from ``env.json`` in ``ALLSKYPATH``.

    The file is expected to contain a flat JSON object mapping key names
    to secret values. Only the requested keys are returned; missing keys
    are silently ignored.

    Args:
        keys:
            Either a single key (string) or a list of key names to fetch.

    Returns:
        If a single key was requested, the value as a string (or None if
        it is not present).

        If multiple keys were requested, a ``dict`` mapping each key to
        its value. Keys that are not found are omitted from the mapping.

        If the file cannot be read or parsed, returns None (single key) or
        an empty dict (multiple keys).
    """
    single = isinstance(keys, str)
    if single:
        keys = [keys]

    try:
        file_path = os.path.join(ALLSKYPATH, 'env.json')
        with open(file_path, 'r') as f:
            secrets = json.load(f)

        results = {k: secrets[k] for k in keys if k in secrets}

        if single:
            return results.get(keys[0])
        return results

    except (IOError, json.JSONDecodeError) as e:
        print(f"Error reading secrets file: {e}")
        return None if single else {}

get_sensor_temperature()

Get the current sensor temperature for the active camera.

For Raspberry Pi cameras, this reads the SensorTemperature value from the Pi metadata. For other camera types it uses the AS_TEMPERATURE_C environment variable.

If no temperature can be determined, 0.0 is returned.

Returns:

Name Type Description
float

Sensor temperature in °C.

Source code in stable/scripts/modules/allsky_shared.py
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
def get_sensor_temperature():
    """
    Get the current sensor temperature for the active camera.

    For Raspberry Pi cameras, this reads the ``SensorTemperature`` value
    from the Pi metadata. For other camera types it uses the
    ``AS_TEMPERATURE_C`` environment variable.

    If no temperature can be determined, 0.0 is returned.

    Returns:
        float:
            Sensor temperature in °C.
    """
    temperature = 0
    camera_type = get_camera_type()

    if camera_type == 'rpi':
        temperature = get_rpi_meta_value('SensorTemperature')
    else:
        temperature = get_environment_variable('AS_TEMPERATURE_C')

    if temperature == None:
        temperature = 0

    return asfloat(temperature)

get_setting(settingName)

Helper for reading a setting from the loaded settings JSON.

This wraps the legacy :func:getSetting and should be used in new code. See :func:getSetting for details.

Source code in stable/scripts/modules/allsky_shared.py
761
762
763
764
765
766
767
768
def get_setting(settingName):
    """
    Helper for reading a setting from the loaded settings JSON.

    This wraps the legacy :func:`getSetting` and should be used in new
    code. See :func:`getSetting` for details.
    """
    return getSetting(settingName)

get_tacho_speed(tacho_id, gpio_pin=None, pulses_per_revolution=2, sample_seconds=10, stale_seconds=5, debounce_ms=0, name='')

Return the current RPM for a tacho monitor.

If gpio_pin is supplied, the monitor is first configured/ensured. This is the usual path for periodic modules: they can call this every run and the server will only reconfigure the GPIO interrupt if the settings changed.

Source code in stable/scripts/modules/allsky_shared.py
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
def get_tacho_speed(
    tacho_id,
    gpio_pin=None,
    pulses_per_revolution=2,
    sample_seconds=10,
    stale_seconds=5,
    debounce_ms=0,
    name="",
):
    """
    Return the current RPM for a tacho monitor.

    If ``gpio_pin`` is supplied, the monitor is first configured/ensured. This
    is the usual path for periodic modules: they can call this every run and the
    server will only reconfigure the GPIO interrupt if the settings changed.
    """
    if gpio_pin is None:
        data = read_tacho(tacho_id)
    else:
        data = configure_tacho(
            tacho_id,
            gpio_pin,
            pulses_per_revolution=pulses_per_revolution,
            sample_seconds=sample_seconds,
            stale_seconds=stale_seconds,
            debounce_ms=debounce_ms,
            name=name,
            enabled=True,
        )

    return float(data.get('rpm', 0))

get_value_from_debug_data(key)

Look up a key in the overlay debug file.

This function is used when running in debug mode to retrieve values that would normally be supplied via environment variables.

Parameters:

Name Type Description Default
key str

Name of the value to retrieve.

required

Returns:

Type Description
str | None

The corresponding value as a string with whitespace removed, or

str | None

None if the file does not exist, cannot be read, or the key is

str | None

not present.

Source code in stable/scripts/modules/allsky_shared.py
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
def get_value_from_debug_data(key: str) -> str | None:
    """
    Look up a key in the overlay debug file.

    This function is used when running in debug mode to retrieve values
    that would normally be supplied via environment variables.

    Args:
        key: Name of the value to retrieve.

    Returns:
        The corresponding value as a string with whitespace removed, or
        None if the file does not exist, cannot be read, or the key is
        not present.
    """
    setup_for_command_line()

    try:
        allsky_tmp = os.environ["ALLSKY_TMP"]
        file_path = os.path.join(allsky_tmp, "overlaydebug.txt")

        try:
            with open(file_path, "r", encoding="utf-8") as f:
                for line in f:
                    if not line.strip() or line.strip().startswith("#"):
                        continue

                    parts = line.split(maxsplit=1)
                    if len(parts) == 2 and parts[0] == key:
                        return "".join(parts[1].split())
        except FileNotFoundError:
            return None
    except:
        return None
    return None

load_extra_data_file(file_name, type='')

Load an extra data file from one or more ALLSKY_EXTRA directories.

This helper looks for the given file name in all configured extra-data directories (via :func:get_extra_dir). If it finds a readable file, it will attempt to parse it according to its extension.

Currently only JSON files are parsed. Text files are recognised but not yet processed (the block is a placeholder).

Parameters:

Name Type Description Default
file_name str

Name of the extra data file to load (e.g. "extra.json").

required
type str

Force the file type. If set to "json", the file is parsed as JSON regardless of its extension.

''

Returns:

Name Type Description
dict

Parsed JSON data if successful; otherwise an empty dict.

Source code in stable/scripts/modules/allsky_shared.py
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
def load_extra_data_file(file_name, type=''):
    """
    Load an extra data file from one or more ALLSKY_EXTRA directories.

    This helper looks for the given file name in all configured extra-data
    directories (via :func:`get_extra_dir`). If it finds a readable file,
    it will attempt to parse it according to its extension.

    Currently only JSON files are parsed. Text files are recognised but
    not yet processed (the block is a placeholder).

    Args:
        file_name (str):
            Name of the extra data file to load (e.g. ``"extra.json"``).
        type (str, optional):
            Force the file type. If set to ``"json"``, the file is parsed
            as JSON regardless of its extension.

    Returns:
        dict:
            Parsed JSON data if successful; otherwise an empty dict.
    """
    result = {}
    extra_data_paths = get_extra_dir()
    for extra_data_path in extra_data_paths:
        if extra_data_path is not None:               # it should never be None
            extra_data_filename = os.path.join(extra_data_path, file_name)
            file_path = Path(extra_data_filename)
            if file_path.is_file() and isFileReadable(file_path):
                file_extension = Path(file_path).suffix

                if file_extension == '.json' or type == 'json':
                    try:
                        with open(extra_data_filename, 'r') as file:
                            result = json.load(file)
                    except json.JSONDecodeError:
                        log(0, f'ERROR: cannot read {extra_data_filename}.')

                if file_extension == '.txt':
                    pass

    return result

load_json_file(path)

Load a JSON file and return its parsed contents.

Parameters:

Name Type Description Default
path str | Path

Path to the JSON file.

required

Returns:

Type Description

The parsed JSON data (dict or list). If the file does not exist,

cannot be read, or contains invalid JSON, an empty dict is

returned.

Source code in stable/scripts/modules/allsky_shared.py
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
def load_json_file(path: str | Path):
    """
    Load a JSON file and return its parsed contents.

    Args:
        path: Path to the JSON file.

    Returns:
        The parsed JSON data (dict or list). If the file does not exist,
        cannot be read, or contains invalid JSON, an empty dict is
        returned.
    """
    try:
        file_path = Path(path)
        if not file_path.is_file():
            return {}

        with file_path.open("r", encoding="utf-8") as file:
            return json.load(file)

    except (OSError, json.JSONDecodeError):
        return {}

load_mask(mask_file_name, target_image=None)

Load a grayscale mask image and resize it to match a target image.

The mask is loaded from ALLSKY_OVERLAY/images/<mask_file_name> and converted to a float mask in the range [0, 1]. If the mask dimensions do not match the target image, it is resized accordingly if a target_image is passed

Parameters:

Name Type Description Default
mask_file_name str

Name of the mask file to load.

required
target_image ndarray

Target image whose shape is used for resizing or None

None

Returns:

Type Description
ndarray | None

numpy.ndarray | None: Float mask array in the range [0, 1], or None if the mask could not be loaded.

Source code in stable/scripts/modules/allsky_shared.py
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
def load_mask(mask_file_name:str, target_image:str = None) -> np.ndarray | None :
    """
    Load a grayscale mask image and resize it to match a target image.

    The mask is loaded from ``ALLSKY_OVERLAY/images/<mask_file_name>`` and
    converted to a float mask in the range [0, 1]. If the mask dimensions do
    not match the target image, it is resized accordingly if a target_image is
    passed

    Args:
        mask_file_name (str):
            Name of the mask file to load.
        target_image (numpy.ndarray):
            Target image whose shape is used for resizing or None

    Returns:
        numpy.ndarray | None:
            Float mask array in the range [0, 1], or None if the mask could
            not be loaded.
    """
    import cv2
    mask = None

    base_path = os.path.join(ALLSKY_OVERLAY, 'images')

    if do_any_files_exist(base_path, [mask_file_name]):
        mask_path = os.path.join(base_path, mask_file_name)

        mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
        if mask is not None:
            if target_image is not None:
                target_shape = target_image.shape[:2]
                if (mask.shape[0] != target_shape[0]) or (mask.shape[1] != target_shape[1]):
                    mask = cv2.resize(mask, (target_shape[1], target_shape[0]))
                mask = mask.astype(np.float32) / 255.0
        else:
            log(4, f'ERROR: Mask exists {mask_file_name} but could not be loaded') 
    else:
      log(4, f'ERROR: Mask {mask_file_name} not found')  

    return mask

load_secrets_file()

Load environment-style secrets from env.json in the Allsky home directory.

Any JSON decoding errors are treated as an empty file.

Returns:

Name Type Description
dict Dict[str, Any]

Parsed secrets dictionary, or an empty dict if missing/invalid.

Source code in stable/scripts/modules/allsky_shared.py
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
def load_secrets_file() -> Dict[str, Any]:
    """
    Load environment-style secrets from ``env.json`` in the Allsky home directory.

    Any JSON decoding errors are treated as an empty file.

    Returns:
        dict:
            Parsed secrets dictionary, or an empty dict if missing/invalid.
    """
    file_path = Path(os.path.join(ALLSKYPATH, 'env.json'))
    env_data: Dict[str, Any] = {}
    if file_path.is_file():
        with file_path.open("r", encoding="utf-8") as f:
            try:
                env_data = json.load(f) or {}
            except json.JSONDecodeError:
                env_data = {}

    return env_data

log(level, text, preventNewline=False, exitCode=None, sendToAllsky=False)

Very simple method to log data if in verbose mode

Log a message to stdout (depending on log level) and optionally forward it to the Allsky WebUI.

Parameters:

Name Type Description Default
level

Numeric log level. The message is printed if the global LOGLEVEL is greater than or equal to this value. Level 0 is treated as an error.

required
text

The message to log.

required
preventNewline

If True, the message is printed without a trailing newline.

False
exitCode

If not None, the process exits with this code after logging the message.

None
sendToAllsky

If True, the message is also passed to the Allsky WebUI via addMessage.sh. Level 0 messages are always sent as errors.

False
Notes

The function does not raise exceptions. If the WebUI message script fails, the error is silently ignored.

Source code in stable/scripts/modules/allsky_shared.py
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
def log(level, text, preventNewline = False, exitCode=None, sendToAllsky=False):
    """ Very simple method to log data if in verbose mode

    Log a message to stdout (depending on log level) and optionally
    forward it to the Allsky WebUI.

    Args:
        level:
            Numeric log level. The message is printed if the global
            ``LOGLEVEL`` is greater than or equal to this value. Level 0
            is treated as an error.
        text:
            The message to log.
        preventNewline:
            If True, the message is printed without a trailing newline.
        exitCode:
            If not None, the process exits with this code after logging
            the message.
        sendToAllsky:
            If True, the message is also passed to the Allsky WebUI via
            ``addMessage.sh``. Level 0 messages are always sent as
            errors.

    Notes:
        The function does not raise exceptions. If the WebUI message
        script fails, the error is silently ignored.
    """
    global LOGLEVEL, ALLSKY_SCRIPTS

    if LOGLEVEL >= level:
        if preventNewline:
            print(text, end="")
        else:
            print(text)

    if sendToAllsky or level == 0:
        if level == 0:
            type = "error"
        else:
            type = "warning"
        # Need to escape single quotes in {text}.
        doubleQuote = '"'
        text = text.replace("'", f"'{doubleQuote}'{doubleQuote}'")
        command = os.path.join(ALLSKY_SCRIPTS, f"addMessage.sh --type {type} --msg '{text}'")
        os.system(command)

    if exitCode is not None:
        sys.exit(exitCode)

mask_image(image, mask_file_name='', log_info=False)

Apply a mask to an image, returning a masked copy.

The mask is loaded via :func:load_mask and applied either directly (for grayscale images) or per-channel (for colour images). The result is clipped and converted back to uint8.

Parameters:

Name Type Description Default
image ndarray

Input image (grayscale or BGR).

required
mask_file_name str

Name of the mask image file. If empty, no masking is performed and None is returned.

''
log_info bool

If True, log a message at level 4 when a mask is applied.

False

Returns:

Type Description

numpy.ndarray | None: Masked image, or None if no mask is applied or an error occurs.

Source code in stable/scripts/modules/allsky_shared.py
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
def mask_image(image, mask_file_name='', log_info=False):
    """
    Apply a mask to an image, returning a masked copy.

    The mask is loaded via :func:`load_mask` and applied either directly
    (for grayscale images) or per-channel (for colour images). The result
    is clipped and converted back to ``uint8``.

    Args:
        image (numpy.ndarray):
            Input image (grayscale or BGR).
        mask_file_name (str, optional):
            Name of the mask image file. If empty, no masking is performed
            and None is returned.
        log_info (bool, optional):
            If True, log a message at level 4 when a mask is applied.

    Returns:
        numpy.ndarray | None:
            Masked image, or None if no mask is applied or an error occurs.
    """
    output = None
    try:
        if mask_file_name != '':
            mask = load_mask(mask_file_name, image)
            if mask is not None:
                if len(image.shape) == 2:
                    image = image.astype(np.float32)
                    output = image * mask
                else:
                    image = image.astype(np.float32)
                    if mask.ndim == 2:
                        mask = mask[..., np.newaxis]
                    output = image * mask

                output = np.clip(output, 0, 255).astype(np.uint8)

                if log_info:
                    log(4, f'INFO: Mask {mask_file_name} applied')

    except Exception as e:
        me = os.path.basename(__file__)
        eType, eObject, eTraceback = sys.exc_info()
        log(0, f'ERROR: mask_image failed on line {eTraceback.tb_lineno} in {me} - {e}')

    return output

normalise_on_off(value)

Normalise an on/off style value to the strings "on" or "off".

Parameters:

Name Type Description Default
value Any

Raw value (e.g. "on", "1", "off", 0).

required

Returns:

Name Type Description
str

"on" if the input looks like an enabled value, otherwise "off".

Source code in stable/scripts/modules/allsky_shared.py
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
def normalise_on_off(value):
    """
    Normalise an on/off style value to the strings ``"on"`` or ``"off"``.

    Args:
        value (Any):
            Raw value (e.g. ``"on"``, ``"1"``, ``"off"``, ``0``).

    Returns:
        str:
            ``"on"`` if the input looks like an enabled value, otherwise ``"off"``.
    """
    value = str(value).strip().lower()
    if value in ('on', '1', 'true', 'yes'):    
        return 'on'
    return 'off'

obfuscate_password(password)

Obfuscate a password, leaving the first and last characters visible.

This is used when logging configuration without exposing full credentials.

Parameters:

Name Type Description Default
password str

Original password string.

required

Returns:

Type Description
str

Obfuscated password. Very short passwords are completely masked.

Source code in stable/scripts/modules/allsky_shared.py
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
def obfuscate_password(password: str) -> str:
    """
    Obfuscate a password, leaving the first and last characters visible.

    This is used when logging configuration without exposing full
    credentials.

    Args:
        password: Original password string.

    Returns:
        Obfuscated password. Very short passwords are completely masked.
    """
    if not password:
        return ""
    if len(password) <= 2:
        return "*" * len(password)
    return password[0] + "*" * (len(password) - 2) + password[-1]

read_environment_variable(name)

Read an environment variable without any Allsky-specific fallback.

This is a very thin wrapper around os.environ access and does not attempt to pull values from variables.json or any debug store.

Parameters:

Name Type Description Default
name

Environment variable name.

required

Returns:

Type Description

The value as a string, or None if the variable is not defined.

Source code in stable/scripts/modules/allsky_shared.py
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
def read_environment_variable(name):
    """
    Read an environment variable without any Allsky-specific fallback.

    This is a very thin wrapper around ``os.environ`` access and does not
    attempt to pull values from ``variables.json`` or any debug store.

    Args:
        name: Environment variable name.

    Returns:
        The value as a string, or None if the variable is not defined.
    """
    result = None
    try:
        result = os.environ[name]
    except KeyError:
        result = None        

    return result

read_gpio_pin(gpio_pin, pi=None, show_errors=False)

Read the logical state of a GPIO pin via the Allsky HTTP API.

A GET request is sent to the Allsky API, and the returned JSON is expected to contain a "value" field with the string "on" or "off".

Parameters:

Name Type Description Default
gpio_pin int | str

Pin identifier understood by the Allsky API.

required
pi Any

Unused placeholder for compatibility with other GPIO interfaces.

None
show_errors bool

Currently unused; errors are propagated via exceptions.

False

Returns:

Name Type Description
bool

True if the GPIO value is "on", False otherwise.

Raises:

Type Description
HTTPError

If the HTTP request fails (via raise_for_status).

Source code in stable/scripts/modules/allsky_shared.py
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
def read_gpio_pin(gpio_pin, pi=None, show_errors=False):
    """
    Read the logical state of a GPIO pin via the Allsky HTTP API.

    A GET request is sent to the Allsky API, and the returned JSON is
    expected to contain a ``"value"`` field with the string ``"on"`` or
    ``"off"``.

    Args:
        gpio_pin (int | str):
            Pin identifier understood by the Allsky API.
        pi (Any, optional):
            Unused placeholder for compatibility with other GPIO interfaces.
        show_errors (bool, optional):
            Currently unused; errors are propagated via exceptions.

    Returns:
        bool:
            True if the GPIO value is ``"on"``, False otherwise.

    Raises:
        requests.HTTPError:
            If the HTTP request fails (via ``raise_for_status``).
    """
    api_url = get_api_url()
    response = requests.get(
        f'{api_url}/gpio/digital/{gpio_pin}',
        timeout=2
    )
    response.raise_for_status()
    data = response.json()

    return data.get('value') == 'on'

read_tacho(tacho_id)

Read a configured server-side tachometer monitor.

Parameters:

Name Type Description Default
tacho_id str

Unique monitor identifier used when configuring the tacho.

required

Returns:

Name Type Description
dict

Parsed server status, including rpm.

Raises:

Type Description
HTTPError

If the HTTP request fails (via raise_for_status).

Source code in stable/scripts/modules/allsky_shared.py
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
def read_tacho(tacho_id):
    """
    Read a configured server-side tachometer monitor.

    Args:
        tacho_id (str):
            Unique monitor identifier used when configuring the tacho.

    Returns:
        dict:
            Parsed server status, including ``rpm``.

    Raises:
        requests.HTTPError:
            If the HTTP request fails (via ``raise_for_status``).
    """
    api_url = get_api_url()
    response = requests.get(
        f'{api_url}/gpio/tacho/{tacho_id}',
        timeout=2
    )
    response.raise_for_status()
    return response.json()

run_python_script(script, args=None, cwd=None)

Run a Python script using the same interpreter as the current process (e.g., inside a venv).

This function ensures the target script is executed with the current Python interpreter (sys.executable), so that packages installed in the active virtual environment are available.

Parameters:

Name Type Description Default
script str

Path to the Python script to execute.

required
args Optional[List[str]]

Additional arguments to pass to the script. Defaults to None.

None
cwd Optional[str]

Working directory in which to run the script. If None, uses the current directory.

None

Returns:

Type Description
Tuple[int, str]

Tuple[int, str]: A tuple containing: - return code (int): The process's exit code, or 127 if the script is not found. - output (str): Combined standard output and standard error from the script, stripped of trailing whitespace.

Example

code, output = run_python_script("myscript.py", ["--option", "value"]) print(code, output) 0 Script ran successfully

Source code in stable/scripts/modules/allsky_shared.py
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
def run_python_script(script: str, args: Optional[List[str]] = None, cwd: Optional[str] = None) -> Tuple[int, str]:
    """
    Run a Python script using the same interpreter as the current process (e.g., inside a venv).

    This function ensures the target script is executed with the current Python interpreter
    (`sys.executable`), so that packages installed in the active virtual environment are available.

    Args:
        script (str): Path to the Python script to execute.
        args (Optional[List[str]]): Additional arguments to pass to the script. Defaults to None.
        cwd (Optional[str]): Working directory in which to run the script. If None, uses the current directory.

    Returns:
        Tuple[int, str]: A tuple containing:
            - return code (int): The process's exit code, or 127 if the script is not found.
            - output (str): Combined standard output and standard error from the script, stripped of trailing whitespace.

    Example:
        >>> code, output = run_python_script("myscript.py", ["--option", "value"])
        >>> print(code, output)
        0 Script ran successfully
    """
    args = args or []
    try:
        proc = subprocess.run(
            [sys.executable, script, *args],
            capture_output=True,
            text=True,
            check=False,
            cwd=cwd,
        )
        output = (proc.stdout or "") + (proc.stderr or "")
        return proc.returncode, output.strip()
    except FileNotFoundError:
        return 127, f"Script not found: {script}"

run_script(script)

Run an arbitrary executable script and capture its output.

Parameters:

Name Type Description Default
script str

Path to the script or binary to execute.

required

Returns:

Type Description
int

Tuple (returncode, output) where:

str
  • returncode is the process exit code, or 127 if the script was not found.
Tuple[int, str]
  • output is the combined stdout and stderr as a single string.
Source code in stable/scripts/modules/allsky_shared.py
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
def run_script(script: str) -> Tuple[int, str]:
    """
    Run an arbitrary executable script and capture its output.

    Args:
        script: Path to the script or binary to execute.

    Returns:
        Tuple ``(returncode, output)`` where:

        - ``returncode`` is the process exit code, or 127 if the script
          was not found.
        - ``output`` is the combined stdout and stderr as a single
          string.
    """
    try:
        result = subprocess.run(
            [script],
            capture_output=True,
            text=True,
            check=False
        )
        output = result.stdout + result.stderr
        return result.returncode, output.strip()
    except FileNotFoundError:
        return 127, f"Script not found: {script}"

save_extra_data(file_name='', extra_data={}, source='', structure={}, custom_fields={}, event='postcapture')

Persist "extra data" for use by Allsky overlay modules.

This function writes the provided data to a file inside the current ALLSKY_EXTRA directory (resolved via get_extra_dir(True)), using a temporary file in ALLSKY_TMP and an atomic move to avoid partial writes. It ensures the destination directory exists and is web-server accessible, applies final permissions, and (optionally) updates a database when the structure indicates one is in use.

Behavior

1) Ensure extra data directory exists (checkAndCreateDirectory) and enable web access (create_file_web_server_access). 2) If the target filename ends with .json, normalize/shape the payload via format_extra_data_json(extra_data, structure, source). 3) Merge any custom_fields into the payload (overrides existing keys). 4) Serialize to JSON (pretty-printed) and write to a temp file created in ALLSKY_TMP, then atomically move it to the final path. 5) Set mode 0o770 and call set_permissions() for owner/group alignment. 6) If structure contains a "database" key, call update_database().

Parameters:

Name Type Description Default
file_name str

File name (with extension) to write into the extra data directory.

''
extra_data Any

Data to persist. If file_name ends with .json, this should be JSON-serializable. Non-JSON targets are written as the JSON string.

{}
source str

Context or origin tag passed to the JSON formatter. Default: ''.

''
structure dict

Schema/metadata guiding JSON formatting and optional DB updates. If it contains "database", update_database() will be invoked. Default: {}.

{}
custom_fields dict

Extra key/values to inject into the payload before serialization. Keys here override the same keys in extra_data. Default: {}.

{}
event str

Event type (e.g. 'postcapture', 'periodic') used when deciding if database updates should occur.

'postcapture'

Returns:

Type Description

None

Side Effects
  • Creates/updates a file in ALLSKY_EXTRA.
  • Applies filesystem permissions to the output file.
  • May perform a database update if requested by structure.
Error Handling

Any exception is (currently) allowed to propagate only into the surrounding code; earlier versions logged and swallowed errors.

Source code in stable/scripts/modules/allsky_shared.py
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
def save_extra_data(file_name: str = '', extra_data: dict = {}, source: str = '', structure: dict = {}, custom_fields: dict = {}, event: str = 'postcapture'):
    """
    Persist "extra data" for use by Allsky overlay modules.

    This function writes the provided data to a file inside the current
    ALLSKY_EXTRA directory (resolved via `get_extra_dir(True)`), using a
    temporary file in ALLSKY_TMP and an atomic move to avoid partial writes.
    It ensures the destination directory exists and is web-server accessible,
    applies final permissions, and (optionally) updates a database when the
    `structure` indicates one is in use.

    Behavior:
      1) Ensure extra data directory exists (`checkAndCreateDirectory`) and
         enable web access (`create_file_web_server_access`).
      2) If the target filename ends with `.json`, normalize/shape the payload
         via `format_extra_data_json(extra_data, structure, source)`.
      3) Merge any `custom_fields` into the payload (overrides existing keys).
      4) Serialize to JSON (pretty-printed) and write to a temp file created in
         `ALLSKY_TMP`, then atomically move it to the final path.
      5) Set mode 0o770 and call `set_permissions()` for owner/group alignment.
      6) If `structure` contains a `"database"` key, call `update_database()`.

    Args:
        file_name (str):
            File name (with extension) to write into the extra data directory.
        extra_data (Any):
            Data to persist. If `file_name` ends with `.json`, this should be
            JSON-serializable. Non-JSON targets are written as the JSON string.
        source (str, optional):
            Context or origin tag passed to the JSON formatter. Default: ''.
        structure (dict, optional):
            Schema/metadata guiding JSON formatting and optional DB updates.
            If it contains `"database"`, `update_database()` will be invoked.
            Default: {}.
        custom_fields (dict, optional):
            Extra key/values to inject into the payload before serialization.
            Keys here override the same keys in `extra_data`. Default: {}.
        event (str, optional):
            Event type (e.g. ``'postcapture'``, ``'periodic'``) used when
            deciding if database updates should occur.

    Returns:
        None

    Side Effects:
        - Creates/updates a file in ALLSKY_EXTRA.
        - Applies filesystem permissions to the output file.
        - May perform a database update if requested by `structure`.

    Error Handling:
        Any exception is (currently) allowed to propagate only into the
        surrounding code; earlier versions logged and swallowed errors.
    """
    saveExtraData(file_name, extra_data, source, structure, custom_fields, event)

save_json_file(data, filename)

Save a dictionary to a JSON file with pretty formatting.

Parameters:

Name Type Description Default
data dict

Dictionary to save. Must be JSON-serializable.

required
filename Union[str, Path]

Path or string of the file to write.

required

Returns:

Type Description
None

True if the file could be written successfully, False otherwise.

Source code in stable/scripts/modules/allsky_shared.py
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
def save_json_file(data: dict, filename: Union[str, Path]) -> None:
    """
    Save a dictionary to a JSON file with pretty formatting.

    Args:
        data: Dictionary to save. Must be JSON-serializable.
        filename: Path or string of the file to write.

    Returns:
        True if the file could be written successfully, False otherwise.
    """
    file_path = Path(filename)

    try:
        with file_path.open('w', encoding='utf-8') as file:
            json.dump(data, file, ensure_ascii=False, indent=4)
    except:
        return False

    return True

set_gpio_pin(gpio_pin, state, name='', pi=None, show_errors=False)

Set the logical state of a GPIO pin via the Allsky HTTP API.

Parameters:

Name Type Description Default
gpio_pin int | str

Pin identifier understood by the Allsky API.

required
state Any

Desired state; normalised using :func:normalise_on_off to "on" or "off".

required
pi Any

Unused placeholder for compatibility with other GPIO interfaces.

None
show_errors bool

Currently unused; errors are propagated via exceptions.

False

Returns:

Name Type Description
dict

Parsed JSON response from the API.

Raises:

Type Description
HTTPError

If the HTTP request fails (via raise_for_status).

Source code in stable/scripts/modules/allsky_shared.py
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
def set_gpio_pin(gpio_pin, state, name="", pi=None, show_errors=False):
    """
    Set the logical state of a GPIO pin via the Allsky HTTP API.

    Args:
        gpio_pin (int | str):
            Pin identifier understood by the Allsky API.
        state (Any):
            Desired state; normalised using :func:`normalise_on_off` to
            ``"on"`` or ``"off"``.
        pi (Any, optional):
            Unused placeholder for compatibility with other GPIO interfaces.
        show_errors (bool, optional):
            Currently unused; errors are propagated via exceptions.

    Returns:
        dict:
            Parsed JSON response from the API.

    Raises:
        requests.HTTPError:
            If the HTTP request fails (via ``raise_for_status``).
    """
    api_url = get_api_url()
    state = normalise_on_off(state)
    response = requests.post(
        f'{api_url}/gpio/digital',
        json={
            'pin': str(gpio_pin),
            'state': state.lower(),
            'name': name
        },
        timeout=2
    )
    response.raise_for_status()
    return response.json()

set_last_run(module)

Helper to record that a module has just run.

This function simply calls the legacy :func:setLastRun. New code should use this name; the camelCase variant is kept for older code.

Source code in stable/scripts/modules/allsky_shared.py
453
454
455
456
457
458
459
460
def set_last_run(module):
    """
    Helper to record that a module has just run.

    This function simply calls the legacy :func:`setLastRun`. New code
    should use this name; the camelCase variant is kept for older code.
    """
    setLastRun(module)

set_pwm(gpio_pin, duty_cycle, name='', pi=None, show_errors=False)

Set PWM output on a GPIO pin via the Allsky HTTP API.

This helper posts the requested duty cycle (and a fixed frequency of 1000Hz) to the API.

Parameters:

Name Type Description Default
gpio_pin int | str

Pin identifier understood by the Allsky API.

required
duty_cycle int | float

Duty cycle value; interpreted by the remote API.

required
pi Any

Unused placeholder for compatibility with other GPIO interfaces.

None
show_errors bool

Currently unused; errors are propagated via exceptions.

False

Returns:

Name Type Description
dict

Parsed JSON response from the API.

Raises:

Type Description
HTTPError

If the HTTP request fails (via raise_for_status).

Source code in stable/scripts/modules/allsky_shared.py
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
def set_pwm(gpio_pin, duty_cycle, name="", pi=None, show_errors=False):
    """
    Set PWM output on a GPIO pin via the Allsky HTTP API.

    This helper posts the requested duty cycle (and a fixed frequency of
    1000Hz) to the API.

    Args:
        gpio_pin (int | str):
            Pin identifier understood by the Allsky API.
        duty_cycle (int | float):
            Duty cycle value; interpreted by the remote API.
        pi (Any, optional):
            Unused placeholder for compatibility with other GPIO interfaces.
        show_errors (bool, optional):
            Currently unused; errors are propagated via exceptions.

    Returns:
        dict:
            Parsed JSON response from the API.

    Raises:
        requests.HTTPError:
            If the HTTP request fails (via ``raise_for_status``).
    """
    api_url = get_api_url()
    frequency = 1000
    response = requests.post(
        f'{api_url}/gpio/pwm',
        json={
            'pin': str(gpio_pin),
            'duty': duty_cycle,
            'frequency': frequency,
            'name': name
        },
        timeout=2
    )
    response.raise_for_status()
    return response.json()

should_run(module, period)

Helper to check whether a module should run again.

This wrapper simply calls the legacy :func:shouldRun. New code should use this snake_case name; the camelCase version is retained for backwards compatibility.

See :func:shouldRun for details.

Source code in stable/scripts/modules/allsky_shared.py
406
407
408
409
410
411
412
413
414
415
416
def should_run(module, period):
    """
    Helper to check whether a module should run again.

    This wrapper simply calls the legacy :func:`shouldRun`. New code
    should use this snake_case name; the camelCase version is retained
    for backwards compatibility.

    See :func:`shouldRun` for details.
    """
    return shouldRun(module, period)

stop_pwm(gpio_pin)

Stop PWM output on a GPIO pin via the Allsky HTTP API.

This is implemented by sending a PWM request with 0% duty cycle.

Parameters:

Name Type Description Default
gpio_pin int | str

Pin identifier understood by the Allsky API.

required

Returns:

Name Type Description
dict

Parsed JSON response from the API.

Raises:

Type Description
HTTPError

If the HTTP request fails (via raise_for_status).

Source code in stable/scripts/modules/allsky_shared.py
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
def stop_pwm(gpio_pin):
    """
    Stop PWM output on a GPIO pin via the Allsky HTTP API.

    This is implemented by sending a PWM request with 0% duty cycle.

    Args:
        gpio_pin (int | str):
            Pin identifier understood by the Allsky API.

    Returns:
        dict:
            Parsed JSON response from the API.

    Raises:
        requests.HTTPError:
            If the HTTP request fails (via ``raise_for_status``).
    """
    api_url = get_api_url()
    frequency = 1000
    duty_cycle = 0
    response = requests.post(
        f'{api_url}/gpio/pwm',
        json={
            'pin': str(gpio_pin),
            'duty': duty_cycle,
            'frequency': frequency
        },
        timeout=2
    )
    response.raise_for_status()
    return response.json()

stop_tacho(tacho_id)

Stop and release a server-side tachometer monitor.

Parameters:

Name Type Description Default
tacho_id str

Unique monitor identifier used when configuring the tacho.

required

Returns:

Name Type Description
dict

Parsed server status.

Raises:

Type Description
HTTPError

If the HTTP request fails (via raise_for_status).

Source code in stable/scripts/modules/allsky_shared.py
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
def stop_tacho(tacho_id):
    """
    Stop and release a server-side tachometer monitor.

    Args:
        tacho_id (str):
            Unique monitor identifier used when configuring the tacho.

    Returns:
        dict:
            Parsed server status.

    Raises:
        requests.HTTPError:
            If the HTTP request fails (via ``raise_for_status``).
    """
    api_url = get_api_url()
    response = requests.delete(
        f'{api_url}/gpio/tacho/{tacho_id}',
        timeout=2
    )
    response.raise_for_status()
    return response.json()

to_bool(v)

Normalise a value to a boolean, supporting several truthy strings.

This version differs slightly from the earlier :func:to_bool in this file: it treats "true", "1", "yes" and "y" (case- insensitive) as True, and everything else as False. It is used when normalising configuration dictionaries.

Parameters:

Name Type Description Default
v bool | str | None

Input value.

required

Returns:

Name Type Description
bool bool

Normalised boolean value.

Source code in stable/scripts/modules/allsky_shared.py
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
def to_bool(v: bool | str) -> bool:
    """
    Normalise a value to a boolean, supporting several truthy strings.

    This version differs slightly from the earlier :func:`to_bool` in this
    file: it treats ``"true"``, ``"1"``, ``"yes"`` and ``"y"`` (case-
    insensitive) as True, and everything else as False. It is used when
    normalising configuration dictionaries.

    Args:
        v (bool | str | None):
            Input value.

    Returns:
        bool:
            Normalised boolean value.
    """
    if isinstance(v, bool):
        return v
    if v is None:
        return False
    return str(v).strip().lower() in ("true", "1", "yes", "y")

update_setting(values)

Helper to update one or more settings.

This wraps the legacy :func:updateSetting. New code should use this snake_case name.

Source code in stable/scripts/modules/allsky_shared.py
835
836
837
838
839
840
841
842
def update_setting(values):
    """
    Helper to update one or more settings.

    This wraps the legacy :func:`updateSetting`. New code should use
    this snake_case name.
    """
    updateSetting(values)