#!/bin/sh

#months=(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)

while read line; do
	if [ "${line:0:10}" == "OKX GB2 19" ]; then

		echo -en "$line" | tr -d '\015'
		echo -en "\t\t\t\t";

		echo -en $( date )
		#date
		echo -en "\t"
		#echo -en "${line}"
		#echo "${line}"
		#echo -en "\t"
		#printf "%s\t%s\t" "$d" "$line"

		# weather
		# 0000000000111111111122222222
		# 0123456789012345678901234567
		# OKX GB2 195EF01E61000207A6D4

	        # 10 msg type
	        # 11 12 station ID
	        # 13 14 15 temperature tents of C
	        # 16 17 humidity
	        # 18 19 windspeed
	        # 20 21 windspeed gust
	        # 22 23 24 25 cummulative rain
	        # 26 27 CRC

		llen=$(( ${#line} ))

		if [ $llen -eq 21 ]; then

			# ele status
			# 00000000001111111111
			# 01234567890123456789
			# OKX GB2 1940170289B8

			#    11 12 13 solar panel voltage
			#    14 15 16 solar panel idle voltage
			#    17 18 19 battery voltage

			if [ ${line:10:1} == '4' ]; then
				solar=$( printf "3.24 * 3.327 * %d / 4096\n" 0x0${line:11:3} | bc -l )
				solid=$( printf "3.24 * 3.327 * %d / 4096\n" 0x0${line:14:3} | bc -l )
				batt=$( printf "3.24 * 2.0113 * %d / 4096\n" 0x0${line:17:3} | bc -l )
				#printf "Sol %.2fV Idle %.2fV Bat %.2fV\n" $solar $solid $batt
				printf "Sol %.3f Idle %.3f Bat %.3f\n" $solar $solid $batt
			fi
		else
			if [ $llen -eq 13 -o $llen -eq 29 ]; then

			        buf=${line:10}
				buflen=$(( 2 * ( ${#buf} / 2 ) ))

				i=0
				k=0
				crc=0

		        	while [ $k -lt $buflen ]; do
			                byte=$(( 0x${buf:$k:2} ))
        			        k=$(( $k + 2 ))
	                		for i in $( seq 8 -1 1 ); do
        	                		mix=$(( 0xFF & ( ( $crc ^ $byte ) & 0x80 ) ))
		                        	crc=$(( 0xFF & ( $crc << 1 ) ))
	        		                [ $mix -gt 0 ] && crc=$(( 0xFF & ( $crc ^ 0x31 ) ))
		        	                byte=$(( 0xFF & ( $byte << 1 ) ))
        		        	done
		        	done

				if [ $crc -eq 0 ]; then

					if [ ${line:10:1} == '5' ]; then
						temp=$( printf "%d\n" 0x0${line:13:3} | bc -l)
						[ ${temp} -gt 2048 ] && temp=$(( - ${temp} + 2048 ))
						temp=$( printf "0.1 * %d\n" ${temp} | bc -l)
						humid=$( printf %d 0x${line:16:2} )
						wind=$( printf "0.34 * %d\n" 0x${line:18:2} | bc -l )
						gust=$( printf "0.34 * %d\n" 0x${line:20:2} | bc -l )
						rain=$( printf "0.3 * %d\n" 0x${line:22:4} | bc -l )
						#printf "Temp %.2f rHum %d%% Wvel %.2fm/s Wmax %.2fm/s Rain %.1fmm\n" $temp $humid $wind $gust $rain
						printf "Temp %.1f rHum %d Wvel %.2f Wmax %.2f Rain %.1f\n" $temp $humid $wind $gust $rain
					else

						# time/date
						#0000000000111111111122222222
						#0123456789012345678901234567
						#OKX GB2 1669EA9103561388175E
						#   10 11 ??
						#   14 15 hour & 0x3F
						#   16 17 min
						#   18 19 sec
						#   20 21 year
						#   22 23 month & 0x1F
						#   24 25 day of week

						# time/date
						#0000000000111111111122222222
						#0123456789012345678901234567
						#OKX GB2 196EFA50031814D029CE    Wed Oct 29 10:03:19 CE T 2014    29-Oct-2014 10:03:18
						#OKX GB2 196EFA50040614D029B8    Wed Oct 29 10:04:07 CE T 2014    29-Oct-2014 10:04:06
						#OKX GB2 196EFA50045414D02967    Wed Oct 29 10:04:55 CE T 2014    29-Oct-2014 10:04:54
						#OKX GB2 196EFA50054214D02985    Wed Oct 29 10:05:43 CE T 2014    29-Oct-2014 10:05:42
						#OKX GB2 196EFA50063014D0296E    Wed Oct 29 10:06:31 CE T 2014    29-Oct-2014 10:06:30
						#OKX GB2 196EFA480323141026DD    Sun Oct 26 08:03:23 CE T 2014    26-Oct-2014 08:03:23
						#OKX GB2 196EFA50063414D0264E    Sun Oct 26 10:28:26 CE T 2014    26-Oct-2014 10:06:34
						#OKX GB2 196EFA920439140907D4    Sun Sep  7 12:04:40 CEST 2014    07-Sep-2014 12:04:39
						#              ^
						#              4 - CET
						#              8 - CEST

						if [ ${line:10:1} == '6' ]; then
							dst="CEST"
							hour=$( printf %d 0x0${line:14:1} )
							[ ${hour} -ge 8 ] && hour=$(( $hour - 8 ))
							[ ${hour} -ge 4 ] && hour=$(( $hour - 4 )) && dst="CET"
							hour=$(( ${hour} * 10 ))
							hour=$(( ${hour} + ${line:15:1} ))

							min=$(( 10 * ${line:16:1} + ${line:17:1} ))
							sec=$(( 10 * ${line:18:1} + ${line:19:1} ))
							year=$( printf "2000 + ${line:20:2}\n" | bc )
							month=$( printf %d 0x0${line:22:1} )
							[ ${month} -ge 8 ] && month=$(( $month - 8 ))
							[ ${month} -ge 4 ] && month=$(( $month - 4 ))
							[ ${month} -ge 2 ] && month=$(( $month - 2 ))
							month=$(( ${month} * 10 ))
							month=$(( ${month} + ${line:23:1} ))

							case ${month} in
								1) month="Jan";;
								2) month="Feb";;
								3) month="Mar";;
								4) month="Apr";;
								5) month="May";;
								6) month="Jun";;
								7) month="Jul";;
								8) month="Aug";;
								9) month="Sep";;
								10) month="Oct";;
								11) month="Nov";;
								12) month="Dec";;
							esac

							day=$(( 10 * ${line:24:1} + ${line:25:1} ))
#							printf "%02d-%02d-%4d %02d:%02d:%02d - crc: 0x%02x\n" $day $month $year $hour $min $sec $crc
#							printf "%02d-%02d-%4d %02d:%02d:%02d\n" $day $month $year $hour $min $sec
							printf "%02d-%3s-%4d %s %02d:%02d:%02d\n" $day $month $year $dst $hour $min $sec
						fi
					fi
#				else
#					echo "crc: $crc"
				fi
			else
				echo "wrong length ($llen)"
			fi
		fi
#	else
#		echo "NOT OK"
	fi
done < $1
