
#. /etc/node.conf
contract_abi=[{\"constant\":false,\"inputs\":[{\"name\":\"_newAddress\",\"type\":\"address\"}],\"name\":\"setMultisigAddress\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"name\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"approve\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"node\",\"type\":\"address\"}],\"name\":\"parkingOff\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"totalSupply\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"maxCap\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"chargingRate\",\"type\":\"uint256\"},{\"name\":\"parkingRate\",\"type\":\"uint256\"}],\"name\":\"registerNode\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_from\",\"type\":\"address\"},{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transferFrom\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transferToContributor\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"address\"}],\"name\":\"parkingSwitches\",\"outputs\":[{\"name\":\"node\",\"type\":\"address\"},{\"name\":\"startTime\",\"type\":\"uint256\"},{\"name\":\"endTime\",\"type\":\"uint256\"},{\"name\":\"fixedRate\",\"type\":\"uint256\"},{\"name\":\"initialized\",\"type\":\"bool\"},{\"name\":\"predefinedAmount\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"decimals\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"address\"}],\"name\":\"chargingSwitches\",\"outputs\":[{\"name\":\"node\",\"type\":\"address\"},{\"name\":\"startTime\",\"type\":\"uint256\"},{\"name\":\"endTime\",\"type\":\"uint256\"},{\"name\":\"fixedRate\",\"type\":\"uint256\"},{\"name\":\"initialized\",\"type\":\"bool\"},{\"name\":\"predefinedAmount\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"END_TIME\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"address\"}],\"name\":\"reservedFundsCharging\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"minCap\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"multisig\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"standard\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"rate\",\"type\":\"uint256\"}],\"name\":\"updateChargingRate\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_eth\",\"type\":\"uint256\"}],\"name\":\"calculateEthToChargcoin\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"address\"}],\"name\":\"rateOfCharging\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"address\"}],\"name\":\"rateOfParking\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"node\",\"type\":\"address\"},{\"name\":\"time\",\"type\":\"uint256\"}],\"name\":\"parkingOn\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_subtractedValue\",\"type\":\"uint256\"}],\"name\":\"decreaseApproval\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_owner\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"outputs\":[{\"name\":\"balance\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"address\"}],\"name\":\"reservedFundsParking\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_numberOfReturns\",\"type\":\"uint256\"}],\"name\":\"batchReturnEthIfFailed\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"node\",\"type\":\"address\"}],\"name\":\"chargeOff\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"owner\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"address\"}],\"name\":\"contributorList\",\"outputs\":[{\"name\":\"contributionAmount\",\"type\":\"uint256\"},{\"name\":\"tokensIssued\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"symbol\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"getPrice\",\"outputs\":[{\"name\":\"result\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"node\",\"type\":\"address\"}],\"name\":\"blockNode\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transfer\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"node\",\"type\":\"address\"},{\"name\":\"time\",\"type\":\"uint256\"}],\"name\":\"chargeOn\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"address\"}],\"name\":\"authorized\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"tokens\",\"type\":\"uint256\"}],\"name\":\"getAmountBonus\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"BEGIN_TIME\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"rate\",\"type\":\"uint256\"}],\"name\":\"updateParkingRate\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_contributor\",\"type\":\"address\"}],\"name\":\"createTokens\",\"outputs\":[],\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_addedValue\",\"type\":\"uint256\"}],\"name\":\"increaseApproval\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_owner\",\"type\":\"address\"},{\"name\":\"_spender\",\"type\":\"address\"}],\"name\":\"allowance\",\"outputs\":[{\"name\":\"remaining\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"crowdsaleState\",\"outputs\":[{\"name\":\"\",\"type\":\"uint8\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"newOwner\",\"type\":\"address\"}],\"name\":\"transferOwnership\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"ethRaised\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"fallback\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"blockNumber\",\"type\":\"uint256\"}],\"name\":\"CrowdsaleStarted\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"blockNumber\",\"type\":\"uint256\"}],\"name\":\"CrowdsaleEnded\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"ErrorSendingETH\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"blockNumber\",\"type\":\"uint256\"}],\"name\":\"MinCapReached\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"blockNumber\",\"type\":\"uint256\"}],\"name\":\"MaxCapReached\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"owner\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"spender\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Approval\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Transfer\",\"type\":\"event\"}]
contract_address=$(uci get node.service.contract)


GETH=/ext/geth-linux-mipsle-1.7.3-4bb3c89d/geth 
RPC=$(uci get node.service.rpc)
SWITCH_PORT=$(uci get node.service.switch_port)
DEBUG=$(uci get node.service.debug)
interval=$(uci get node.service.interval)
node_values=""


timestamp() {
	date +"%s"
}


jsonval() {
    temp=`echo $1 | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $2`
    echo ${temp##*|}
}


read_contract() {

	echo "
		var contractAddress = '$contract_address';
		var contract=eth.contract($contract_abi).at(contractAddress);
		var clientAddress = '$client_address';
		contract.chargingSwitches( clientAddress, function(error,result) {
			console.log('{\"node\":\"'+result[0]+'\",\"startTime\":\"'+result[1]+'\",\"endTime\":\"'+result[2]+'\",\"fixedRate\":\"'+result[3]+'\",\"initialized\":\"'+result[4]+'\",\"predefinedAmount\":\"'+result[5]+'\"}')
		});
	" > /tmp/run.js
	res=$($GETH --exec 'loadScript("/tmp/run.js")' attach $RPC)
	echo $res

}

check_balance() {

	echo "
		var contractAddress = '$contract_address';
		var contract=eth.contract($contract_abi).at(contractAddress);
		var clientAddress = '$client_address';
		contract.balanceOf( clientAddress, function(error,result) {
			console.log('{\"balance\":\"'+result+'\"}')
		});
	" > /tmp/run.js
	res=$($GETH --exec 'loadScript("/tmp/run.js")' attach $RPC)
	echo $res

}

switch_on() {

	gpioctl dirout-high $SWITCH_PORT
	echo 1 > /sys/class/leds/omega2p\:amber\:system/brightness
	
}


switch_off() {

	gpioctl dirout-low $SWITCH_PORT
	echo 0 > /sys/class/leds/omega2p\:amber\:system/brightness

}

json_output() {

	echo "{$node_values}" > /tmp/output.json

}

echo "{\"node_status\":\"running\",\"sdcard_ready\":\"1\"}" > /tmp/node_status.json

#led initialize
cat /sys/class/leds/omega2p\:amber\:system/trigger
echo gpio > /sys/class/leds/omega2p\:amber\:system/trigger

#init switch
gpioctl dirout $SWITCH_PORT
switch_off

if [ "$DEBUG" = 1 ]; then
	echo "Running service..."
fi

while [ true ]
do

	already_charged_time=0
	echo "{\"station_state\":\"idle\",\"client_state\":\"\"}" > /tmp/state.json
	node_values="\"switch_port\":\"$SWITCH_PORT\",\"interval\":\"$interval\",\"rpc\":\"$RPC\",\"contract_address\":\"$contract_address\""
	begin_t=$(timestamp) 

	node_address=$(uci get node.station.address)
	node_address=$(echo $node_address | tr /A-Z/  /a-z/) #to lower

	contract_address=$(uci get node.service.contract)
	contract_address=$(echo $contract_address | tr /A-Z/  /a-z/) #to lower

	RPC=$(uci get node.service.rpc)
	SWITCH_PORT=$(uci get node.service.switch_port)
	DEBUG=$(uci get node.service.debug)

	interval=$(uci get node.service.interval)
	protocol=$(uci get node.service.protocol)
	
	if [ "$node_address" = "" ]; then
		node_values="$node_values,\"error\":\"node address is not set\""
		echo "{$node_values}" > /tmp/output.json
		echo "{\"station_state\":\"no eth address\",\"client_state\":\"-\"}" > /tmp/state.json
		#json_output()
		sleep $interval
		continue
	fi

	connect=$(ping -c1 charg-stations.firebaseio.com | wc -l)
	if [ "$connect" != "6" ]; then
		node_values="$node_values,\"error\":\"no connection to db\""
		echo "{$node_values}" > /tmp/output.json
		echo "{\"station_state\":\"no connection\",\"client_state\":\"-\"}" > /tmp/state.json
		sleep $interval
		continue
	fi
		
	# register node in firebird
	db_call $node_address idle 
	node_values="$node_values,\"node_address\":\"$node_address\""
	
	# 1) read from db
	
	client_address=""
	
	firebase_res=$(db_call $node_address)
    firebase_res=${firebase_res// /}

	client_address=$(jsonval $firebase_res client_address)
	client_state=$(jsonval $firebase_res client_state)

	current_t=$(timestamp)
	diff_t=$((current_t-begin_t))
	if [ "$DEBUG" = 1 ]; then
		echo
		echo "Firebase data $firebase_res $diff_t"
	fi

	node_values="$node_values,\"client_address\":\"$client_address\""
	
	#balance_res=$(check_balance)
	#balance_res=${balance_res// true/}
	#node_values="$node_values,balance:\"$balance_res\""
	
	echo "{$node_values}" > /tmp/output.json
	#json_output()

	echo "{station_state:\"ready\",\"client_state\":\"$client_state\"}" > /tmp/state.json

	# 2) if there is a request then read contract
    
	if [ "$client_state" = "request" ]; then

		if [ "$DEBUG" = 1 ]; then
			echo "We have a request for charging ..."
		fi


		# 3) set ready state
		#uci set node.client.address=$client_address
		#uci set node.client.state=$client_state
		#uci set node.station.state='ready'
		#uci node commit
		
		station_state=""
		while [ "$station_state" != "ready" ]; do

			db_call $node_address ready 
			firebase_res=$(db_call $node_address)
			firebase_res=${firebase_res// /}
			station_state=$(jsonval $firebase_res station_state)
			current_t=$(timestamp)
			diff_t=$((current_t-begin_t))

			if [ "$DEBUG" = 1 ]; then
				echo "Setting station ready state $diff_t"
			fi
			sleep $interval
		
		done

		echo "{\"station_state\":\"ready\",\"client_state\":\"$client_state\"}" > /tmp/state.json
	
		# 4) wait until client is ready

		time_to_wait=300 # wait 5 min for client
		client_state=""
		while [ "$client_state" != "ready" ]; do

			firebase_res=$(db_call $node_address)
			firebase_res=${firebase_res// /}
			client_state=$(jsonval $firebase_res client_state)
			current_t=$(timestamp)
			diff_t=$((current_t-begin_t))

			if [ "$DEBUG" = 1 ]; then
				echo "Waiting client ready state $diff_t $time_to_wait"
			fi

			echo "{\"station_state\":\"ready\",\"client_state\":\"$client_state\"}" > /tmp/state.json
			
			time_to_wait=$((time_to_wait-diff_t))
			if [ $time_to_wait -lt 0 ]; then
				break
			fi
			sleep $interval
		done
		
		if [ $time_to_wait -lt 0 ]; then
			continue # client does not responde, go to beginning
		fi

		if [ "$protocol" = "fast" ]; then
			echo "Fast protocol, imidiately start charging"

			# 5) client is ready - start charging
			#uci set node.client.state=$client_state
			#uci set node.station.state='charging'
			#uci node commit
			echo "{\"station_state\":\"charging\",\"client_state\":\"$client_state\"}" > /tmp/state.json

			firebase_res=$(db_call $node_address charging)
			current_t=$(timestamp)

			switch_on
			echo "waiting blockchain approvement for 3 min"
			sleep 180
			already_charged_time=180
		fi

		contract_res=$(read_contract)
		contract_res=${contract_res// true/}
		current_t=$(timestamp)
		diff_t=$((current_t-begin_t))
		
		if [ "$DEBUG" = 1 ]; then
			echo "Contract data $contract_res $diff_t"
		fi

		contract_node=$(jsonval $contract_res node)
		contract_node=$(echo $contract_node | tr /A-Z/  /a-z/) #to lower
		initialized=$(jsonval $contract_res initialized)

		node_values="$node_values,\"contract_node\":\"$contract_node\""
		echo "{$node_values}" > /tmp/output.json
		#json_output()

		# 5) check if payment was to our node

		if [ "$contract_node" = "$node_address" ]; then

			start_time=$(jsonval $contract_res startTime)
			end_time=$(jsonval $contract_res endTime)
			charge_period=$(($end_time-$start_time))
			
			node_values="$node_values,start_time:\"$start_time\""
			node_values="$node_values,end_time:\"$end_time\""
			node_values="$node_values,charge_period:\"$charge_period\""
			echo "{$node_values}" > /tmp/output.json
			#json_output()
			
			if [ "$DEBUG" = 1 ]; then
				echo "Request for charging $charge_period sec"
			fi

			charge_finish=$(($current_t+$charge_period-$already_charged_time))
			if [ "$protocol" != "fast" ]; then
				switch_on
			fi

			echo "{\"station_state\":\"charging\",\"client_state\":\"$client_state\"}" > /tmp/state.json

			firebase_res=$(db_call $node_address charging)
			current_t=$(timestamp)

			# 6) check client state while charging
			
			time_to_charge=$charge_period
			#while [ "$client_state" = "ready" ]&&(( time_to_charge > 0 )); do
			while [ $time_to_charge -gt 0 ]; do
			
				#firebase_res=$(curl -s $firebase_url/nodes/$node_address.json)
				firebase_res=$(db_call $node_address)
				firebase_res=${firebase_res// /}
				client_state=$(jsonval $firebase_res client_state)
				
				if [ "$client_state" != "ready" ]; then
					break
				fi	
				
				current_t=$(timestamp)
				time_to_charge=$((charge_finish-current_t))

				echo "{\"station_state\":\"charging\",\"client_state\":\"$client_state\",\"time_to_charge\":\"$time_to_charge\"}" > /tmp/state.json
				
				if [ "$DEBUG" = 1 ]; then
					echo "Charging will stop in $time_to_charge sec"
				fi
				sleep $interval
				
			done
			
			switch_off
			
			current_t=$(timestamp)
			if [ $time_to_charge -gt 0 ]; then
				# 7) charging was stopped by client 
				
				#uci set node.station.state='stopped'
				echo "{\"station_state\":\"stopped\",\"client_state\":\"$client_state\",\"time_to_charge\":\"$time_to_charge\"}" > /tmp/state.json
				
				firebase_res=$(db_call $node_address stopped)
				if [ "$DEBUG" = 1 ]; then
					echo "Charging stopped by client, time to charge: $time_to_charge sec"
				fi
				
			else

				# 8) charging finished

				uci set node.station.state='charged'
				echo "{\"station_state\":\"charged\",\"client_state\":\"$client_state\",\"time_to_charge\":\"0\"}" > /tmp/state.json

				firebase_res=$(db_call $node_address charged)
				if [ "$DEBUG" = 1 ]; then
					echo "Charging finished "
				fi
			fi

		else # wrong node address in contract

			if [ "$protocol" = "fast" ]; then
				echo "Fast protocol: address was wrong"
				switch_off
				# block client here
			fi
		fi	# check contract address

		sleep $interval
		sleep $interval
		#uci set node.station.state='idle'
		#uci node commit

	fi  # if request

	
    if [ "$DEBUG" = 1 ]; then
		echo Wait $interval seconds to check again
	fi
	sleep $interval

done	

	

