Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ jobs:
tools/modbus_codec_test.c app/engine/modbus_codec.c app/cJSON.c -lm
/tmp/modbus_codec_test

scheduler:
name: Day/Night scheduler tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- name: Build and run the sunrise/sunset window tests
run: |
cc -std=gnu11 -Wall -Wextra -Werror -fsanitize=address,undefined \
-o /tmp/scheduler_test tools/scheduler_test.c app/cJSON.c -lm
/tmp/scheduler_test

gps_nmea:
name: GPS NMEA parser tests
runs-on: ubuntu-latest
Expand Down
19 changes: 13 additions & 6 deletions AI_RULE_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -563,15 +563,21 @@ Checks the current state of a VAPIX event instance on the local or a remote devi
"type": "day_night",
"state": "day",
"lat": 59.3293,
"lon": 18.0686
"lon": 18.0686,
"sunrise_offset_minutes": 60,
"sunset_offset_minutes": -30
}
```

| Field | Type | Description |
|-------|------|-------------|
| `state` | `"day"` \| `"night"` | Required state |
| `lat` | number | Latitude (optional, defaults to engine setting) |
| `lat` | number | Latitude (optional, defaults to engine setting; 0/0 also uses the engine setting) |
| `lon` | number | Longitude (optional, defaults to engine setting) |
| `sunrise_offset_minutes` | number | Optional, default 0. Shifts sunrise (positive = later, clamped to ±720). `60` keeps it night until 1 h after sunrise |
| `sunset_offset_minutes` | number | Optional, default 0. Shifts sunset (positive = later, clamped to ±720). `-30` starts night 30 min before sunset |

Day is from sunrise + offset until sunset + offset, and may run past midnight. If the offsets make that window empty, it is night all day. On days with no sunrise or sunset, midnight sun counts as day and polar night as night, whatever the offsets.

### 8. `aoa_occupancy` — AOA Occupancy Check

Expand Down Expand Up @@ -1897,7 +1903,7 @@ For a map, put `register`, `var`, `type`, `order`, and optional scaling fields i
| `scale` | number | `1` | Numeric values are reported as `raw × scale + offset`; must not be zero |
| `offset` | number | `0` | Numeric value offset |
| `decimals` | integer (0–10) | automatic | Optional rounding after scaling |
| `length` | integer (1–125) | `8` | String length in registers (single value or map entry); the text ends at the first NUL |
| `length` | integer (1–125) | `8` | String length in 2-byte registers (single value or map entry), not characters; UTF-8 characters may use multiple bytes. The text ends at the first NUL |
| `encoding` | `"utf-8"` \| `"ascii"` \| `"iso-8859-1"` \| `"iso-8859-2"` \| `"windows-1250"` \| `"windows-1252"` | `"utf-8"` | String encoding (single value or map entry). Text is stored in variables as UTF-8; invalid bytes read as `?` |
| `variable` | string | — | Optional variable updated by a single-value read |
| `map` | array | — | Register-map entries; each requires `register` and a unique `var`, and may include `type`, `order`, `scale`, `offset`, `decimals`, `length`, or `encoding`. One request reads up to 125 registers or 2000 bits; a larger map is read in several requests, spanning at most 2000 registers |
Expand Down Expand Up @@ -1928,7 +1934,7 @@ The trigger data includes `state` (`"failing"` or `"recovered"`), `device`, `sla

## Action: `modbus_write`

Writes one typed value or a block of values to Modbus. Automatic function selection uses FC06/FC05 for one register/coil and FC16/FC15 for multiple contiguous values. In block mode, adjacent values are batched into one request of up to 123 registers or 1968 coils; longer runs and strings longer than 123 registers are split, and gaps start a new request. Strings are padded with NUL to `length`. When tested from the editor (`POST /actions`), the write waits for the device and returns its error, if any.
Writes one typed value or a block of values to Modbus. Automatic function selection uses FC06/FC05 for one register/coil and FC16/FC15 for multiple contiguous values. In block mode, adjacent values are batched into one request of up to 123 registers or 1968 coils; longer runs and strings longer than 123 registers are split, and gaps start a new request. Strings are padded to `length` with NUL, or with spaces as `fill` says. When tested from the editor (`POST /actions`), the write waits for the device and returns its error, if any.

