import http.client
import json
import sys
import urllib.parse

MY_PASSKEY = '34271334ED1FADA6D8B988B14267E55D'
# MY_PASSKEY = '35271334ED1FADA7D8B988B22222E22D'

paramset_a = {
    'PASSKEY': MY_PASSKEY,
    'stationtype': 'EasyWeatherV1.4.9',
    'dateutc': '2020-11-13+17:10:24',
    'tempinf': 76.8,
    'humidityin': 22,
    'baromrelin': 28.760,
    'baromabsin': 28.760,
    'tempf': 65.8,
    'humidity': 24,
    'winddir': 319,
    'windspeedmph': 0.9,
    'windgustmph': 1.1,
    'rainratein': 0.000,
    'eventrainin': 0.000,
    'dailyrainin': 0.000,
    'weeklyrainin': 0.024,
    'monthlyrainin': 0.028,
    'yearlyrainin': 0.843,
    'solarradiation': 375.53,
    'uv': 3,
    'pm25_ch1': 8.0,
    'pm25_avg_24h_ch1': 5.2,
    'freq': '915M',
    'model': 'HP3500_V1.6.2',
    'leak_ch1': 0,
    'leakbatt1': 5,
}

paramset_b = {
    'PASSKEY': MY_PASSKEY,
    'stationtype': 'EasyWeatherV1.5.4',
    'dateutc': '2020-11-16+15:30:24',
    'tempinf': 68.7,
    'humidityin': 52,
    'baromrelin': 29.785,
    'baromabsin': 29.785,
    'tempf': 46.4,
    'humidity': 94,
    'winddir': 260,
    'winddir_avg10m': 260,
    'windspeedmph': 0.0,
    'windspdmph_avg10m': 0.0,
    'windgustmph': 0.0,
    'maxdailygust': 6.9,
    'rainratein': 0.000,
    'eventrainin': 0.118,
    'hourlyrainin': 0.000,
    'dailyrainin': 0.118,
    'weeklyrainin': 0.118,
    'monthlyrainin': 0.378,
    'yearlyrainin': 6.268,
    'solarradiation': 0.00,
    'uv': 0,
    'soilmoisture1': 0,
    'wh65batt': 0,
    'wh25batt': 0,
    'soilbatt1': 1.5,
    'freq': '868M',
    'model': 'HP1000SE-PRO_Pro_V1.6.0',
}

def usage():
    print("Usage: {0} host port".format(sys.argv[0]))


if __name__ == "__main__":
    if len(sys.argv) < 3:
        usage()
        exit(1)

    host = sys.argv[1]
    port = sys.argv[2]

    print("Connecting to host {0} on port {0}".format(host, port))
    conn = http.client.HTTPConnection(host, port)
    headers = {'Content-type': 'application/x-www-form-urlencoded'}
    params = urllib.parse.urlencode(paramset_a)
    print(params)
    conn.request("POST", "", params, headers)
    response = conn.getresponse()
    print(response.status, response.reason)
    conn.close()
