Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gateway id override #62

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@
ca_cert=""
tls_cert=""
tls_key=""

[gateway]
# Gateway ID.
#
# Only set this if you would like to override the Gateway ID fetched from the backend.
# This will allow the MQTT loop to start prior backend setup.
#gateway_id=""
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@
ca_cert=""
tls_cert=""
tls_key=""

[gateway]
# Gateway ID.
#
# Only set this if you would like to override the Gateway ID fetched from the backend.
# This will allow the MQTT loop to start prior backend setup.
#gateway_id=""
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@
ca_cert=""
tls_cert=""
tls_key=""

[gateway]
# Gateway ID.
#
# Only set this if you would like to override the Gateway ID fetched from the backend.
# This will allow the MQTT loop to start prior backend setup.
#gateway_id=""
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@
ca_cert=""
tls_cert=""
tls_key=""

[gateway]
# Gateway ID.
#
# Only set this if you would like to override the Gateway ID fetched from the backend.
# This will allow the MQTT loop to start prior backend setup.
#gateway_id=""
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@
ca_cert=""
tls_cert=""
tls_key=""

[gateway]
# Gateway ID.
#
# Only set this if you would like to override the Gateway ID fetched from the backend.
# This will allow the MQTT loop to start prior backend setup.
#gateway_id=""
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@
ca_cert=""
tls_cert=""
tls_key=""

[gateway]
# Gateway ID.
#
# Only set this if you would like to override the Gateway ID fetched from the backend.
# This will allow the MQTT loop to start prior backend setup.
#gateway_id=""
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@
ca_cert=""
tls_cert=""
tls_key=""

[gateway]
# Gateway ID.
#
# Only set this if you would like to override the Gateway ID fetched from the backend.
# This will allow the MQTT loop to start prior backend setup.
#gateway_id=""
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@
ca_cert=""
tls_cert=""
tls_key=""

[gateway]
# Gateway ID.
#
# Only set this if you would like to override the Gateway ID fetched from the backend.
# This will allow the MQTT loop to start prior backend setup.
#gateway_id=""
10 changes: 10 additions & 0 deletions src/cmd/configfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,16 @@ pub fn run(config: &Configuration) {

# On MQTT connection error.
on_mqtt_connection_error=[]


# Gateway configuration
[gateway]

# Gateway ID.
#
# Only set this if you would like to override the Gateway ID fetched from the backend.
# This will allow the MQTT loop to start prior backend setup.
#gateway_id="{{ gateway.gateway_id }}"
"#;

let reg = Handlebars::new();
Expand Down
15 changes: 15 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Configuration {
pub metadata: Metadata,
pub commands: HashMap<String, Vec<String>>,
pub callbacks: Callbacks,
pub gateway: Gateway,
}

impl Configuration {
Expand Down Expand Up @@ -176,3 +177,17 @@ pub struct Callbacks {
pub on_mqtt_connected: Vec<String>,
pub on_mqtt_connection_error: Vec<String>,
}

#[derive(Serialize, Deserialize)]
#[serde(default)]
pub struct Gateway {
pub gateway_id: Option<String>,
}

impl Default for Gateway {
fn default() -> Self {
Gateway {
gateway_id: None,
}
}
}
20 changes: 18 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,24 @@ async fn main() {

commands::setup(&config).expect("Setup commands error");
metadata::setup(&config).expect("Setup metadata error");
backend::setup(&config).await.expect("Setup backend error");
mqtt::setup(&config).await.expect("Setup MQTT client error");

// Check for overriden gateway_id in config, in which case it will be used
// rather than waiting to fetch it from backend
if let Some(gateway_id) = &config.gateway.gateway_id {
if gateway_id.chars().count() == 0 {
panic!("overriden config gateway_id must not be an empty string");
}

info!("Gateway ID overriden in config, starting MQTT loop before polling for backend.");

mqtt::setup(&config).await.expect("Setup MQTT client error");
backend::setup(&config).await.expect("Setup backend error");
}
else {
// poll for backend first to retrieve gateway_id
backend::setup(&config).await.expect("Setup backend error");
mqtt::setup(&config).await.expect("Setup MQTT client error");
}

let mut signals = Signals::new([SIGINT, SIGTERM]).unwrap();
signals.forever().next();
Expand Down
14 changes: 13 additions & 1 deletion src/mqtt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,19 @@ pub async fn setup(conf: &Configuration) -> Result<()> {
};

// get gateway id
let gateway_id = get_gateway_id().await?;
let gateway_id = if let Some(gateway_id) = &conf.gateway.gateway_id {
// use gateway id overriden in config
gateway_id.clone()
}
else {
// fetch from backend
get_gateway_id().await?
};

info!(
"Gateway ID retrieved, gateway_id: {}",
gateway_id
);

// set client id
let client_id = if conf.mqtt.client_id.is_empty() {
Expand Down
Loading