```json
{
Expand Down Expand Up @@ -1980,8 +1986,9 @@ For multiple values, use `values` instead of `register` and `value`. Each entry
| `word_order` | `"ABCD"` \| `"CDAB"` \| `"BADC"` \| `"DCBA"` | `"ABCD"` | Byte and word order for 32/64-bit values. For 16-bit values and strings only `"ABCD"` and `"BADC"` (bytes swapped) differ |
| `scale` | number | `1` | Input value is converted to raw form using `(value - offset) / scale` |
| `offset` | number | `0` | Numeric value offset |
| `length` | integer (1–125) | `8` | String length in registers; shorter text is padded with NUL (0x00) |
| `length` | integer (1–125) | `8` | String length in 2-byte registers, not characters; UTF-8 characters may use multiple bytes. Shorter text is padded as `fill` says |
| `encoding` | `"utf-8"` \| `"ascii"` \| `"iso-8859-1"` \| `"iso-8859-2"` \| `"windows-1250"` \| `"windows-1252"` | `"utf-8"` | String encoding. Literal text with characters the encoding lacks is rejected; template text gets `?` instead. The length check counts encoded bytes (in UTF-8, `ż` is 2 bytes) |
| `fill` | `"nul"` \| `"space_left"` \| `"space_right"` \| `"space_center"` | `"nul"` | String padding. `nul` puts the text first and fills with 0x00. The `space_*` values fill with spaces (0x20) and put the text left, right or in the middle (an odd space goes after the text). Counted in bytes of the encoding |
| `value` | number \| string | — | Value to write; supports templates. Required for numbers and coils; an empty string clears a text register. A `uint16` also accepts -32768 to -1, written as two's complement. Coils accept `0`, `1`, `true`, `false`, `on`, or `off` |
| `values` | array | — | Block entries with `register`, `value`, and optional `type`, `order`, `scale`, `offset`, `length`, and `encoding`. Overlapping entries are rejected; gaps create separate requests. A request carries at most 123 registers or 1968 coils; longer runs are split, up to 32 requests per action |
| `values` | array | — | Block entries with `register`, `value`, and optional `type`, `order`, `scale`, `offset`, `length`, `encoding`, and `fill`. Overlapping entries are rejected; gaps create separate requests. A request carries at most 123 registers or 1968 coils; longer runs are split, up to 32 requests per action |
| `function` | `"auto"` \| `"fc05"` \| `"fc15"` \| `"fc06"` \| `"fc16"` | `"auto"` | Force a compatible single/multiple-coil or register write function |
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ The web UI is accessible at
| Type | Description |
|------|-------------|
| **Time Window** | Only allow firing between two times of day on specified days. Supports wraparound (e.g. 22:00–06:00) |
| **Day / Night** | Pass only during daytime (after sunrise) or nighttime (after sunset). Uses the sunrise/sunset engine with latitude/longitude from Settings. The UI shows today's computed sunrise and sunset times. Optional per-condition lat/lon override |
| **Day / Night** | Pass only during daytime (after sunrise) or nighttime (after sunset). Uses the sunrise/sunset engine with latitude/longitude from Settings. Optional sunrise/sunset offsets in minutes (e.g. +60 to stay "night" until an hour after sunrise). The UI shows today's resulting times as you type. Optional per-condition lat/lon override |
| **I/O State** | Check the current state of an I/O port. Supports **remote device** target |
| **Counter Compare** | Compare a counter value against a threshold |
| **Variable Compare** | Compare a named variable against a value |
Expand Down Expand Up @@ -227,7 +227,7 @@ stop a siren when motion ends).

| Type | Description |
|------|-------------|
| **Modbus Write** | Write typed values or blocks over Modbus TCP or RTU/RS-485. Supports FC05/FC15 coil writes and FC06/FC16 register writes, including adjacent-register batching (split at the 123-register limit), data types, word order, scaling and strings in several encodings |
| **Modbus Write** | Write typed values or blocks over Modbus TCP or RTU/RS-485. Supports FC05/FC15 coil writes and FC06/FC16 register writes, including adjacent-register batching (split at the 123-register limit), data types, word order, scaling and strings in several encodings, padded with NUL or with spaces (text left, right or centered) |

### Industrial

Expand Down
58 changes: 58 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,63 @@
# Release Notes

## v1.10.4 - Day/Night offsets and Modbus text padding

The Day / Night condition can shift sunrise and sunset by a number of minutes,
and now works after midnight and in polar regions. Modbus Write can pad text
with spaces for displays that show 0x00 as a character. The Rules page appears
sooner, and the rule editor no longer loses edits while its lists load.

### Upgrading to 1.10.4

The `signed_*.eap` packages install normally on AXIS OS 12.10 and later, as an
in-place upgrade. Rules, settings and variables are kept. Two changes can make
existing rules behave differently:

- Day / Night conditions without coordinates (or with 0, 0), such as those
created from the Night-Only Action template, now use the camera's Location
setting instead of 0°N, 0°E.
- During polar night a Night condition now passes, and during midnight sun a
Day condition passes. Before, neither passed.

### New in 1.10.4

- **Day / Night offsets:** shift sunrise and sunset independently, in
minutes (up to ±720). For example, if sunrise is 06:44, a sunrise offset of
`60` keeps the condition at Night until 07:44. The editor shows today's
resulting times as you type coordinates or offsets.
- **Modbus text padding:** a new **Fill** option on Modbus Write chooses what
goes in the bytes the text doesn't use: NUL (0x00, the default and the
previous behavior), or spaces with the text left, right or centered. In the
API it is `fill`: `nul`, `space_left`, `space_right` or `space_center`.
- **Modbus editor:** Encoding is shown only for text values. The Length hint
explains that it counts 2-byte registers, not characters, and that a UTF-8
character can take several bytes.

### Fixes in 1.10.4

- **Location setting ignored:** Day / Night conditions and Sunrise/Sunset
schedule triggers without their own coordinates read the Location setting
from the wrong place, so they used 0°N, 0°E.
- **Day / Night after midnight:** where local sunset falls after midnight
(for example Reykjavik in June), the condition reported Night all day. It
now stays Day until sunset, also when a sunset offset moves sunset past
midnight.
- **Day / Night preview:** the sunrise and sunset times shown in the editor
didn't change when you edited the coordinates; they only updated after
saving and reopening the rule. They now update as you type.
- **Day / Night in polar regions:** during polar night the condition passed
for neither Day nor Night, so a Night-Only rule never ran. Polar night now
counts as Night, and midnight sun as Day.
- **Slow Rules page:** the rule list waited for every device capability
lookup. It now appears first; editor choices such as device parameters can
still take a few seconds to fill in.
- **Rule editor lost edits:** when a device list finished loading while the
editor was open, unsaved changes were discarded. Adding or removing a
trigger or condition also reset schedule days to Monday to Friday and
cleared geofence polygons, and saving kept the reset values.
- **Rule cards:** Modbus Write and the GPS triggers showed their internal
names instead of labels.

## v1.10.3 - No pause after saving; MQTT and Sparkplug B fixes

Saving a rule or the Sparkplug B settings no longer pauses event handling, a
Expand Down
2 changes: 1 addition & 1 deletion app/engine/actions.c
Original file line number Diff line number Diff line change
Expand Up @@ -4706,7 +4706,7 @@ static int mb_encode_value(cJSON* v, const mb_field_t* f, int bits, cJSON* trigg
else if (mb_parse_number(text, &d) == 0) regs[0] = d != 0;
else { snprintf(err, err_size, "'%.40s' is not on/off or a number", text); rc = -1; }
} else if (f->type == MB_T_STRING) {
if (mb_encode_string(text, f->nregs, f->order, f->charset, regs, NULL) != 0) {
if (mb_encode_string(text, f->nregs, f->order, f->charset, f->fill, regs, NULL) != 0) {
snprintf(err, err_size, "text is %d bytes in %s; %d registers hold %d",
(int)mb_text_encode(text, f->charset, NULL, 0, NULL, NULL),
mb_charset_name(f->charset), f->nregs, f->nregs * 2);
Expand Down
24 changes: 19 additions & 5 deletions app/engine/conditions.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,21 @@ static int cond_time_window(cJSON* cfg) {

/*-----------------------------------------------------
* day_night
* config: { "state": "day"|"night", "lat": 51.5, "lon": -0.12 }
* config: { "state": "day"|"night", "lat": 51.5, "lon": -0.12,
* "sunrise_offset_minutes": 60, "sunset_offset_minutes": -30 }
* Uses the sunrise/sunset engine from scheduler.c.
* If no lat/lon provided, reads from engine settings.
* Offsets are optional (default 0, positive = later, clamped to ±12 h).
*-----------------------------------------------------*/
static int day_night_offset_sec(cJSON* cfg, const char* key) {
cJSON* j = cJSON_GetObjectItem(cfg, key);
if (!j || !cJSON_IsNumber(j)) return 0;
double m = j->valuedouble;
if (m > 720.0) m = 720.0;
if (m < -720.0) m = -720.0;
return (int)(m * 60.0);
}

static int cond_day_night(cJSON* cfg) {
const char* want = cJSON_GetStringValue(cJSON_GetObjectItem(cfg, "state"));
if (!want) want = "day";
Expand All @@ -108,8 +119,10 @@ static int cond_day_night(cJSON* cfg) {
if (lat_j) lat = lat_j->valuedouble;
if (lon_j) lon = lon_j->valuedouble;

if (!lat_j && !lon_j) {
cJSON* eng = ACAP_Get_Config("engine");
/* 0/0 means "not set" (older templates saved it), same as astronomical triggers */
if (lat == 0.0 && lon == 0.0) {
cJSON* sett = ACAP_Get_Config("settings");
cJSON* eng = sett ? cJSON_GetObjectItem(sett, "engine") : NULL;
if (eng) {
cJSON* sl = cJSON_GetObjectItem(eng, "latitude");
cJSON* so = cJSON_GetObjectItem(eng, "longitude");
Expand All @@ -118,8 +131,9 @@ static int cond_day_night(cJSON* cfg) {
}
}

int is_day = Scheduler_Is_Daytime(lat, lon);
if (is_day < 0) return 0; /* polar — can't determine */
int is_day = Scheduler_Is_Daytime(lat, lon,
day_night_offset_sec(cfg, "sunrise_offset_minutes"),
day_night_offset_sec(cfg, "sunset_offset_minutes"));
return (strcmp(want, "day") == 0) ? is_day : !is_day;
}

Expand Down
29 changes: 25 additions & 4 deletions app/engine/modbus_codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ const char* mb_charset_name(mb_charset_t cs) {
return (cs >= MB_CS_UTF8 && cs <= MB_CS_CP1252) ? charset_names[cs] : "invalid";
}

static const char* const fill_names[] = { "nul", "space_left", "space_right", "space_center", NULL };

mb_fill_t mb_fill_parse(const char* s) {
if (!s || !s[0]) return MB_FILL_NUL;
for (int i = 0; fill_names[i]; i++)
if (strcmp(s, fill_names[i]) == 0) return (mb_fill_t)i;
return MB_FILL_INVALID;
}

/* Unicode code points of bytes 0x80-0xFF; 0 = not defined. 0x80-0x9F of
* ISO-8859-2 are C1 controls, which are treated as not printable. */
static const uint16_t cs_latin2[128] = {
Expand Down Expand Up @@ -341,12 +350,18 @@ int mb_encode_number(double v, mb_type_t t, mb_order_t o, uint16_t* regs) {
}

int mb_encode_string(const char* s, int nregs, mb_order_t o, mb_charset_t cs,
uint16_t* regs, int* bad) {
mb_fill_t fill, uint16_t* regs, int* bad) {
if (nregs < 1 || nregs > MB_MAX_STRING_REGS) return -1;
uint8_t text[2 * MB_MAX_STRING_REGS];
size_t len = mb_text_encode(s, cs, text, sizeof(text), bad, NULL);
size_t total = (size_t)nregs * 2;
if (len > total) return -1;
size_t pad = total - len, start = 0;
if (fill == MB_FILL_SPACE_RIGHT) start = pad;
else if (fill == MB_FILL_SPACE_CENTER) start = pad / 2;
uint8_t bytes[2 * MB_MAX_STRING_REGS];
memset(bytes, 0, sizeof(bytes));
size_t len = mb_text_encode(s, cs, bytes, sizeof(bytes), bad, NULL);
if (len > (size_t)nregs * 2) return -1;
memset(bytes, fill == MB_FILL_NUL ? 0x00 : 0x20, total);
memcpy(bytes + start, text, len);
int swap = (o == MB_O_BADC || o == MB_O_DCBA);
for (int i = 0; i < nregs; i++) {
uint8_t hi = bytes[2 * i], lo = bytes[2 * i + 1];
Expand Down Expand Up @@ -473,6 +488,12 @@ int mb_field_parse(cJSON* obj, const char* type_key, const char* order_key,
"windows-1250 or windows-1252)", cs);
return -1;
}
const char* fs = cJSON_GetStringValue(cJSON_GetObjectItem(obj, "fill"));
f->fill = mb_fill_parse(fs);
if (f->fill == MB_FILL_INVALID) {
snprintf(err, err_size, "unknown fill '%s' (use nul, space_left, space_right or space_center)", fs);
return -1;
}
}
f->nregs = mb_type_regs(f->type, f->length);
if (f->reg + f->nregs - 1 > 65535) {
Expand Down
21 changes: 16 additions & 5 deletions app/engine/modbus_codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,21 @@ typedef enum {
MB_CS_CP1252 /* Windows-1252 */
} mb_charset_t;

/* NULL or "" give the default (uint16, ABCD, utf-8). Encodings are matched
/* How a string write fills the registers the text doesn't use. */
typedef enum {
MB_FILL_INVALID = -1,
MB_FILL_NUL = 0, /* text first, then 0x00 */
MB_FILL_SPACE_LEFT, /* text first, then spaces */
MB_FILL_SPACE_RIGHT, /* spaces, then text */
MB_FILL_SPACE_CENTER /* text centred in spaces; an odd one goes after */
} mb_fill_t;

/* NULL or "" give the default (uint16, ABCD, utf-8, nul). Encodings are matched
* without case, and also as utf8, latin1, latin2, cp1250 and cp1252. */
mb_type_t mb_type_parse(const char* s);
mb_order_t mb_order_parse(const char* s);
mb_charset_t mb_charset_parse(const char* s);
mb_fill_t mb_fill_parse(const char* s);
const char* mb_type_name(mb_type_t t);
const char* mb_charset_name(mb_charset_t cs);

Expand Down Expand Up @@ -91,11 +101,11 @@ size_t mb_text_encode(const char* s, mb_charset_t cs, uint8_t* out, size_t out_s
* -1 if the value is NaN or out of range for the type. */
int mb_encode_number(double v, mb_type_t t, mb_order_t o, uint16_t* regs);

/* UTF-8 text to nregs registers in cs, padded with NUL to the end. Returns
* -1 if the encoded text is longer than nregs * 2 bytes. *bad as for
* mb_text_encode (may be NULL). */
/* UTF-8 text to nregs registers in cs; the unused bytes are filled as fill
* says. Returns -1 if the encoded text is longer than nregs * 2 bytes. *bad as
* for mb_text_encode (may be NULL). */
int mb_encode_string(const char* s, int nregs, mb_order_t o, mb_charset_t cs,
uint16_t* regs, int* bad);
mb_fill_t fill, uint16_t* regs, int* bad);

/* value = raw * scale + offset, rounded to decimals (< 0: noise of the double
* arithmetic only, 15 significant digits). */
Expand All @@ -120,6 +130,7 @@ typedef struct {
int decimals; /* -1 = not set */
int length; /* string length in registers */
mb_charset_t charset; /* strings */
mb_fill_t fill; /* string writes */
} mb_field_t;

/* Field from a JSON object. type_key/order_key name the keys ("data_type"/
Expand Down
Loading