Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
antonisgotsis committed May 12, 2023
2 parents 1752fa0 + 34912d9 commit 5878330
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 45 deletions.
21 changes: 9 additions & 12 deletions insighioNode/apps/demo_console/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ def execute():
executeDeviceDeinitialization()
executeTimingConfiguration()

def storeMeasurement(measurements, force_store):
if cfg._BATCH_UPLOAD_MESSAGE_BUFFER is None and not force_store:
logging.error("Batch upload not activated, ignoring")
return False

message_buffer.timestamp_measurements(measurements)
return message_buffer.store_measurement(measurements)

def executeDeviceInitialization():
# The demo_config.py is autogenerated by webui
# it is the concatenation of the template configurations in template folder.
Expand Down Expand Up @@ -121,7 +113,7 @@ def executeMeasureAndUploadLoop():

# if the RTC is OK, timestamp message
if buffered_upload_enabled and not execute_connection_procedure:
measurementStored = storeMeasurement(measurements, False)
measurementStored = scenario_utils.storeMeasurement(measurements, False)
# if always on, do run connect and upload
# else run connection only if measurement was not stored
execute_connection_procedure = True if always_on else not measurementStored
Expand Down Expand Up @@ -252,7 +244,7 @@ def executeConnectAndUpload(cfg, measurements, is_first_run, always_on):
if not message_sent:
logging.info("Message transmission failed, storing for later")
if scenario_utils.get_config("_STORE_MEASUREMENT_IF_FAILED_CONNECTION"):
storeMeasurement(measurements, True)
scenario_utils.storeMeasurement(measurements, True)

# disconnect from network
if not always_on or not is_connected:
Expand All @@ -264,13 +256,18 @@ def executeConnectAndUpload(cfg, measurements, is_first_run, always_on):
logging.exception(e, "Exception during disconenction:")
return message_sent

def get_var_from_module(module, key):
return getattr(module, key) if hasattr(module, key) else None

def executeDeviceStatisticsUpload(cfg, network):
stats = {}

try:
import srcInfo
stats["sw_branch"] = srcInfo.branch
stats["sw_commit"] = srcInfo.commit
stats["sw_branch"] = get_var_from_module(srcInfo, "branch")
stats["sw_commit"] = get_var_from_module(srcInfo, "commit")
stats["sw_custom_branch"] = get_var_from_module(srcInfo, "custom_branch")
stats["sw_custom_commit"] = get_var_from_module(srcInfo, "custom_commit")
except:
logging.error("no srcInfo file found")

Expand Down
29 changes: 18 additions & 11 deletions insighioNode/apps/demo_console/scenario_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,26 @@ def watchdog_reset():


# functions
def get_measurements(cfg):
def get_measurements(cfg_dummy=None):
measurements = {}

try:
if cfg._MEAS_BATTERY_STAT_ENABLE:
if get_config("_MEAS_BATTERY_STAT_ENABLE"):
vbatt = read_battery_voltage()
set_value_int(measurements, "vbatt", vbatt, SenmlSecondaryUnits.SENML_SEC_UNIT_MILLIVOLT)

if cfg._MEAS_BOARD_STAT_ENABLE:
if get_config("_MEAS_BOARD_STAT_ENABLE"):
(mem_alloc, mem_free) = device_info.get_heap_memory()
set_value(measurements, "reset_cause", device_info.get_reset_cause())
set_value(measurements, "mem_alloc", mem_alloc, SenmlUnits.SENML_UNIT_BYTE)
set_value(measurements, "mem_free", mem_free, SenmlUnits.SENML_UNIT_BYTE)
if cfg._MEAS_TEMP_UNIT_IS_CELSIUS:
set_value_float(measurements, "cpu_temp", device_info.get_cpu_temp(), SenmlUnits.SENML_UNIT_DEGREES_CELSIUS)
else:
set_value_float(measurements, "cpu_temp", device_info.get_cpu_temp(False), SenmlSecondaryUnits.SENML_SEC_UNIT_FAHRENHEIT)
try:
if get_config("_MEAS_TEMP_UNIT_IS_CELSIUS"):
set_value_float(measurements, "cpu_temp", device_info.get_cpu_temp(), SenmlUnits.SENML_UNIT_DEGREES_CELSIUS)
else:
set_value_float(measurements, "cpu_temp", device_info.get_cpu_temp(False), SenmlSecondaryUnits.SENML_SEC_UNIT_FAHRENHEIT)
except:
logging.error("Error getting cpu_temp.")
except Exception as e:
logging.exception(e, "unable to measure board sensors")

