#!/bin/bash
#
# Integration loopback test using villas node.
#
# @author Steffen Vogel <svogel2@eonerc.rwth-aachen.de>
# @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
# @license GNU General Public License (version 3)
#
# VILLASnode
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
##################################################################################

set -e

DIR=$(mktemp -d)
pushd ${DIR}

function finish {
	popd
	rm -rf ${DIR}
}
trap finish EXIT

cat > expect_all.dat <<EOF
1599745986.343431058+1.451000e-06(7)	4.000000	20.000000	100.000000
1599745986.843081878+8.820000e-07(16)	9.000000	50.000000	200.000000
1599745987.343081848+9.440000e-07(24)	14.000000	70.000000	300.000000
1599745987.843081745+8.630000e-07(33)	19.000000	100.000000	400.000000
EOF

cat > expect_any.dat <<EOF
1637846509.292258908(0) 0.00000000000000000     nan     nan
1637846509.392250279(1) 1.00000000000000000     nan     nan
1637846509.392340108(2) 1.00000000000000000     10.00000000000000000    nan
1637846509.492248409(3) 2.00000000000000000     10.00000000000000000    nan
1637846509.592247486(4) 3.00000000000000000     10.00000000000000000    nan
1637846509.592325514(5) 3.00000000000000000     20.00000000000000000    nan
1637846509.692247962(6) 4.00000000000000000     20.00000000000000000    nan
1637846509.692366850(7) 4.00000000000000000     20.00000000000000000    100.00000000000000000
1637846509.792249190(8) 5.00000000000000000     20.00000000000000000    100.00000000000000000
1637846509.792325438(9) 5.00000000000000000     30.00000000000000000    100.00000000000000000
1637846509.892248488(10)        6.00000000000000000     30.00000000000000000    100.00000000000000000
1637846509.992245288(11)        7.00000000000000000     30.00000000000000000    100.00000000000000000
1637846509.992326612(12)        7.00000000000000000     40.00000000000000000    100.00000000000000000
1637846510.092248626(13)        8.00000000000000000     40.00000000000000000    100.00000000000000000
1637846510.192246680(14)        9.00000000000000000     40.00000000000000000    100.00000000000000000
1637846510.192325483(15)        9.00000000000000000     50.00000000000000000    100.00000000000000000
1637846510.192357620(16)        9.00000000000000000     50.00000000000000000    200.00000000000000000
1637846510.292239659(17)        10.00000000000000000    50.00000000000000000    200.00000000000000000
1637846510.392247156(18)        11.00000000000000000    50.00000000000000000    200.00000000000000000
1637846510.392325530(19)        11.00000000000000000    60.00000000000000000    200.00000000000000000
1637846510.492248814(20)        12.00000000000000000    60.00000000000000000    200.00000000000000000
1637846510.592247753(21)        13.00000000000000000    60.00000000000000000    200.00000000000000000
1637846510.592325526(22)        13.00000000000000000    70.00000000000000000    200.00000000000000000
1637846510.692242575(23)        14.00000000000000000    70.00000000000000000    200.00000000000000000
1637846510.692357658(24)        14.00000000000000000    70.00000000000000000    300.00000000000000000
1637846510.792248421(25)        15.00000000000000000    70.00000000000000000    300.00000000000000000
1637846510.792325568(26)        15.00000000000000000    80.00000000000000000    300.00000000000000000
1637846510.892249615(27)        16.00000000000000000    80.00000000000000000    300.00000000000000000
1637846510.992238325(28)        17.00000000000000000    80.00000000000000000    300.00000000000000000
1637846510.992325360(29)        17.00000000000000000    90.00000000000000000    300.00000000000000000
1637846511.092247974(30)        18.00000000000000000    90.00000000000000000    300.00000000000000000
1637846511.192247896(31)        19.00000000000000000    90.00000000000000000    300.00000000000000000
1637846511.192325488(32)        19.00000000000000000    100.00000000000000000   300.00000000000000000
1637846511.192357421(33)        19.00000000000000000    100.00000000000000000   400.00000000000000000
EOF

for MODE in all any; do

cat > config.json <<EOF
{
	"nodes": {
		"sig_1": {
			"type": "signal",

			"signal": "counter",
			"values": 1,
			"offset": 0.0,
			"rate": 10.0,
			"limit": 20
		},
		"sig_2": {
			"type": "signal",

			"signal": "counter",
			"values": 1,
			"offset": 10.0,
			"amplitude": 10.0,
			"rate": 5.0,
			"limit": 10
		},
		"sig_3": {
			"type": "signal",

			"signal": "counter",
			"values": 1,
			"offset": 100.0,
			"amplitude": 100.0,
			"rate": 2.0,
			"limit": 10
		},
		"file_1": {
			"type": "file",
			"uri": "output.dat"
		}
	},
	"paths": [
		{
			"in": [
				"sig_1.data[counter]",
				"sig_2.data[counter]",
				"sig_3.data[counter]"
			],
			"out": "file_1",
			"mode": "${MODE}"
		}
	]
}
EOF

villas node config.json

villas compare output.dat expect_${MODE}.dat

done