Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #51 from matthenning/master
Browse files Browse the repository at this point in the history
v1.1-beta
  • Loading branch information
matthenning authored Jan 19, 2017
2 parents 7810d83 + 018f09a commit d1f68ef
Show file tree
Hide file tree
Showing 191 changed files with 56,395 additions and 1,818 deletions.
222 changes: 219 additions & 3 deletions app/Animal.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function terrarium()
*/
public function files()
{
return $this->hasMany('App\File', 'belongsTo_id')->where('belongsTo_type', 'Animal');
return $this->hasMany('App\File', 'belongsTo_id')->where('belongsTo_type', 'Animal')->orderBy('created_at', 'DESC');
}

/**
Expand All @@ -94,7 +94,7 @@ public function properties()
*/
public function events()
{
return $this->hasMany('App\Event', 'belongsTo_id')->where('belongsTo_type', 'Animal');
return $this->hasMany('App\Event', 'belongsTo_id')->where('belongsTo_type', 'Animal')->orderBy('created_at', 'DESC');
}

/**
Expand All @@ -120,7 +120,7 @@ public function feeding_schedules()
public function weighings()
{
return $this->events()->where('type', 'AnimalWeighing')
->orderBy('created_at', 'desc');
->orderBy('created_at', 'desc');
}

/**
Expand Down Expand Up @@ -195,6 +195,222 @@ public function last_weighing()
return $this->weighings()->limit(1)->get()->first();
}

/**
* @return mixed
*/
public function biography_entries()
{
return $this->hasMany('App\Event', 'belongsTo_id')->where('type', 'BiographyEntry')
->where('belongsTo_type', 'Animal')
->with('properties');
}

/**
* @return mixed
*/
public function caresheets()
{
return $this->hasMany('App\Event', 'belongsTo_id')->where('type', 'AnimalCaresheet')
->where('belongsTo_type', 'Animal')
->with('properties');
}

