def heartBeat(self, data): stationID = data.get("stationID", None) if not stationID: raise Exception("[ERR]: stationID required.") station = DB.select("stationSet", where="id=$id", vars={"id": stationID}) if len(station) == 0: raise Exception("[ERR]: station not exists for id %s" % stationID) deviceIP = web.ctx.ip checkinDev = DB.select("checkinDev", where="deviceIP=$deviceIP", vars={"deviceIP": deviceIP}) current_time = datetime.datetime.now() if len(checkinDev) == 0: DB.insert("checkinDev", stationID=stationID, deviceIP=deviceIP, lastDateTime=current_time) else: DB.update("checkinDev", where="deviceIP=$deviceIP", vars={"deviceIP": deviceIP}, stationID=stationID, lastDateTime=current_time) Date = current_time.date().strftime("%Y%m%d") time = current_time.time().strftime("%H%M%S") result = {"Date": Date, "time": time} return result
def edit(self, data): """Interface to edit the information of a media box. Args: data: JSON format data. For example: { "token": " safe action", "id": "12", "speed": "50", "volume": "50", "pitch" : "50" } If the data does not have `speed`, `pitch` or `volume` field, raise an Exception. Returns: Apply the settings to the media box and update the database for input data. If success, returns JSON format data, otherwise raise an Exception. """ id = data.get("id", None) if id is None: raise Exception("[ERR]: id required.") mediabox = DB.select("publish", what="deviceIP", where="id=$id", vars={"id": id}) if len(mediabox) == 0: raise Exception("[ERR]: mediabox not exists.") ip = mediabox[0].deviceIP speed = data.get("speed", 50) pitch = data.get("pitch", 50) volume = data.get("volume", 100) # if not speed or not pitch or not volume: # raise Exception("[ERR]: speed, pitch, volume required.") url = "%s/setting/%s/%s/%s" % (str(ip), str(volume), str(pitch), str(speed)) r = requests.get(url) if r.status_code == 200: current_time = datetime.datetime.now() DB.update("publish", where="id=$id", vars={"id": id}, speed=speed, volume=volume, pitch=pitch, lastDateTime=current_time) result = {"result": "success"} else: result = {"result": "failed"} return result
def heartBeat(self, data): """Interface to handle heart beat request. It will set status for a media box. We can use the status to check a media box is online or offline. """ stationId = data.get("stationId", None) if stationId is None: raise Exception("[Err]: stationId required.") stationSet = DB.select("stationSet", where="id=$id", vars={"id": stationId}) if len(stationSet) == 0: raise Exception("[ERR]: statitonSet not exists.") deviceIP = data.get("serverAddress", None) if deviceIP is None: raise Exception("[Err]: serverAddress required.") isHttpServerAlive = data.get("isHttpServerAlive", None) isSpeechManagerInit = data.get("isSpeechManagerInit", None) if not isHttpServerAlive or not isSpeechManagerInit: raise Exception("[Err]: mediaBox is not alive.") speakerParams = data.get("speakerParams", None) if speakerParams is None: speakerParams = {"speed": "50", "pitch": "50", "volume": "100"} speed = speakerParams["speed"] pitch = speakerParams["pitch"] volume = speakerParams["volume"] speechList = data.get("speechList", None) if not speechList or len(speechList) == 0: speechState = "free" else: speechState = "busy" current_time = datetime.datetime.now() # TODO:下次版本更新时删除这两个字段 voiceFormate = cfg.voiceFormateDefault displayFormate = cfg.displayFormateDefault mediaBox = DB.where("publish", deviceIP=deviceIP) if len(mediaBox) == 0: DB.insert("publish", stationId=stationId, deviceIP=deviceIP, speed=speed, pitch=pitch, volume=volume, speechState=speechState, lastDateTime=current_time, voiceFormate=voiceFormate, displayFormate=displayFormate) else: DB.update("publish", where="deviceIP=$deviceIP", vars={"deviceIP": deviceIP}, stationId=stationId, speechState=speechState, lastDateTime=current_time, voiceFormate=voiceFormate, displayFormate=displayFormate) Date = current_time.date().strftime("%Y%m%d") time = current_time.time().strftime("%H%M%S") result = {"Date": Date, "time": time} return result