aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjørn Mork <bjorn@mork.no>2019-05-28 10:36:21 +0200
committerBjørn Mork <bjorn@mork.no>2019-05-28 10:36:21 +0200
commitbcf83f289ecdcea9e56b9d4d43c21e52314d9c5c (patch)
treeecd473f17b63208747246a2a48c018feec72ef39
parentb8853b4a5f3d360bab6be693b568d29d4fceb2b2 (diff)
refactor scale and unit handling
Signed-off-by: Bjørn Mork <bjorn@mork.no>
-rw-r--r--aidon_v0001.json41
-rw-r--r--kamstrup_v0001.json38
-rw-r--r--kfm_001.json (renamed from kaifa_v0001.json)53
-rw-r--r--obinsectd.c91
4 files changed, 145 insertions, 78 deletions
diff --git a/aidon_v0001.json b/aidon_v0001.json
index 9c3f69d..9fe1305 100644
--- a/aidon_v0001.json
+++ b/aidon_v0001.json
@@ -1,6 +1,11 @@
{
"_metadata": {
- "revision": 1
+ "listurl" : "https://www.nek.no/wp-content/uploads/2019/02/Aidon-HAN-Interface-Description-v11A-ID-34331.pdf",
+ "listfile" : "Aidon_V0001.xlsx",
+ "listversion" : "10.05.2016",
+ "meter" : [ "6515", "6525", "6534", "6540", "6550" ],
+ "doc" : "https://www.nek.no/info-ams-han-utviklere/",
+ "revision" : "1"
},
"AIDON_V0001":{
"alias":{
@@ -26,8 +31,8 @@
"unit":{
"1-0:1.7.0.255":"kW",
"1-0:2.7.0.255":"kW",
- "1-0:3.7.0.255":"kW",
- "1-0:4.7.0.255":"kW",
+ "1-0:3.7.0.255":"kVAr",
+ "1-0:4.7.0.255":"kVAr",
"1-0:31.7.0.255":"A",
"1-0:51.7.0.255":"A",
"1-0:71.7.0.255":"A",
@@ -39,21 +44,21 @@
"1-0:3.8.0.255":"kVArh",
"1-0:4.8.0.255":"kVArh"
},
- "scale":{
- "1-0:1.7.0.255":0.001,
- "1-0:2.7.0.255":0.001,
- "1-0:3.7.0.255":0.001,
- "1-0:4.7.0.255":0.001,
- "1-0:31.7.0.255":0.1,
- "1-0:51.7.0.255":0.1,
- "1-0:71.7.0.255":0.1,
- "1-0:32.7.0.255":0.1,
- "1-0:52.7.0.255":0.1,
- "1-0:72.7.0.255":0.1,
- "1-0:1.8.0.255":0.01,
- "1-0:2.8.0.255":0.01,
- "1-0:3.8.0.255":0.01,
- "1-0:4.8.0.255":0.01
+ "scaler":{
+ "1-0:1.7.0.255":-3,
+ "1-0:2.7.0.255":-3,
+ "1-0:3.7.0.255":-3,
+ "1-0:4.7.0.255":-3,
+ "1-0:31.7.0.255":-1,
+ "1-0:51.7.0.255":-1,
+ "1-0:71.7.0.255":-1,
+ "1-0:32.7.0.255":-1,
+ "1-0:52.7.0.255":-1,
+ "1-0:72.7.0.255":-1,
+ "1-0:1.8.0.255":-2,
+ "1-0:2.8.0.255":-2,
+ "1-0:3.8.0.255":-2,
+ "1-0:4.8.0.255":-2
}
}
}
diff --git a/kamstrup_v0001.json b/kamstrup_v0001.json
index 69202c0..583a9d0 100644
--- a/kamstrup_v0001.json
+++ b/kamstrup_v0001.json
@@ -1,7 +1,13 @@
{
"_metadata": {
- "revision": 1
- },
+ "listurl" : "https://www.nek.no/wp-content/uploads/2018/11/Kamstrup_V0001-181022.pdf",
+ "listfile" : "Kamstrup_V0001.xlsx",
+ "listversion" : "03.05.2016",
+ "comment" : "listversion is likely wrong - assuming real version is 2018.10.22",
+ "meter" : [ "OMNIPOWER" ],
+ "doc" : "https://www.nek.no/info-ams-han-utviklere/",
+ "revision" : "1"
+ },
"Kamstrup_V0001":{
"alias":{
"1-1:0.2.129.255":"ListId",
@@ -26,8 +32,8 @@
"unit":{
"1-1:1.7.0.255":"kW",
"1-1:2.7.0.255":"kW",
- "1-1:3.7.0.255":"kW",
- "1-1:4.7.0.255":"kW",
+ "1-1:3.7.0.255":"kVAr",
+ "1-1:4.7.0.255":"kVAr",
"1-1:31.7.0.255":"A",
"1-1:51.7.0.255":"A",
"1-1:71.7.0.255":"A",
@@ -39,18 +45,18 @@
"1-1:3.8.0.255":"kVArh",
"1-1:4.8.0.255":"kVArh"
},
- "scale":{
- "1-1:1.7.0.255":0.001,
- "1-1:2.7.0.255":0.001,
- "1-1:3.7.0.255":0.001,
- "1-1:4.7.0.255":0.001,
- "1-1:31.7.0.255":0.01,
- "1-1:51.7.0.255":0.01,
- "1-1:71.7.0.255":0.01,
- "1-1:1.8.0.255":0.01,
- "1-1:2.8.0.255":0.01,
- "1-1:3.8.0.255":0.01,
- "1-1:4.8.0.255":0.01
+ "scaler":{
+ "1-1:1.7.0.255":-3,
+ "1-1:2.7.0.255":-3,
+ "1-1:3.7.0.255":-3,
+ "1-1:4.7.0.255":-3,
+ "1-1:31.7.0.255":-2,
+ "1-1:51.7.0.255":-2,
+ "1-1:71.7.0.255":-2,
+ "1-1:1.8.0.255":-2,
+ "1-1:2.8.0.255":-2,
+ "1-1:3.8.0.255":-2,
+ "1-1:4.8.0.255":-2
}
}
}
diff --git a/kaifa_v0001.json b/kfm_001.json
index 902129b..0527f8b 100644
--- a/kaifa_v0001.json
+++ b/kfm_001.json
@@ -1,6 +1,11 @@
{
"_metadata": {
- "revision": 1
+ "listurl" : "https://www.nek.no/wp-content/uploads/2018/11/Kaifa-KFM_001.pdf",
+ "listfile" : "KFM_001.xlsx",
+ "listversion" : "09.11.2018",
+ "meter" : ["MA105H2E", "MA304H3E", "MA304H4", "MA304T3", "MA304T4"],
+ "doc" : "https://www.nek.no/info-ams-han-utviklere/",
+ "revision" : "1"
},
"KFM_001":{
"alias":{
@@ -24,36 +29,36 @@
"1-0:4.8.0.255":"CumulativeReactiveEnergyExport"
},
"unit":{
- "1-0:1.7.0.255":"kW",
- "1-0:2.7.0.255":"kW",
- "1-0:3.7.0.255":"kW",
- "1-0:4.7.0.255":"kW",
+ "1-0:1.7.0.255":"W",
+ "1-0:2.7.0.255":"W",
+ "1-0:3.7.0.255":"Var",
+ "1-0:4.7.0.255":"Var",
"1-0:31.7.0.255":"A",
"1-0:51.7.0.255":"A",
"1-0:71.7.0.255":"A",
"1-0:32.7.0.255":"V",
"1-0:52.7.0.255":"V",
"1-0:72.7.0.255":"V",
- "1-0:1.8.0.255":"kWh",
- "1-0:2.8.0.255":"kWh",
- "1-0:3.8.0.255":"kVArh",
- "1-0:4.8.0.255":"kVArh"
+ "1-0:1.8.0.255":"Wh",
+ "1-0:2.8.0.255":"Wh",
+ "1-0:3.8.0.255":"VArh",
+ "1-0:4.8.0.255":"VArh"
},
- "scale":{
- "1-0:1.7.0.255":0.001,
- "1-0:2.7.0.255":0.001,
- "1-0:3.7.0.255":0.001,
- "1-0:4.7.0.255":0.001,
- "1-0:31.7.0.255":0.1,
- "1-0:51.7.0.255":0.1,
- "1-0:71.7.0.255":0.1,
- "1-0:32.7.0.255":0.1,
- "1-0:52.7.0.255":0.1,
- "1-0:72.7.0.255":0.1,
- "1-0:1.8.0.255":0.001,
- "1-0:2.8.0.255":0.001,
- "1-0:3.8.0.255":0.001,
- "1-0:4.8.0.255":0.001
+ "scaler":{
+ "1-0:1.7.0.255":0,
+ "1-0:2.7.0.255":0,
+ "1-0:3.7.0.255":0,
+ "1-0:4.7.0.255":0,
+ "1-0:31.7.0.255":-3,
+ "1-0:51.7.0.255":-3,
+ "1-0:71.7.0.255":-3,
+ "1-0:32.7.0.255":-1,
+ "1-0:52.7.0.255":-1,
+ "1-0:72.7.0.255":-1,
+ "1-0:1.8.0.255":0,
+ "1-0:2.8.0.255":0,
+ "1-0:3.8.0.255":0,
+ "1-0:4.8.0.255":0
}
}
}
diff --git a/obinsectd.c b/obinsectd.c
index a9a4ddc..53b9100 100644
--- a/obinsectd.c
+++ b/obinsectd.c
@@ -995,16 +995,16 @@ static const char *get_unit(const char *key)
return NULL;
}
-static double get_scale(const char *key)
+static int get_scaler(const char *key)
{
json_object *scale, *ret;
if (unscaled || !current_list)
return 0;
- if (!json_object_object_get_ex(current_list, "scale", &scale))
+ if (!json_object_object_get_ex(current_list, "scaler", &scale))
return 0;
if (json_object_object_get_ex(scale, key, &ret))
- return json_object_get_double(ret);
+ return json_object_get_int(ret);
return 0;
}
@@ -1060,41 +1060,86 @@ static void set_current_list(const char *listname)
current_list = ret;
}
-static json_object *get_val_with_unit(const char *key, json_object *val)
+static json_object *format_value(const char *key, json_object *val)
{
const char *unit = get_unit(key);
- double scale = get_scale(key);
+ const int scaler = get_scaler(key);
+ double factor;
+ char *format;
+ int ival, ifactor = 0;
- if (!scale && !unit)
+ /* no formatting necessary */
+ if (!scaler && !unit)
return val;
- if (!printbuffer || !unit)
- return json_object_new_double(json_object_get_int(val) * scale);
+ /* failsafe... */
+ if (!json_object_is_type(val, json_type_int))
+ return val;
- if (!scale)
- sprintf(printbuffer, "%d %s", json_object_get_int(val), unit);
- else
- sprintf(printbuffer, "%0.3f %s", json_object_get_int(val) * scale, unit);
+ /* lookup instead of calulate - simpler and faster as long as we only support -3..3 */
+ switch (scaler) {
+ case -3:
+ factor = 0.001;
+ format = "%0.3f %s";
+ break;
+ case -2:
+ factor = 0.01;
+ format = "%0.2f %s";
+ break;
+ case -1:
+ factor = 0.1;
+ format = "%0.1f %s";
+ break;
+ case 0:
+ if (!unit)
+ return val;
+ ifactor = 1;
+ break;
+ case 1:
+ ifactor = 10;
+ break;
+ case 2:
+ ifactor = 100;
+ break;
+ case 3:
+ ifactor = 1000;
+ break;
+ default:
+ debug("unsupported scaler for %s: %d\n", key, scaler);
+ return val;
+ }
+ ival = json_object_get_int(val);
+ if (ifactor) {
+ if (!unit)
+ return json_object_new_int(ival * ifactor);
+ sprintf(printbuffer, "%d %s", ival * ifactor, unit);
+ } else {
+ if (!unit)
+ return json_object_new_double(ival * factor);
+ sprintf(printbuffer, format, ival * factor, unit);
+ }
return json_object_new_string(printbuffer);
}
static void add_obis(json_object *pubcfg, json_object *pub, const char *key, json_object *val)
{
const char *alias = get_alias(key);
- json_object *normal, *newval = val;
+ json_object *tmp, *newval = val;
if (!current_list && !strcmp(key, "1-1:0.2.129.255"))
set_current_list(json_object_get_string(val));
- newval = get_val_with_unit(key, val);
- if (json_object_object_get_ex(pub, "normal", &normal))
- json_object_object_add(normal, key, newval);
+ newval = format_value(key, val);
+ if (json_object_object_get_ex(pub, "normal", &tmp))
+ json_object_object_add(tmp, key, newval);
add_keyval(pubcfg, pub, key, newval, true);
- if (alias)
+ if (alias) {
+ if (json_object_object_get_ex(pub, "alias", &tmp))
+ json_object_object_add(tmp, alias, newval);
add_keyval(pubcfg, pub, alias, newval, true);
+ }
}
-
/*
* post process the parsed packet, converting the data to simple key => value pairs
*/
@@ -1118,6 +1163,9 @@ static json_object *normalize(json_object *pubcfg, json_object *json)
if (json_object_object_get_ex(pubcfg, "normal", NULL))
json_object_object_add(ret, "normal", json_object_new_object());
+ /* create an "alias" result list with all OBIS aliases as keys? */
+ if (json_object_object_get_ex(pubcfg, "alias", NULL))
+ json_object_object_add(ret, "alias", json_object_new_object());
/* overall formatting differs between the 3:
@@ -1515,9 +1563,11 @@ static void process_cfg(json_object *cfg, char *buf, size_t bufsize)
name = json_object_get_string(tmp);
set_publish(publish, name, NULL);
- /* include "timestamp" in the special "normal" object */
+ /* include "timestamp" in the special "normal" and "alias" objects */
if (!strcmp(name, "normal"))
set_publish(publish, "timestamp", tmp);
+ if (!strcmp(name, "alias"))
+ set_publish(publish, "timestamp", tmp);
}
}
@@ -1577,7 +1627,8 @@ static json_object *read_config(const char *fname, char *buf, size_t bufsize)
for (i = 0; i < json_object_array_length(tmp); i++) {
name = json_object_get_string(json_object_array_get_idx(tmp, i));
- parse_obisfile(list, name, buf, bufsize);
+ if (!parse_obisfile(list, name, buf, bufsize))
+ debug("failed to parse '%s'\n", name);
}
}