diff --git a/design/api.md b/design/api.md index 71b11d0..95f0162 100644 --- a/design/api.md +++ b/design/api.md @@ -658,13 +658,12 @@ make. It should be a dict with these attributes: * `signalIds`: a list of signal ids to change. Must be sorted. * `states`: a list (one per `signalIds` entry) of states to set. -* `startTime90k`: (optional) The start of the observation in 90 kHz units - since 1970-01-01 00:00:00 UTC; commonly taken from an earlier response. If - absent, assumed to be now. -* `endBase`: if `epoch`, `relEndTime90k` is relative to 1970-01-01 00:00:00 - UTC. If `now`, epoch is relative to the current time. -* `relEndTime90k` (optional): The end of the observation, relative to the - specified base. Note this time is allowed to be in the future. +* `start`: the starting time of the change, as a dict of the form + `{'base': 'epoch', 'rel90k': t}` or `{'base': 'now', 'rel90k': t}`. In + the `epoch` form, `rel90k` is 90 kHz units since 1970-01-01 00:00:00 UTC. + In the `now` form, `rel90k` is relative to current time and may be + negative. +* `end`: the ending time of the change, in the same form as `start`. The response will be an `application/json` body dict with the following attributes: @@ -687,8 +686,8 @@ Request: { "signalIds": [1], "states": [2], - "endBase": "now", - "relEndTime90k": 5400000 + "start": {"base": "now", "rel90k": 0}, + "end": {"base": "now", "rel90k": 5400000} } ``` @@ -711,8 +710,8 @@ Request: { "signalIds": [1], "states": [2], - "endBase": "now", - "relEndTime90k": 5400000 + "start": {"base": "epoch", "rel90k": 140067468000000}, + "end": {"base": "now", "rel90k": 5400000} } ``` @@ -735,8 +734,8 @@ Request: { "signalIds": [1], "states": [2], - "endBase": "now", - "relEndTime90k": 5400000 + "start": {"base": "now", "rel90k": 0}, + "end": {"base": "now", "rel90k": 5400000} } } ``` diff --git a/server/Cargo.lock b/server/Cargo.lock index 3251056..d2cf040 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -1165,6 +1165,8 @@ dependencies = [ "log", "nom", "parking_lot", + "serde", + "serde_json", "time", ] diff --git a/server/base/Cargo.toml b/server/base/Cargo.toml index 6fc53b6..135ba9f 100644 --- a/server/base/Cargo.toml +++ b/server/base/Cargo.toml @@ -19,4 +19,6 @@ libc = "0.2" log = "0.4" parking_lot = { version = "0.11.1", features = [] } nom = "6.0.0" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" time = "0.1" diff --git a/server/base/time.rs b/server/base/time.rs index e6e6cd8..e3959eb 100644 --- a/server/base/time.rs +++ b/server/base/time.rs @@ -9,6 +9,7 @@ use nom::branch::alt; use nom::bytes::complete::{tag, take_while_m_n}; use nom::combinator::{map, map_res, opt}; use nom::sequence::{preceded, tuple}; +use serde::{Deserialize, Serialize}; use std::fmt; use std::ops; use std::str::FromStr; @@ -19,7 +20,7 @@ type IResult<'a, I, O> = nom::IResult>; pub const TIME_UNITS_PER_SEC: i64 = 90_000; /// A time specified as 90,000ths of a second since 1970-01-01 00:00:00 UTC. -#[derive(Clone, Copy, Default, Eq, Ord, PartialEq, PartialOrd)] +#[derive(Clone, Copy, Default, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)] pub struct Time(pub i64); /// Returns a parser for a `len`-digit non-negative number which fits into an i32. @@ -221,7 +222,7 @@ impl fmt::Display for Time { /// A duration specified in 1/90,000ths of a second. /// Durations are typically non-negative, but a `moonfire_db::db::CameraDayValue::duration` may be /// negative. -#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd)] +#[derive(Clone, Copy, Default, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)] pub struct Duration(pub i64); impl Duration { @@ -230,6 +231,13 @@ impl Duration { } } +impl fmt::Debug for Duration { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + // Write both the raw and display forms. + write!(f, "{} /* {} */", self.0, self) + } +} + impl fmt::Display for Duration { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let mut seconds = self.0 / TIME_UNITS_PER_SEC; diff --git a/server/src/json.rs b/server/src/json.rs index 6db8429..f87efb8 100644 --- a/server/src/json.rs +++ b/server/src/json.rs @@ -2,6 +2,7 @@ // Copyright (C) 2020 The Moonfire NVR Authors; see AUTHORS and LICENSE.txt. // SPDX-License-Identifier: GPL-v3.0-or-later WITH GPL-3.0-linking-exception. +use base::time::{Duration, Time}; use db::auth::SessionHash; use failure::{format_err, Error}; use serde::ser::{Error as _, SerializeMap, SerializeSeq, Serializer}; @@ -77,9 +78,9 @@ pub struct CameraConfig<'a> { #[serde(rename_all = "camelCase")] pub struct Stream<'a> { pub retain_bytes: i64, - pub min_start_time_90k: Option, - pub max_end_time_90k: Option, - pub total_duration_90k: i64, + pub min_start_time_90k: Option