Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 17 | #include "host.hpp" |
| 18 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 19 | #include <cmath> |
| 20 | #include <iostream> |
| 21 | #include <memory> |
| 22 | #include <mutex> |
| 23 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 24 | namespace pid_control |
| 25 | { |
| 26 | |
James Feist | 8154e32 | 2020-06-22 17:12:18 -0700 | [diff] [blame] | 27 | template <typename T> |
| 28 | void scaleHelper(T& ptr, int64_t value) |
| 29 | { |
| 30 | if constexpr (std::is_same_v<ValueType, int64_t>) |
| 31 | { |
| 32 | ptr->scale(value); |
| 33 | } |
| 34 | } |
| 35 | |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 36 | std::unique_ptr<Sensor> HostSensor::createTemp( |
| 37 | const std::string& name, int64_t timeout, sdbusplus::bus_t& bus, |
| 38 | const char* objPath, bool defer) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 39 | { |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 40 | auto sensor = |
| 41 | std::make_unique<HostSensor>(name, timeout, bus, objPath, defer); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 42 | sensor->value(0); |
| 43 | |
Patrick Venture | f55349d | 2018-06-11 11:20:42 -0700 | [diff] [blame] | 44 | // DegreesC and value of 0 are the defaults at present, therefore testing |
| 45 | // this code only sees scale get updated as a property. |
| 46 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 47 | // TODO(venture): Need to not hard-code that this is DegreesC and scale |
| 48 | // 10x-3 unless it is! :D |
| 49 | sensor->unit(ValueInterface::Unit::DegreesC); |
James Feist | 8154e32 | 2020-06-22 17:12:18 -0700 | [diff] [blame] | 50 | scaleHelper(sensor, -3); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 51 | sensor->emit_object_added(); |
Patrick Venture | f55349d | 2018-06-11 11:20:42 -0700 | [diff] [blame] | 52 | // emit_object_added() can be called twice, harmlessly, the second time it |
| 53 | // doesn't actually happen, but we don't want to call it before we set up |
| 54 | // the initial values, so we should not let someone call this with |
| 55 | // defer=false. |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 56 | |
| 57 | /* TODO(venture): Need to set that _updated is set to epoch or something |
| 58 | * else. what is the default value? |
| 59 | */ |
| 60 | return sensor; |
| 61 | } |
| 62 | |
James Feist | 8154e32 | 2020-06-22 17:12:18 -0700 | [diff] [blame] | 63 | template <typename T> |
| 64 | int64_t getScale(T* sensor) |
| 65 | { |
| 66 | if constexpr (std::is_same_v<ValueType, int64_t>) |
| 67 | { |
| 68 | return sensor->scale(); |
| 69 | } |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | ValueType HostSensor::value(ValueType value) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 74 | { |
| 75 | std::lock_guard<std::mutex> guard(_lock); |
| 76 | |
| 77 | _updated = std::chrono::high_resolution_clock::now(); |
James Feist | 8154e32 | 2020-06-22 17:12:18 -0700 | [diff] [blame] | 78 | _value = value * pow(10, getScale(this)); /* scale value */ |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 79 | |
| 80 | return ValueObject::value(value); |
| 81 | } |
| 82 | |
| 83 | ReadReturn HostSensor::read(void) |
| 84 | { |
| 85 | std::lock_guard<std::mutex> guard(_lock); |
| 86 | |
| 87 | /* This doesn't sanity check anything, that's the caller's job. */ |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 88 | ReadReturn r = {_value, _updated}; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 89 | |
| 90 | return r; |
| 91 | } |
| 92 | |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 93 | void HostSensor::write([[maybe_unused]] double value) |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 94 | { |
| 95 | throw std::runtime_error("Not Implemented."); |
| 96 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 97 | |
Harvey Wu | 17971ef | 2021-02-17 14:36:38 +0800 | [diff] [blame] | 98 | bool HostSensor::getFailed(void) |
| 99 | { |
| 100 | if (std::isfinite(_value)) |
| 101 | { |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | return true; |
| 106 | } |
| 107 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 108 | } // namespace pid_control |