def createLEDDeviceInfo(): ledDevice = dao.addDevice(deviceName='LED', deviceType='LED', deviceInCategory='发光二极管', deviceInterfaceType=INTERFACE['GPIO']) deviceId = ledDevice.id pinList = [] GNDPin = {'pinMode': None, 'pinFunction': PIN_FUCNTION['GND']} VPIN = {'pinMode': PIN_MODE['OUT'], 'pinFunction': PIN_FUCNTION['GPIO']} pinList.append(GNDPin) pinList.append(VPIN) dao.addDevicePin(deviceId, pinList)
def createHSensorRotationDeviceInfo(): rotationDevice = dao.addDevice(deviceName='HRotation', deviceType='HRotation', deviceInCategory='霍尔转速', deviceInterfaceType=INTERFACE['GPIO']) deviceId = rotationDevice.id pinList = [] GNDPin = {'pinMode': None, 'pinFunction': PIN_FUCNTION['GND']} VPIN = {'pinMode': PIN_MODE['OUT'], 'pinFunction': PIN_FUCNTION['GPIO']} pinList.append(GNDPin) pinList.append(VPIN) dao.addDevicePin(deviceId, pinList)
def createDHT22DeviceInfo(): dht22Device = dao.addDevice(deviceName='温湿传感器DHT22', deviceType='DHT22', deviceInCategory='温度湿度传感器', deviceInterfaceType=INTERFACE['GPIO']) deviceId = dht22Device.id pinList = [] GNDPin = {'pinMode': None, 'pinFunction': PIN_FUCNTION['GND']} VCCPin = {'pinMode': None, 'pinFunction': PIN_FUCNTION['3V']} VPIN = {'pinMode': PIN_MODE['IN'], 'pinFunction': PIN_FUCNTION['GPIO']} pinList.append(GNDPin) pinList.append(VCCPin) pinList.append(VPIN) dao.addDevicePin(deviceId, pinList)
def registNewDevice(deviceId, name): #pinDataList [{function: 0, mode: 1}] device = dao.addDevice(deviceId, name) pinList = [] for pinData in pinList: # if not regist pin to Pi Board, default to -1 pinObj = { -1: { 'pinMode': pinData.mode, 'pinFunction': pinData.function, 'value': '' } } pin = dao.addDevicePin(device.id, pinObj) pinList.append(pin) return {device: device, pinList: pinList}
def getPiDevicesByDeviceId(request, deviceId): piDeviceList = dao.getPiDeviceByDeviceId(deviceId) piDeviceObjList = [] for piDevice in piDeviceList: piDeviceObj = piDevice._convertToDict() piDeviceObjList.append(piDeviceObj) return ResponseProcessor.processSuccessResponse(piDeviceObjList)
def createRainDropDeviceInfo(): rainDropDevice = dao.addDevice(deviceName='水滴传感器', deviceType='RAINDROP', deviceInCategory='水滴传感器', deviceInterfaceType=INTERFACE['I2C'], i2cAddress=0x77) deviceId = rainDropDevice.id pinList = [] GNDPin = {'pinMode': None, 'pinFunction': PIN_FUCNTION['GND']} VCCPin = {'pinMode': None, 'pinFunction': PIN_FUCNTION['3V']} VPIN = {'pinMode': PIN_MODE['IN'], 'pinFunction': PIN_FUCNTION['GPIO']} pinList.append(GNDPin) pinList.append(VCCPin) pinList.append(VPIN) dao.addDevicePin(deviceId, pinList)
def createMotorDeviceInfo(): motorDevice = dao.addDevice(deviceName='直流电机驱动', deviceType='L298N', deviceInCategory='直流电机驱动', deviceInterfaceType=INTERFACE['GPIO']) deviceId = motorDevice.id pinList = [] GNDPin = {'pinMode': None, 'pinFunction': PIN_FUCNTION['GND']} VCCPin = {'pinMode': None, 'pinFunction': PIN_FUCNTION['VCC']} VPIN1 = {'pinMode': PIN_MODE['OUT'], 'pinFunction': PIN_FUCNTION['GPIO']} VPIN2 = {'pinMode': PIN_MODE['OUT'], 'pinFunction': PIN_FUCNTION['GPIO']} pinList.append(GNDPin) pinList.append(VCCPin) pinList.append(VPIN1) pinList.append(VPIN2) dao.addDevicePin(deviceId, pinList)
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 createBY30DeviceInfo(): gy30Device = dao.addDevice(deviceName='光线强度传感器GY30', deviceType='GY30', deviceInCategory='光线强度传感器', deviceInterfaceType=INTERFACE['I2C'], i2cAddress=0x77) deviceId = gy30Device.id pinList = [] GNDPin = {'pinMode': None, 'pinFunction': PIN_FUCNTION['GND']} VCCPin = {'pinMode': None, 'pinFunction': PIN_FUCNTION['3V']} SDAPin = {'pinMode': PIN_MODE['I2C'], 'pinFunction': PIN_FUCNTION['SDA']} SDLPin = {'pinMode': PIN_MODE['I2C'], 'pinFunction': PIN_FUCNTION['SDL']} pinList.append(GNDPin) pinList.append(VCCPin) pinList.append(SDAPin) pinList.append(SDLPin) dao.addDevicePin(deviceId, pinList)
def _getPiDevicePin(piDeviceId): devicePinList = dao.getPiDevicePinByPiDeviceId(piDeviceId) devicePinObjList = [] for pidevicepind in devicePinList: deviceObj = pidevicepind._convertToDict() devicePin = _getPiDevicePinDetail(deviceObj['devicePinID']) deviceObj['devicePinDetail'] = devicePin devicePinObjList.append(deviceObj) return devicePinObjList
def _getPiDevicePinDetail(devicePinId): devicePin = dao.getDevicePinById(devicePinId) if (devicePin is not None): devicePinObj = devicePin._convertToDict() devicePinObj['pinMode_TR'] = getDictKeyByName(PIN_MODE, devicePinObj['pinMode']) devicePinObj['pinFunction_TR'] = getDictKeyByName( PIN_FUCNTION, devicePinObj['pinFunction']) return devicePinObj else: return {}
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 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 _getRotationCountDataV2(piDeviceId): pinList = dao.getPiDevicePinByPiDeviceId(piDeviceId) deviceData = -1 for pin in pinList: piDevicePinObj = pin._convertToDict() devicePinObj = _getPiDevicePinDetail(piDevicePinObj['devicePinID']) boardID = pin.pinBoardID if (devicePinObj['pinFunction'] == PIN_FUCNTION['GPIO']): rotationSensor = HSensorRotationV2.getInstance( pi.getPinByBoardId(boardID)) deviceData = rotationSensor.getAvgData(5) break return deviceData
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 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 _getRainDropData(piDeviceId): pinList = dao.getPiDevicePinByPiDeviceId(piDeviceId) boardPinObject = None for pin in pinList: piDevicePinObj = pin._convertToDict() devicePinObj = _getPiDevicePinDetail(piDevicePinObj['devicePinID']) if (devicePinObj['pinFunction'] == PIN_FUCNTION['GPIO']): boardPinID = pin.pinBoardID boardPinObject = pi.getPinByBoardId(boardPinID) dropDataObj = {'rain': 'no rain'} if (boardPinObject is not None): rainDropDevice = RainDrop(boardPinObject) if (rainDropDevice.isDrop()): dropDataObj['rain'] = 'rain' return dropDataObj
def run(): print('init database') if (len(dao.getDeviceByType('LED')) == 0): createLEDDeviceInfo() if (len(dao.getDeviceByType('DHT22')) == 0): createDHT22DeviceInfo() if (len(dao.getDeviceByType('BMP180')) == 0): createBMP180DeviceInfo() if (len(dao.getDeviceByType('GY30')) == 0): createBY30DeviceInfo() if (len(dao.getDeviceByType('RAINDROP')) == 0): createRainDropDeviceInfo() if (len(dao.getDeviceByType('HRotation')) == 0): createHSensorRotationDeviceInfo() if (len(dao.getDeviceByType('L298N')) == 0): createMotorDeviceInfo() if (len(dao.getDeviceByType('HallV2')) == 0): createRotationDeviceInfo()
def _getGY30Data(piDeviceId): pinList = dao.getPiDevicePinByPiDeviceId(piDeviceId) SDAPin = None SCLPin = None for pin in pinList: piDevicePinObj = pin._convertToDict() devicePinObj = _getPiDevicePinDetail(piDevicePinObj['devicePinID']) if (devicePinObj['pinFunction'] == PIN_FUCNTION['SDA']): SDAPin = pin.pinBoardID elif (devicePinObj['pinFunction'] == PIN_FUCNTION['SDL']): SCLPin = pin.pinBoardID gy30DataObj = {'lx': None} if (SDAPin is not None and SCLPin is not None): gy30 = GY30() lightData = gy30.getLightData() gy30DataObj['lx'] = round(lightData, 0) return gy30DataObj
def _getDHT22Data(piDeviceId): 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 dhtDataObj = {'temperature': None, 'humidity': None} if (boardID is not None): dht22 = DHT22(pi.getPinByBoardId(boardID)) dhtData = dht22.getData() temperature = dhtData[0] humidity = dhtData[1] dhtDataObj['temperature'] = temperature dhtDataObj['humidity'] = humidity return dhtDataObj
def _getBMP180Data(piDeviceId): pinList = dao.getPiDevicePinByPiDeviceId(piDeviceId) SDAPin = None SDLPin = None for pin in pinList: piDevicePinObj = pin._convertToDict() devicePinObj = _getPiDevicePinDetail(piDevicePinObj['devicePinID']) if (devicePinObj['pinFunction'] == PIN_FUCNTION['SDA']): SDAPin = pin.pinBoardID elif (devicePinObj['pinFunction'] == PIN_FUCNTION['SDL']): SDLPin = pin.pinBoardID bmpDataObj = {'temperature': None, 'pressure': None, 'altitude': None} if (SDAPin is not None and SDLPin is not None): bmp180 = BMP180() pressure = bmp180.getPressure() altitude = bmp180.getAltitude() + 95 temperature = bmp180.getTemperature() bmpDataObj['temperature'] = temperature bmpDataObj['pressure'] = round(pressure / 100, 0) bmpDataObj['altitude'] = round(altitude, 0) return bmpDataObj
def unplugDevicePin(pinId): updatedPin = dao.updateDevicePinBoardId(pinId, -1) return updatedPin
def pluginDevicePin(pinId, boardID): updatedPin = dao.updateDevicePinBoardId(pinId, boardID) return updatedPin
def deletePiDeviceById(request, piDeviceId): dao.deletePiDevice(piDeviceId) return ResponseProcessor.processSuccessResponse()
def updatePiDevice(request, piDeviceId): name = request.GET.get('name') updatedPiDevice = dao.updatePiDevice(piDeviceId, name) return ResponseProcessor.processSuccessResponse( updatedPiDevice._convertToDict())
def _getDeviceById(deviceId): device = dao.getDeviceById(deviceId) return device._convertToDict()
def getPiDeviceById(request, piDeviceId): piDevice = dao.getPiDeviceById(piDeviceId) return ResponseProcessor.processSuccessResponse(piDevice._convertToDict())
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 _saveJob(piDeviceId): savedDataList = [] piDevice = eqDao.getPiDeviceById(piDeviceId) deviceId = piDevice.deviceID device = eqDao.getDeviceById(deviceId) jobName = device.deviceType if (jobName == 'DHT22'): data = DevicesView._getDHT22Data(piDeviceId) # data = { # 'temperature': 10, # 'humidity': 40 # } d1 = eqDao.saveDeviceData(piDeviceId, 'temperature', data['temperature']) d2 = eqDao.saveDeviceData(piDeviceId, 'humidity', data['humidity']) savedDataList.append(d1._convertToDict()) savedDataList.append(d2._convertToDict()) elif (jobName == 'BMP180'): data = DevicesView._getBMP180Data(piDeviceId) d1 = eqDao.saveDeviceData(piDeviceId, 'temperature', data['temperature']) d2 = eqDao.saveDeviceData(piDeviceId, 'pressure', data['pressure']) d3 = eqDao.saveDeviceData(piDeviceId, 'altitude', data['altitude']) savedDataList.append(d1._convertToDict()) savedDataList.append(d2._convertToDict()) savedDataList.append(d3._convertToDict()) elif (jobName == 'GY30'): data = DevicesView._getGY30Data(piDeviceId) d1 = eqDao.saveDeviceData(piDeviceId, 'lx', data['lx']) savedDataList.append(d1._convertToDict()) elif (jobName == 'RAINDROP'): data = DevicesView._getRainDropData(piDeviceId) lastDataList = eqDao.getDeviceDataByPiDeviceIdAndDeviceDataName( piDeviceId, 'rain') if (len(lastDataList) == 0): d1 = eqDao.saveDeviceData(piDeviceId, 'rain', data['rain']) savedDataList.append(d1._convertToDict()) else: lastData = lastDataList[len(lastDataList) - 1] lastDataObj = lastData._convertToDict() rainData = lastDataObj['deviceDataValue'] if (rainData != data['rain']): d1 = eqDao.saveDeviceData(piDeviceId, 'rain', data['rain']) savedDataList.append(d1._convertToDict()) elif (jobName == 'HRotation'): data = DevicesView._getRotationCountData(piDeviceId) d1 = eqDao.saveDeviceData(piDeviceId, 'rpm', data['rpm']) savedDataList.append(d1._convertToDict()) elif (jobName == 'HallV2'): data = DevicesView._getRotationCountDataV2(piDeviceId) d1 = eqDao.saveDeviceData(piDeviceId, 'rpm', data) savedDataList.append(d1._convertToDict()) return savedDataList