Expand All @@ -100,7 +103,7 @@ def get_measurements(cfg):

# read internal temperature and humidity
try:
if cfg._MEAS_BOARD_SENSE_ENABLE:
if get_config("_MEAS_BOARD_SENSE_ENABLE"):
if cfg._UC_INTERNAL_TEMP_HUM_SENSOR == cfg._CONST_SENSOR_SI7021:
from sensors import si7021 as sens
elif cfg._UC_INTERNAL_TEMP_HUM_SENSOR == cfg._UC_INTERNAL_TEMP_HUM_SENSOR:
Expand All @@ -118,7 +121,7 @@ def get_measurements(cfg):
else: #if shield_name == get_config("_CONST_SHIELD_DIG_ANALOG"):
default_board_measurements(measurements)

if hasattr(cfg, '_MEAS_KEYVALUE') and cfg._MEAS_KEYVALUE:
if get_config("_MEAS_KEYVALUE"):
add_explicit_key_values(measurements)

except Exception as e:
Expand Down Expand Up @@ -318,9 +321,13 @@ def execute_transformation(measurements, name, raw_value, transformator):
logging.exception(e, "transformator name:{}, raw_value:{}, code:{}".format(name, raw_value, transformator))
pass

def storeMeasurement(measurements, force_store):
def storeMeasurement(measurements, force_store=False):
if get_config("_BATCH_UPLOAD_MESSAGE_BUFFER") is None and not force_store:
logging.error("Batch upload not activated, ignoring")
return False

message_buffer.timestamp_measurements(measurements)
return message_buffer.store_measurement(measurements)
return message_buffer.store_measurement(measurements, force_store)

def read_accelerometer():
logging.info("starting XL thread")
Expand Down
18 changes: 13 additions & 5 deletions insighioNode/lib/networking/modem/modem_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ def init(self, ip_version, apn, technology):

self.set_technology(technology)
if technology.lower() == "nbiot":
self.send_at_cmd("AT+COPS=1,2,20201,9")
self.send_at_cmd("AT+COPS=1,2,20201,9", 180000)
else:
self.send_at_cmd("AT+COPS=0")
self.send_at_cmd("AT+COPS=0", 180000)

# disable command echo
self.send_at_cmd('ATE0')
Expand Down Expand Up @@ -220,12 +220,20 @@ def disconnect(self):

def get_rssi(self):
if self.ppp is None:
regex_rssi = r"\+CSQ:\s*(\d+),\d+"
self.rssi = -141
(status, lines) = self.send_at_cmd('AT+CSQ')
if status and len(lines) > 0:
rssi_tmp = int(lines[0].split(',')[0].split(' ')[-1])
if(rssi_tmp >= 0 and rssi_tmp <= 31):
self.rssi = -113 + rssi_tmp * 2
for line in lines:
match_res = ure.search(regex_rssi, line)
if match_res is not None:
try:
rssi_tmp = int(match_res.group(1))
if(rssi_tmp >= 0 and rssi_tmp <= 31):
self.rssi = -113 + rssi_tmp * 2
except:
pass
break
return self.rssi

