def main():
    #Check Argv
    if len(sys.argv) != 3:
        print("argv Error")
        exit()

    print("Detector on " + sys.argv[1])
    print("Data log in " + sys.argv[2])

    LogFile = sys.argv[2]

    #Get ang Resolve data
    JsonData = GetJsonData()
    print(JsonData)
    data = json.loads(JsonData)
    temperature = data['Temp']
    humidity = data['Humi']
    light_value = data['light']
    UV_value = data['UV']
    RainFall = data['Rain']

    RainFall = 1023 - RainFall  #Fixed RainFall Value

    #Get Time
    UploadTime = time.strftime("%Y/%m/%d %H:%M:%S")
    current_weather = UploadTime + ' '

    #Start Data Check
    if temperature == "NAN":
        current_weather += 'Temp=Err'
    else:
        current_weather += 'Temp=' + str(temperature) + '*'

    if humidity == "NAN":
        current_weather += ' Humidity=Err'
    else:
        current_weather += ' Humidity=' + str(humidity) + '%'

    if light_value > 1023 or light_value < 0:
        light_value = "Err"

    if UV_value > 1023 or UV_value < 0:
        UV_value = "Err"

    if RainFall > 1023 or RainFall < 0:
        RainFall = "Err"

    current_weather += ' light_value=' + str(light_value) + ' UV=' + str(
        UV_value) + ' Rain=' + str(RainFall)

    print(current_weather)
    #因增加系統穩定性,暫時移除本機Log
    #WriteFile(current_weather+'\n',LogFile)

    #Call Connect to Weather Service DataBase
    database.insert_weather((UploadTime, temperature, float(humidity),
                             float(UV_value), light_value, RainFall, 2))
Beispiel #2
0
def check_newest_weather(id,weather_id):
    data_set = weather_crawler(id, weather_id)
    db_time = get_newest_time_from_db(id)
    print("check_newest_weather: 資料庫最新一筆資料時間:{}".format(db_time))
    if db_time == 0:
        print("空資料庫")
        for i,j in enumerate(data_set):
            print("第{}筆資料{}插入".format(i,j))
            try:
                database.insert_weather(tuple(j))
            except Exception as err:
                print("{}".format(err))
    elif db_time == None:
        print("日期有問題!!!快去檢查")
    else:
        check = 0
        for i, j in enumerate(data_set):
            print("資料庫最新時間;{}".format(db_time))
            print("網路上最新時間:{}".format(j[0]))
            if j[0] > db_time:
                print("資料待更新")
                if j[0] - db_time == timedelta(minutes=15):
                    print("資料符合順序")
                    new_data = data_set[:i]
                    for k,l in enumerate(new_data):
                        print("第{}筆資料插入".format(k))
                        check = 1
                        database.insert_weather(tuple(l))
            else:
                print("已更新")
                check=1
                break
        if(not check):
            print("資料落後過多,增加整頁資料")
            for i, j in enumerate(data_set):
                if j[0] != db_time:
                    print("第{}筆資料插入".format(i+1))
                    database.insert_weather(tuple(j))
Beispiel #3
0
import database

data = ("2000/02/11  8:49:54", 20, 30, 40, 50, 60)
print(data)

database.insert_weather(("2000/02/11 8:49:54", 23, 3, 4, 5, 6))
            #print (response2) #debug only
            if response1 and response2 and response3 and response4 != '':
                break

    except KeyboardInterrupt:
        ser.close()
    return response1, response2, response3, response4


#while True:
light_value, UV_value, humidity, temperature = GetAnalogIndex()
light_value = 1023 - int(light_value, 10)

#if humidity is not None and temperature is not None:
current_weather = time.strftime(
    "%Y/%m/%d %H:%M:%S "
) + 'Temp={0:0.1f}* Humidity={1:0.1f}% light_value={2:0.1f}'.format(
    temperature, humidity, light_value) + ' UV_value=' + UV_value
print(current_weather)
#	print(UV_value)
WriteFile(current_weather + '\n')
upload(temperature, humidity, UV_value, light_value)
#time.sleep(52)

#else:
#	print('Failed to get reading. Try again!')
#	sys.exit(1)

time = "2017/7/12 12:34"
database.insert_weather(time, temperature, humidity, UV_value, light_value)