def get(self): # If we've already imported farmers, don't bother to import them from # the JSON file. if Farmer.query().count() > 0: return self.json_response({'status': 'already_populated'}) with open('attendees.json') as attendees_file: attendees = json.loads(attendees_file.read()) farmers = [] for name, cell_number in attendees.iteritems(): first_name, last_name = name.split(' ') farmers.append(Farmer( first_name=first_name, last_name=last_name, cell_number=cell_number, house_number=cell_number, farmer_id=generate_farmer_id(), verified=True, dob=datetime.datetime.now() - datetime.timedelta(days=20 * 365), address='Computer Science Postgrab Lab', alias=generate_alias(first_name, last_name), farmer_idx=random.randint(1, 100000), parish='UWI', main_activity='Farming', )) node = Node(node_id='Tomatoes') ndb.put_multi(farmers + [node]) for _ in range(50): DataPoint( temperature=random.randint(40, 90), pressure=random.randint(20, 30), humidity=random.randint(65, 100), light=random.randint(0, 100), saturation=random.randint(30, 90), parent=node.key, ).put() message = """ Low light on tomato patch. Low barometric pressure. Possibility of rain. """ for farmer in farmers: send_sms(farmer.cell_number, message) return self.json_response({'status': 'done'})
def reply_farmer_id(cell_number): farmer = Farmer.get_by_cell_number(cell_number) if farmer is None: logging.error('Farmer not found') return send_sms( cell_number, 'Register with RADA to access this service.' ) message = """ Hello, {farmer.first_name} {farmer.last_name}! Your farmer ID is {farmer.farmer_id}. """.format(farmer=farmer) send_sms( cell_number, message, sender='*****@*****.**', ) logging.info(message)
def reply_node_status(farmer_id, node): """Responds to a message in the form STATUS <FARMER_ID> <NODE_ID> with the status of the area compared to optimal values for that crop. """ data_points = node.data_points() temp_readings = Counter([data_point.temperature for data_point in data_points]) pressure_readings = Counter([data_point.temperature for data_point in data_points]) humidity_readings = Counter([data_point.temperature for data_point in data_points]) light_readings = Counter([data_point.temperature for data_point in data_points]) saturation_readings = Counter([data_point.temperature for data_point in data_points]) sensor_summary = { 'mode_temperature': temp_readings.most_common(1)[0][0], 'mode_pressure': pressure_readings.most_common(1)[0][0], 'mode_humidity': humidity_readings.most_common(1)[0][0], 'mode_light': light_readings.most_common(1)[0][0], 'mode_saturation': saturation_readings.most_common(1)[0][0], } message = """ Stats for {node_id}: Temperature: {mode_temperature} C Pressure: {mode_pressure} Bar Humidity: {mode_humidity}% Light: {mode_light} Saturation: {mode_saturation} """.format( node_id=node.node_id, **sensor_summary ) send_sms(farmer_id, message) logging.info(message)
def handle(sms_message): message_body = sms_message.body if sms_message.to == '*****@*****.**': logging.info('Message received') return elif sms_message.to == '*****@*****.**': logging.info('Message received from: ' + sms_message.sender) return reply_farmer_id(sms_message.sender) elif sms_message.to == '*****@*****.**': farmer_id, node_id = message_body.split(' ')[1:] node = Node.get_by_node_id(node_id) if node is None: logging.error('Node not found') return send_sms( sms_message.sender, 'You don\'t have any %s' % node_id ) logging.info('Node found') return reply_node_status(sms_message.sender, node)