<commit_before>#!/bin/bash

#This script manages all available  sensors 
#
# Usage: sudo sh mazi-sense.sh [senseName] [options]
#set  -x

##### Initialization  ######
DUR="0"     #initialization of duration
interval="0"     #initialization of interval
domain="localhost"
conf="/etc/mazi/mazi.conf"
path_sense="/root/back-end/lib"
IP="$(ifconfig wlan0 | grep 'inet addr' | awk '{printf $2}'| grep -o '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*')"
i=0

#### Functions ####
usage() { echo "Usage: sudo sh mazi-sense.sh [SenseName] [Options] [SensorOptions]"
	  echo ""
	  echo "[SenseName]"
	  echo "  -n,--name                         Set the name of the sensor"
	  echo "                                        {sht11,sensehat....}"
          echo ""
	  echo "[Options]"
          echo "  -s , --store                       Store the measurements in the Database"
          echo "  -d , --duration                    Duration in seconds to take a measurement"
          echo "  -i , --interval                    Seconds between periodic measurement"
          echo "  -a , --available                   Displays the status of the available sensors"
          echo "  -D , --domain                      Set a remote server domain.( Default is localhost )"
          echo "  --status                           Displays the status of store process"
          echo ""
	  echo "[SensorOptions]"
          read -a sensors <<<$(find lib/ -maxdepth 1 -name "*.py")
          for s in ${sensors[@]}
          do
             python $s --help
          done                     1>&2; exit 1; }



status_call() {
  if [ -f /etc/mazi/rest.log ];then
    response=$(tac /etc/mazi/rest.log| grep "$1" | awk -v FS="($1:|http_code:)" '{print $2}')
    http_code=$(tac /etc/mazi/rest.log| grep "$1" | head -1 | awk '{print $NF}')
  fi
  [ "$http_code" = "200" -a "$(echo $response | grep "OK")" ] && call_st="OK" && error=""
  [ "$http_code" = "000" ] && call_st="ERROR:" && error="Connection refused"
  [ "$http_code" = "200" -a ! "$(echo $response | grep "OK")" ] && call_st="ERROR:" && error="$response"
  [ "$http_code" = "500" ] && call_st="ERROR:" && error="The server encountered an unexpected condition which prevented it from fulfilling the request"
}

make_data(){
 

 TIME=$(date  "+%Y-%m-%d %H:%M:S")
 read -a values <<<$(python lib/$NAME.py ${argum[@]})
 data='{'$(echo $(printf "\"%s\":\"%s\", "  "${values[@]}"))'
        "time":"'$TIME'",
        "sensor_id":"'$ID'"}'

 echo ${values[@]}
}