/**
* Create an animal caresheet
* Settings:
* sensor_history_days: How far should ciliatus go back to fetch historic data for the following:
* include_terrarium_history
* include_biography_entries
* include_weight
* include_feedings
*
* @param array $settings
* @return CiliatusModel|Event
*/
public function generate_caresheet($settings = null)
{
if (is_null($settings)) {
$settings = [
'include_terrarium_history',
'include_biography_entries',
'include_weight',
'include_feedings'
];
}

if (!isset($settings['sensor_history_days'])) {
$settings['sensor_history_days'] = env('DEFAULT_CARESHEET_SENSOR_HISTORY_DAYS', 14);
}

if (!isset($settings['data_history_days'])) {
$settings['data_history_days'] = env('DEFAULT_CARESHEET_DATA_HISTORY_DAYS', 60);
}

$caresheet = Event::create([
'belongsTo_type' => 'Animal',
'belongsTo_id' => $this->id,
'type' => 'AnimalCaresheet',
'name' => trans_choice('components.caresheets', 1) . ' ' . $this->display_name
]);

$caresheet->create_property('AnimalCaresheetProperty', 'sensor_history_days', $settings['sensor_history_days']);
$caresheet->create_property('AnimalCaresheetProperty', 'data_history_days', $settings['data_history_days']);

if (!is_null($this->terrarium) && in_array('include_terrarium_history', $settings)) {
$caresheet->create_property('AnimalCaresheetProperty', 'terrarium_id', $this->terrarium_id);
$caresheet->create_property('AnimalCaresheetProperty', 'history_from', Carbon::now()->subDays($settings['sensor_history_days']));

/*
* Get Humidity Stats
*/
$all_stats =
$this->terrarium->getHumidityStats(
$settings['sensor_history_days'],
Carbon::today()
);

$caresheet->create_property('AnimalCaresheetProperty', 'terrarium_average_humidity',
$all_stats->avg_rawvalue
);
$caresheet->create_property('AnimalCaresheetProperty', 'terrarium_min_humidity',
$all_stats->min_rawvalue
);
$caresheet->create_property('AnimalCaresheetProperty', 'terrarium_max_humidity',
$all_stats->max_rawvalue
);

$day_stats =
$this->terrarium->getHumidityStats(
$settings['sensor_history_days'],
Carbon::today(),
Carbon::createFromTime(7, 0, 0),
Carbon::createFromTime(20, 0, 0)
);
$caresheet->create_property('AnimalCaresheetProperty', 'terrarium_average_humidity_day',
$day_stats->avg_rawvalue
);
$caresheet->create_property('AnimalCaresheetProperty', 'terrarium_min_humidity_day',
$day_stats->min_rawvalue
);
$caresheet->create_property('AnimalCaresheetProperty', 'terrarium_max_humidity_day',
$day_stats->max_rawvalue
);

$night_stats =
$this->terrarium->getHumidityStats(
$settings['sensor_history_days'],
Carbon::today(),
Carbon::createFromTime(20, 0, 0),
Carbon::createFromTime(7, 0, 0)
);
$caresheet->create_property('AnimalCaresheetProperty', 'terrarium_average_humidity_night',
$night_stats->avg_rawvalue
);
$caresheet->create_property('AnimalCaresheetProperty', 'terrarium_min_humidity_night',
$night_stats->min_rawvalue
);
$caresheet->create_property('AnimalCaresheetProperty', 'terrarium_max_humidity_night',
$night_stats->max_rawvalue
);

/*
* Get Temperature stats
*/
$all_stats =
$this->terrarium->getTemperatureStats(
$settings['sensor_history_days'],
Carbon::today()
);

$caresheet->create_property('AnimalCaresheetProperty', 'terrarium_average_temperature',
$all_stats->avg_rawvalue
);
$caresheet->create_property('AnimalCaresheetProperty', 'terrarium_min_temperature',
$all_stats->min_rawvalue
);
$caresheet->create_property('AnimalCaresheetProperty', 'terrarium_max_temperature',
$all_stats->max_rawvalue
);

$day_stats =
$this->terrarium->getTemperatureStats(
$settings['sensor_history_days'],
Carbon::today(),
Carbon::createFromTime(7, 0, 0),
Carbon::createFromTime(20, 0, 0)
);
$caresheet->create_property('AnimalCaresheetProperty', 'terrarium_average_temperature_day',
$day_stats->avg_rawvalue
);
$caresheet->create_property('AnimalCaresheetProperty', 'terrarium_min_temperature_day',
$day_stats->min_rawvalue
);
$caresheet->create_property('AnimalCaresheetProperty', 'terrarium_max_temperature_day',
$day_stats->max_rawvalue
);

$night_stats =
$this->terrarium->getTemperatureStats(
$settings['sensor_history_days'],
Carbon::today(),
Carbon::createFromTime(20, 0, 0),
Carbon::createFromTime(7, 0, 0)
);
$caresheet->create_property('AnimalCaresheetProperty', 'terrarium_average_temperature_night',
$night_stats->avg_rawvalue
);
$caresheet->create_property('AnimalCaresheetProperty', 'terrarium_min_temperature_night',
$night_stats->min_rawvalue
);
$caresheet->create_property('AnimalCaresheetProperty', 'terrarium_max_temperature_night',
$night_stats->max_rawvalue
);
}

if (in_array('include_biography_entries', $settings)) {
$caresheet->create_property('AnimalCaresheetProperty', 'biography_entries',
$this->biography_entries()
->where('created_at', '>=', Carbon::now()->subDays((int)$settings['data_history_days'])->toDateTimeString())
->get()
->implode('id', ',')
);
}

if (in_array('include_weight', $settings)) {
$caresheet->create_property('AnimalCaresheetProperty', 'weighings',
$this->weighings()
->where('created_at', '>=', Carbon::now()->subDays((int)$settings['data_history_days'])->toDateTimeString())
->get()
->implode('id', ',')
);
}

if (in_array('include_feedings', $settings)) {
$caresheet->create_property('AnimalCaresheetProperty', 'feedings',
$this->feedings()
->where('created_at', '>=', Carbon::now()->subDays((int)$settings['data_history_days'])->toDateTimeString())
->get()
->implode('id', ',')
);
}

return $caresheet;
}

