def getPiDevicesByDeviceId(request, deviceId): piDeviceList = dao.getPiDeviceByDeviceId(deviceId) piDeviceObjList = [] for piDevice in piDeviceList: piDeviceObj = piDevice._convertToDict() piDeviceObjList.append(piDeviceObj) return ResponseProcessor.processSuccessResponse(piDeviceObjList)
def gpio_overview(request): mode = pi.getMode() piType = pi.getType() pinList = pi.pins pinListData = [] for pin in pinList: pinStatus = '' pinBCM = pin.getBCM() pinBoard = pin.getBOARD() pinName = pin.getName() pinMode = pin.getMode() if (pinMode == 'IN'): pinStatus = pin.read() elif (pinMode == 'OUT'): pinStatus = pin.getValue() else: pinStatus = '' pinData = {} pinData['pinStatus'] = pinStatus pinData['BCM'] = pinBCM pinData['Physical'] = pinBoard pinData['Name'] = pinName pinData['mode'] = pinMode pinListData.append(pinData) piData = {} piData["mode"] = mode piData["type"] = piType piData["pinList"] = pinListData return ResponseProcessor.processSuccessResponse(piData)
def create_scheduler(request): scheduleName = request.GET.get('scheduleName') scheduleMode = Constants.SCHEDULE_MODE[request.GET.get('scheduleMode')] scheduleValue = int(request.GET.get('scheduleValue')) scheduleJobName = request.GET.get('scheduleJobName') createdScheduler = dao.createScheduler(scheduleName, scheduleMode, scheduleValue, scheduleJobName) return ResponseProcessor.processSuccessResponse( createdScheduler._convertToDict())
def getDevices(request): deviceList = dao.getDevices() deviceObjList = [] for device in deviceList: deviceObj = device._convertToDict() deviceObj['deviceInterfaceType_TR'] = getDictKeyByName( INTERFACE, deviceObj['deviceInterfaceType']) deviceObjList.append(deviceObj) return ResponseProcessor.processSuccessResponse(deviceObjList)
def getPiDevices(request): deviceList = dao.getPiDevices() deviceObjList = [] for device in deviceList: deviceObj = device._convertToDict() deviceObj['status_TR'] = getDictKeyByName(STATUS, deviceObj['status']) piDevicePinList = _getPiDevicePin(device.id) deviceObj['pinList'] = piDevicePinList device = _getDeviceById(deviceObj['deviceID']) deviceObj['deviceDetail'] = device deviceObjList.append(deviceObj) return ResponseProcessor.processSuccessResponse(deviceObjList)
def gpio_setPinStatus(request, boardID, level): pin = pi.getPinByBoardId(boardID) setupLevel = None if (level == 1): setupLevel = Pin.PIN_HIGH else: setupLevel = Pin.PIN_LOW if (pin is not None): pin.output_setup(setupLevel) return ResponseProcessor.processSuccessResponse({ 'boardId': boardID, 'level': setupLevel })
def addHistoryChart(request): postBody = request.body postData = json.loads(postBody) piDeviceId = postData['piDeviceID'] deviceDataName = postData['deviceDataName'] title = postData['title'] unit = postData['unit'] displayType = postData['displayType'] resultChart = historyDao.addNewDeviceHistoryChart(piDeviceId, deviceDataName, title, unit, displayType) return ResponseProcessor.processSuccessResponse( resultChart._convertToDict())
def led(request, piDeviceId, switch): pinList = dao.getPiDevicePinByPiDeviceId(piDeviceId) boardID = None for pin in pinList: piDevicePinObj = pin._convertToDict() devicePinObj = _getPiDevicePinDetail(piDevicePinObj['devicePinID']) if (devicePinObj['pinFunction'] == PIN_FUCNTION['GPIO']): boardID = pin.pinBoardID if (boardID is not None): led = SimpleEquipt.LED(pi.getPinByBoardId(boardID)) if (switch == 'on'): led.light() else: led.shutdown() return ResponseProcessor.processSuccessResponse()
def startMotor(request, piDeviceId, direction, speed): pinList = dao.getPiDevicePinByPiDeviceId(piDeviceId) gpioList = [] for pin in pinList: piDevicePinObj = pin._convertToDict() devicePinObj = _getPiDevicePinDetail(piDevicePinObj['devicePinID']) boardID = pin.pinBoardID if (devicePinObj['pinFunction'] == PIN_FUCNTION['GPIO']): gpioList.append(pi.getPinByBoardId(boardID)) motor = Motor(gpioList[0], gpioList[1]) motor.start(direction, speed) motorCacheData = {} motorCacheData['piDeviceId'] = piDeviceId motorCacheData['motorObj'] = motor return ResponseProcessor.processSuccessResponse()
def updateHistoryChart(request, chartId): postBody = request.body postData = json.loads(postBody) piDeviceId = None if ('piDeviceID' not in postData) else postData['piDeviceID'] deviceDataName = None if ('deviceDataName' not in postData) else postData['deviceDataName'] title = None if ('title' not in postData) else postData['title'] unit = None if ('unit' not in postData) else postData['unit'] displayType = None if ('displayType' not in postData) else postData['displayType'] resultChart = historyDao.updateDeviceHistoryChart(chartId, piDeviceId, deviceDataName, title, unit, displayType) return ResponseProcessor.processSuccessResponse( resultChart._convertToDict())
def getHistoryData(request, piDeviceId): dateFrom = request.GET.get('from') dateTo = request.GET.get('to') dateFromObj = None dateToObj = None if (dateFrom is None): dateFromObj = timezone.now() else: dateFromObj = datetime.strptime(dateFrom, '%Y-%m-%d %H:%M:%S') if (dateTo is None): dateToObj = timezone.now() else: dateToObj = datetime.strptime(dateTo, '%Y-%m-%d %H:%M:%S') deviceDataNames = request.GET.get('names') historyDataObjList = _getHistoryData(piDeviceId, dateFromObj, dateToObj, deviceDataNames) return ResponseProcessor.processSuccessResponse(historyDataObjList)
def createPiDevice(request, deviceId): name = request.GET.get('name') if (name is not None): piDevice = dao.addPiDevice(deviceId, name) piDeviceObj = piDevice._convertToDict() devicePinList = dao.getDevicePinByDeviceId(deviceId) devicePinObjList = [] for devicePin in devicePinList: devicePinID = devicePin.id piDevicePinObj = dao.addPiDevicePin(piDevice.id, [{ 'pinBoardId': -1, 'devicePinID': devicePinID, 'value': None }]) piDevicePinObj = piDevicePinObj[0] piDevicePinObj = piDevicePinObj._convertToDict() devicePinObjList.append(piDevicePinObj) piDeviceObj['pinList'] = devicePinObjList return ResponseProcessor.processSuccessResponse(piDeviceObj) else: return ResponseProcessor.processFailResponse("设备名字为空")
def start_process_scheduler(request): activeScheduleList = dao.findAllActiveScheduler() scheduledJobs = [] for scheduler in activeScheduleList: schedulerObj = scheduler._convertToDict() schedulerId = schedulerObj['id'] jobName = schedulerObj['scheduleJobName'] piDeviceId = schedulerObj['piDeviceID'] scheduleMode = schedulerObj['scheduleMode'] scheduleValue = schedulerObj['scheduleValue'] if (scheduleMode == Constants.SCHEDULE_MODE['SECONDS']): schedule.every(scheduleValue).seconds.do(saveJob, request, piDeviceId) elif (scheduleMode == Constants.SCHEDULE_MODE['MINUTS']): schedule.every(scheduleValue).minutes.do(saveJob, request, piDeviceId) elif (scheduleMode == Constants.SCHEDULE_MODE['HOUR']): schedule.every(scheduleValue).hour.do(saveJob, request, piDeviceId) elif (scheduleMode == Constants.SCHEDULE_MODE['DAY']): schedule.every(scheduleValue).day.do(saveJob, request, piDeviceId) scheduledJobs.append(dao.startAScheduler(schedulerId)._convertToDict()) return ResponseProcessor.processSuccessResponse(scheduledJobs)
def getTodaySingleGraphData(request, piDeviceId, deviceDataName): category = [] data = [] dateFrom = date.today() dateTo = date.today() dateTo = dateTo.replace(day=dateTo.day + 1) print(dateFrom) print(dateTo) # dateFrom = datetime.strptime('2020-04-27 00:00:00', '%Y-%m-%d %H:%M:%S') # dateTo = datetime.strptime('2020-04-28 00:00:00', '%Y-%m-%d %H:%M:%S') listData = _getHistoryData(piDeviceId, dateFrom, dateTo, deviceDataName) for listitem in listData: data.append(listitem['deviceDataValue']) timeObj = listitem['dataDateTime'] timeObj = timeObj.astimezone(pytz.timezone('Asia/Shanghai')) displayTime = timeObj.strftime('%H:%M') category.append(displayTime) result = {} result['category'] = category result['data'] = data return ResponseProcessor.processSuccessResponse(result)
def getRotationCountDataV2(request, piDeviceId): deviceData = _getRotationCountDataV2(piDeviceId) return ResponseProcessor.processSuccessResponse(deviceData)
def init(request): InitialDatabaseSetup.run() return ResponseProcessor.processSuccessResponse()
def getDHT22Data(request, piDeviceId): dhtDataObj = _getDHT22Data(piDeviceId) return ResponseProcessor.processSuccessResponse(dhtDataObj)
def unAttachPiDevicePinToBoard(request, piDevicePinId): updatedPiDevicePin = dao.updateDevicePinBoardId(piDevicePinId, -1) updatedPiDevicePin = dao.getPiDevicePinById(piDevicePinId) return ResponseProcessor.processSuccessResponse( updatedPiDevicePin._convertToDict())
def attachPiDevicePinToBoard(request, piDevicePinId): boardID = request.GET.get('boardId') updatedPiDevicePin = dao.updateDevicePinBoardId(piDevicePinId, boardID) updatedPiDevicePin = dao.getPiDevicePinById(piDevicePinId) return ResponseProcessor.processSuccessResponse( updatedPiDevicePin._convertToDict())
def deletePiDeviceById(request, piDeviceId): dao.deletePiDevice(piDeviceId) return ResponseProcessor.processSuccessResponse()
def getPiDeviceById(request, piDeviceId): piDevice = dao.getPiDeviceById(piDeviceId) return ResponseProcessor.processSuccessResponse(piDevice._convertToDict())
def gpio_pwn_change_frequency(request, boardID, frequency): pin = pi.getPinByBoardId(boardID) if (pin is not None): pin.PWM_ChangeFrequency(frequency) return ResponseProcessor.processSuccessResponse()
def gpio_pwn_change_duty_cycle(request, boardID, cycle): pin = pi.getPinByBoardId(boardID) if (pin is not None): pin.PWM_ChangeDutyCycle(cycle) return ResponseProcessor.processSuccessResponse()
def gpio_getPinStatus(request, boardID): data = None pin = pi.getPinByBoardId(boardID) if (pin is not None): data = pin.read() return ResponseProcessor.processSuccessResponse(data)
def stop_scheduler(request, schedulerId): scheduler = dao.stopAScheduler(schedulerId) return ResponseProcessor.processSuccessResponse(scheduler._convertToDict())
def getBMP180Data(request, piDeviceId): bmpDataObj = _getBMP180Data(piDeviceId) return ResponseProcessor.processSuccessResponse(bmpDataObj)
def updatePiDevice(request, piDeviceId): name = request.GET.get('name') updatedPiDevice = dao.updatePiDevice(piDeviceId, name) return ResponseProcessor.processSuccessResponse( updatedPiDevice._convertToDict())
def getPiDevicePin(request, piDeviceId): devicePinObjList = _getPiDevicePin(piDeviceId) return ResponseProcessor.processSuccessResponse(devicePinObjList)
def gpio_pwn_start(request, boardID): pin = pi.getPinByBoardId(boardID) if (pin is not None): pin.PWM_setup() return ResponseProcessor.processSuccessResponse()
def saveJob(request, piDeviceId): data = _saveJob(piDeviceId) return ResponseProcessor.processSuccessResponse(data)