sensor_id(){
 ##### Sensors types ###########
 read -a senstypes <<<$(python lib/$NAME.py ${argum[@]} | awk '{print $1}')
 senstypes="$(printf '%s\n' "${senstypes[@]}" | jq -R . | jq -s .)"

 ID=$(curl -s -H 'Content-Type: application/json' -X GET --data "{\"deployment\":$(jq ".deployment" $conf),\"sensor_name\":\"$NAME\",\"ip\":\"$IP\"}" http://$domain:4567/sensors/id)
 ### Register the sensor ####
 if [ ! $ID ];then
    device_id=$(curl -s -X GET -d @$conf http://$domain:4567/device/id)
    [ ! $device_id ] && device_id=$(curl -s -X POST -d @$conf http://$domain:4567/monitoring/register)
    ID=$(curl -s -H 'Content-Type: application/json' -X POST --data "{\"deployment\":$(jq ".deployment" $conf),\"sensor_name\":\"$NAME\",\"ip\":\"$IP\",\"device_id\":\"$device_id\"}" http://$domain:4567/sensor/register)
 fi

 ### Create the table measurements or update it with new types of sensors
 curl -s -H "Content-Type: application/json" -H 'Accept: application/json' -X POST -d "{\"senstypes\":$senstypes}" http://$domain:4567/create/measurements > /dev/null
}


available_fun(){

 read -a sensors <<<$(find lib/ -maxdepth 1 -name "*.py" -exec basename \{} .py \;)
 for s in ${sensors[@]}
 do
    var=$(python lib/$s.py --detect 2>&1)
    if [ "$var" == "$s" ];then
       SERVICE="$(ps -ef | grep $s | grep -v 'grep')"
       [ "$SERVICE" != "" ] && echo "$s active $IP" || echo "$s inactive $IP"
    fi
 done
 exit 0;
}


status_fun(){
  read -a sensors <<<$(find lib/ -maxdepth 1 -name "*.py" -exec basename \{} .py \;)
  for s in ${sensors[@]}
  do
     var=$(python lib/$s.py --detect)
     [ "$var" == "$s" ] &&  NAME=$s
  done
  status_call $NAME
  [ "$(ps aux | grep "store\|\-s" | grep "mazi-sense.sh" | grep "\-n $NAME "| grep -v 'grep')" ] && echo "$NAME active $call_st $error" || echo "$NAME inactive"<commit_msg>redirect errors to stdout<commit_after>#!/bin/bash

#This script manages all available  sensors 
#
# Usage: sudo sh mazi-sense.sh [senseName] [options]
#set  -x

##### Initialization  ######
DUR="0"     #initialization of duration
interval="0"     #initialization of interval
domain="localhost"
conf="/etc/mazi/mazi.conf"
path_sense="/root/back-end/lib"
IP="$(ifconfig wlan0 | grep 'inet addr' | awk '{printf $2}'| grep -o '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*')"
i=0

#### Functions ####
usage() { echo "Usage: sudo sh mazi-sense.sh [SenseName] [Options] [SensorOptions]"
	  echo ""
	  echo "[SenseName]"
	  echo "  -n,--name                         Set the name of the sensor"
	  echo "                                        {sht11,sensehat....}"
          echo ""
	  echo "[Options]"
          echo "  -s , --store                       Store the measurements in the Database"
          echo "  -d , --duration                    Duration in seconds to take a measurement"
          echo "  -i , --interval                    Seconds between periodic measurement"
          echo "  -a , --available                   Displays the status of the available sensors"
          echo "  -D , --domain                      Set a remote server domain.( Default is localhost )"
          echo "  --status                           Displays the status of store process"
          echo ""
	  echo "[SensorOptions]"
          read -a sensors <<<$(find lib/ -maxdepth 1 -name "*.py")
          for s in ${sensors[@]}
          do
             python $s --help
          done                     1>&2; exit 1; }



status_call() {
  if [ -f /etc/mazi/rest.log ];then
    response=$(tac /etc/mazi/rest.log| grep "$1" | awk -v FS="($1:|http_code:)" '{print $2}')
    http_code=$(tac /etc/mazi/rest.log| grep "$1" | head -1 | awk '{print $NF}')
  fi
  [ "$http_code" = "200" -a "$(echo $response | grep "OK")" ] && call_st="OK" && error=""
  [ "$http_code" = "000" ] && call_st="ERROR:" && error="Connection refused"
  [ "$http_code" = "200" -a ! "$(echo $response | grep "OK")" ] && call_st="ERROR:" && error="$response"
  [ "$http_code" = "500" ] && call_st="ERROR:" && error="The server encountered an unexpected condition which prevented it from fulfilling the request"
}

make_data(){
 

 TIME=$(date  "+%Y-%m-%d %H:%M:S")
 read -a values <<<$(python lib/$NAME.py ${argum[@]})
 data='{'$(echo $(printf "\"%s\":\"%s\", "  "${values[@]}"))'
        "time":"'$TIME'",
        "sensor_id":"'$ID'"}'

 echo ${values[@]}
}

sensor_id(){
 ##### Sensors types ###########
 read -a senstypes <<<$(python lib/$NAME.py ${argum[@]} | awk '{print $1}')
 senstypes="$(printf '%s\n' "${senstypes[@]}" | jq -R . | jq -s .)"

 ID=$(curl -s -H 'Content-Type: application/json' -X GET --data "{\"deployment\":$(jq ".deployment" $conf),\"sensor_name\":\"$NAME\",\"ip\":\"$IP\"}" http://$domain:4567/sensors/id)
 ### Register the sensor ####
 if [ ! $ID ];then
    device_id=$(curl -s -X GET -d @$conf http://$domain:4567/device/id)
    [ ! $device_id ] && device_id=$(curl -s -X POST -d @$conf http://$domain:4567/monitoring/register)
    ID=$(curl -s -H 'Content-Type: application/json' -X POST --data "{\"deployment\":$(jq ".deployment" $conf),\"sensor_name\":\"$NAME\",\"ip\":\"$IP\",\"device_id\":\"$device_id\"}" http://$domain:4567/sensor/register)
 fi

 ### Create the table measurements or update it with new types of sensors
 curl -s -H "Content-Type: application/json" -H 'Accept: application/json' -X POST -d "{\"senstypes\":$senstypes}" http://$domain:4567/create/measurements > /dev/null
}


available_fun(){

 read -a sensors <<<$(find lib/ -maxdepth 1 -name "*.py" -exec basename \{} .py \;)
 for s in ${sensors[@]}
 do
    var=$(python lib/$s.py --detect 2>&1)
    if [ "$var" == "$s" ];then
       SERVICE="$(ps -ef | grep $s | grep -v 'grep')"
       [ "$SERVICE" != "" ] && echo "$s active $IP" || echo "$s inactive $IP"
    fi
 done
 exit 0;
}


status_fun(){
  read -a sensors <<<$(find lib/ -maxdepth 1 -name "*.py" -exec basename \{} .py \;)
  for s in ${sensors[@]}
  do
     var=$(python lib/$s.py --detect 2>&1)
     [ "$var" == "$s" ] &&  NAME=$s
  done
  status_call $NAME
  [ "$(ps aux | grep "store\|\-s" | grep "mazi-sense.sh" | grep "\-n $NAME "| grep -v 'grep')" ] && echo "$NAME active $call_st $error" || echo "$NAME inactive"
{'fraction_non_alphanumeric': 0.21627880050943615, 'fraction_numerical': 0.014588398749565822, 'mean_word_length': 2.5014187271990274, 'pattern_counts': {'":': 22, '<': 33, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1144967', 'n_tokens_mistral': 3069, 'n_tokens_neox': 2887, 'n_words': 821}