def get_extended_signal_quality(self):
Expand Down
4 changes: 3 additions & 1 deletion insighioNode/lib/networking/modem/modem_bg600.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def set_technology(self, technology):
else:
self.send_at_cmd('AT+QCFG="nwscanseq",00,0')
self.send_at_cmd('AT+QCFG="nwscanmode",0,0')
self.send_at_cmd('AT+CFUN=1,1', 30000, "APP RDY")
self.send_at_cmd('AT+CFUN=1,1', 15000, "APP RDY")
self.send_at_cmd('ATE0')
utime.sleep_ms(1000)

def prioritizeWWAN(self):
Expand Down Expand Up @@ -98,6 +99,7 @@ def set_gps_state(self, poweron=True):
command = ""
if poweron:
command = 'AT+QGPS=1'
self.send_at_cmd('AT+QGPSCFG="gnssconfig",5')
else:
command = 'AT+QGPSEND'

Expand Down
34 changes: 19 additions & 15 deletions insighioNode/lib/utils/configuration_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"_UC_IO_SCALE_OFFSET": "meas_scale_offset",
"_UC_IO_SCALE_SCALE": "meas_scale_scale",
"protocol": "protocol",
"network": "network",
"_ALWAYS_ON_CONNECTION": "always_on_connection",
"_FORCE_ALWAYS_ON_CONNECTION": "force_always_on_connection",
"_ALWAYS_ON_PERIOD": "always_on_period",
Expand All @@ -105,6 +106,16 @@

NoneType = type(None)

def fixValue(val):
if val == "true":
return "True"
elif val == "false":
return "False"
elif val == "":
return "None"
else:
return val

def get_config_values(fillWithUndefinedIfNotExists=True, prepareForInternalUse=False):
configKeyValues = dict()

Expand Down Expand Up @@ -170,13 +181,7 @@ def get_config_values(fillWithUndefinedIfNotExists=True, prepareForInternalUse=F
tmpDict = dict()
for key in configKeyValues.keys():
newKey = key.replace("_", "-")
newValue = configKeyValues[key]
if newValue == "true":
newValue = "True"
elif newValue == "false":
newValue = "False"
elif newValue == "":
newValue = "None"
newValue = fixValue(configKeyValues[key])
tmpDict[newKey] = newValue
return tmpDict
else:
Expand Down Expand Up @@ -247,10 +252,7 @@ def stringParamsToDict(configurationParameters):
for keyValueStr in keyValueStrings:
keyValue = keyValueStr.split("=")
if len(keyValue) == 2:
if keyValue[1] == "true":
keyValue[1] = "True"
elif keyValue[1] == "false":
keyValue[1] = "False"
keyValue[1] = fixValue(keyValue[1])
keyValueDict[keyValue[0]] = keyValue[1]
logging.debug("key value added [{}] -> {}".format(keyValue[0], keyValue[1]))
else:
Expand All @@ -265,12 +267,14 @@ def apply_configuration(keyValuePairDictionary):
# fix naming of keys to use '-' instead of '_'
# unfortunatelly it is technical burden from old implementations, to be fixed in future release
for param in keyValuePairDictionary:
val = keyValuePairDictionary[param]
key = param
if '_' in param:
new_key = param.replace("_", '-')
print("changing key [{}] to [{}]".format(param, new_key))
val = keyValuePairDictionary[param]
key = param.replace("_", '-')
print("changing key [{}] to [{}]".format(param, key))
del keyValuePairDictionary[param]
keyValuePairDictionary[new_key] = val

keyValuePairDictionary[key] = fixValue(val)

gc.collect()

Expand Down
2 changes: 1 addition & 1 deletion insighioNode/www/step-5-measurements.html
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@
showElement('input-s1-sensor-a-d-p2-trans-div', adp2 === "analog")
showElement('input-s1-sensor-a-d-p3-trans-div', adp3 === "analog")

//setElemValueBool('input-store-meas-if-failed-conn', data.store_meas_if_failed_conn, false)
setElemValueBool('input-store-meas-if-failed-conn', data.store_meas_if_failed_conn, false)

fillKeyValuePairsFromDictionary(data.meas_keyvalue)

Expand Down

0 comments on commit 5878330

Please sign in to comment.