@Override
 public void onUpdateDevice(StateDevice stateDevice) {
   if (stateDevice.getType() == StateDeviceProtos.StateDeviceMessage.Type.DOORBELL) {
     // If the state is set to inactive by the client
     Log.i(TAG, "Device updated");
     if (stateDevice.getState() == StateDeviceProtos.StateDeviceMessage.State.INACTIVE) {
       StateDeviceProtos.StateDeviceMessage msg =
           StateDeviceProtos.StateDeviceMessage.newBuilder()
               .setId(stateDevice.getId())
               .setName(stateDevice.getName())
               .setState(stateDevice.getState())
               .setType(stateDevice.getType())
               .build();
       // Send the message
       try {
         Log.i(TAG, "Sending message: \n" + msg.toString());
         OutputStream out = Client.getConnection().getOutputStream();
         msg.writeDelimitedTo(out);
       } catch (IOException e) {
         Client.removeConnection();
         Log.e(TAG, "unable to write to output stream", e);
       }
     }
   }
 }
    @Override
    public void onMessageReceived(StateDeviceProtos.StateDeviceMessage msg) {
      if (msg != null && msg.getType() == StateDeviceProtos.StateDeviceMessage.Type.DOORBELL) {

        Log.i(TAG, "New message received");

        // create a device from the message
        StateDevice device = new StateDevice(msg);

        if (StateDeviceManager.contains(msg.getId())) {
          // update the device
          StateDeviceManager.updateStateDevice(device);
        } else {
          // add the device
          StateDeviceManager.addStateDevice(device);
        }

        // notification and save image
        if (msg.getType() == StateDeviceProtos.StateDeviceMessage.Type.DOORBELL) {
          if (msg.hasData()) {
            Notifications.sendDoorbellAlertNotification(mContext, msg);
            saveVisitor(msg);
          } else {
            Log.i(TAG, msg.toString());
          }
        }
      }
    }
    /**
     * Helper method to get the contents of the visitor message, save the image, and save the event
     *
     * @param msg
     */
    private void saveVisitor(StateDeviceProtos.StateDeviceMessage msg) {
      Log.i(TAG, "Logging Event");
      Visitor visitor = new Visitor();
      Long time = System.currentTimeMillis();
      ByteString data = msg.getData();
      String filename = "visitor" + System.currentTimeMillis() + ".jpg";
      visitor.setImagePath(filename);
      File imageDirectory = new File(mContext.getFilesDir() + ConstantManager.IMAGE_DIR);

      // create the image directory if it doesn't exist
      if (!imageDirectory.exists()) {
        Log.i(TAG, "Directory being created? " + imageDirectory.mkdirs());
      }

      // save the image file
      File image = new File(imageDirectory, filename);
      try {
        if (!image.exists()) {
          Log.i(TAG, "File being created? " + image.createNewFile());
        }
        FileOutputStream fos = new FileOutputStream(image, true);
        fos.write(data.toByteArray());
        fos.close();
      } catch (IOException e) {
        e.printStackTrace();
      }

      // Log the visitor
      visitor.setTime(time);
      visitor.setLocation(msg.getName());
      VisitorLog.logVisitor(visitor, mContext);
    }