/**
* @param $id
*/
public function delete_caresheet($id)
{
$caresheet = Event::where('type', 'AnimalCaresheet')->find($id);
if (!is_null($caresheet)) {
foreach ($caresheet->properties as $prop) {
$prop->delete();
}
$caresheet->delete();
}
}

/**
* @return string
*/
Expand Down
2 changes: 1 addition & 1 deletion app/Controlunit.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function save(array $options = [])
*/
public function physical_sensors()
{
return $this->hasMany('App\PhysicalSensor');
return $this->hasMany('App\PhysicalSensor')->with('logical_sensors');
}

/**
Expand Down
37 changes: 35 additions & 2 deletions app/CriticalState.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,24 @@ public function notify()

foreach (User::get() as $u) {
if ($u->setting('notifications_enabled') == 'on') {
$u->message(trans('user.messages.critical_state_notification'));
switch ($this->belongsTo_type) {
case 'LogicalSensor':
$ls = LogicalSensor::find($this->belongsTo_id);
if (is_null($ls)) {
\Log::error('CriticalState ' . $this->id . ' belongs to LogicalSensor ' . $this->belongsTo_id . ' which could not be found.');
break;
}
$u->message(trans('messages.critical_state_notification_logical_sensor.' . $ls->type, [
'logical_sensor' => $ls->name,
$ls->type => $ls->getCurrentCookedValue()
]));
break;
default:
$u->message(trans('messages.critical_state_generic', [
'critical_state' => $this->name
]));
}

}
}

Expand Down Expand Up @@ -156,7 +173,23 @@ public function notifyRecovered()

foreach (User::get() as $u) {
if ($u->setting('notifications_enabled') == 'on') {
$u->message(trans('user.messages.critical_state_notification_recovered'));
switch ($this->belongsTo_type) {
case 'LogicalSensor':
$ls = LogicalSensor::find($this->belongsTo_id);
if (is_null($ls)) {
\Log::error('CriticalState ' . $this->id . ' recovered belongs to LogicalSensor ' . $this->belongsTo_id . ' which could not be found.');
break;
}
$u->message(trans('messages.critical_state_recovery_notification_logical_sensor.' . $ls->type, [
'logical_sensor' => $ls->name,
$ls->type => $ls->getCurrentCookedValue()
]));
break;
default:
$u->message(trans('messages.critical_state_generic', [
'critical_state' => $this->name
]));
}
}
}

Expand Down
60 changes: 60 additions & 0 deletions app/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ class Event extends CiliatusModel
*/
protected $dates = [];

/**
* Models the File can belong to
*
* @var array
*/
protected static $belongTo_Types = [
'Animal'
];

/**
* @var array
*/
Expand All @@ -55,6 +64,57 @@ public function belongsTo_object()
return $this->belongsTo('App\\'. $this->belongsTo_type, 'belongsTo_id');
}

/**
* @return mixed
*/
public function properties()
{
return $this->hasMany('App\Property', 'belongsTo_id')->where('belongsTo_type', 'Event');
}

/**
* @param $name
* @return Property|null
*/
public function property($name)
{
return $this->properties()->where('name', $name)->limit(1)->get()->first();
}

/**
* @param $type
* @return mixed
*/
public function propertyOfType($type)
{
return $this->properties()->where('type', $type)->limit(1)->get()->first();
}

/**
* @param $type
* @param $name
* @param null $value
* @return CiliatusModel|Property
*/
public function create_property($type, $name, $value = null)
{
return Property::create([
'belongsTo_type' => 'Event',
'belongsTo_id' => $this->id,
'type' => $type,
'name' => $name,
'value' => $value
]);
}

/**
* @return array
*/
public static function belongTo_Types()
{
return self::$belongTo_Types;
}

/**
* @return string
*/
Expand Down
Loading

0 comments on commit d1f68ef

Please sign in to comment.