def to_json(self): """ Return a parsed dictionary that is JSON compatible. """ return { 'user': self.user, 'code': self.code, 'created': udatetime.to_string(self.created), 'expires_at': udatetime.to_string(self.expires_at()), 'expires': self.expires }
def test_raise_on_not_TZFixedOffset(self): class TZInvalid(tzinfo): def utcoffset(self, dt=None): return timedelta(seconds=0) def dst(self, dt=None): return timedelta(seconds=0) dt = datetime.now(TZInvalid()) with self.assertRaises(ValueError): udatetime.to_string(dt)
def test_from_and_to_string(self): rfc3339 = '2016-07-15T12:33:20.123000+01:30' dt = udatetime.from_string(rfc3339) self.assertIsInstance(dt, datetime) self.assertEqual(dt.year, 2016) self.assertEqual(dt.month, 7) self.assertEqual(dt.day, 15) self.assertEqual(dt.hour, 12) self.assertEqual(dt.minute, 33) self.assertEqual(dt.second, 20) self.assertEqual(dt.microsecond, 123000) self.assertEqual(udatetime.to_string(dt), rfc3339) rfc3339 = '2016-07-18T12:58:26.485897-02:00' dt = udatetime.from_string(rfc3339) self.assertEqual(udatetime.to_string(dt), rfc3339)
def set_value(self, model, value: 'datetime.datetime'): if isinstance(value, datetime.datetime): if value.utcoffset() is None: offset = 0 else: offset = value.utcoffset().seconds // 60 value = udatetime.to_string( value.astimezone(TZFixedOffset(offset)) + datetime.timedelta(hours=2)) super().set_value(model, value)
def default(self, o): if hasattr(o, '__json__'): return o.__json__() if isinstance(o, datetime.datetime): return udatetime.to_string(o) if isinstance(o, (datetime.date, datetime.time)): return o.isoformat() if isinstance(o, integer_types): return int(o) if isinstance(o, decimal.Decimal): return str(o) return _json.JSONEncoder.default(self, o)
def to_redis(self): output = { 'user': self.user, 'code': self.code, 'created': udatetime.to_string(self.created) } if self.used: output['used'] = '1' else: output['used'] = '0' return output
def _custom_json(o): if hasattr(o, 'custom_json') and callable(o.custom_json): return o.custom_json() if isinstance(o, datetime.datetime): return udatetime.to_string(o) elif isinstance(o, (datetime.date, datetime.time)): return o.isoformat() elif isinstance(o, integer_types): return int(o) elif isinstance(o, decimal.Decimal): return str(o) elif isinstance(o, TElement): return str(o) elif isinstance(o, TAG): return str(o) elif hasattr(o, 'as_list') and callable(o.as_list): return o.as_list() elif hasattr(o, 'as_dict') and callable(o.as_dict): return o.as_dict() else: raise TypeError(repr(o) + " is not JSON serializable")
def get_task_status(tid): rconn = get_redis_conn() rtkey = DOWNLOADER_TASK_PREFIX + tid try: tdata = rconn.hgetall(rtkey) if len(tdata) == 0: return None tdata["query"] = json.loads(tdata["query"]) tdata["status_url"] = url_for(".status", tid=tid, _external=True) ttl = rconn.ttl(rtkey) if ttl != -1: ttl = timedelta(seconds=ttl) tdata["expires"] = udatetime.to_string(udatetime.utcnow() + ttl) if tdata.get('task_status'): # `task_status` isn't set in redis while pending, so if # set then it is complete tdata["complete"] = True else: ar = downloader.AsyncResult(tid) tdata['task_status'] = ar.status tdata["complete"] = ar.ready() if ar.ready(): rconn.hset(rtkey, "task_status", ar.status) if ar.successful(): tdata["download_url"] = ar.result rconn.hset(rtkey, "download_url", ar.result) elif ar.failed(): tdata["error"] = str(ar.result) rconn.hset(rtkey, "error", str(ar.result)) gevent.spawn(dissociate_query_hash, tid, tdata) except Exception as e: logger.exception("Failed getting status of download %s", tid) tdata = { "complete": False, "task_status": "UNKNOWN", "status_url": url_for(".status", tid=tid, _external=True), "error": str(e) } return tdata
def to_redis(self): """ Representation of this User as a dictionary, ready for insertion into Redis. Does a special schema validation to ensure the data is sound. """ output = { 'email': self.email, 'username': self.username, 'joined': udatetime.to_string(self.joined), 'enabled': int(self.enabled), 'id': self.id } output['password'] = self.password output['admin'] = int(self.admin) output['activated'] = int(self.activated) # double check if the data is legit redis_schema.check(output) return output
def udatetime_format(): udatetime.to_string(DATETIME_OBJ)
def rfc3339strFromTimestamp(ts): return udatetime.to_string(udatetime.utcfromtimestamp(ts))
def to_redis(self): return { 'user': self.user, 'code': self.code, 'created': udatetime.to_string(self.created) }
def noise_thread(config): # Memory backlog backlog_record = [] # File backlog file = open("NOI_{}.csv".format(config['room_id']), "a+", newline='', encoding="utf-8") writer = csv.writer(file, dialect='excel', lineterminator='\n') if (file.tell() == 0): writer.writerow(["TIME", "NOISE"]) file.flush() #file.close() # header for API requests headers = {'Content-Type': 'application/json'} # Check if token is available if config['token'] == '': config['token'] = userLogin(config['user'], config['pwd']) headers['token'] = config['token'] NOI_URL = URL + 'sleep/send_room_data' # Init sensor #create the spi bus spi_n = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI) #create the chip select cs_n = digitalio.DigitalInOut( board.D5) # non sappiamo a quale board collegarlo cs_n.direction = digitalio.Direction.OUTPUT cs_n.value = True #create mcp object mcp_n = MCP.MCP3008(spi_n, cs_n) #create an analog input channel #chan_n = AnalogIn(mcp_n, MCP.P0)#non sapppiamo a che pin collegato sensore_n = SPW2430(mcp_n) while (True): # Wait 30 seconds #sleep(30) noise = 0 counter = 0 tStart = perf_counter() while (perf_counter() < (tStart + 30)): noise = noise + sensore_n.read_noise() counter = counter + 1 noise = noise / counter # Assemble data timestamp = udatetime.to_string(udatetime.now()) # saving data inside the csv file #file = open("noise.csv","a", encoding = "utf-8") #writer = csv.writer(file) writer.writerow([timestamp, noise]) file.flush() #file.close() #file = open("noise.csv","r+", encoding = "utf-8") #file.read() data = {} # Check if token is available if config['token'] == '': config['token'] = userLogin(config['user'], config['pwd']) data['input'] = { 'room_id': config['room_id'], 'timestamp': timestamp, 'noise': json.dumps(noise) } data['token'] = config['token'] # Send data to server try: print("Sending NOI {0:4.5} at time {1:}".format(noise, timestamp)) req = requests.post(url=NOI_URL, headers=headers, json=data) # Check request result if req.status_code != 200: print("Req status {}:saving NOISE record on backlog".format( req.status_code)) backlog_record.append(data) else: while len(backlog_record) > 0 and requests.post( url=NOI_URL, headers=headers, json=backlog_record[0]).status_code == 200: del backlog_record[0] except requests.exceptions.ConnectionError: print("Connection refused: saving NOISE record on backlog") backlog_record.append(data) except KeyboardInterrupt: file.close() break
def carbon_thread(config, co2_optimal): # Memory backlog backlog_record = [] # File backlog file = open("CARB_{}.csv".format(config['room_id']), "a+", newline='', encoding="utf-8") writer = csv.writer(file, dialect='excel', lineterminator='\n') if (file.tell() == 0): writer.writerow(["TIME", "CO2"]) file.flush() #file.close() # header for API requests headers = {'Content-Type': 'application/json'} # Check if token is available if config['token'] == '': config['token'] = userLogin(config['user'], config['pwd']) headers['token'] = config['token'] CO_URL = URL + 'sleep/send_room_data' # Setup GPIO switch for sensor reset GPIO.setmode(GPIO.BCM) GPIO.setup(20, GPIO.IN) #with SMBusWrapper(1) as bus: i2c_bus = busio.I2C(board.SCL, board.SDA, frequency=100000) sgp = sgp30(i2c_bus) #Sgp30(bus, baseline_filename = "sgpbaseline.txt") if (GPIO.input(20) == 1): sgp_calibrate(sgp) else: with open("sgpbaseline.txt", "r") as base: baseline = json.load(base) base.close() print("get baseline from file") sgp.set_iaq_baseline(baseline[0], baseline[1]) #sgp.read_measurements() sgp.iaq_measure() # Warm up the sensor for _ in range(20): sleep(1) #sgp.read_measurements() sgp.iaq_measure() IFTTT_EVENT = '{HighCarbon}' sys_carbon_active = False while (True): # Wait 1 second to keep sensor active for _ in range(30): sleep(1) # Read data from sensors co2, _ = sgp.iaq_measure() #read_measurements() #co2 = getattr(co2, "data")[0] # Assemble data timestamp = udatetime.to_string(udatetime.now()) # Recalibrate sensor if necessary tnow = datetime.now() tset = [ datetime(tnow.year, tnow.month, tnow.day, hour=11, minute=58), datetime(tnow.year, tnow.month, tnow.day, hour=12, minute=2) ] if ((tnow > tset[0]) & (tnow > tset[1])): sgp_calibrate(sgp) #File csv #file = open("CO2.csv","a", encoding = "utf-8") #writer = csv.writer(file) writer.writerow([timestamp, co2]) file.flush() #file.close() #file = open("CO2.csv","r+", encoding = "utf-8") #file.read() data = {} # Check if token is available if config['token'] == '': config['token'] = userLogin(config['user'], config['pwd']) data['token'] = config['token'] data['input'] = { 'room_id': config['room_id'], 'timestamp': timestamp, 'co2': json.dumps(co2) } if sys_carbon_active == False: if co2 > co2_optimal + 1500: # Throw high co2 -> ventilate the room ifttt_req = requests.post(IFTTT_URL + IFTTT_EVENT + IFTTT_KEY) sendMessageToServer("HC") sys_carbon_active = True else: if co2 < co2_optimal: # Back to normal -> shutdown carbon control sys_carbon_active = False ifttt_req = requests.post(IFTTT_URL + "{ShutCarbon}" + IFTTT_KEY) sendMessageToServer("CO") ### IFTTT REQUEST # iftttreq = requests.post(url=IFTTT_URL + IFTTT_EVENT + IFTTT_KEY, data={'CO2': json.dumps(co2)}) # print("****IFTTT REQUEST STATUS: " + iftttreq.status_code + "\n CO2: " + co2 + "****") # Send data to server try: print("Sending CO2 {0:} at time {1:}".format(co2, timestamp)) req = requests.post(url=CO_URL, headers=headers, json=data) # Check request result if req.status_code != 200: print("Req status {}:saving CO2 record on backlog".format( req.status_code)) backlog_record.append(data) else: while len(backlog_record) > 0 and requests.post( url=CO_URL, headers=headers, json=backlog_record[0]).status_code == 200: del backlog_record[0] except requests.exceptions.ConnectionError: print("Connection refused: saving CO2 record on backlog") backlog_record.append(data) except KeyboardInterrupt: file.close() break
def TH_thread(config, temp_optimal, hum_optimal): thsensor = si7021.si7021(1) # Memory backlog backlog_record = [] # File backlog file = open("TH_{}.csv".format(config['room_id']), "a+", newline='', encoding="utf-8") writer = csv.writer(file, dialect='excel', lineterminator='\n') if (file.tell() == 0): writer.writerow(["TIME", "TEMPERATURE", "HUMIDITY"]) file.flush() #file.close() # header for API requests headers = {'Content-Type': 'application/json'} # Check if token is available if config['token'] == '': config['token'] = userLogin(config['user'], config['pwd']) headers['token'] = config['token'] TH_URL = URL + 'sleep/send_room_data' sys_temp_active = False sys_hum_active = False while (True): # Wait 30 seconds sleep(30) # Read data from sensors temperature = thsensor.read_temperature() humidity = thsensor.read_humidity() if sys_temp_active == False: if temperature < temp_optimal - 2.5: # Throw Low Temp -> heat up the room ifttt_req = requests.post(IFTTT_URL + "{LowTemp}" + IFTTT_KEY) sendMessageToServer("LT") turnLed(19, True) sys_temp_active = True if temperature > temp_optimal + 2.5: # Throw High Temp -> cool down the room ifttt_req = requests.post(IFTTT_URL + "{HighTemp}" + IFTTT_KEY) sendMessageToServer("HT") turnLed(19, True) sys_temp_active = True else: if temp_optimal - 1.5 < temperature < temp_optimal + 1.5: # Back to normal -> Shut down temp activity ifttt_req = requests.post(IFTTT_URL + "{ShutTemp}" + IFTTT_KEY) sendMessageToServer("TO") turnLed(19, False) sys_temp_active = False if sys_hum_active == False: if humidity < hum_optimal - 20: # Throw Low Humidity -> humidify the room ifttt_req = requests.post(IFTTT_URL + "{LowHumidity}" + IFTTT_KEY) sendMessageToServer("LH") turnLed(26, True) GPIO.output(26, GPIO.HIGH) sys_hum_active = True if humidity > hum_optimal + 20: # Throw High Humidity -> dehumidify the room ifttt_req = requests.post(IFTTT_URL + "{HighHumidity}" + IFTTT_KEY) sendMessageToServer("HH") turnLed(26, True) GPIO.output(26, GPIO.HIGH) sys_hum_active = False else: if hum_optimal - 20 < humidity < hum_optimal + 20: # Back to normal -> shut down humidity activity ifttt_req = requests.post(IFTTT_URL + "{ShutHumidity}" + IFTTT_KEY) sendMessageToServer("HO") turnLed(26, False) GPIO.output(26, GPIO.LOW) sys_hum_active = False ### IFTTT request # iftttreq = requests.post(url=IFTTT_URL + IFTTT_EVENT + IFTTT_KEY, data={'temperature': temperature, 'humidity': humidity}) # print("****IFTTT REQUEST STATUS: " + iftttreq.status_code + " \n Temperature = " + temperature + " \n Humidity = " + humidity + "****") # Assemble data timestamp = udatetime.to_string(udatetime.now()) data = {} # Check if token is available if config['token'] == '': config['token'] = userLogin(config['user'], config['pwd']) data['token'] = config['token'] data['input'] = { 'room_id': config['room_id'], 'timestamp': timestamp, 'temperature': json.dumps(temperature), 'humidity': json.dumps(humidity) } # Il file csv per ora lo lasciamo fuori quindi viene fatto indipendentemente dalla connessione o meno #file = open("TH.csv","a", encoding = "utf-8") #writer = csv.writer(file) writer.writerow([timestamp, temperature, humidity]) file.flush() #file.close() #file = open("TH.csv","r+", encoding = "utf-8") #file.read() # Send data to server try: print("Sending T {0:2.5} H {1:2.5} at time {2:}".format( temperature, humidity, timestamp)) req = requests.post(url=TH_URL, headers=headers, json=data) # Check request result if req.status_code != 200: print("Req status {}:saving TH record on backlog".format( req.status_code)) backlog_record.append(data) else: while len(backlog_record) > 0 and requests.post( url=TH_URL, headers=headers, json=backlog_record[0]).status_code == 200: del backlog_record[0] except requests.exceptions.ConnectionError: print("Connection refused: saving TH record on backlog") backlog_record.append(data) except KeyboardInterrupt: file.close() break
def sendMessageToServer(message): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((REMOTE_HOST, PORT)) print("Connected") s.sendall(message.encode('ascii')) print("Message " + message + " sent") s.settimeout(2) serverMessage = s.recv(1024).decode("utf-8") s.close() return serverMessage if __name__ == "__main__": timestamp = udatetime.to_string(udatetime.now()) ### INITIALISE CSV file = open("LOG_" + timestamp + ".csv", "a+") writer = csv.writer(file, dialect='excel', lineterminator='\n') if file.tell() == 0: writer.writerow(["TIME", "TEMP", "HUMIDITY", "CO2"]) file.flush() # header for API requests headers = {'Content-Type': 'application/json'} ### INITIALISE CONFIG TOKEN with open("config.json", "r") as configFile: config = json.load(configFile)
def _udatetime(): return udatetime.to_string(uda)
def light_thread(config): # Memory backlog backlog_record = [] # File backlog file = open("LHT_{}.csv".format(config['room_id']), "a+", newline='', encoding="utf-8") writer = csv.writer(file, dialect='excel', lineterminator='\n') if (file.tell() == 0): writer.writerow(["TIME", "LIGHT"]) file.flush() #file.close() IFTTT_EVENT = '{getLight}' # header for API requests headers = {'Content-Type': 'application/json'} # Check if token is available if config['token'] == '': config['token'] = userLogin(config['user'], config['pwd']) headers['token'] = config['token'] LH_URL = URL + 'sleep/send_room_data' # create the spi bus spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI) #create the chip select cs = digitalio.DigitalInOut(board.D5) mcp = MCP.MCP3008(spi, cs) #create an analog input channel on pin 0 #chan = AnalogIn(mcp, MCP.P1) sensore = GA1A12S202(mcp) while (True): # Come misura della luce prenderei la stessa che aveva usato nel vecchio main che una media di quelle # ottenute in 30 secondi (fa 60 prove con uno sleep di 0.5) light = 0.0 for _ in range(60): light = light + sensore.read_light() sleep(0.5) light = light / 60.0 ### IFTTT request iftttreq = requests.post(url=IFTTT_URL + IFTTT_EVENT + IFTTT_KEY, data={'value1': light}) # print("****IFTTT REQUEST STATUS: " + iftttreq.status_code + "\n Light = " + light + "****") # Assemble data timestamp = udatetime.to_string(udatetime.now()) # Csv file #file = open("light.csv","a", encoding = "utf-8") #writer = csv.writer(file) writer.writerow([timestamp, light]) file.flush() #file.close() #file = open("light.csv","r+", encoding = "utf-8") #file.read() data = {} # Check if token is available if config['token'] == '': config['token'] = userLogin(config['user'], config['pwd']) data['token'] = config['token'] data['input'] = { 'room_id': config['room_id'], 'timestamp': timestamp, 'light': json.dumps(light) } # Send data to server try: print("Sending LHT {0:4.5} at time {1:}".format(light, timestamp)) req = requests.post(url=LH_URL, headers=headers, json=data) # Check request result if req.status_code != 200: print("Req status {}:saving LIGHT record on backlog".format( req.status_code)) backlog_record.append(data) else: while len(backlog_record) > 0 and requests.post( url=LH_URL, headers=headers, json=backlog_record[0]).status_code == 200: del backlog_record[0] except requests.exceptions.ConnectionError: print("Connection refused: saving LIGHT record on backlog") backlog_record.append(data) except KeyboardInterrupt: file.close() break