58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
|
package fi
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"joylink.club/ecs"
|
||
|
"joylink.club/rtsssimulation/component"
|
||
|
"joylink.club/rtsssimulation/entity"
|
||
|
)
|
||
|
|
||
|
// FiMotorSwitch 电机电源开关定义
|
||
|
type FiMotorSwitch uint8
|
||
|
|
||
|
const (
|
||
|
FiMsOff FiMotorSwitch = iota //关闭电源
|
||
|
FiMsOnForward //正转电源
|
||
|
FiMsOnReverse //反转电源
|
||
|
)
|
||
|
|
||
|
func MotorOperate(w ecs.World, deviceId string, fiMs FiMotorSwitch, fc uint16) error {
|
||
|
if fc < 0 || fc > 100 {
|
||
|
return fmt.Errorf("设置变频器频率[%d]不在[0,100]范围内", fc)
|
||
|
}
|
||
|
ms, e := func(fiMs FiMotorSwitch) (component.MotorSwitch, error) {
|
||
|
switch fiMs {
|
||
|
case FiMsOff:
|
||
|
return component.MsOff, nil
|
||
|
case FiMsOnForward:
|
||
|
return component.MsOnForward, nil
|
||
|
case FiMsOnReverse:
|
||
|
return component.MsOnReverse, nil
|
||
|
default:
|
||
|
return 0, fmt.Errorf("参数fiMs[%d]无法映射到component.MotorSwitch", fiMs)
|
||
|
}
|
||
|
}(fiMs)
|
||
|
if e != nil {
|
||
|
return e
|
||
|
}
|
||
|
r := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
|
||
|
wd := entity.GetWorldData(w)
|
||
|
deviceEntry, ok := wd.EntityMap[deviceId]
|
||
|
if !ok {
|
||
|
return ecs.NewErrResult(fmt.Errorf("设备[%s]实体不存在", deviceId))
|
||
|
}
|
||
|
//
|
||
|
if !deviceEntry.HasComponent(component.MotorType) {
|
||
|
return ecs.NewErrResult(fmt.Errorf("设备[%s]不是电机", deviceId))
|
||
|
}
|
||
|
motor := component.MotorType.Get(deviceEntry)
|
||
|
motor.Ms = ms
|
||
|
if deviceEntry.HasComponent(component.MotorFcType) { //变频电机
|
||
|
component.MotorFcType.Get(deviceEntry).F = fc
|
||
|
}
|
||
|
//
|
||
|
return ecs.NewOkEmptyResult()
|
||
|
})
|
||
|
return r.Err
|
||
|
}
|