def read(self): t = int(time.time()) lt = time.localtime(t) if t == IOStream.timestamp: print("IOData.read() was broken, cause the time has been used.") return # print(lt.tm_sec, IOData.timestamp, t) IOStream.timestamp = t master = modbus.ModbusMaster.instance() values = master.read() if values: log.Log.instance().d("modbus" + str(values)) else: # no value read from modbus log.Log.instance().d("modbus: None") self.write("Read modbus: None") return d = Data.instance() if values: d.timestamp = t d.pressure = DataDefines.round('pressure', values[0]) d.water_level = DataDefines.round('water_level', values[1]) d.ac_flow = values[2] d.in_flow = DataDefines.round('in_flow', values[3]) d.v1 = values[4] d.v2 = values[5] d.v3 = values[6] d.c1 = DataDefines.round('c1', values[7]) d.c2 = DataDefines.round('c2', values[8]) d.c3 = DataDefines.round('c3', values[9]) d.power_con = values[10] d.reactive_power = values[11] d.power_factor = DataDefines.round('power_factor', values[12]) d.frequency = values[13] d.energy = DataDefines.round('energy', values[14]) d.set_on_off(t, values[15]) if d.high_frequency_record( ): # high speed write into database every second if not self._hfr: self._hfr = True self._hfr_time = int(time.time()) self._hfr_count = 0 print("Start high frequency record...") self.write("---------------------------------") self.write("-> Start high frequency record...") self.write("---------------------------------") self._hfr_count += 1 tornado.ioloop.IOLoop.instance().add_callback( self.write_into_db, d) else: if self._hfr: self._hfr = False print("End high frequency record...", int(time.time()) - self._hfr_time, self._hfr_count) self.write("---------------------------------") self.write("-> End high frequency record...", int(time.time()) - self._hfr_time, self._hfr_count) self.write("---------------------------------") if lt.tm_sec == 0: # low speed write into database every minute tornado.ioloop.IOLoop.instance().add_callback( self.write_into_db, d)
def post(self): json_object = Data.instance().to_json() json_object["is_high_frequency"] = Data.instance().high_frequency_record() json_object["high_frequency_interval"] = model.data.HIGH_FREQUENCY_INTERVAL json_string = json.dumps(json_object) self.write(json_string)