File size: 6,384 Bytes
ae4627d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
angular.module("tbs.adjust", ["tbs.common"]).controller("AdjustController", 
	function AdjustController($rootScope, $scope, $http, $uibModal,

	$filter, tbsWS, config, utils, auth) {
	$rootScope.title = "校秤";
	$scope.item = null;
	$scope.method="method1";
	$scope.stepNum = 0;
	$scope.valueIndex = 0;
	$scope.isGoing=false;// if user is adjusting, this value is true;
	$scope.isCurrentStep = function(stepName){
		return (stepName === $scope.method+'_'+$scope.stepNum)&&$scope.isGoing;

	};
	$scope.adjustMethods = {

		method1: {
			values: []
		},
		method2: {
			values: []
		},
		method3: {
			values: []
		}
	};
	$scope.$watch('isGoing',function(newValue,oldValue){
		if(!$scope.adjustForm){
			return;
		}
		if(newValue===true){
			$scope.adjustForm.$setDirty();
		}else{
			$scope.adjustForm.$setPristine();
			$scope.adjustForm.$setUntouched();
		}
	});
	$scope.preSelect = function (s){
		if(s.scaleNum === $scope.item.scaleNum && $scope.isGoing){
			return;
		}
		if($scope.isGoing){
			utils.confirmDataLost($scope.adjustForm).then(
				function(){
					$scope.selectItem(s);
				}
			);
		}else{
			$scope.selectItem(s);
		}
	};
	$scope.selectItem = function (s) {
		$scope.isGoing = false;
		$scope.item = s;
		$scope.stepNum = 0;
		$scope.valueIndex = 0;
		//Init method1 value
		//$scope.adjustMethods.method1.values.length=10;
		function makeValue1_2() {
			var v = {
				target: s.adjustWeight,
				actul: undefined,
				lowLimit: s.adjustWeight - s.adjustTolerance,
				upLimit: s.adjustWeight + s.adjustTolerance
			};
			return v
		};
		function makeValue3(value, index) {


			if (index == 0 ) {
				return {
					target: 0,
					actul: undefined,
					lowLimit: 0,
					upLimit: 0
				}
			} else {
				return {
					target: s["lineAdjustWeight" + index],
					actul: undefined,
					lowLimit: s["lineAdjustWeight" + index] - s["lineAdjustTole" + index],
					upLimit: s["lineAdjustWeight" + index] + s["lineAdjustTole" + index]
				}

			}
		};

		$scope.adjustMethods.method1.values = new Array(5).fill().map(makeValue1_2);
		$scope.adjustMethods.method2.values = new Array(10).fill().map(makeValue1_2);

		//Init method2 value
		var a1 = new Array(4).fill().map(makeValue3);
		var a2 = new Array(4).fill().map(makeValue3);
		
		$scope.adjustMethods.method3.values =a1.concat(a2.reverse());
		// $scope.adjustMethods.method4.values = new Array(4).fill().map(makeValue4);

	};

	$scope.start = function () {
		console.log("HI");
		$scope.isGoing = true;
		tbsWS.switch($scope.item.scaleNum);
		tbsWS.ZI().then(function () {
			tbsWS.SIR();
		});

		$scope.adjust($scope.method);
		//
	};

	$scope.adjust = function (methodName) {

		var currentMethod = methodName;
		//var $scope.stepNum = 0;
		var justCompleteAstep = false; //标志是否刚刚完成一个校验值
		if (methodName === 'method1' || methodName === 'method2') {
			tbsWS.setCallback(function (w) {
				var lowLimit = $scope.adjustMethods[currentMethod].values[$scope.stepNum].lowLimit;
				var upLimit = $scope.adjustMethods[currentMethod].values[$scope.stepNum].upLimit;
				var valueCount = $scope.adjustMethods[currentMethod].values.length;
				$scope.scaleWeighting = angular.copy(w);
				var weighting = $scope.scaleWeighting.weighting;
				if (weighting == 0 && w.status === 'S') {
					//If 0 S, means a new step start
					if (justCompleteAstep) {

						justCompleteAstep = false;
						$scope.stepNum++;
						console.log("S:" + $scope.stepNum);
						if ($scope.stepNum > (valueCount - 1)) {
							tbsWS.cancelCallback();
							$scope.isGoing = false;

							var adjustLog = {
							  "methodNum" : methodName,
							  "scaleNum" : $scope.item.scaleNum
							};

							$scope.selectItem($scope.item);
							$http.post(config.baseUrl + "adjustLog", adjustLog).then(function (response) {
							        utils.showDialog({
				        				title: "消息",
				        				message: "校秤信息保存成功!",
				        				type: "success"
				        			});
							});

						}

					}


				} else if ((lowLimit <= weighting) && (weighting <= upLimit) && w.status === 'S') {
					// valid
					$scope.adjustMethods[currentMethod].values[$scope.stepNum].actul = weighting;
					justCompleteAstep = true;
					console.log(currentMethod, $scope.stepNum, weighting);
				}

				$scope.$apply();
			}
			);
		} else {
			tbsWS.setCallback(function (w) {
				var lowLimit = $scope.adjustMethods[currentMethod].values[$scope.stepNum].lowLimit;
				var upLimit = $scope.adjustMethods[currentMethod].values[$scope.stepNum].upLimit;
				var valueCount = $scope.adjustMethods[currentMethod].values.length;
				$scope.scaleWeighting = angular.copy(w);
				var weighting = $scope.scaleWeighting.weighting;
				if ((lowLimit <= weighting) && (weighting <= upLimit) && w.status === 'S') {
					// valid
					$scope.adjustMethods[currentMethod].values[$scope.stepNum].actul = weighting;
					$scope.stepNum++;
					if ($scope.stepNum > (valueCount - 1)) {
		
							$scope.$apply();
							tbsWS.cancelCallback();
							var adjustLog = {
							  "methodNum" : methodName,
							  "scaleNum" : $scope.item.scaleNum
							};
							$scope.isGoing = false;
							$scope.selectItem($scope.item);
							
							$http.post(config.baseUrl + "adjustLog", adjustLog).then(function (response) {
							    utils.showDialog({
			        				title: "消息",
			        				message: "校秤信息保存成功!",
			        				type: "success"
			        			});
							});
						
					}
					console.log(currentMethod, $scope.stepNum, weighting);
				}

				$scope.$apply();
			}
			);
		}

	};

	$scope.cancel = function () {
		$scope.isGoing = false;
		$scope.selectItem($scope.item);
		tbsWS.cancelCallback();
	};

	$scope.init = function () {

		//$http.get(config.baseUrl + "scaleSetting").then(function (response) {

		tbsWS.getScales().then(function (response) {

			$rootScope.scales = response.data;
			$scope.scales = $rootScope.scales.filter(function (scale) { return scale && scale.isConnected; });
			if ($scope.scales && $scope.scales.length > 0 && $scope.item == null) {
				$scope.selectItem($rootScope.scales[0]);
			}
		});
	};
	$scope.init();
});