Yeelight /
miot-api
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Created by PhpStorm. |
||
| 4 | * User: sheldon |
||
| 5 | * Date: 18-6-8 |
||
| 6 | * Time: 下午3:43. |
||
| 7 | */ |
||
| 8 | |||
| 9 | namespace MiotApi\Contract\Specification; |
||
| 10 | |||
| 11 | use MiotApi\Exception\SpecificationErrorException; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * 属性描述需要表达这几个意思: |
||
| 15 | * 语义是什么? |
||
| 16 | * 数据格式是什么? |
||
| 17 | * 是否可读?是否可写?数据变化了是否有通知? |
||
| 18 | * 值是否有约束?如果有,取值范围是离散值还是连续值? |
||
| 19 | * 单位是否定义?如果有定义,单位是什么? |
||
| 20 | * |
||
| 21 | * Class PropertySpecification |
||
| 22 | */ |
||
| 23 | class PropertySpecification extends Specification |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * 数据格式. |
||
| 27 | * |
||
| 28 | * 数据格式 描述 |
||
| 29 | * bool 布尔值: true/false 或 1/0 |
||
| 30 | * uint8 无符号8位整型 |
||
| 31 | * uint16 无符号16位整型 |
||
| 32 | * uint32 无符号32位整型 |
||
| 33 | * int8 有符号8位整型 |
||
| 34 | * int16 有符号16位整型 |
||
| 35 | * int32 有符号32位整型 |
||
| 36 | * int64 有符号64位整型 |
||
| 37 | * float 浮点数 |
||
| 38 | * string 字符串 |
||
| 39 | * |
||
| 40 | * @var |
||
| 41 | */ |
||
| 42 | protected $format; |
||
| 43 | |||
| 44 | protected $formatMap = [ |
||
| 45 | 'bool', |
||
| 46 | 'uint8', |
||
| 47 | 'uint16', |
||
| 48 | 'uint32', |
||
| 49 | 'int8', |
||
| 50 | 'int16', |
||
| 51 | 'int32', |
||
| 52 | 'int64', |
||
| 53 | 'float', |
||
| 54 | 'string', |
||
| 55 | ]; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * 访问方式 |
||
| 59 | * 值 描述 |
||
| 60 | * read 读 |
||
| 61 | * write 写 |
||
| 62 | * notify 通知. |
||
| 63 | * |
||
| 64 | * @var |
||
| 65 | */ |
||
| 66 | protected $access; |
||
| 67 | |||
| 68 | protected $accessMap = [ |
||
| 69 | 'read', |
||
| 70 | 'write', |
||
| 71 | 'notify', |
||
| 72 | ]; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * 对取值范围进行约束,可选字段 |
||
| 76 | * 当format为整型或浮点数,可定义value-range,比如: |
||
| 77 | * 最小值 最大值 步长 |
||
| 78 | * 16 32 0.5. |
||
| 79 | * |
||
| 80 | * @var |
||
| 81 | */ |
||
| 82 | protected $valueRange; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * 对取值范围进行约束,可选字段 |
||
| 86 | * 当format为整型,可定义"value-list",每个元素都包含:value description. |
||
| 87 | * |
||
| 88 | * @var |
||
| 89 | */ |
||
| 90 | protected $valueList; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * 单位,可选字段 |
||
| 94 | * 当format为整型或浮点型,可定义unit值 |
||
| 95 | * |
||
| 96 | * 值 描述 |
||
| 97 | * percentage 百分比 |
||
| 98 | * celsius 摄氏度 |
||
| 99 | * senconds 秒 |
||
| 100 | * minutes 分 |
||
| 101 | * hours 小时 |
||
| 102 | * days 天 |
||
| 103 | * kelvin 开氏温标 |
||
| 104 | * pascal 帕斯卡(大气压强单位) |
||
| 105 | * arcdegrees 弧度(角度单位) |
||
| 106 | * |
||
| 107 | * @var |
||
| 108 | */ |
||
| 109 | protected $unit; |
||
| 110 | |||
| 111 | protected $unitMap = [ |
||
| 112 | 'percentage', |
||
| 113 | 'celsius', |
||
| 114 | 'senconds', |
||
| 115 | 'minutes', |
||
| 116 | 'hours', |
||
| 117 | 'days', |
||
| 118 | 'kelvin', |
||
| 119 | 'pascal', |
||
| 120 | 'arcdegrees', |
||
| 121 | 'rgb', |
||
| 122 | ]; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @throws SpecificationErrorException |
||
| 126 | */ |
||
| 127 | public function init() |
||
| 128 | { |
||
| 129 | parent::init(); |
||
| 130 | |||
| 131 | if ($this->has('format')) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 132 | $format = $this->__get('format'); |
||
| 133 | if (!in_array($format, $this->formatMap)) { |
||
| 134 | throw new SpecificationErrorException('属性-》数据格式(format)的取值不在合法范围内'); |
||
| 135 | } |
||
| 136 | $this->format = $format; |
||
| 137 | } |
||
| 138 | |||
| 139 | if ($this->has('access')) { |
||
| 140 | $access = $this->__get('access'); |
||
| 141 | if (!empty($access)) { |
||
| 142 | foreach ($access as $item) { |
||
| 143 | if (!in_array($item, $this->accessMap)) { |
||
| 144 | throw new SpecificationErrorException('属性-》访问方式(access)的取值不在合法范围内'); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | $this->access = $access; |
||
| 148 | } else { |
||
| 149 | $this->access = []; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | if ($this->has('value-range')) { |
||
| 154 | $valueRange = $this->__get('value-range'); |
||
| 155 | $this->valueRange = $valueRange; |
||
| 156 | } |
||
| 157 | |||
| 158 | if ($this->has('value-list')) { |
||
| 159 | $valueList = $this->__get('value-list'); |
||
| 160 | |||
| 161 | // TODO 当format为整型,可定义"value-list"的验证 |
||
| 162 | $this->valueList = $valueList; |
||
| 163 | } |
||
| 164 | |||
| 165 | if ($this->has('unit')) { |
||
| 166 | $unit = $this->__get('unit'); |
||
| 167 | if (!in_array($unit, $this->unitMap)) { |
||
| 168 | throw new SpecificationErrorException('属性-》单位(unit)的取值不在合法范围内'); |
||
| 169 | } |
||
| 170 | |||
| 171 | // TODO 当format为整型或浮点型,可定义unit值 的验证 |
||
| 172 | $this->unit = $unit; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @return mixed |
||
| 178 | */ |
||
| 179 | public function getFormat() |
||
| 180 | { |
||
| 181 | return $this->format; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return mixed |
||
| 186 | */ |
||
| 187 | public function getAccess() |
||
| 188 | { |
||
| 189 | return $this->access; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @return mixed |
||
| 194 | */ |
||
| 195 | public function getValueRange() |
||
| 196 | { |
||
| 197 | return $this->valueRange; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @return mixed |
||
| 202 | */ |
||
| 203 | public function getValueList() |
||
| 204 | { |
||
| 205 | return $this->valueList; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return mixed |
||
| 210 | */ |
||
| 211 | public function getUnit() |
||
| 212 | { |
||
| 213 | return $this->unit; |
||
| 214 | } |
||
| 215 | } |
||
| 216 |